CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
adjointVectorAccess.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 <array>
38#include <cstddef>
39
40#include "../../config.h"
41#include "../../misc/macros.hpp"
42#include "../../tools/data/direction.hpp"
43#include "../../traits/adjointVectorTraits.hpp"
44#include "../../traits/realTraits.hpp"
45#include "vectorAccessInterface.hpp"
46
48namespace codi {
49
60 template<typename T_Real, typename T_Identifier, typename T_AdjointVector>
61 struct AdjointVectorAccess : public VectorAccessInterface<T_Real, T_Identifier> {
62 public:
63 using Real = CODI_DD(T_Real, double);
64 using Identifier = CODI_DD(T_Identifier, int);
66 using AdjointVector = CODI_DD(T_AdjointVector, double*);
67
68 protected:
69
71
72 private:
73
74 std::array<Gradient, Config::MaxArgumentSize> lhs;
76 size_t lhsPos;
77
78 std::array<Real, GradientTraits::dim<Gradient>()> buffer;
79
80 public:
81
84
85 /*******************************************************************************/
87
89 size_t getVectorSize() const {
91 }
92
94 bool isLhsZero() {
95 return RealTraits::isTotalZero(lhs[lhsPos]);
96 }
97
100 return new AdjointVectorAccess(this->adjointVector);
101 }
102
103 /*******************************************************************************/
105
107 void setLhsAdjoint(Identifier const& index) {
108 lhs[lhsPos] = adjointVector[index];
109 adjointVector[index] = Gradient();
110 }
111
113 void updateAdjointWithLhs(Identifier const& index, Real const& jacobian) {
114 adjointVector[index] += jacobian * lhs[lhsPos];
115 }
116
117 /*******************************************************************************/
119
121 void setLhsTangent(Identifier const& index) {
122 adjointVector[index] = lhs[lhsPos];
123 lhs[lhsPos] = Gradient();
124 }
125
127 void updateTangentWithLhs(Identifier const& index, Real const& jacobian) {
128 lhs[lhsPos] += jacobian * adjointVector[index];
129 }
130
131 /*******************************************************************************/
133
136 codiAssert(pos < lhs.size());
137 lhsPos = pos;
138 }
139
140 /*******************************************************************************/
142
144 void resetAdjoint(Identifier const& index, size_t dim) {
146 }
147
149 void resetAdjointVec(Identifier const& index) {
150 adjointVector[index] = Gradient();
151 }
152
154 Real getAdjoint(Identifier const& index, size_t dim) {
155 CODI_UNUSED(dim);
156
157 return (Real)GradientTraits::at(adjointVector[index], dim);
158 }
159
161 void getAdjointVec(Identifier const& index, Real* const vec) {
162 for (size_t i = 0; i < getVectorSize(); ++i) {
163 vec[i] = (Real)GradientTraits::at(adjointVector[index], i);
164 }
165 }
166
168 Real const* getAdjointVec(Identifier const& index) {
169 getAdjointVec(index, buffer.data());
170 return buffer.data();
171 }
172
174 void updateAdjoint(Identifier const& index, size_t dim, Real const& adjoint) {
175 GradientTraits::at(adjointVector[index], dim) += adjoint;
176 }
177
179 void updateAdjointVec(Identifier const& index, Real const* const vec) {
180 for (size_t i = 0; i < getVectorSize(); ++i) {
181 GradientTraits::at(adjointVector[index], i) += vec[i];
182 }
183 }
184
185 /*******************************************************************************/
187
190 void setPrimal(Identifier const& index, Real const& primal) {
191 CODI_UNUSED(index, primal);
192 }
193
196 Real getPrimal(Identifier const& index) {
197 CODI_UNUSED(index);
198
199 return Real();
200 }
201
204 bool hasPrimals() {
205 return false;
206 }
207 };
208}
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
typename GradientImplementation< AdjointVector >::Gradient Gradient
Deduce the entry type from an adjoint vector type, usually identical to the gradient type of a tape.
Definition adjointVectorTraits.hpp:92
typename TraitsImplementation< Gradient >::Real Real
The base value used in the gradient entries.
Definition gradientTraits.hpp:92
inlinesize_t constexpr dim()
Number of dimensions this gradient value has.
Definition gradientTraits.hpp:96
inlinetypename TraitsImplementation< Gradient >::Real & at(Gradient &gradient, size_t dim)
Get the entry at the given index.
Definition gradientTraits.hpp:102
inlinebool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:145
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
Real const * getAdjointVec(Identifier const &index)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:168
void setLhsTangent(Identifier const &index)
Definition adjointVectorAccess.hpp:121
void updateAdjointVec(Identifier const &index, Real const *const vec)
Update the adjoint entry.
Definition adjointVectorAccess.hpp:179
AdjointVectorAccess(AdjointVector adjointVector)
Constructor. See interface documentation for details about the adjoint vector.
Definition adjointVectorAccess.hpp:83
void setLhsAdjoint(Identifier const &index)
Definition adjointVectorAccess.hpp:107
void resetAdjoint(Identifier const &index, size_t dim)
Set the adjoint component to zero.
Definition adjointVectorAccess.hpp:144
void setPrimal(Identifier const &index, Real const &primal)
Set the primal value.
Definition adjointVectorAccess.hpp:190
AdjointVector adjointVector
Definition adjointVectorAccess.hpp:70
Real Real
Definition adjointVectorAccess.hpp:63
void getAdjointVec(Identifier const &index, Real *const vec)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:161
VectorAccessInterface< Real, Identifier > * clone() const
Definition adjointVectorAccess.hpp:99
bool hasPrimals()
Set the primal value.
Definition adjointVectorAccess.hpp:204
void updateTangentWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:127
void resetAdjointVec(Identifier const &index)
Set the adjoint entry to zero.
Definition adjointVectorAccess.hpp:149
void setActiveVariableForIndirectAccess(size_t pos)
Definition adjointVectorAccess.hpp:135
Identifier Identifier
Definition adjointVectorAccess.hpp:64
void updateAdjoint(Identifier const &index, size_t dim, Real const &adjoint)
Update the adjoint component.
Definition adjointVectorAccess.hpp:174
Real getAdjoint(Identifier const &index, size_t dim)
Get the adjoint component.
Definition adjointVectorAccess.hpp:154
bool isLhsZero()
True if the adjoint set with setLhsAdjoint is zero.
Definition adjointVectorAccess.hpp:94
void updateAdjointWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:113
size_t getVectorSize() const
Vector size in the current tape evaluation.
Definition adjointVectorAccess.hpp:89
AdjointVectorTraits::Gradient< AdjointVector > Gradient
Definition adjointVectorAccess.hpp:65
AdjointVector AdjointVector
Definition adjointVectorAccess.hpp:66
Real getPrimal(Identifier const &index)
Get the primal value.
Definition adjointVectorAccess.hpp:196
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94