CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
forwardEvaluation.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include "../config.h"
38#include "../expressions/aggregate/aggregatedActiveType.hpp"
39#include "../expressions/aggregate/arrayAccessExpression.hpp"
40#include "../expressions/lhsExpressionInterface.hpp"
41#include "../expressions/logic/helpers/jacobianComputationLogic.hpp"
42#include "../misc/macros.hpp"
43#include "../traits/expressionTraits.hpp"
44#include "../traits/realTraits.hpp"
45#include "../traits/tapeTraits.hpp"
46#include "interfaces/gradientAccessTapeInterface.hpp"
47#include "interfaces/internalStatementRecordingTapeInterface.hpp"
48
50namespace codi {
51
72 template<typename T_Real, typename T_Gradient>
74 public GradientAccessTapeInterface<T_Gradient, T_Gradient> {
75 public:
76
77 using Real = CODI_DD(T_Real, double);
78 using Gradient = CODI_DD(T_Gradient, double);
79
83
84 /*******************************************************************************/
87
88 static bool constexpr AllowJacobianOptimization = true;
89
91 template<typename Real>
93 CODI_UNUSED(value);
94 data = ActiveTypeTapeData();
95 }
96
98 template<typename Real>
100 CODI_UNUSED(value, data);
101 }
102
104
105 private:
106
107 struct LocalReverseLogic : public JacobianComputationLogic<LocalReverseLogic> {
108 public:
109 template<typename Node>
110 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient& lhsGradient) {
112 lhsGradient += node.gradient() * jacobian;
113 }
114 }
115 };
116
117 public:
118
120
122 template<typename Lhs, typename Rhs>
125 LocalReverseLogic reversal;
126
127 Gradient newGradient = Gradient();
128 reversal.eval(rhs.cast(), 1.0, newGradient);
129
130 lhs.cast().value() = rhs.cast().getValue();
131 lhs.cast().gradient() = newGradient;
132 }
133
136 template<typename Aggregated, typename Type, typename Lhs, typename Rhs>
139 using AggregatedTraits = RealTraits::AggregatedTypeTraits<Aggregated>;
140 int constexpr Elements = AggregatedTraits::Elements;
141 LocalReverseLogic reversal;
142
143 Gradient newGradient[Elements] = {};
145 reversal.eval(ArrayAccessExpression<Aggregated, i.value, Rhs>(rhs), Real(1.0), newGradient[i.value]);
146 });
147
148 Aggregated newValue = rhs.cast().getValue();
150 lhs.values[i.value].value() = AggregatedTraits::template arrayAccess<i.value>(newValue);
151 lhs.values[i.value].gradient() = newGradient[i.value];
152 });
153 }
154
157 template<typename Lhs, typename Rhs>
160 lhs.cast().value() = rhs.cast().getValue();
161 lhs.cast().gradient() = rhs.cast().getGradient();
162 }
163
166 template<typename Lhs>
168 lhs.cast().value() = rhs;
169 lhs.cast().gradient() = Gradient();
170 }
171
173 /*******************************************************************************/
176
182 CODI_UNUSED(adjointsManagement);
183 identifier = gradient;
184 }
185
190 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
191 CODI_UNUSED(adjointsManagement);
192 return identifier;
193 }
194
200 CODI_UNUSED(adjointsManagement);
201 return identifier;
202 }
203
208 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
209 CODI_UNUSED(adjointsManagement);
210 return identifier;
211 }
212
213 /*******************************************************************************/
216
219 return data;
220 }
221
224 return data;
225 }
226
228
229 private:
230
231 CODI_INLINE void setGradient(Identifier const& identifier, Gradient const& gradient) {
232 CODI_UNUSED(identifier, gradient);
233 }
234
235 CODI_INLINE Gradient& gradient(Identifier const& identifier) {
236 CODI_UNUSED(identifier);
237 static Gradient temp = Gradient();
238 return temp;
239 }
240 };
241
244 template<typename T_Type>
245 struct RealTraits::IsTotalFinite<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
246 public:
247
248 using Type = CODI_DD(
250 T_Type>));
251
253 static CODI_INLINE bool isTotalFinite(Type const& v) {
254 using std::isfinite;
255 return RealTraits::isTotalFinite(v.getValue()) && RealTraits::isTotalFinite(v.getGradient());
256 }
257 };
258
261 template<typename T_Type>
262 struct RealTraits::IsTotalZero<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
263 public:
264
265 using Type = CODI_DD(
267 T_Type>));
268 using Real = typename Type::Real;
270
272 static CODI_INLINE bool isTotalZero(Type const& v) {
273 return Real() == v.getValue() && typename Type::Gradient() == v.getGradient();
274 }
275 };
276}
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:62
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
bool constexpr IgnoreInvalidJacobians
Ignore invalid Jacobians like NaN or Inf.
Definition config.h:240
inlinebool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:139
typename TraitsImplementation< Type >::Real Real
Inner type of the real value.
Definition realTraits.hpp:119
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
Definition tapeTraits.hpp:63
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
inlinevoid static_for(F func, Args &&... args)
Static for with i = 0 .. (N - 1). See CompileTimeLoop for details.
Definition compileTimeLoop.hpp:110
typename ArrayAccessExpressionImpl< Aggregated, element >::template Expression< Arg > ArrayAccessExpression
Expression that performs a[element] in a compile time context.
Definition arrayAccessExpression.hpp:98
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:100
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Definition tapeParameters.hpp:102
InnerActiveType values[Elements]
Array representation.
Definition aggregatedActiveType.hpp:75
Represents a concrete aggregated lvalue int the CoDiPack expression tree.
Definition aggregatedActiveType.hpp:164
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
Implementation of a tape-free forward AD mode through the internal expression interfaces.
Definition forwardEvaluation.hpp:74
T_Gradient Gradient
See ForwardEvaluation.
Definition forwardEvaluation.hpp:78
inlinevoid setGradient(Identifier &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Set the gradient.
Definition forwardEvaluation.hpp:180
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition forwardEvaluation.hpp:80
inlineGradient const & getGradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Set the gradient.
Definition forwardEvaluation.hpp:189
inlinevoid store(AggregatedActiveType< Aggregated, Type, Lhs > &lhs, ExpressionInterface< Aggregated, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition forwardEvaluation.hpp:137
inlineIdentifier & getIdentifier(ActiveTypeTapeData &data)
Definition forwardEvaluation.hpp:223
inlinevoid destroyTapeData(Real &value, ActiveTypeTapeData &data)
Has to be called for each tape data, before it is deallocated.
Definition forwardEvaluation.hpp:99
inlinevoid store(LhsExpressionInterface< Real, Gradient, ForwardEvaluation, Lhs > &lhs, ExpressionInterface< Real, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition forwardEvaluation.hpp:123
T_Real Real
See ForwardEvaluation.
Definition forwardEvaluation.hpp:77
inlineGradient & gradient(Identifier &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reference access to gradient.
Definition forwardEvaluation.hpp:198
static bool constexpr AllowJacobianOptimization
See InternalStatementRecordingTapeInterface.
Definition forwardEvaluation.hpp:88
inlinevoid store(LhsExpressionInterface< Real, Gradient, ForwardEvaluation, Lhs > &lhs, Real const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition forwardEvaluation.hpp:167
Gradient ActiveTypeTapeData
Same as the gradient type. Tangent data is stored in the active types.
Definition forwardEvaluation.hpp:82
inlineGradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Constant reference access to gradient.
Definition forwardEvaluation.hpp:207
Gradient Identifier
Same as the gradient type. Tangent data is stored in the active types.
Definition forwardEvaluation.hpp:81
inlineIdentifier const & getIdentifier(ActiveTypeTapeData const &data)
Definition forwardEvaluation.hpp:218
inlinevoid store(LhsExpressionInterface< Real, Gradient, ForwardEvaluation, Lhs > &lhs, LhsExpressionInterface< Real, Gradient, ForwardEvaluation, Rhs > const &rhs)
Has to be called by an AD variable every time it is assigned.
Definition forwardEvaluation.hpp:158
inlinevoid initTapeData(Real &value, ActiveTypeTapeData &data)
< Has to be called for each tape data, after it is allocated.
Definition forwardEvaluation.hpp:92
Allow for a direct access to the gradient information computed by the tape.
Definition gradientAccessTapeInterface.hpp:67
Internal tape interface that is used by active types to trigger the storing of an expression.
Definition internalStatementRecordingTapeInterface.hpp:66
Definition jacobianComputationLogic.hpp:54
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:103
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:233
T_Type Type
See RealTraits::IsTotalFinite.
Definition forwardEvaluation.hpp:248
static inlinebool isTotalFinite(Type const &v)
Checks if the values are all finite.
Definition forwardEvaluation.hpp:253
Function for checking if all values of the type are finite.
Definition realTraits.hpp:88
static inlinebool isTotalZero(Type const &v)
Checks if the values are completely zero.
Definition forwardEvaluation.hpp:272
T_Type Type
See RealTraits::IsTotalZero.
Definition forwardEvaluation.hpp:265
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:106
inlinevoid node(Node const &node, Args &&... args)
Definition traversalLogic.hpp:87