OpenLB 1.7
Loading...
Searching...
No Matches
derivativeF.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Julius Jessberger, Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24
25#ifndef DERIVATIVE_F_H
26#define DERIVATIVE_F_H
27
28
29#include "analyticalBaseF.h"
32
33#include <stdlib.h>
34
35
36
37
38namespace olb {
39
40
42template <typename T>
44protected:
46 T _eps {1.e-10};
47public:
50 bool operator() (T output[], const T input[]) override;
51};
52
54template <typename T>
55bool AnalyticalDerivativeFD1D<T>::operator() (T output[], const T input[])
56{
57 _f(output,input);
58 T x = output[0];
59 T input2[1];
60 input2[0] = input[0] + _eps;
61 _f(output,input2);
62 output[0] -= x;
63 output[0] /= _eps;
64 return true;
65}
66
67
68
70template <unsigned D, typename T, typename S>
72public:
75
77 bool operator() (T output[], const S input[]) override;
78
79protected:
80 AnalyticalF<D,TAD,SAD>& _f; // the functor which is to be differentiated
81};
82
84template <unsigned D, typename T, typename S>
85bool AnalyticalDerivativeAD1D<D,T,S>::operator() (T output[], const S input[]) {
86 // Define corresponding AD-data types
87 SAD inpAD(input[0]);
88 inpAD.setDiffVariable(0);
89 TAD outAD(output[0]);
90
91 // Apply f to the AD versions of input, output.
92 _f(&outAD, &inpAD);
93
94 // Return the derivative.
95 output[0] = outAD.d(0);
96
97 return true;
98}
99
100
101template<
102 class F,
103 typename T, typename S, unsigned sourceDIM,
104 typename... ARGS
105>
106class AnalyticalDerivativeAD : public AnalyticalF<sourceDIM,T,S> {
107
108public:
111
112protected:
113 typename F::template exchange_type<TAD,SAD> _f;
114
115public:
116 explicit AnalyticalDerivativeAD(unsigned targetDIM, ARGS&&... args)
117 : AnalyticalF<sourceDIM,T,S>(targetDIM*sourceDIM), _f(args...) { }
118
119 explicit AnalyticalDerivativeAD(const F& f)
120 : AnalyticalF<sourceDIM,T,S>(f.getTargetDim()*sourceDIM),
121 _f(f.template copyAs<TAD,SAD>()) { }
122
127 bool operator()(T output[], const S input[]) override {
128 SAD inputAD[sourceDIM];
129 util::copyN(inputAD, input, sourceDIM);
130 util::iniDiagonal(inputAD, sourceDIM);
131 TAD outputAD[_f.getTargetDim()];
132
133 _f(outputAD, inputAD);
134
135 util::copyDerivatives(output, outputAD, _f.getTargetDim());
136
137 return true;
138 }
139};
140
141/* // Note: this cannot work since the mapping AFGS -> F is not unique
142template <class F, typename... ARGS>
143AnalyticalDerivativeAD(unsigned, ARGS&&...) -> AnalyticalDerivativeAD <F,
144 typename F::targetType, typename F::sourceType, F::dim, ARGS...>;
145*/
146
147template <class F>
149 typename F::targetType, typename F::sourceType, F::dim>;
150
151
152
153} // end namespace olb
154
155#endif
The description of a algoritmic differentiation data type using the forward method – header file.
Some helper functions for the ADf data type.
Class for AD Differentiation of 1-dim Functor F: S -> T.
Definition derivativeF.h:71
bool operator()(T output[], const S input[]) override
AD Differentiation operator implementation.
Definition derivativeF.h:85
AnalyticalF< D, TAD, SAD > & _f
Definition derivativeF.h:80
AnalyticalDerivativeAD1D(AnalyticalF< D, TAD, SAD > &f)
Definition derivativeF.h:76
bool operator()(T output[], const S input[]) override
Computes the partial derivatives of _f and writes them into output.
AnalyticalDerivativeAD(unsigned targetDIM, ARGS &&... args)
F::template exchange_type< TAD, SAD > _f
Class for computing the derivative of a given 1D functor with a finite difference.
Definition derivativeF.h:43
AnalyticalF1D< T, T > & _f
Definition derivativeF.h:45
AnalyticalDerivativeFD1D(AnalyticalF1D< T, T > &f, T eps)
Definition derivativeF.h:49
AnalyticalDerivativeFD1D(AnalyticalF1D< T, T > &f)
Definition derivativeF.h:48
bool operator()(T output[], const T input[]) override
Finite Difference computation.
Definition derivativeF.h:55
int getTargetDim() const
read only access to member variable _n
Definition genericF.hh:45
Definition of a description of a algoritmic differentiation data type using the forward method.
Definition aDiff.h:64
constexpr void setDiffVariable(unsigned iD)
Definition aDiff.h:270
constexpr T & d(unsigned i)
Definition aDiff.h:252
void copyDerivatives(T *target, const TAD *source, int length)
Copy the derivatives from an ADf array into an array.
Definition adHelpers.h:61
void copyN(T c[], const T a[], const unsigned dim) any_platform
void iniDiagonal(SAD *a, int dim)
The variables of an array are set to be the differential variables.
Definition adHelpers.h:42
Top level namespace for all of OpenLB.
AnalyticalDerivativeAD(const F &) -> AnalyticalDerivativeAD< F, typename F::targetType, typename F::sourceType, F::dim >