OpenLB 1.8.1
Loading...
Searching...
No Matches
aDiffTape.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 202021 Max Sagebaum, Nicolas Gauger
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#ifndef A_DIFF_TAPE_H
25#define A_DIFF_TAPE_H
26
27#include <cstdint>
28#include <vector>
29
30namespace olb {
31
32namespace util {
33
34namespace ADtape {
35
36template<typename T_Tape>
37struct ActiveType {
38 using Tape = T_Tape;
39 using Real = typename Tape::Real;
40 using Gradient = Real;
41
42private:
43 static Tape globalTape;
44
45 Real value_;
46 int id_;
47
48public:
49 ActiveType() = default;
50
51 ActiveType(ActiveType const& value) = default; // We do not need to store copy operations
52
53 ActiveType(Real const& value) : value_(value), id_(0) {}
54
56 value_ = value;
57 id_ = 0;
58
59 return *this;
60 }
61
62 ActiveType& operator =(ActiveType const& value) = default; // We do not need to store copy operations
63
64#define OPERATOR(OP, rhs) \
65 ActiveType& OP(ActiveType const& value) { \
66 *this = rhs; \
67 return *this; \
68 }
69
70 OPERATOR(operator +=, *this + value)
71 OPERATOR(operator -=, *this - value)
72 OPERATOR(operator *=, *this * value)
73 OPERATOR(operator /=, *this / value)
74
75#undef OPERATOR
76
78 return value_;
79 }
80
81 Real const& value() const {
82 return value_;
83 }
84
86 return id_;
87 }
88
89 int const& getIdentifier() const {
90 return id_;
91 }
92
94 return getTape().gradient(id_);
95 }
96
97 Real const& gradient() const {
98 return getTape().gradient(id_);
99 }
100
101 static Tape& getTape() {
102 return globalTape;
103 }
104};
105
106template<typename Tape>
107Tape ActiveType<Tape>::globalTape;
108
109#define BINARY_OPERATOR(OP, VAL, JAC1, JAC2) \
110template<typename Tape> \
111ActiveType<Tape> OP(ActiveType<Tape> const& arg1, ActiveType<Tape> const& arg2) { \
112 using Real = typename Tape::Real; \
113 \
114 Real val1 = arg1.value(); \
115 Real val2 = arg2.value(); \
116 ActiveType<Tape> lhs; \
117 ActiveType<Tape>::getTape().store(lhs, VAL, arg1, JAC1, arg2, JAC2); \
118 \
119 return lhs; \
120} \
121\
122template<typename Tape> \
123ActiveType<Tape> OP(typename Tape::Real const& val1, ActiveType<Tape> const& arg2) { \
124 using Real = typename Tape::Real; \
125 \
126 Real val2 = arg2.value(); \
127 ActiveType<Tape> lhs; \
128 ActiveType<Tape>::getTape().store(lhs, VAL, arg2, JAC2); \
129 \
130 return lhs; \
131} \
132\
133template<typename Tape> \
134ActiveType<Tape> OP(ActiveType<Tape> const& arg1, typename Tape::Real const& val2) { \
135 using Real = typename Tape::Real; \
136 \
137 Real val1 = arg1.value(); \
138 ActiveType<Tape> lhs; \
139 ActiveType<Tape>::getTape().store(lhs, VAL, arg1, JAC1); \
140 \
141 return lhs; \
142}
143
144using std::atan2;
145using std::hypot;
146using std::max;
147using std::min;
148using std::pow;
149
150BINARY_OPERATOR(operator+, val1 + val2, 1.0, 1.0)
151BINARY_OPERATOR(operator-, val1 - val2, 1.0, -1.0)
152BINARY_OPERATOR(operator*, val1 * val2, val2, val1)
153BINARY_OPERATOR(operator/, val1 / val2, 1.0 / val2, -val1 / val2 / val2)
154BINARY_OPERATOR(atan2, atan2(val1, val2), val2 / (val1 * val1 + val2 * val2), -val1 / (val1 * val1 + val2 * val2))
155BINARY_OPERATOR(hypot, hypot(val1, val2), val1 / hypot(val1, val2), val2 / hypot(val1, val2))
156BINARY_OPERATOR(max, max(val1, val2), val1 > val2 ? 1.0 : 0.0, val1 > val2 ? 0.0 : 1.0)
157BINARY_OPERATOR(min, min(val1, val2), val1 < val2 ? 1.0 : 0.0, val1 < val2 ? 0.0 : 1.0)
158BINARY_OPERATOR(pow, pow(val1, val2), val2 * pow(val1, val2 - 1.0), log(val1) * pow(val1, val2))
159#undef BINARY_OPERATOR
160
161#define UNARY_OPERATOR(OP, VAL, JAC) \
162template<typename Tape> \
163ActiveType<Tape> OP(ActiveType<Tape> const& arg) { \
164 using Real = typename Tape::Real; \
165 \
166 Real val = arg.value(); \
167 ActiveType<Tape> lhs; \
168 ActiveType<Tape>::getTape().store(lhs, VAL, arg, JAC); \
169 \
170 return lhs; \
171}
172
173using olb::util::abs;
174//using std::acos;
175//using std::asin;
176//using std::atan;
177//using std::atanh;
178//using std::cbrt;
179//using std::cos;
180//using std::cosh;
181//using std::exp;
182//using std::log;
183//using std::log10;
184//using std::sin;
185//using std::sinh;
186using olb::util::sqrt;
187//using std::tan;
188//using std::tanh;
189
190UNARY_OPERATOR(operator+, val, 1.0)
191UNARY_OPERATOR(operator-, -val, -1.0)
192UNARY_OPERATOR(abs, abs(val), val == 0.0 ? 0.0 : (val > 0.0 ? 1.0 : -1.0))
193//UNARY_OPERATOR(acos, acos(val), -1.0 / sqrt(1.0 - val * val))
194//UNARY_OPERATOR(asin, asin(val), 1.0 / sqrt(1.0 - val * val))
195//UNARY_OPERATOR(atan, atan(val), 1.0 / (1.0 + val * val))
196//UNARY_OPERATOR(atanh, atanh(val), 1.0 / (1.0 - val * val))
197//UNARY_OPERATOR(cbrt, cbrt(val), 1.0 / (3.0 * cbrt(val) * cbrt(val)))
198//UNARY_OPERATOR(cos, cos(val), -sin(val))
199//UNARY_OPERATOR(cosh, cosh(val), sinh(val))
200//UNARY_OPERATOR(exp, exp(val), exp(val))
201//UNARY_OPERATOR(log, log(val), 1.0 / val)
202//UNARY_OPERATOR(log10, log10(val), 0.434294481903252 / val)
203//UNARY_OPERATOR(sin, sin(val), cos(val))
204//UNARY_OPERATOR(sinh, sinh(val), cosh(val))
205UNARY_OPERATOR(sqrt, sqrt(val), 0.5 / sqrt(val))
206//UNARY_OPERATOR(tan, tan(val), 1.0 / cos(val) / cos(val))
207//UNARY_OPERATOR(tanh, tanh(val), 1.0 - tanh(val) * tanh(val))
208#undef UNARY_OPERATOR
209
210#define CONDITION_OPERATOR(OP) \
211template<typename Tape> \
212bool operator OP(ActiveType<Tape> const& arg1, ActiveType<Tape> const& arg2) { \
213 return arg1.value() OP arg2.value(); \
214}
215
224
225#undef CONDITION_OPERATOR
226
227#define CONDITION_OPERATOR(OP) \
228template<typename Tape> \
229bool operator OP(ActiveType<Tape> const& arg) { \
230 return OP arg.value(); \
231}
232
233using std::swap;
234
235template<typename Tape>
237 swap(arg1.value(), arg2.value());
238 swap(arg1.getIdentifier(), arg2.getIdentifier());
239}
240
242#undef CONDITION_OPERATOR
243
244template<typename T_Real>
245struct Tape {
246public:
247 using Real = T_Real;
248 using OpType = uint8_t;
249
251
252 std::vector<Real> jacobianVector;
253 std::vector<int> idVector;
254 std::vector<OpType> opVector;
255
256 std::vector<Real> adjoints;
257
258private:
259 void pushJacobian(int const& id, Real const& jacobi) {
260 jacobianVector.push_back(jacobi);
261 idVector.push_back(id);
262 }
263
264 void pushStmt(OpType const& opType) {
265 opVector.push_back(opType);
266 }
267
268 void assignValue(ActiveType<Tape>& lhs, Real const& lhsValue) {
269 maxIdentifier += 1;
270
271 lhs.value() = lhsValue;
272 lhs.getIdentifier() = maxIdentifier;
273 }
274
275 void resizeAdjointVector() {
276 if((int)adjoints.size() <= maxIdentifier) {
277 adjoints.resize(maxIdentifier + 1);
278 }
279 }
280
281public:
282 void store(ActiveType<Tape>& lhs, Real const& lhsValue) {
283 // Register input always store a statement
284 pushStmt(0);
285
286 assignValue(lhs, lhsValue);
287 }
288
289 void store(ActiveType<Tape>& lhs, Real const& lhsValue, ActiveType<Tape> const& rhs, Real const& rhsJac) {
290 pushJacobian(rhs.getIdentifier(), rhsJac);
291 pushStmt(1);
292
293 assignValue(lhs, lhsValue);
294 }
295
296 void store(ActiveType<Tape>& lhs, Real const& lhsValue,
297 ActiveType<Tape> const& rhs1, Real const& rhs1Jac,
298 ActiveType<Tape> const& rhs2, Real const& rhs2Jac) {
299 pushJacobian(rhs1.getIdentifier(), rhs1Jac);
300 pushJacobian(rhs2.getIdentifier(), rhs2Jac);
301 pushStmt(2);
302
303 assignValue(lhs, lhsValue);
304 }
305
306 void evaluate() {
307
308 resizeAdjointVector();
309
310 int lhsId = maxIdentifier;
311
312 int stmtPos = (size_t)opVector.size();
313 int argPos = (size_t)jacobianVector.size();
314
315 while(lhsId > 0) {
316
317 stmtPos -= 1;
318
319 OpType nArgs = opVector[stmtPos];
320 Real adjoint = adjoints[lhsId];
321
322 for(OpType i = 0; i < nArgs; i += 1) {
323 argPos -= 1;
324 adjoints[idVector[argPos]] += jacobianVector[argPos] * adjoint;
325 }
326
327 lhsId -= 1;
328 }
329 }
330
331 void reset() {
332 maxIdentifier = 0;
333
334 jacobianVector.clear();
335 idVector.clear();
336 opVector.clear();
337
338 std::fill(adjoints.begin(), adjoints.end(), Real());
339 }
340
342 store(value, value.value());
343 }
344
346 store(value, value.value(), value, Real(1.0));
347 }
348
349 Real& gradient(int const& id) {
350
351 resizeAdjointVector();
352
353 if(id <= maxIdentifier) {
354 return adjoints[id];
355 } else {
356 return adjoints[0];
357 }
358 }
359};
360
362
363}
364
365template <class TAPE>
367{
368 return util::ADtape::sqrt(a);
369}
370
371}
372
373}
374
375#endif
#define OPERATOR(OP, rhs)
Definition aDiffTape.h:64
#define UNARY_OPERATOR(OP, VAL, JAC)
Definition aDiffTape.h:161
#define BINARY_OPERATOR(OP, VAL, JAC1, JAC2)
Definition aDiffTape.h:109
#define CONDITION_OPERATOR(OP)
val1 val2 val2 log(val1) *pow(val1
sqrt(val)) using std void swap(ActiveType< Tape > &arg1, ActiveType< Tape > &arg2)
Definition aDiffTape.h:236
ADf< T, DIM > abs(const ADf< T, DIM > &a)
Definition aDiff.h:1019
Expr sqrt(Expr x)
Definition expr.cpp:225
Top level namespace for all of OpenLB.
Real const & value() const
Definition aDiffTape.h:81
Real const & gradient() const
Definition aDiffTape.h:97
int const & getIdentifier() const
Definition aDiffTape.h:89
ActiveType(Real const &value)
Definition aDiffTape.h:53
ActiveType(ActiveType const &value)=default
ActiveType & operator=(Real const &value)
Definition aDiffTape.h:55
typename Tape::Real Real
Definition aDiffTape.h:39
std::vector< Real > adjoints
Definition aDiffTape.h:256
void store(ActiveType< Tape > &lhs, Real const &lhsValue, ActiveType< Tape > const &rhs, Real const &rhsJac)
Definition aDiffTape.h:289
void registerInput(ActiveType< Tape > &value)
Definition aDiffTape.h:341
Real & gradient(int const &id)
Definition aDiffTape.h:349
void store(ActiveType< Tape > &lhs, Real const &lhsValue)
Definition aDiffTape.h:282
void registerOutput(ActiveType< Tape > &value)
Definition aDiffTape.h:345
void store(ActiveType< Tape > &lhs, Real const &lhsValue, ActiveType< Tape > const &rhs1, Real const &rhs1Jac, ActiveType< Tape > const &rhs2, Real const &rhs2Jac)
Definition aDiffTape.h:296
std::vector< Real > jacobianVector
Definition aDiffTape.h:252
std::vector< OpType > opVector
Definition aDiffTape.h:254
std::vector< int > idVector
Definition aDiffTape.h:253