OpenLB 1.7
Loading...
Searching...
No Matches
adHelpers.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2021 Julius Jeßberger
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
28#ifndef AD_HELPERS_H
29#define AD_HELPERS_H
30
31#include "aDiff.h"
32
33
34namespace olb {
35
36namespace util {
37
41template <typename SAD>
42void iniDiagonal(SAD* a, int dim) {
43 for (int i = 0; i < dim; ++i) {
44 a[i].setDiffVariable(i);
45 }
46}
47
48template <typename C>
49void iniDiagonal(C& a) {
50 for (unsigned i = 0; i < a.size(); ++i) {
51 a[i].setDiffVariable(i);
52 }
53}
54
55
60template <typename T, typename TAD>
61void copyDerivatives(T* target, const TAD* source, int length) {
62 for (int j = 0; j < length; ++j) {
63 for (unsigned i = 0; i < TAD::dim; ++i) {
64 target[j*(TAD::dim)+i] = source[j].d(i);
65 }
66 }
67}
68
69
71template<typename T, typename S, template<typename> typename C>
72C<T> copyAs(const C<S>& input) {
73 C<T> result = ContainerCreator<C<T>>::create(input.size());
74
75 for(std::size_t it = 0; it < input.size(); ++it) {
76 result[it] = T(input[it]);
77 }
78 return result;
79}
80
81
83template<typename S>
84ADf<S,1> iniAD(const S source) {
85 ADf<S,1> result(source);
86 result.setDiffVariable(0);
87 return result;
88}
89
91template<unsigned n, typename S>
92ADf<S,n>* iniAD(const S* source) {
93 ADf<S,n>* result = new ADf<S,n>[n];
94 util::copyN (result, source, n);
95 util::iniDiagonal (result, n);
96 return result;
97}
98
100template<unsigned n, typename S, template<typename> typename C>
101C<ADf<S,n>> iniAD(const C<S>& source) {
102 auto result = copyAs<ADf<S,n>,S,C> (source);
103 util::iniDiagonal (result);
104 return result;
105}
106
107
108
114template <typename S, typename F>
115auto derivativeFAD (F f, const S input) {
116 ADf<S,1> inputAD = input;
117 inputAD.setDiffVariable(0);
118
119 return f(inputAD).d(0);
120}
121
127template <unsigned sourceDIM, typename S, typename F>
128auto derivativeFAD (F f, const S* input) {
129 using SAD = ADf<S,sourceDIM>;
130 using T = decltype(f(input));
131 using TAD = ADf<T,sourceDIM>;
132
133 SAD inputAD[sourceDIM];
134 util::copyN(inputAD, input, sourceDIM);
135 util::iniDiagonal(inputAD, sourceDIM);
136
137 TAD resultAD = f(inputAD);
138 T* result = new T[sourceDIM];
139 copyDerivatives(result, &resultAD, 1);
140 return result;
141}
142
149template <unsigned sourceDIM, typename T, typename S, typename F>
150void derivativeFAD (F f, T* output, const S* input, unsigned targetDIM) {
151 using SAD = ADf<S,sourceDIM>;
152 using TAD = ADf<T,sourceDIM>;
153
154 SAD inputAD[sourceDIM];
155 util::copyN(inputAD, input, sourceDIM);
156 util::iniDiagonal(inputAD, sourceDIM);
157 TAD outputAD[targetDIM];
158
159 f(outputAD, inputAD);
160 copyDerivatives(output, outputAD, targetDIM);
161 return;
162}
163
164}
165
166}
167
168#endif
The description of a algoritmic differentiation data type using the forward method – header file.
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
void copyDerivatives(T *target, const TAD *source, int length)
Copy the derivatives from an ADf array into an array.
Definition adHelpers.h:61
C< T > copyAs(const C< S > &input)
copy vector with specified typecast
Definition adHelpers.h:72
void copyN(T c[], const T a[], const unsigned dim) any_platform
auto derivativeFAD(F f, const S input)
Compute derivatives of a function f: S -> T via forward AD Signature is "V f(U)" so f is expected to ...
Definition adHelpers.h:115
void iniDiagonal(SAD *a, int dim)
The variables of an array are set to be the differential variables.
Definition adHelpers.h:42
ADf< S, 1 > iniAD(const S source)
copy value and initialize derivative
Definition adHelpers.h:84
Top level namespace for all of OpenLB.
Creates a container of type C.