CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
aggregatedTypeVectorAccessWrapper.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 <complex>
38#include <vector>
39
40#include "../../config.h"
41#include "../../expressions/aggregate/aggregatedActiveType.hpp"
42#include "../../expressions/lhsExpressionInterface.hpp"
43#include "../../misc/macros.hpp"
44#include "../../tapes/misc/vectorAccessInterface.hpp"
45#include "../../traits/computationTraits.hpp"
46#include "../../traits/expressionTraits.hpp"
47#include "../../traits/realTraits.hpp"
48
50namespace codi {
51
77 template<typename T_Type, typename = void>
78 struct AggregatedTypeVectorAccessWrapper : public VectorAccessInterface<CODI_ANY, CODI_ANY> {
79 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
80 "Instantiation of unspecialized AggregatedTypeVectorAccessWrapper.");
81
82 using Type = CODI_DD(T_Type, CODI_ANY);
83 };
84
88 template<typename T_Type>
89 struct AggregatedTypeVectorAccessWrapper<T_Type, RealTraits::EnableIfAggregatedActiveType<T_Type>> {
90 public:
91
92 using Type = CODI_DD(
93 T_Type,
95 using InnerType = typename Type::InnerActiveType;
96 static int constexpr Elements = Type::Elements;
97
100
104
106
109
110 std::vector<Real> buffer;
111
115
116 /*******************************************************************************/
118
120 CODI_INLINE size_t getVectorSize() const {
121 return innerInterface.getVectorSize();
122 }
123
125 CODI_INLINE bool isLhsZero() const {
126 bool isZero = true;
127
129 innerInterface.setActiveVariableForIndirectAccess(lhsOffset + i.value);
130 isZero &= innerInterface.isLhsZero();
131 });
132
133 return isZero;
134 }
135
136 /*******************************************************************************/
138
142 innerInterface.setActiveVariableForIndirectAccess(lhsOffset + i.value);
143 innerInterface.setLhsAdjoint(index[i.value]);
144 });
145 }
146
148 CODI_INLINE void updateAdjointWithLhs(Identifier const& index, Real const& jacobian) {
150 innerInterface.setActiveVariableForIndirectAccess(lhsOffset + i.value);
151 innerInterface.updateAdjointWithLhs(index[i.value], Traits::template arrayAccess<i.value>(jacobian));
152 });
153 }
154
155 /*******************************************************************************/
157
161 innerInterface.setActiveVariableForIndirectAccess(lhsOffset + i.value);
162 innerInterface.setLhsTangent(index[i.value]);
163 });
164 }
165
167 CODI_INLINE void updateTangentWithLhs(Identifier const& index, Real const& jacobian) {
169 innerInterface.setActiveVariableForIndirectAccess(lhsOffset + i.value);
170 innerInterface.updateTangentWithLhs(index[i.value], Traits::template arrayAccess<i.value>(jacobian));
171 });
172 }
173
174 /*******************************************************************************/
176
181
182 /*******************************************************************************/
184
186 CODI_INLINE void resetAdjoint(Identifier const& index, size_t dim) {
188 innerInterface.resetAdjoint(index[i.value], dim);
189 });
190 }
191
195 innerInterface.resetAdjointVec(index[i.value]);
196 });
197 }
198
200 CODI_INLINE Real getAdjoint(Identifier const& index, size_t dim) {
201 Real adjoint{};
202
204 Traits::template arrayAccess<i.value>(adjoint) = innerInterface.getAdjoint(index[i.value], dim);
205 });
206
207 return adjoint;
208 }
209
211 CODI_INLINE void getAdjointVec(Identifier const& index, Real* const vec) {
212 for (size_t curDim = 0; curDim < innerInterface.getVectorSize(); curDim += 1) {
213 vec[curDim] = this->getAdjoint(index, curDim);
214 }
215 }
216
218 Real const* getAdjointVec(Identifier const& index) {
219 getAdjointVec(index, buffer.data());
220 return buffer.data();
221 }
222
224 CODI_INLINE void updateAdjoint(Identifier const& index, size_t dim, Real const& adjoint) {
226 innerInterface.updateAdjoint(index[i.value], dim, Traits::template arrayAccess<i.value>(adjoint));
227 });
228 }
229
231 CODI_INLINE void updateAdjointVec(Identifier const& index, Real const* const vec) {
232 for (size_t curDim = 0; curDim < innerInterface.getVectorSize(); curDim += 1) {
233 this->updateAdjoint(index, curDim, vec[curDim]);
234 }
235 }
236
237 /*******************************************************************************/
239
242 return innerInterface.hasPrimals();
243 }
244
246 CODI_INLINE void setPrimal(Identifier const& index, Real const& primal) {
248 innerInterface.setPrimal(index[i.value], Traits::template arrayAccess<i.value>(primal));
249 });
250 }
251
254 Real primal{};
255
257 Traits::template arrayAccess<i.value>(primal) = innerInterface.getPrimal(index[i.value]);
258 });
259
260 return primal;
261 }
262 };
263
275 template<typename T_Type, typename = void>
277 public:
278 using Type = CODI_DD(T_Type, CODI_ANY);
279
281
285 template<typename Real, typename Identifier>
287 return new RType(access);
288 }
289
291 static void destroy(RType* access) {
292 delete access;
293 }
294 };
295
296#ifndef DOXYGEN_DISABLE
300 template<typename T_Type>
301 struct AggregatedTypeVectorAccessWrapperFactory<T_Type, ExpressionTraits::EnableIfLhsExpression<T_Type>> {
302 public:
303 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
304
305 using RType = VectorAccessInterface<typename Type::Real, typename Type::Identifier>;
306
308 static RType* create(RType* access) {
309 return access;
310 }
311
313 static void destroy(RType* access) {
314 CODI_UNUSED(access);
315
316 // Do nothing
317 }
318 };
319#endif
320}
#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_ANY
Used in default declarations of expression templates.
Definition macros.hpp:100
#define CODI_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:129
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:204
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
Represents a concrete aggregated lvalue int the CoDiPack expression tree.
Definition aggregatedActiveType.hpp:164
Factory for the creation of AggregatedTypeVectorAccessWrapper instances.
Definition aggregatedTypeVectorAccessWrapper.hpp:276
AggregatedTypeVectorAccessWrapper< Type > RType
Which instances this factory creates.
Definition aggregatedTypeVectorAccessWrapper.hpp:280
static void destroy(RType *access)
Delete the AggregatedTypeVectorAccessWrapper instance created by the crate method.
Definition aggregatedTypeVectorAccessWrapper.hpp:291
T_Type Type
See AggregatedTypeVectorAccessWrapperBase.
Definition aggregatedTypeVectorAccessWrapper.hpp:278
static RType * create(VectorAccessInterface< Real, Identifier > *access)
Definition aggregatedTypeVectorAccessWrapper.hpp:286
inlinebool isLhsZero() const
True if the adjoint set with setLhsAdjoint is zero.
Definition aggregatedTypeVectorAccessWrapper.hpp:125
int lhsOffset
Offset of indirect access if this aggregated type is part of an outer aggregated type.
Definition aggregatedTypeVectorAccessWrapper.hpp:108
inlinevoid updateTangentWithLhs(Identifier const &index, Real const &jacobian)
Definition aggregatedTypeVectorAccessWrapper.hpp:167
inlinevoid updateAdjointVec(Identifier const &index, Real const *const vec)
Update the adjoint entry.
Definition aggregatedTypeVectorAccessWrapper.hpp:231
inlinevoid setLhsAdjoint(Identifier const &index)
Definition aggregatedTypeVectorAccessWrapper.hpp:140
inlinevoid setActiveVariableForIndirectAccess(size_t pos)
Definition aggregatedTypeVectorAccessWrapper.hpp:178
typename Type::InnerActiveType InnerType
Active inner type of the aggregate.
Definition aggregatedTypeVectorAccessWrapper.hpp:95
typename RealTraits::DataExtraction< T_Type >::Identifier Identifier
Identifier value of the aggregate.
Definition aggregatedTypeVectorAccessWrapper.hpp:102
std::vector< Real > buffer
Temporary storage for getAdjointVec.
Definition aggregatedTypeVectorAccessWrapper.hpp:110
inlinevoid resetAdjointVec(Identifier const &index)
Set the adjoint entry to zero.
Definition aggregatedTypeVectorAccessWrapper.hpp:193
inlineReal getAdjoint(Identifier const &index, size_t dim)
Get the adjoint component.
Definition aggregatedTypeVectorAccessWrapper.hpp:200
InnerInterface & innerInterface
Reference to inner interface.
Definition aggregatedTypeVectorAccessWrapper.hpp:107
inlinesize_t getVectorSize() const
Vector size in the current tape evaluation.
Definition aggregatedTypeVectorAccessWrapper.hpp:120
typename RealTraits::DataExtraction< T_Type >::Real Real
Real value of the aggregate.
Definition aggregatedTypeVectorAccessWrapper.hpp:101
Real const * getAdjointVec(Identifier const &index)
Get the adjoint entry.
Definition aggregatedTypeVectorAccessWrapper.hpp:218
VectorAccessInterface< typename InnerType::Real, typename InnerType::Identifier > InnerInterface
Inner interface expected by this wrapper.
Definition aggregatedTypeVectorAccessWrapper.hpp:99
inlinebool hasPrimals()
True if the tape/vector interface has primal values.
Definition aggregatedTypeVectorAccessWrapper.hpp:241
inlinevoid updateAdjointWithLhs(Identifier const &index, Real const &jacobian)
Definition aggregatedTypeVectorAccessWrapper.hpp:148
inlinevoid getAdjointVec(Identifier const &index, Real *const vec)
Get the adjoint entry.
Definition aggregatedTypeVectorAccessWrapper.hpp:211
static int constexpr Elements
Number of elements in the aggregate.
Definition aggregatedTypeVectorAccessWrapper.hpp:96
T_Type Type
See AggregatedTypeVectorAccessWrapper.
Definition aggregatedTypeVectorAccessWrapper.hpp:92
inlinevoid setLhsTangent(Identifier const &index)
Definition aggregatedTypeVectorAccessWrapper.hpp:159
inlinevoid setPrimal(Identifier const &index, Real const &primal)
Set the primal value.
Definition aggregatedTypeVectorAccessWrapper.hpp:246
inlineReal getPrimal(Identifier const &index)
Get the primal value.
Definition aggregatedTypeVectorAccessWrapper.hpp:253
RealTraits::AggregatedTypeTraits< Real > Traits
Aggregated type traits.
Definition aggregatedTypeVectorAccessWrapper.hpp:105
inlinevoid resetAdjoint(Identifier const &index, size_t dim)
Set the adjoint component to zero.
Definition aggregatedTypeVectorAccessWrapper.hpp:186
inlinevoid updateAdjoint(Identifier const &index, size_t dim, Real const &adjoint)
Update the adjoint component.
Definition aggregatedTypeVectorAccessWrapper.hpp:224
Generalized wrapper of the VectorAccessInterface for aggregated data types, e.g. std::complex<codi::R...
Definition aggregatedTypeVectorAccessWrapper.hpp:78
T_Type Type
See AggregatedTypeVectorAccessWrapperBase.
Definition aggregatedTypeVectorAccessWrapper.hpp:82
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
typename Type::Real Real
Type of a corresponding aggregate of primal values.
Definition realTraits.hpp:171
typename Type::Identifier Identifier
Type of a corresponding aggregate of identifiers.
Definition realTraits.hpp:172
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94
virtual void getAdjointVec(Identifier const &index, Real *const vec)=0
virtual void updateAdjoint(Identifier const &index, size_t dim, Real const &adjoint)=0
virtual Real getAdjoint(Identifier const &index, size_t dim)=0