CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
aggregatedActiveType.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 "../../misc/compileTimeLoop.hpp"
39#include "../../misc/eventSystem.hpp"
40#include "../../misc/macros.hpp"
41#include "../../traits/realTraits.hpp"
42#include "../lhsExpressionInterface.hpp"
43#include "../logic/constructStaticContext.hpp"
44
46namespace codi {
47
58 template<typename T_Real, typename T_InnerActiveType, typename T_Impl, bool T_isStatic>
59 struct AggregatedActiveTypeBase : public ExpressionInterface<T_Real, T_Impl> {
60 public:
61
62 using Real = T_Real;
63 using InnerActiveType = CODI_DD(T_InnerActiveType,
64 CODI_DEFAULT_LHS_EXPRESSION);
66 static bool constexpr isStatic = T_isStatic;
67
68 using Tape = typename InnerActiveType::Tape;
70 static int constexpr Elements = Traits::Elements;
71
72 using InnerReal = typename Traits::InnerType;
73 using InnerIdentifier = typename Tape::Identifier;
74
76
80
81 /*******************************************************************************/
83
85 using StoreAs = typename std::conditional<isStatic, Impl, Impl const&>::type;
86 using ADLogic = Tape;
87
89 CODI_INLINE Real const getValue() const {
90 Real value{};
92 Traits::template arrayAccess<i.value>(value) = values[i.value].getValue();
93 });
94 return value;
95 }
96
98 template<size_t argNumber>
99 CODI_INLINE Real applyTangent(InnerReal const& tangent) const {
100 CODI_UNUSED(tangent);
101 return Real(); // TODO: return Traits::template tangentOfConstructor<argNumber>(getValue(), adjoint);
102 }
103
107 template<size_t argNumber>
108 CODI_INLINE InnerReal applyAdjoint(Real const& adjoint) const {
109 return Traits::template adjointOfConstructor<argNumber>(getValue(), adjoint);
110 }
111
113 static CODI_INLINE std::string getMathRep() {
114 return Traits::getMathRep();
115 }
116
117 /*******************************************************************************/
119
120 static size_t constexpr LinkCount = Elements;
121
123 template<size_t argNumber>
125 return values[argNumber];
126 }
127
128 protected:
131 CODI_INLINE Impl const& cast() const {
132 return static_cast<Impl const&>(*this);
133 }
134
137 return static_cast<Impl&>(*this);
138 }
139 };
140
163 template<typename T_Real, typename T_InnerActiveType, typename T_Impl>
164 struct AggregatedActiveType : public AggregatedActiveTypeBase<T_Real, T_InnerActiveType, T_Impl, false> {
165 public:
166 using Real = T_Real;
167 using InnerActiveType = CODI_DD(T_InnerActiveType, CODI_DEFAULT_LHS_EXPRESSION);
169
170 using Base =
174
175 using Base::Base;
177
179 template<typename Expr>
181 store(expr, EventHints::Statement::Expression);
182 }
183
186 store(expr, EventHints::Statement::Copy);
187 }
188
192 Base::values[i.value] = Traits::template arrayAccess<i.value>(expr);
193 });
194 }
195
197 template<typename Expr>
199 store(expr, EventHints::Statement::Expression);
200
201 return Base::cast();
202 }
203
206 store(expr, EventHints::Statement::Copy);
207
208 return Base::cast();
209 }
210
214 Base::values[i.value] = Traits::template arrayAccess<i.value>(expr);
215 });
216
217 return Base::cast();
218 }
219
220 protected:
221
223 template<typename Rhs>
225 static_for<Traits::Elements>([&](auto i) {
226 InnerActiveType& lhs = Base::values[i.value];
228 InnerActiveType::getTape(), lhs.getValue(), lhs.getIdentifier(),
229 Traits::template arrayAccess<i.value>(rhs.cast().getValue()), eventType);
230 });
231
232 InnerActiveType::getTape().store(*this, rhs.cast());
233 }
234 };
235}
#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
Statement
Classify statements.
Definition eventSystem.hpp:65
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
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
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
Defines an aggregated type via an array and implements the ExpressionInterface.
Definition aggregatedActiveType.hpp:59
typename Tape::Identifier InnerIdentifier
Identifier of the underlying tape.
Definition aggregatedActiveType.hpp:73
inlineInnerReal applyAdjoint(Real const &adjoint) const
Definition aggregatedActiveType.hpp:108
T_Impl Impl
See AggregatedActiveTypeBase.
Definition aggregatedActiveType.hpp:65
inlineInnerActiveType const & getLink() const
an expression.
Definition aggregatedActiveType.hpp:124
typename Traits::InnerType InnerReal
Inner real type of the active type.
Definition aggregatedActiveType.hpp:72
inlineReal const getValue() const
Compute the primal value that is usually evaluated by the statement/expression.
Definition aggregatedActiveType.hpp:89
T_InnerActiveType InnerActiveType
See AggregatedActiveTypeBase.
Definition aggregatedActiveType.hpp:63
InnerActiveType values[Elements]
Definition aggregatedActiveType.hpp:75
inlineReal applyTangent(InnerReal const &tangent) const
Definition aggregatedActiveType.hpp:99
inline ~AggregatedActiveTypeBase()=default
Destructor.
static bool constexpr isStatic
Definition aggregatedActiveType.hpp:66
static size_t constexpr LinkCount
Definition aggregatedActiveType.hpp:120
T_Real Real
See AggregatedActiveTypeBase.
Definition aggregatedActiveType.hpp:62
inlineImpl & cast()
Cast to implementation.
Definition aggregatedActiveType.hpp:136
Tape ADLogic
AD logic that governs the expression. Needs to be the same for all inputs of the expression.
Definition aggregatedActiveType.hpp:86
typename InnerActiveType::Tape Tape
The tape of the inner active type.
Definition aggregatedActiveType.hpp:68
typename std::conditional< isStatic, Impl, Impl const & >::type StoreAs
Implementation of ExpressionInterface.
Definition aggregatedActiveType.hpp:85
RealTraits::AggregatedTypeTraits< Real > Traits
The traits for the aggregated type.
Definition aggregatedActiveType.hpp:69
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition aggregatedActiveType.hpp:113
inlineImpl const & cast() const
Cast to implementation.
Definition aggregatedActiveType.hpp:131
static int constexpr Elements
Definition aggregatedActiveType.hpp:70
Represents a concrete aggregated lvalue int the CoDiPack expression tree.
Definition aggregatedActiveType.hpp:164
T_Real Real
See AggregatedActiveType.
Definition aggregatedActiveType.hpp:166
inline ~AggregatedActiveType()=default
< Use base constructors.
RealTraits::PassiveReal< Real > PassiveReal
Passive value type of the real.
Definition aggregatedActiveType.hpp:173
inlineImpl & operator=(AggregatedActiveType const &expr)
Assign operation.
Definition aggregatedActiveType.hpp:205
T_InnerActiveType InnerActiveType
See AggregatedActiveType.
Definition aggregatedActiveType.hpp:167
AggregatedActiveTypeBase< T_Real, T_InnerActiveType, T_Impl, false > Base
Abbreviation for base class.
Definition aggregatedActiveType.hpp:170
inlineImpl & operator=(PassiveReal const &expr)
Assign operation.
Definition aggregatedActiveType.hpp:212
RealTraits::AggregatedTypeTraits< Real > Traits
The traits for the aggregated type.
Definition aggregatedActiveType.hpp:172
inlinevoid store(ExpressionInterface< Real, Rhs > const &rhs, EventHints::Statement const &eventType)
Has to be called by an AD variable every time it is assigned.
Definition aggregatedActiveType.hpp:224
T_Impl Impl
See AggregatedActiveType.
Definition aggregatedActiveType.hpp:168
inlineImpl & operator=(ExpressionInterface< Real, Expr > const &expr)
Assign operation.
Definition aggregatedActiveType.hpp:198
static inlinevoid notifyStatementPrimalListeners(Tape &tape, Real const &lhsValue, Identifier const &lhsIdentifier, Real const &newValue, EventHints::Statement statement)
Invoke callbacks for StatementPrimal events.
Definition eventSystem.hpp:264
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
ExpressionInterface()=default
Constructor.
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
int InnerType
Definition realTraits.hpp:229
static int constexpr Elements
Definition realTraits.hpp:232
static inlinestd::string getMathRep()
Definition realTraits.hpp:291