| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2000 - 2025 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_derivative_h |
| 16 |
#define dealii_function_derivative_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/auto_derivative_function.h> |
| 21 |
#include <deal.II/base/exceptions.h> |
| 22 |
#include <deal.II/base/function.h> |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
|
| 26 |
|
| 27 |
DEAL_II_NAMESPACE_OPEN |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Derivative of a function object. The value access functions of this class |
| 32 |
* return the directional derivative of a function with respect to a direction |
| 33 |
* provided on construction. If <tt>b</tt> is the vector, the derivative <tt>b |
| 34 |
* . grad f</tt> is computed. This derivative is evaluated directly, not by |
| 35 |
* computing the gradient of <tt>f</tt> and its scalar product with |
| 36 |
* <tt>b</tt>. |
| 37 |
* |
| 38 |
* The derivative is computed numerically, using one of the provided |
| 39 |
* difference formulas (see <tt>set_formula</tt> for available schemes). |
| 40 |
* Experimenting with <tt>h</tt> and the difference scheme may be necessary to |
| 41 |
* obtain sufficient results. |
| 42 |
* |
| 43 |
* @ingroup functions |
| 44 |
*/ |
| 45 |
template <int dim> |
| 46 |
class FunctionDerivative : public AutoDerivativeFunction<dim> |
| 47 |
{ |
| 48 |
public: |
| 49 |
/** |
| 50 |
* Constructor. Provided are the functions to compute derivatives of, the |
| 51 |
* direction vector of the differentiation and the step size <tt>h</tt> of |
| 52 |
* the difference formula. |
| 53 |
*/ |
| 54 |
FunctionDerivative(const Function<dim> &f, |
| 55 |
const Point<dim> &direction, |
| 56 |
const double h = 1.e-6); |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructor. Provided are the functions to compute derivatives of and the |
| 60 |
* direction vector of the differentiation in each quadrature point and the |
| 61 |
* difference step size. |
| 62 |
* |
| 63 |
* This is the constructor for a variable velocity field. Most probably, a |
| 64 |
* new object of <tt>FunctionDerivative</tt> has to be constructed for each |
| 65 |
* set of quadrature points. |
| 66 |
* |
| 67 |
* The number of quadrature point must still be the same, when values are |
| 68 |
* accessed. |
| 69 |
*/ |
| 70 |
FunctionDerivative(const Function<dim> &f, |
| 71 |
const std::vector<Point<dim>> &direction, |
| 72 |
const double h = 1.e-6); |
| 73 |
|
| 74 |
/** |
| 75 |
* Choose the difference formula. This is set to the default in the |
| 76 |
* constructor. |
| 77 |
* |
| 78 |
* Formulas implemented right now are first order backward Euler |
| 79 |
* (<tt>UpwindEuler</tt>), second order symmetric Euler (<tt>Euler</tt>) and |
| 80 |
* a symmetric fourth order formula (<tt>FourthOrder</tt>). |
| 81 |
*/ |
| 82 |
void |
| 83 |
set_formula(typename AutoDerivativeFunction<dim>::DifferenceFormula formula = |
| 84 |
AutoDerivativeFunction<dim>::Euler); |
| 85 |
/** |
| 86 |
* Change the base step size of the difference formula |
| 87 |
*/ |
| 88 |
void |
| 89 |
set_h(const double h); |
| 90 |
|
| 91 |
virtual double |
| 92 |
value(const Point<dim> &p, const unsigned int component = 0) const override; |
| 93 |
|
| 94 |
virtual void |
| 95 |
vector_value(const Point<dim> &p, Vector<double> &value) const override; |
| 96 |
|
| 97 |
virtual void |
| 98 |
value_list(const std::vector<Point<dim>> &points, |
| 99 |
std::vector<double> &values, |
| 100 |
const unsigned int component = 0) const override; |
| 101 |
|
| 102 |
/** |
| 103 |
* Return an estimate for the memory consumption, in bytes, of this object. |
| 104 |
* This is not exact (but will usually be close) because calculating the |
| 105 |
* memory usage of trees (e.g., <tt>std::map</tt>) is difficult. |
| 106 |
*/ |
| 107 |
virtual std::size_t |
| 108 |
memory_consumption() const override; |
| 109 |
|
| 110 |
private: |
| 111 |
/** |
| 112 |
* Function for differentiation. |
| 113 |
*/ |
| 114 |
const Function<dim> &f; |
| 115 |
|
| 116 |
/** |
| 117 |
* Step size of the difference formula. |
| 118 |
*/ |
| 119 |
double h; |
| 120 |
|
| 121 |
/** |
| 122 |
* Difference formula. |
| 123 |
*/ |
| 124 |
typename AutoDerivativeFunction<dim>::DifferenceFormula formula; |
| 125 |
|
| 126 |
/** |
| 127 |
* Helper object. Contains the increment vector for the formula. |
| 128 |
*/ |
| 129 |
std::vector<Tensor<1, dim>> incr; |
| 130 |
}; |
| 131 |
|
| 132 |
DEAL_II_NAMESPACE_CLOSE |
| 133 |
|
| 134 |
#endif |