CoDiPack  2.3.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/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 Gradient lhs;
75
76 std::array<Real, GradientTraits::dim<Gradient>()> buffer;
77
78 public:
79
82
83 /*******************************************************************************/
85
87 size_t getVectorSize() const {
88 return GradientTraits::dim<Gradient>();
89 }
90
92 bool isLhsZero() {
93 return RealTraits::isTotalZero(lhs);
94 }
95
98 return new AdjointVectorAccess(this->adjointVector);
99 }
100
101 /*******************************************************************************/
103
105 void setLhsAdjoint(Identifier const& index) {
106 lhs = adjointVector[index];
107 adjointVector[index] = Gradient();
108 }
109
111 void updateAdjointWithLhs(Identifier const& index, Real const& jacobian) {
112 adjointVector[index] += jacobian * lhs;
113 }
114
115 /*******************************************************************************/
117
119 void setLhsTangent(Identifier const& index) {
120 adjointVector[index] = lhs;
121 lhs = Gradient();
122 }
123
125 void updateTangentWithLhs(Identifier const& index, Real const& jacobian) {
126 lhs += jacobian * adjointVector[index];
127 }
128
129 /*******************************************************************************/
131
133 void resetAdjoint(Identifier const& index, size_t dim) {
135 }
136
138 void resetAdjointVec(Identifier const& index) {
139 adjointVector[index] = Gradient();
140 }
141
143 Real getAdjoint(Identifier const& index, size_t dim) {
144 CODI_UNUSED(dim);
145
146 return (Real)GradientTraits::at(adjointVector[index], dim);
147 }
148
150 void getAdjointVec(Identifier const& index, Real* const vec) {
151 for (size_t i = 0; i < getVectorSize(); ++i) {
152 vec[i] = (Real)GradientTraits::at(adjointVector[index], i);
153 }
154 }
155
157 Real const* getAdjointVec(Identifier const& index) {
158 getAdjointVec(index, buffer.data());
159 return buffer.data();
160 }
161
163 void updateAdjoint(Identifier const& index, size_t dim, Real const& adjoint) {
164 GradientTraits::at(adjointVector[index], dim) += adjoint;
165 }
166
168 void updateAdjointVec(Identifier const& index, Real const* const vec) {
169 for (size_t i = 0; i < getVectorSize(); ++i) {
170 GradientTraits::at(adjointVector[index], i) += vec[i];
171 }
172 }
173
174 /*******************************************************************************/
176
179 void setPrimal(Identifier const& index, Real const& primal) {
180 CODI_UNUSED(index, primal);
181 }
182
185 Real getPrimal(Identifier const& index) {
186 CODI_UNUSED(index);
187
188 return Real();
189 }
190
193 bool hasPrimals() {
194 return false;
195 }
196 };
197}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
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
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:91
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:61
Real const * getAdjointVec(Identifier const &index)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:157
void setLhsTangent(Identifier const &index)
Definition adjointVectorAccess.hpp:119
void updateAdjointVec(Identifier const &index, Real const *const vec)
Update the adjoint entry.
Definition adjointVectorAccess.hpp:168
AdjointVectorAccess(AdjointVector adjointVector)
Constructor. See interface documentation for details about the adjoint vector.
Definition adjointVectorAccess.hpp:81
void setLhsAdjoint(Identifier const &index)
Definition adjointVectorAccess.hpp:105
void resetAdjoint(Identifier const &index, size_t dim)
Set the adjoint component to zero.
Definition adjointVectorAccess.hpp:133
void setPrimal(Identifier const &index, Real const &primal)
Set the primal value.
Definition adjointVectorAccess.hpp:179
AdjointVector adjointVector
Pointer/reference to an array-accessible collection of gradients.
Definition adjointVectorAccess.hpp:70
T_Real Real
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:63
void getAdjointVec(Identifier const &index, Real *const vec)
Get the adjoint entry.
Definition adjointVectorAccess.hpp:150
VectorAccessInterface< Real, Identifier > * clone() const
Definition adjointVectorAccess.hpp:97
bool hasPrimals()
Set the primal value.
Definition adjointVectorAccess.hpp:193
void updateTangentWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:125
void resetAdjointVec(Identifier const &index)
Set the adjoint entry to zero.
Definition adjointVectorAccess.hpp:138
T_Identifier Identifier
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:64
void updateAdjoint(Identifier const &index, size_t dim, Real const &adjoint)
Update the adjoint component.
Definition adjointVectorAccess.hpp:163
Real getAdjoint(Identifier const &index, size_t dim)
Get the adjoint component.
Definition adjointVectorAccess.hpp:143
bool isLhsZero()
True if the adjoint set with setLhsAdjoint is zero.
Definition adjointVectorAccess.hpp:92
void updateAdjointWithLhs(Identifier const &index, Real const &jacobian)
Definition adjointVectorAccess.hpp:111
size_t getVectorSize() const
Vector size in the current tape evaluation.
Definition adjointVectorAccess.hpp:87
AdjointVectorTraits::Gradient< T_AdjointVector > Gradient
Adjoint vector entry type.
Definition adjointVectorAccess.hpp:65
T_AdjointVector AdjointVector
See AdjointVectorAccess.
Definition adjointVectorAccess.hpp:66
Real getPrimal(Identifier const &index)
Get the primal value.
Definition adjointVectorAccess.hpp:185
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91