| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 1999 - 2020 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_function_time_h |
| 16 |
#define dealii_function_time_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
|
| 23 |
DEAL_II_NAMESPACE_OPEN |
| 24 |
/** |
| 25 |
* Support for time dependent functions. The library was also designed for |
| 26 |
* time dependent problems. For this purpose, the function objects also |
| 27 |
* contain a field which stores the time, as well as functions manipulating |
| 28 |
* them. Time independent problems should not access or even abuse them for |
| 29 |
* other purposes, but since one normally does not create thousands of |
| 30 |
* function objects, the gain in generality weighs out the fact that we need |
| 31 |
* not store the time value for not time dependent problems. The second |
| 32 |
* advantage is that the derived standard classes like <tt>ZeroFunction</tt>, |
| 33 |
* <tt>ConstantFunction</tt> etc also work for time dependent problems. |
| 34 |
* |
| 35 |
* Access to the time goes through the following functions: |
| 36 |
* <ul> |
| 37 |
* <li> <tt>get_time</tt>: return the present value of the time variable. |
| 38 |
* <li> <tt>set_time</tt>: set the time value to a specific value. |
| 39 |
* <li> <tt>advance_time</tt>: increase the time by a certain time step. |
| 40 |
* </ul> |
| 41 |
* The latter two functions are virtual, so that derived classes can perform |
| 42 |
* computations which need only be done once for every new time. For example, |
| 43 |
* if a time dependent function had a factor <tt>sin(t)</tt>, then it may be a |
| 44 |
* reasonable choice to calculate this factor in a derived version of |
| 45 |
* set_time(), store it in a member variable and use that one rather than |
| 46 |
* computing it every time <tt>value()</tt>, <tt>value_list</tt> or one of the |
| 47 |
* other functions of class Function is called. |
| 48 |
* |
| 49 |
* By default, the advance_time() function calls the set_time() function with |
| 50 |
* the new time, so it is sufficient in most cases to overload only set_time() |
| 51 |
* for computations as sketched out above. |
| 52 |
* |
| 53 |
* The constructor of this class takes an initial value for the time variable, |
| 54 |
* which defaults to zero. Because a default value is given, none of the |
| 55 |
* derived classes needs to take an initial value for the time variable if not |
| 56 |
* needed. |
| 57 |
* |
| 58 |
* @tparam Number The data type in which time values are to be stored. This |
| 59 |
* will, in almost all cases, simply be the default @p double, but there are |
| 60 |
* cases where one may want to store the time in a different (and always |
| 61 |
* scalar) type. An example would be an interval type that can store a value |
| 62 |
* as well as its uncertainty. Another example would be a type that allows for |
| 63 |
* Automatic Differentiation (see, for example, the Sacado type used in |
| 64 |
* step-33) and thereby can generate analytic (temporal) derivatives of a |
| 65 |
* function. |
| 66 |
* |
| 67 |
* |
| 68 |
* @ingroup functions |
| 69 |
*/ |
| 70 |
template <typename Number = double> |
| 71 |
class FunctionTime |
| 72 |
{ |
| 73 |
public: |
| 74 |
/** |
| 75 |
* Constructor. May take an initial value for the time variable, which |
| 76 |
* defaults to zero. |
| 77 |
*/ |
| 78 |
FunctionTime(const Number initial_time = Number(0.0)); |
| 79 |
|
| 80 |
/** |
| 81 |
* Virtual destructor. |
| 82 |
*/ |
| 83 |
virtual ~FunctionTime() = default; |
| 84 |
|
| 85 |
/** |
| 86 |
* Return the value of the time variable. |
| 87 |
*/ |
| 88 |
Number |
| 89 |
get_time() const; |
| 90 |
|
| 91 |
/** |
| 92 |
* Set the time to <tt>new_time</tt>, overwriting the old value. |
| 93 |
*/ |
| 94 |
virtual void |
| 95 |
set_time(const Number new_time); |
| 96 |
|
| 97 |
/** |
| 98 |
* Advance the time by the given time step <tt>delta_t</tt>. |
| 99 |
*/ |
| 100 |
virtual void |
| 101 |
advance_time(const Number delta_t); |
| 102 |
|
| 103 |
/** |
| 104 |
* The type this class is initialized with and that is used to represent time. |
| 105 |
*/ |
| 106 |
using time_type = Number; |
| 107 |
|
| 108 |
private: |
| 109 |
/** |
| 110 |
* Store the present time. |
| 111 |
*/ |
| 112 |
Number time; |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
/*----------------------------- Inline functions ----------------------------*/ |
| 118 |
|
| 119 |
#ifndef DOXYGEN |
| 120 |
|
| 121 |
template <typename Number> |
| 122 |
inline Number |
| 123 |
FunctionTime<Number>::get_time() const |
| 124 |
{ |
| 125 |
return time; |
| 126 |
} |
| 127 |
|
| 128 |
#endif |
| 129 |
DEAL_II_NAMESPACE_CLOSE |
| 130 |
|
| 131 |
#endif |