CoDiPack  2.2.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-2024 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/lhsExpressionInterface.hpp"
39#include "../expressions/logic/helpers/jacobianComputationLogic.hpp"
40#include "../misc/macros.hpp"
41#include "../traits/expressionTraits.hpp"
42#include "../traits/realTraits.hpp"
43#include "../traits/tapeTraits.hpp"
44#include "interfaces/gradientAccessTapeInterface.hpp"
45#include "interfaces/internalStatementRecordingTapeInterface.hpp"
46
48namespace codi {
49
70 template<typename T_Real, typename T_Gradient>
72 public GradientAccessTapeInterface<T_Gradient, T_Gradient> {
73 public:
74
75 using Real = CODI_DD(T_Real, double);
76 using Gradient = CODI_DD(T_Gradient, double);
77
80
81 /*******************************************************************************/
84
85 static bool constexpr AllowJacobianOptimization = true;
86
88 template<typename Real>
89 CODI_INLINE void initIdentifier(Real& value, Identifier& identifier) {
90 CODI_UNUSED(value);
91 identifier = Identifier();
92 }
93
95 template<typename Real>
96 CODI_INLINE void destroyIdentifier(Real& value, Identifier& identifier) {
97 CODI_UNUSED(value, identifier);
98 }
99
101
102 private:
103
104 struct LocalReverseLogic : public JacobianComputationLogic<LocalReverseLogic> {
105 public:
106 template<typename Node>
107 CODI_INLINE void handleJacobianOnActive(Node const& node, Real jacobian, Gradient& lhsGradient) {
109 lhsGradient += node.gradient() * jacobian;
110 }
111 }
112 };
113
114 public:
115
117
119 template<typename Lhs, typename Rhs>
122 LocalReverseLogic reversal;
123
124 Gradient newGradient = Gradient();
125 reversal.eval(rhs.cast(), 1.0, newGradient);
126
127 lhs.cast().value() = rhs.cast().getValue();
128 lhs.cast().gradient() = newGradient;
129 }
130
133 template<typename Lhs, typename Rhs>
136 lhs.cast().value() = rhs.cast().getValue();
137 lhs.cast().gradient() = rhs.cast().getGradient();
138 }
139
142 template<typename Lhs>
144 lhs.cast().value() = rhs;
145 lhs.cast().gradient() = Gradient();
146 }
147
149 /*******************************************************************************/
152
158 CODI_UNUSED(adjointsManagement);
159 identifier = gradient;
160 }
161
166 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
167 CODI_UNUSED(adjointsManagement);
168 return identifier;
169 }
170
176 CODI_UNUSED(adjointsManagement);
177 return identifier;
178 }
179
184 Identifier const& identifier, AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
185 CODI_UNUSED(adjointsManagement);
186 return identifier;
187 }
188
190
191 private:
192
193 CODI_INLINE void setGradient(Identifier const& identifier, Gradient const& gradient) {
194 CODI_UNUSED(identifier, gradient);
195 }
196
197 CODI_INLINE Gradient& gradient(Identifier const& identifier) {
198 CODI_UNUSED(identifier);
199 static Gradient temp = Gradient();
200 return temp;
201 }
202 };
203
206 template<typename T_Type>
207 struct RealTraits::IsTotalFinite<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
208 public:
209
210 using Type = CODI_DD(
212 T_Type>));
213
215 static CODI_INLINE bool isTotalFinite(Type const& v) {
216 using std::isfinite;
217 return RealTraits::isTotalFinite(v.getValue()) && RealTraits::isTotalFinite(v.getGradient());
218 }
219 };
220
223 template<typename T_Type>
224 struct RealTraits::IsTotalZero<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>> {
225 public:
226
227 using Type = CODI_DD(
229 T_Type>));
230 using Real = typename Type::Real;
232
234 static CODI_INLINE bool isTotalZero(Type const& v) {
235 return Real() == v.getValue() && typename Type::Gradient() == v.getGradient();
236 }
237 };
238}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:53
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
bool constexpr IgnoreInvalidJacobians
Ignore invalid Jacobians like NaN or Inf.
Definition config.h:240
typename TraitsImplementation< Type >::Real Real
Inner type of the real value.
Definition realTraits.hpp:113
bool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:133
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition realTraits.hpp:117
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:98
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:59
Impl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:75
Implementation of a tape-free forward AD mode through the internal expression interfaces.
Definition forwardEvaluation.hpp:72
T_Gradient Gradient
See ForwardEvaluation.
Definition forwardEvaluation.hpp:76
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition forwardEvaluation.hpp:78
void 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:134
void 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:143
void setGradient(Identifier &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Set the gradient.
Definition forwardEvaluation.hpp:156
void destroyIdentifier(Real &value, Identifier &identifier)
Has to be called for each identifier, before it is deallocated.
Definition forwardEvaluation.hpp:96
T_Real Real
See ForwardEvaluation.
Definition forwardEvaluation.hpp:75
void 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:120
Gradient const & getGradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Set the gradient.
Definition forwardEvaluation.hpp:165
static bool constexpr AllowJacobianOptimization
See InternalStatementRecordingTapeInterface.
Definition forwardEvaluation.hpp:85
void initIdentifier(Real &value, Identifier &identifier)
Definition forwardEvaluation.hpp:89
Gradient & gradient(Identifier &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Reference access to gradient.
Definition forwardEvaluation.hpp:174
Gradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Constant reference access to gradient.
Definition forwardEvaluation.hpp:183
Gradient Identifier
Same as the gradient type. Tangent data is stored in the active types.
Definition forwardEvaluation.hpp:79
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:53
void link(Child const &child, Root const &root, Jacobian const &jacobian, Args &&... args)
Definition jacobianComputationLogic.hpp:84
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
Impl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
T_Type Type
See RealTraits::IsTotalFinite.
Definition forwardEvaluation.hpp:210
static bool isTotalFinite(Type const &v)
Checks if the values are all finite.
Definition forwardEvaluation.hpp:215
Function for checking if all values of the type are finite.
Definition realTraits.hpp:82
static bool isTotalZero(Type const &v)
Checks if the values are completely zero.
Definition forwardEvaluation.hpp:234
T_Type Type
See RealTraits::IsTotalZero.
Definition forwardEvaluation.hpp:227
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:100
void node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition traversalLogic.hpp:86