CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
customAdjoints.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
36#pragma once
37
38#include <map>
39#include <vector>
40
41#include "../../misc/macros.hpp"
42#include "../../traits/adjointVectorTraits.hpp"
43
45namespace codi {
46
60 template<typename T_Identifier, typename T_Gradient>
62 public:
63 using Identifier = CODI_DD(T_Identifier, int);
64 using Gradient = CODI_DD(T_Gradient, double);
65
68 mutable std::map<Identifier, Gradient> adjoints;
69
71 Gradient const& operator[](Identifier const& i) const {
72 return adjoints[i];
73 }
74
77 return adjoints[i];
78 }
79 };
80
81#ifndef DOXYGEN_DISABLE
82 // Specialize adjoint vector traits.
83 namespace AdjointVectorTraits {
84 template<typename T_Identifier, typename T_Gradient>
85 struct GradientImplementation<MappedAdjoints<T_Identifier, T_Gradient>> {
86 public:
87 using Gradient = T_Gradient;
88 };
89 }
90#endif
91
104 template<typename T_Identifier, typename T_Gradient>
106 public:
107 using Identifier = CODI_DD(T_Identifier, int);
108 using Gradient = CODI_DD(T_Gradient, double);
109
112
115
118 return adjoints[identifier - offset];
119 }
120
122 CODI_INLINE Gradient const& operator[](Identifier const& identifier) const {
123 return adjoints[identifier - offset];
124 }
125 };
126
127#ifndef DOXYGEN_DISABLE
128 // Specialize adjoint vector traits.
129 namespace AdjointVectorTraits {
130 template<typename T_Identifier, typename T_Gradient>
131 struct GradientImplementation<AdjointVectorWithOffset<T_Identifier, T_Gradient>> {
132 public:
133 using Gradient = T_Gradient;
134 };
135 }
136#endif
137}
#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
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
Identifier const offset
Offset that is subtracted when addressing into the adjoints memory.
Definition customAdjoints.hpp:111
AdjointVectorWithOffset(Gradient *adjoints, Identifier const &offset)
Constructor.
Definition customAdjoints.hpp:114
T_Gradient Gradient
See AdjointVectorWithOffset.
Definition customAdjoints.hpp:108
Gradient * adjoints
Points to user-provided adjoints memory.
Definition customAdjoints.hpp:110
inlineGradient & operator[](Identifier const &identifier)
Access operator in non-constant call contexts.
Definition customAdjoints.hpp:117
inlineGradient const & operator[](Identifier const &identifier) const
Access operator in constant call contexts.
Definition customAdjoints.hpp:122
T_Identifier Identifier
See AdjointVectorWithOffset.
Definition customAdjoints.hpp:107
Implementation of adjoints via a map.
Definition customAdjoints.hpp:61
Gradient const & operator[](Identifier const &i) const
Access operator in constant call contexts.
Definition customAdjoints.hpp:71
std::map< Identifier, Gradient > adjoints
Definition customAdjoints.hpp:68
T_Gradient Gradient
See MappedAdjoints.
Definition customAdjoints.hpp:64
Gradient & operator[](Identifier const &i)
Access operator in non-constant call contexts.
Definition customAdjoints.hpp:76
T_Identifier Identifier
See MappedAdjoints.
Definition customAdjoints.hpp:63