CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
direction.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 <initializer_list>
38
39#include "../../config.h"
40#include "../../misc/macros.hpp"
41#include "../../traits/atomicTraits.hpp"
42#include "../../traits/gradientTraits.hpp"
43#include "../../traits/realTraits.hpp"
44
46namespace codi {
47
56 template<typename T_Real, size_t T_dim>
57 struct Direction {
58 public:
59
60 using Real = CODI_DD(T_Real, double);
61
62 static size_t constexpr dim = T_dim;
63
64 private:
65 Real vector[dim];
66
67 public:
68
70 CODI_INLINE Direction() : vector() {}
71
73 CODI_INLINE Direction(Real const& s) : vector() {
74 for (size_t i = 0; i < dim; ++i) {
75 vector[i] = s;
76 }
77 }
78
80 CODI_INLINE Direction(Direction const& v) : vector() {
81 for (size_t i = 0; i < dim; ++i) {
82 vector[i] = v.vector[i];
83 }
84 }
85
87 CODI_INLINE Direction(std::initializer_list<Real> l) : vector() {
88 size_t size = std::min(dim, l.size());
89 Real const* array = l.begin(); // This is possible because the standard requires an array storage.
90 for (size_t i = 0; i < size; ++i) {
91 vector[i] = array[i];
92 }
93 }
94
96 CODI_INLINE Real& operator[](size_t const& i) {
97 return vector[i];
98 }
99
101 CODI_INLINE Real const& operator[](size_t const& i) const {
102 return vector[i];
103 }
104
107 for (size_t i = 0; i < dim; ++i) {
108 this->vector[i] = v.vector[i];
109 }
110
111 return *this;
112 }
113
116 for (size_t i = 0; i < dim; ++i) {
117 this->vector[i] += v.vector[i];
118 }
119
120 return *this;
121 }
122
125 for (size_t i = 0; i < dim; ++i) {
126 this->vector[i] -= v.vector[i];
127 }
128
129 return *this;
130 }
131 };
132
133 template<typename Real, size_t dim>
134 size_t constexpr Direction<Real, dim>::dim;
135
137 template<typename Real, size_t dim>
140 for (size_t i = 0; i < dim; ++i) {
141 r[i] = s * v[i];
142 }
143
144 return r;
145 }
146
148 template<typename Real, size_t dim, typename = RealTraits::EnableIfNotPassiveReal<Real>>
151 for (size_t i = 0; i < dim; ++i) {
152 r[i] = s * v[i];
153 }
154
155 return r;
156 }
157
159 template<typename Real, size_t dim, typename = AtomicTraits::EnableIfAtomic<Real>>
162 for (size_t i = 0; i < dim; ++i) {
163 r[i] = s * v[i];
164 }
165
166 return r;
167 }
168
170 template<typename Real, size_t dim>
172 return s * v;
173 }
174
176 template<typename Real, size_t dim, typename = RealTraits::EnableIfNotPassiveReal<Real>>
180
182 template<typename Real, size_t dim, typename = AtomicTraits::EnableIfAtomic<Real>>
186
188 template<typename Real, size_t dim>
191 for (size_t i = 0; i < dim; ++i) {
192 r[i] = v[i] / s;
193 }
194
195 return r;
196 }
197
199 template<typename Real, size_t dim, typename = RealTraits::EnableIfNotPassiveReal<Real>>
202 for (size_t i = 0; i < dim; ++i) {
203 r[i] = v[i] / s;
204 }
205
206 return r;
207 }
208
210 template<typename Real, size_t dim, typename = AtomicTraits::EnableIfAtomic<Real>>
213 for (size_t i = 0; i < dim; ++i) {
214 r[i] = v[i] / s;
215 }
216
217 return r;
218 }
219
221 template<typename Real, size_t dim>
224 for (size_t i = 0; i < dim; ++i) {
225 r[i] = v1[i] + v2[i];
226 }
227
228 return r;
229 }
230
232 template<typename Real, size_t dim>
235 for (size_t i = 0; i < dim; ++i) {
236 r[i] = v1[i] - v2[i];
237 }
238
239 return r;
240 }
241
243 template<typename Real, size_t dim>
246 for (size_t i = 0; i < dim; ++i) {
247 r[i] = -v[i];
248 }
249
250 return r;
251 }
252
254 template<typename Real, size_t dim>
256 return !(v1 == v2);
257 }
258
260 template<typename A, typename Real, size_t dim>
261 CODI_INLINE bool operator!=(A const& s, Direction<Real, dim> const& v) {
262 return !(s == v);
263 }
264
266 template<typename A, typename Real, size_t dim>
267 CODI_INLINE bool operator!=(Direction<Real, dim> const& v, A const& s) {
268 return s != v;
269 }
270
272 template<typename Real, size_t dim>
274 for (size_t i = 0; i < dim; ++i) {
275 if (v1[i] != v2[i]) {
276 return false;
277 }
278 }
279
280 return true;
281 }
282
284 template<typename A, typename Real, size_t dim>
285 CODI_INLINE bool operator==(A const& s, Direction<Real, dim> const& v) {
286 for (size_t i = 0; i < dim; ++i) {
287 if (s != v[i]) {
288 return false;
289 }
290 }
291
292 return true;
293 }
294
296 template<typename A, typename Real, size_t dim>
297 CODI_INLINE bool operator==(Direction<Real, dim> const& v, A const& s) {
298 return s == v;
299 }
300
302 template<typename Real, size_t dim>
303 std::ostream& operator<<(std::ostream& os, Direction<Real, dim> const& v) {
304 os << "{";
305 for (size_t i = 0; i < dim; ++i) {
306 if (i != 0) {
307 os << ", ";
308 }
309 os << v[i];
310 }
311 os << "}";
312
313 return os;
314 }
315
316#ifndef DOXYGEN_DISABLE
317 template<typename T_Type>
318 struct RealTraits::IsTotalZero<T_Type, GradientTraits::EnableIfDirection<T_Type>> {
319 public:
320
321 using Type = CODI_DD(T_Type, CODI_T(Direction<double, 1>));
322 using Real = typename GradientTraits::Real<Type>;
323
324 static CODI_INLINE bool isTotalZero(Type const& v) {
325 for (size_t i = 0; i < GradientTraits::dim<Type>(); ++i) {
327 return false;
328 }
329 }
330 return true;
331 }
332 };
333
334 template<typename T_Type>
335 struct RealTraits::IsTotalFinite<T_Type, GradientTraits::EnableIfDirection<T_Type>> {
336 public:
337
338 using Type = CODI_DD(T_Type, CODI_T(Direction<double, 1>));
339
340 static CODI_INLINE bool isTotalFinite(Type const& v) {
341 for (size_t i = 0; i < GradientTraits::dim<Type>(); ++i) {
343 return false;
344 }
345 }
346 return true;
347 }
348 };
349
350 namespace GradientTraits {
351
352 template<typename T_Gradient>
353 struct TraitsImplementation<T_Gradient, EnableIfDirection<T_Gradient>> {
354 public:
355
356 using Gradient = CODI_DD(T_Gradient, CODI_T(Direction<double, 1>));
357 using Real = typename Gradient::Real;
358
359 static size_t constexpr dim = Gradient::dim;
360
361 CODI_INLINE static Real& at(Gradient& gradient, size_t dim) {
362 return gradient[dim];
363 }
364
365 CODI_INLINE static Real const& at(Gradient const& gradient, size_t dim) {
366 return gradient[dim];
367 }
368
369 CODI_INLINE static std::array<AtomicTraits::RemoveAtomic<Real>, dim> toArray(Gradient const& gradient) {
370 std::array<AtomicTraits::RemoveAtomic<Real>, dim> result;
371 for (size_t i = 0; i < dim; ++i) {
372 result[i] = at(gradient, i);
373 }
374 return result;
375 }
376 };
377 }
378#endif
379}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
typename RemoveAtomicImpl< Type >::Type RemoveAtomic
Wrapper for removing atomic from a type.
Definition atomicTraits.hpp:83
typename std::enable_if< IsDirection< Gradient >::value >::type EnableIfDirection
Enable if wrapper for EnableIfDirection.
Definition gradientTraits.hpp:144
bool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:133
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition realTraits.hpp:117
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
bool operator==(Enum a, EnumBitset< Enum > const &b)
Equal comparison for an enum and a bitset.
Definition enumBitset.hpp:247
Direction< Real, dim > operator-(Direction< Real, dim > const &v1, Direction< Real, dim > const &v2)
Subtraction of two vectors.
Definition direction.hpp:233
ExpressionTraits::EnableIfExpression< Expr, std::ostream > & operator<<(std::ostream &out, Expr const &v)
Write the primal value to the stream.
Definition expressionInterface.hpp:129
Direction< Real, dim > operator/(Direction< Real, dim > const &v, Real const &s)
Division by a scalar.
Definition direction.hpp:189
bool operator!=(Enum a, EnumBitset< Enum > const &b)
Not equal comparison for an enum and a bitset.
Definition enumBitset.hpp:241
ExpressionInterface< Real, Arg > const & operator+(ExpressionInterface< Real, Arg > const &arg)
Function overload for operator +.
Definition unaryOperators.hpp:79
Direction< Real, dim > operator*(Real const &s, Direction< Real, dim > const &v)
Multiplication with a scalar.
Definition direction.hpp:138
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Fixed size vector mode implementation.
Definition direction.hpp:57
Direction(Real const &s)
Constructor.
Definition direction.hpp:73
Direction()
Constructor.
Definition direction.hpp:70
T_Real Real
See Direction.
Definition direction.hpp:60
Direction & operator-=(Direction const &v)
Update operator.
Definition direction.hpp:124
Real & operator[](size_t const &i)
Per reference element access.
Definition direction.hpp:96
Real const & operator[](size_t const &i) const
Per value element access.
Definition direction.hpp:101
Direction(std::initializer_list< Real > l)
Constructor.
Definition direction.hpp:87
Direction & operator+=(Direction const &v)
Update operator.
Definition direction.hpp:115
static size_t constexpr dim
See Direction.
Definition direction.hpp:62
Direction(Direction const &v)
Constructor.
Definition direction.hpp:80
Direction & operator=(Direction const &v)
Assignment operator.
Definition direction.hpp:106
static size_t constexpr dim
Number of dimensions this gradient value has.
Definition gradientTraits.hpp:70
static Real & at(Gradient &gradient, size_t dim)
Get the entry at the given index.
Definition gradientTraits.hpp:73
static std::array< AtomicTraits::RemoveAtomic< Real >, dim > toArray(Gradient const &gradient)
Converts the (possibly multi-component) gradient to an array of Reals.
Definition gradientTraits.hpp:85
T_Type Type
See IsTotalFinite.
Definition realTraits.hpp:85
static bool isTotalFinite(Type const &v)
Checks if the values are all finite.
Definition realTraits.hpp:88
T_Type Type
See IsTotalZero.
Definition realTraits.hpp:103
static bool isTotalZero(Type const &v)
Checks if the values are completely zero.
Definition realTraits.hpp:106