CoDiPack  3.0.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-2025 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
82
83 /*******************************************************************************/
86
87 static bool constexpr AllowJacobianOptimization = true;
88
90 template<typename Real>
91 CODI_INLINE void initIdentifier(Real& value, Identifier& identifier) {
92 CODI_UNUSED(value);
93 identifier = Identifier();
94 }
95
97 template<typename Real>
98 CODI_INLINE void destroyIdentifier(Real& value, Identifier& identifier) {
99 CODI_UNUSED(value, identifier);
100 }
101
103
104 private:
105
106 struct LocalReverseLogic : public JacobianComputationLogic<LocalReverseLogic> {
107 public:
108 template<typename Node>
109 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient& lhsGradient) {
111 lhsGradient += node.gradient() * jacobian;
112 }
113 }
114 };
115
116 public:
117
119
121 template<typename Lhs, typename Rhs>
124 LocalReverseLogic reversal;
125
126 Gradient newGradient = Gradient();
127 reversal.eval(rhs.cast(), 1.0, newGradient);
128
129 lhs.cast().value() = rhs.cast().getValue();
130 lhs.cast().gradient() = newGradient;
131 }
132
135 template<typename Aggregated, typename Type, typename Lhs, typename Rhs>
138 using AggregatedTraits = RealTraits::AggregatedTypeTraits<Aggregated>;
139 int constexpr Elements = AggregatedTraits::Elements;
140 LocalReverseLogic reversal;
141
142 Gradient newGradient[Elements] = {};
144 reversal.eval(ArrayAccessExpression<Aggregated, i.value, Rhs>(rhs), Real(1.0), newGradient[i.value]);
145 });
146
147 Aggregated newValue = rhs.cast().getValue();
149 lhs.values[i.value].value() = AggregatedTraits::template arrayAccess<i.value>(newValue);
150 lhs.values[i.value].gradient() = newGradient[i.value];
151 });
152 }
153
156 template<typename Lhs, typename Rhs>
159 lhs.cast().value() = rhs.cast().getValue();
160 lhs.cast().gradient() = rhs.cast().getGradient();
161 }
162
165 template<typename Lhs>
167 lhs.cast().value() = rhs;
168 lhs.cast().gradient() = Gradient();
169 }
170
172 /*******************************************************************************/
175
181 CODI_UNUSED(adjointsManagement);
182 identifier = gradient;
183 }
184
189 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
190 CODI_UNUSED(adjointsManagement);
191 return identifier;
192 }
193
199 CODI_UNUSED(adjointsManagement);
200 return identifier;
201 }
202
207 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
208 CODI_UNUSED(adjointsManagement);
209 return identifier;
210 }
211
213
214 private:
215
216 CODI_INLINE void setGradient(Identifier const& identifier, Gradient const& gradient) {
217 CODI_UNUSED(identifier, gradient);
218 }
219
220 CODI_INLINE Gradient& gradient(Identifier const& identifier) {
221 CODI_UNUSED(identifier);
222 static Gradient temp = Gradient();
223 return temp;
224 }
225 };
226
229 template<typename T_Type>
230 struct RealTraits::IsTotalFinite<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
231 public:
232
233 using Type = CODI_DD(
235 T_Type>));
236
238 static CODI_INLINE bool isTotalFinite(Type const& v) {
239 using std::isfinite;
240 return RealTraits::isTotalFinite(v.getValue()) && RealTraits::isTotalFinite(v.getGradient());
241 }
242 };
243
246 template<typename T_Type>
247 struct RealTraits::IsTotalZero<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
248 public:
249
250 using Type = CODI_DD(
252 T_Type>));
253 using Real = typename Type::Real;
255
257 static CODI_INLINE bool isTotalZero(Type const& v) {
258 return Real() == v.getValue() && typename Type::Gradient() == v.getGradient();
259 }
260 };
261}
#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:96
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:61
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
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:94
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:54
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:179
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:188
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:136
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:122
T_Real Real
See ForwardEvaluation.
Definition forwardEvaluation.hpp:77
inlinevoid initIdentifier(Real &value, Identifier &identifier)
Definition forwardEvaluation.hpp:91
inlineGradient & gradient(Identifier &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reference access to gradient.
Definition forwardEvaluation.hpp:197
static bool constexpr AllowJacobianOptimization
See InternalStatementRecordingTapeInterface.
Definition forwardEvaluation.hpp:87
inlinevoid destroyIdentifier(Real &value, Identifier &identifier)
Has to be called for each identifier, before it is deallocated.
Definition forwardEvaluation.hpp:98
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:166
inlineGradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Constant reference access to gradient.
Definition forwardEvaluation.hpp:206
Gradient Identifier
Same as the gradient type. Tangent data is stored in the active types.
Definition forwardEvaluation.hpp:81
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:157
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:65
Definition jacobianComputationLogic.hpp:54
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
T_Type Type
See RealTraits::IsTotalFinite.
Definition forwardEvaluation.hpp:233
static inlinebool isTotalFinite(Type const &v)
Checks if the values are all finite.
Definition forwardEvaluation.hpp:238
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:257
T_Type Type
See RealTraits::IsTotalZero.
Definition forwardEvaluation.hpp:250
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