CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
jacobian.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://www.scicomp.uni-kl.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://www.scicomp.uni-kl.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 <vector>
38
39#include "../../config.h"
40#include "../../misc/constructVector.hpp"
41#include "../../misc/macros.hpp"
42#include "../../traits/realTraits.hpp"
43#include "delayAccessor.hpp"
44#include "dummy.hpp"
45#include "jacobianInterface.hpp"
46#include "staticDummy.hpp"
47
49namespace codi {
50
59 template<typename T_T, typename T_Store = std::vector<T_T>>
60 struct Jacobian : public JacobianInterface<T_T> {
61 public:
62
63 using T = CODI_DD(T_T, double);
64 using Store = CODI_DD(T_Store, std::vector<double>);
65
66 protected:
68
69 size_t m;
70 size_t n;
71
72 public:
73
75 explicit Jacobian(size_t const m, size_t const n) : values(constructVector<Store>(n * m)), m(m), n(n) {}
76
78 CODI_INLINE size_t getM() const {
79 return m;
80 }
81
83 CODI_INLINE size_t getN() const {
84 return n;
85 }
86
88 CODI_INLINE T operator()(size_t const i, size_t const j) const {
89 return values[computeIndex(i, j)];
90 }
91
93 CODI_INLINE T& operator()(size_t const i, size_t const j) {
94 return values[computeIndex(i, j)];
95 }
96
100 CODI_INLINE void resize(size_t const m, size_t const n) {
101 this->m = m;
102 this->n = n;
103
104 values.resize(m * n);
105 }
106
108 CODI_INLINE size_t size() const {
109 return m * n;
110 }
111
112 protected:
113
115 CODI_INLINE size_t computeIndex(size_t const i, size_t const j) const {
116 return i * n + j;
117 }
118 };
119
128 template<typename T_T, typename T_Store = std::vector<T_T>>
129 struct JacobianCountNonZerosRow : public Jacobian<T_T, T_Store> {
130 public:
131
133
134 using T = typename Base::T;
135 using Store = typename Base::Store;
136
138
139 private:
140
141 std::vector<int> nonZerosRowVector;
142
143 public:
144
146 explicit JacobianCountNonZerosRow(size_t m, size_t n) : Base(m, n), nonZerosRowVector(m) {}
147
149 CODI_INLINE T operator()(size_t const i, size_t const j) const {
150 return Base::operator()(i, j);
151 }
152
156 CODI_INLINE DelayAcc operator()(size_t const i, size_t const j) {
157 return DelayAcc(i, j, *this);
158 }
159
161 CODI_INLINE void resize(size_t const m, size_t const n) {
162 Base::resize(m, n);
163 nonZerosRowVector.resize(m);
164 }
165
167 CODI_INLINE int& nonZerosRow(size_t const i) {
168 return nonZerosRowVector[i];
169 }
170
172 CODI_INLINE void setLogic(size_t const i, size_t const j, T const& v) {
173 if (T() != v) {
174 nonZerosRowVector[i] += 1;
175 }
176 Base::operator()(i, j) = v;
177 }
178 };
179
185 template<typename T_Nested>
187 public:
189 using T = typename Nested::T;
191
192 private:
193 Nested& nested;
194
195 public:
196
198 explicit JacobianConvertWrapper(Nested& nested) : nested(nested) {}
199
201 CODI_INLINE T operator()(size_t const i, size_t const j) const {
202 return nested(i, j);
203 }
204
207 CODI_INLINE DelayAcc operator()(size_t const i, size_t const j) {
208 return DelayAcc(i, j, *this);
209 }
210
212 template<typename SetT>
213 CODI_INLINE void setLogic(size_t const i, size_t const j, SetT const& v) {
214 nested(i, j) = RealTraits::getPassiveValue<SetT>(v);
215 }
216 };
217
219 struct DummyJacobian : public JacobianInterface<DummyValue> {
220 public:
221
223 size_t getM() const {
224 return 0;
225 }
226
228 size_t getN() const {
229 return 0;
230 }
231
233 CODI_INLINE DummyValue operator()(size_t const i, size_t const j) const {
234 CODI_UNUSED(i, j);
235
236 return DummyValue();
237 }
238
240 CODI_INLINE_NO_FA DummyValue& operator()(size_t const i, size_t const j) {
241 CODI_UNUSED(i, j);
242
244 }
245
247 void resize(size_t const m, size_t const n) {
248 CODI_UNUSED(m, n);
249 }
250
252 size_t size() const {
253 return 0;
254 }
255 };
256}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition: config.h:457
#define CODI_INLINE_NO_FA
See codi::Config::ForcedInlines.
Definition: config.h:459
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition: macros.hpp:94
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
V constructVector(size_t const size)
Helper for the construction of vector types provided by the user.
Definition: constructVector.hpp:83
Dummy Jacobian. Has size zero and no logic in any call.
Definition: jacobian.hpp:219
size_t getM() const
Get size of rows (output variables).
Definition: jacobian.hpp:223
DummyValue operator()(size_t const i, size_t const j) const
Value access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:233
void resize(size_t const m, size_t const n)
Resize the Jacobian.
Definition: jacobian.hpp:247
DummyValue & operator()(size_t const i, size_t const j)
Reference access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:240
size_t size() const
Get total size of the Jacobian.
Definition: jacobian.hpp:252
size_t getN() const
Get size of columns (input variables).
Definition: jacobian.hpp:228
Dummy value that can be assigned.
Definition: dummy.hpp:43
Wrapper for JacboianInterfaces that requires a passive value conversion.
Definition: jacobian.hpp:186
DelayAcc operator()(size_t const i, size_t const j)
Reference access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:207
void setLogic(size_t const i, size_t const j, SetT const &v)
Called by the JacobianDelayAccessor when a value is set.
Definition: jacobian.hpp:213
JacobianDelayAccessor< JacobianConvertWrapper > DelayAcc
Return type for reference access.
Definition: jacobian.hpp:190
T_Nested Nested
See JacobianConvertWrapper.
Definition: jacobian.hpp:188
JacobianConvertWrapper(Nested &nested)
Constructor.
Definition: jacobian.hpp:198
typename Nested::T T
See JacobianInterface.
Definition: jacobian.hpp:189
T operator()(size_t const i, size_t const j) const
Value access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:201
Adds counting of nonzero entries.
Definition: jacobian.hpp:129
JacobianDelayAccessor< JacobianCountNonZerosRow > DelayAcc
Delayed accessor for reference access.
Definition: jacobian.hpp:137
int & nonZerosRow(size_t const i)
Reference to the number of nonzero entries for the specified row.
Definition: jacobian.hpp:167
void resize(size_t const m, size_t const n)
Resize the Jacobian.
Definition: jacobian.hpp:161
JacobianCountNonZerosRow(size_t m, size_t n)
m = rows (output variables), n = columns (input variables)
Definition: jacobian.hpp:146
T operator()(size_t const i, size_t const j) const
Value access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:149
DelayAcc operator()(size_t const i, size_t const j)
Reference access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:156
typename Base::T T
See Jacobian.
Definition: jacobian.hpp:134
typename Base::Store Store
See Jacobian.
Definition: jacobian.hpp:135
void setLogic(size_t const i, size_t const j, T const &v)
Checks if the element is non zero and updates the counter.
Definition: jacobian.hpp:172
Definition: delayAccessor.hpp:51
General interface for Jacobian access in CoDiPack.
Definition: jacobianInterface.hpp:54
Default implementation of the Jacobian interface.
Definition: jacobian.hpp:60
size_t n
Number of columns (input variables).
Definition: jacobian.hpp:70
Store values
Array store for the data.
Definition: jacobian.hpp:67
size_t m
Number of rows (output variables).
Definition: jacobian.hpp:69
T operator()(size_t const i, size_t const j) const
Value access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:88
size_t size() const
Get total size of the Jacobian.
Definition: jacobian.hpp:108
size_t getM() const
Get size of rows (output variables).
Definition: jacobian.hpp:78
T & operator()(size_t const i, size_t const j)
Reference access, i in [0, ..., m), j in [0, ..., n).
Definition: jacobian.hpp:93
void resize(size_t const m, size_t const n)
Resize the Jacobian.
Definition: jacobian.hpp:100
size_t getN() const
Get size of columns (input variables).
Definition: jacobian.hpp:83
size_t computeIndex(size_t const i, size_t const j) const
Convert row and column to an index in the storage array.
Definition: jacobian.hpp:115
T_T T
See Jacobian.
Definition: jacobian.hpp:63
Jacobian(size_t const m, size_t const n)
m = rows (output variables), n = columns (input variables)
Definition: jacobian.hpp:75
T_Store Store
See Jacobian.
Definition: jacobian.hpp:64
Static dummy objects for e.g. default reference arguments.
Definition: staticDummy.hpp:42