CoDiPack  2.2.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-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 <array>
38#include <cstddef>
39
40#include "../../config.h"
41#include "../../misc/macros.hpp"
42#include "../../tools/data/direction.hpp"
43#include "../../traits/realTraits.hpp"
44#include "vectorAccessInterface.hpp"
45
47namespace codi {
48
58 template<typename T_Real, typename T_Identifier, typename T_Gradient>
59 struct AdjointVectorAccess : public VectorAccessInterface<T_Real, T_Identifier> {
60 using Real = CODI_DD(T_Real, double);
61 using Identifier = CODI_DD(T_Identifier, int);
62 using Gradient = CODI_DD(T_Gradient, double);
63
64 protected:
65
67
68 private:
69
70 Gradient lhs;
71
72 std::array<Real, GradientTraits::dim<Gradient>()> buffer;
73
74 public:
75
78
79 /*******************************************************************************/
81
83 size_t getVectorSize() const {
84 return GradientTraits::dim<Gradient>();
85 }
86
88 bool isLhsZero() {
89 return RealTraits::isTotalZero(lhs);
90 }
91
94 return new AdjointVectorAccess(this->adjointVector);
95 }
96
97 /*******************************************************************************/
99
101 void setLhsAdjoint(Identifier const& index) {
102 lhs = adjointVector[index];
103 adjointVector[index] = Gradient();
104 }
105
107 void updateAdjointWithLhs(Identifier const& index, Real const& jacobian) {
108 adjointVector[index] += jacobian * lhs;
109 }
110
111 /*******************************************************************************/
113
115 void setLhsTangent(Identifier const& index) {
116 adjointVector[index] = lhs;
117 lhs = Gradient();
118 }
119
121 void updateTangentWithLhs(Identifier const& index, Real const& jacobian) {
122 lhs += jacobian * adjointVector[index];
123 }
124
125 /*******************************************************************************/
127
129 void resetAdjoint(Identifier const& index, size_t dim) {
131 }
132
134 void resetAdjointVec(Identifier const& index) {
135 adjointVector[index] = Gradient();
136 }
137
139 Real getAdjoint(Identifier const& index, size_t dim) {
140 CODI_UNUSED(dim);
141
142 return (Real)GradientTraits::at(adjointVector[index], dim);
143 }
144
146 void getAdjointVec(Identifier const& index, Real* const vec) {
147 for (size_t i = 0; i < getVectorSize(); ++i) {
148 vec[i] = (Real)GradientTraits::at(adjointVector[index], i);
149 }
150 }
151
153 Real const* getAdjointVec(Identifier const& index) {
154 getAdjointVec(index, buffer.data());
155 return buffer.data();
156 }
157
159 void updateAdjoint(Identifier const& index, size_t dim, Real const& adjoint) {
160 GradientTraits::at(adjointVector[index], dim) += adjoint;
161 }
162
164 void updateAdjointVec(Identifier const& index, Real const* const vec) {
165 for (size_t i = 0; i < getVectorSize(); ++i) {
166 GradientTraits::at(adjointVector[index], i) += vec[i];
167 }
168 }
169
170 /*******************************************************************************/
172
175 void setPrimal(Identifier const& index, Real const& primal) {
176 CODI_UNUSED(index, primal);
177 }
178
181 Real getPrimal(Identifier const& index) {
182 CODI_UNUSED(index);
183
184 return Real();
185 }
186
189 bool hasPrimals() {
190 return false;
191 }
192 };
193}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
typename TraitsImplementation< Gradient >::Real Real
The base value used in the gradient entries.
Definition gradientTraits.hpp:92
TraitsImplementation< Gradient >::Real & at(Gradient &gradient, size_t dim)
Get the entry at the given index.
Definition gradientTraits.hpp:102
bool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:139
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
Implementation of VectorAccessInterface for adjoint vectors.
Definition adjointVectorAccess.hpp:59
void setLhsTangent(Identifier const &index)
Definition adjointVectorAccess.hpp:115
void updateTangentWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:121
T_Real Real
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:60
void updateAdjointWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:107
void updateAdjointVec(Identifier const &index, Real const *const vec)
Update the adjoint entry.
Definition adjointVectorAccess.hpp:164
void resetAdjoint(Identifier const &index, size_t dim)
Set the adjoint component to zero.
Definition adjointVectorAccess.hpp:129
Real getAdjoint(Identifier const &index, size_t dim)
Get the adjoint component.
Definition adjointVectorAccess.hpp:139
Real const * getAdjointVec(Identifier const &index)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:153
size_t getVectorSize() const
Vector size in the current tape evaluation.
Definition adjointVectorAccess.hpp:83
bool hasPrimals()
Set the primal value.
Definition adjointVectorAccess.hpp:189
AdjointVectorAccess(Gradient *adjointVector)
Constructor. See interface documentation for details about the adjoint vector.
Definition adjointVectorAccess.hpp:77
VectorAccessInterface< Real, Identifier > * clone() const
Definition adjointVectorAccess.hpp:93
T_Identifier Identifier
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:61
Gradient * adjointVector
Pointer to the gradient vector.
Definition adjointVectorAccess.hpp:66
void setLhsAdjoint(Identifier const &index)
Definition adjointVectorAccess.hpp:101
void updateAdjoint(Identifier const &index, size_t dim, Real const &adjoint)
Update the adjoint component.
Definition adjointVectorAccess.hpp:159
T_Gradient Gradient
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:62
void getAdjointVec(Identifier const &index, Real *const vec)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:146
void setPrimal(Identifier const &index, Real const &primal)
Set the primal value.
Definition adjointVectorAccess.hpp:175
void resetAdjointVec(Identifier const &index)
Set the adjoint entry to zero.
Definition adjointVectorAccess.hpp:134
bool isLhsZero()
True if the adjoint set with setLhsAdjoint is zero.
Definition adjointVectorAccess.hpp:88
Real getPrimal(Identifier const &index)
Get the primal value.
Definition adjointVectorAccess.hpp:181
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91