CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
realTraits.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#pragma once
36
37#include <array>
38#include <cmath>
39#include <complex>
40
41#include "../config.h"
42#include "../misc/compileTimeLoop.hpp"
43#include "../misc/macros.hpp"
44#include "computationTraits.hpp"
45#include "expressionTraits.hpp"
46
48namespace codi {
49
50 template<typename T_Real, typename T_InnerActiveType, typename T_Impl, bool T_isStatic>
52
54 namespace RealTraits {
55
56 /*******************************************************************************/
59
65 template<typename T_Type, typename = void>
67 public:
68
69 using Type = CODI_DD(T_Type, double);
70
71 using Real = Type;
72 using PassiveReal = Type;
73
74 static int constexpr MaxDerivativeOrder = 0;
75
78 return v;
79 }
80 };
81
87 template<typename T_Type, typename = void>
89 public:
90
91 using Type = CODI_DD(T_Type, double);
92
94 static CODI_INLINE bool isTotalFinite(Type const& v) {
95 using std::isfinite;
96 return isfinite(v);
97 }
98 };
99
105 template<typename T_Type, typename = void>
106 struct IsTotalZero {
107 public:
108
109 using Type = CODI_DD(T_Type, double);
110
112 static CODI_INLINE bool isTotalZero(Type const& v) {
113 return Type() == v;
114 }
115 };
116
118 template<typename Type>
120
122 template<typename Type>
124
126 template<typename Type>
130
132 template<typename Type>
136
138 template<typename Type>
139 CODI_INLINE bool isTotalFinite(Type const& v) {
141 }
142
144 template<typename Type>
145 CODI_INLINE bool isTotalZero(Type const& v) {
147 }
148
150 /*******************************************************************************/
153
163 template<typename T_Type, typename = void>
165 public:
166 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
167 "Instantiation of unspecialized RealTraits::DataExtraction.");
168
169 using Type = CODI_DD(T_Type, ActiveTypeProxy);
170
171 using Real = typename Type::Real;
172 using Identifier = typename Type::Identifier;
173
175 CODI_INLINE static Real getValue(Type const& v);
176
179
181 CODI_INLINE static void setValue(Type& v, Real const& value);
182
184 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier);
185 };
186
196 template<typename T_Type, typename = void>
198 public:
199 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
200 "Instantiation of unspecialized RealTraits::TapeRegistration.");
201
202 using Type = CODI_DD(T_Type, CODI_ANY);
203
205
208
211
214 };
215
225 template<typename T_Type, typename = void>
227 public:
228 using Type = CODI_DD(T_Type, double);
230 using Real = CODI_ANY;
231
232 static int constexpr Elements = 0;
233
236 static Type arrayConstructor(InnerType const* v) {
237 CODI_UNUSED(v);
238 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
239 "Instantiation of unspecialized AggregatedTypeTraits.");
240
241 return Type{};
242 }
243
248 template<size_t element>
249 static InnerType adjointOfConstructor(Type const& w, Type const& w_b) {
250 CODI_UNUSED(w, w_b);
251 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
252 "Instantiation of unspecialized AggregatedTypeTraits.");
253
254 return InnerType{};
255 }
256
259 template<size_t element>
260 static InnerType arrayAccess(Type const& w) {
261 CODI_UNUSED(w);
262 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
263 "Instantiation of unspecialized AggregatedTypeTraits.");
264
265 return InnerType{};
266 }
267
269 template<size_t element>
271 CODI_UNUSED(v);
272 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
273 "Instantiation of unspecialized AggregatedTypeTraits.");
274
275 static InnerType temp = {};
276 return temp;
277 }
278
281 template<size_t element>
282 static Type adjointOfArrayAccess(Type const& w, InnerType const& v_b) {
283 CODI_UNUSED(w, v_b);
284 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
285 "Instantiation of unspecialized AggregatedTypeTraits.");
286
287 return Type{};
288 }
289
291 static CODI_INLINE std::string getMathRep() {
292 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
293 "Instantiation of unspecialized AggregatedTypeTraits.");
294
295 return "";
296 }
297 };
298
300 template<typename T_Type, typename T_InnerType, typename T_Real, int T_Elements>
302 public:
303 static_assert(
304 sizeof(T_Type) == T_Elements * sizeof(T_InnerType),
305 "ArrayAggregatedTypeTraitsBase is designed for aggregated types that resemble arrays of their inner type."
306 " The sizes in the given instantiation do not match this use case.");
307
308 using Type = CODI_DD(T_Type, CODI_ANY);
309 using InnerType = CODI_DD(T_InnerType, CODI_ANY);
310 using Real = CODI_DD(T_Real, CODI_ANY);
311
312 static int constexpr Elements = CODI_DD(T_Elements, 1);
313
316 Type w{};
317
318 InnerType* wArray = reinterpret_cast<InnerType*>(&w);
319
321 wArray[i.value] = v[i.value];
322 });
323
324 return w;
325 }
326
328 template<size_t element>
329 CODI_INLINE static InnerType adjointOfConstructor(Type const& w, Type const& w_b) {
330 CODI_UNUSED(w);
331
332 // We assume that dT/dv is the identity and is not holomorphic. That means no one is changed to a minus one on
333 // a transpose.
334 InnerType const* w_bArray = reinterpret_cast<InnerType const*>(&w_b);
335 return ComputationTraits::transpose(w_bArray[element]);
336 }
337
339 template<size_t element>
341 InnerType* wArray = reinterpret_cast<InnerType*>(&w);
342
343 return wArray[element];
344 }
345
347 template<size_t element>
348 CODI_INLINE static InnerType const& arrayAccess(Type const& w) {
349 InnerType const* wArray = reinterpret_cast<InnerType const*>(&w);
350
351 return wArray[element];
352 }
353
355 template<size_t element>
356 CODI_INLINE static Type adjointOfArrayAccess(Type const& w, InnerType const& v_b) {
357 CODI_UNUSED(w);
358
359 // We assume that dw[element]/w is the identity and is not holomorphic. That means no one is changed to a
360 // minus one on a transpose.
361 Type w_b{};
362 InnerType* w_bArray = reinterpret_cast<InnerType*>(&w_b);
364 w_bArray[i.value] = InnerType();
365 });
366 w_bArray[element] = v_b;
367
368 return w_b;
369 }
370 };
371
373 template<typename Type>
377
379 template<typename Type>
383
385 template<typename Type>
386 CODI_INLINE void setValue(Type& v, typename DataExtraction<Type>::Real const& value) {
387 return DataExtraction<Type>::setValue(v, value);
388 }
389
391 template<typename Type>
395
397 template<typename Type>
401
403 template<typename Type>
407
409 /*******************************************************************************/
412
414 template<typename Type>
416 typename std::enable_if<(AggregatedTypeTraits<Type>::Elements != 0) &
418
420 template<typename Expr>
422 typename enable_if_base_of<AggregatedActiveTypeBase<typename Expr::Real, typename Expr::InnerActiveType,
423 typename Expr::Impl, Expr::isStatic>,
424 Expr>::type;
425
427 template<typename Type>
428 using IsPassiveReal = std::is_same<Type, PassiveReal<Type>>;
429
431 template<typename Expr>
433
435 template<typename Type>
436 using EnableIfNotPassiveReal = typename std::enable_if<!IsPassiveReal<Type>::value>::type;
437
439 template<typename Type>
440 using EnableIfPassiveReal = typename std::enable_if<IsPassiveReal<Type>::value>::type;
441
442#ifndef DOXYGEN_DISABLE
443
445 /*******************************************************************************/
448
450 template<typename T_Type>
451 struct DataExtraction<T_Type, typename std::enable_if<std::is_floating_point<T_Type>::value>::type> {
452 public:
453 using Type = CODI_DD(T_Type, double);
454
455 using Real = double;
456 using Identifier = int;
457
459 CODI_INLINE static Real getValue(Type const& v) {
460 return v;
461 }
462
464 CODI_INLINE static Identifier getIdentifier(Type const& v) {
465 CODI_UNUSED(v);
466
467 return 0;
468 }
469
471 CODI_INLINE static void setValue(Type& v, Real const& value) {
472 v = value;
473 }
474
476 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier) {
477 CODI_UNUSED(v, identifier);
478 }
479 };
480
482 template<typename T_Type>
483 struct DataExtraction<T_Type, EnableIfAggregatedTypeTraitsIsSpecialized<T_Type>> {
484 public:
485
486 using Type = CODI_DD(T_Type, double);
487
488 using TypeTraits = AggregatedTypeTraits<Type>;
489 using InnerType = typename TypeTraits::InnerType;
490 static int constexpr Elements = TypeTraits::Elements;
491
492 using InnerDataExtraction = DataExtraction<InnerType>;
493
494 using Real = typename TypeTraits::Real;
495 using Identifier = std::array<CODI_DD(typename InnerType::Identifier, int), Elements>;
496
497 using RealTypeTraits = AggregatedTypeTraits<Real>;
498
500 CODI_INLINE static Real getValue(Type const& v) {
501 Real real{};
502
504 RealTypeTraits::template arrayAccess<i.value>(real) =
505 InnerDataExtraction::getValue(TypeTraits::template arrayAccess<i.value>(v));
506 });
507
508 return real;
509 }
510
512 CODI_INLINE static Identifier getIdentifier(Type const& v) {
513 Identifier res;
514
516 res[i.value] = InnerDataExtraction::getIdentifier(TypeTraits::template arrayAccess<i.value>(v));
517 });
518
519 return res;
520 }
521
523 CODI_INLINE static void setValue(Type& v, Real const& value) {
525 InnerDataExtraction::setValue(TypeTraits::template arrayAccess<i.value>(v),
526 RealTypeTraits::template arrayAccess<i.value>(value));
527 });
528 }
529
531 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier) {
533 InnerDataExtraction::setIdentifier(TypeTraits::template arrayAccess<i.value>(v), identifier[i.value]);
534 });
535 }
536 };
537
539 template<typename T_Type>
541 public:
542
543 using Type = CODI_DD(T_Type, CODI_ANY);
544
545 using TypeTraits = AggregatedTypeTraits<Type>;
546 using InnerType = typename TypeTraits::InnerType;
547 static int constexpr Elements = TypeTraits::Elements;
548
549 using Real = typename TypeTraits::Real;
550
551 using RealTypeTraits = RealTraits::AggregatedTypeTraits<Real>;
552 using InnerTraits = TapeRegistration<InnerType>;
553
555 CODI_INLINE static void registerInput(Type& v) {
557 InnerTraits::registerInput(TypeTraits::template arrayAccess<i.value>(v));
558 });
559 }
560
562 CODI_INLINE static void registerOutput(Type& v) {
564 InnerTraits::registerOutput(TypeTraits::template arrayAccess<i.value>(v));
565 });
566 }
567
570 Real res{};
571
573 RealTypeTraits::template arrayAccess<i.value>(res) =
574 InnerTraits::registerExternalFunctionOutput(TypeTraits::template arrayAccess<i.value>(v));
575 });
576
577 return res;
578 }
579 };
580 template<typename T_Type>
581 struct AggregatedTypeTraits<T_Type, typename std::enable_if<std::is_floating_point<T_Type>::value>::type>
582 : ArrayAggregatedTypeTraitsBase<T_Type, T_Type, T_Type, 1> {};
583
584 template<typename T_Type>
585 struct AggregatedTypeTraits<T_Type, typename std::enable_if<std::is_integral<T_Type>::value>::type>
586 : ArrayAggregatedTypeTraitsBase<T_Type, T_Type, T_Type, 1> {};
587
588 template<>
590
591#endif
592
594
595 }
596}
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#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
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:100
#define CODI_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:129
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:204
inlinebool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:139
typename std::enable_if<(AggregatedTypeTraits< Type >::Elements !=0) &(!ExpressionTraits::IsLhsExpression< Type >::value)>::type EnableIfAggregatedTypeTraitsIsSpecialized
Enable if helper when a type has been specialized for the AggregatedTypeTraits.
Definition realTraits.hpp:415
typename std::enable_if< IsPassiveReal< Type >::value >::type EnableIfPassiveReal
Enable if wrapper for IsPassiveReal.
Definition realTraits.hpp:440
typename TraitsImplementation< Type >::Real Real
Inner type of the real value.
Definition realTraits.hpp:119
inlinetypename DataExtraction< Type >::Real registerExternalFunctionOutput(Type &v)
Register all active types of an aggregated type as external function outputs.
Definition realTraits.hpp:404
inlinebool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:145
inlinePassiveReal< Type > getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:133
inlinetypename DataExtraction< Type >::Identifier getIdentifier(Type const &v)
Extract an aggregate of identifiers from an aggregate of active types.
Definition realTraits.hpp:380
inlinesize_t constexpr MaxDerivativeOrder()
CoDiPack derivative order of the type.
Definition realTraits.hpp:127
typename enable_if_base_of< AggregatedActiveTypeBase< typename Expr::Real, typename Expr::InnerActiveType, typename Expr::Impl, Expr::isStatic >, Expr >::type EnableIfAggregatedActiveType
Enable if helper for AggregatedActiveType.
Definition realTraits.hpp:421
inlinetypename DataExtraction< Type >::Real getValue(Type const &v)
Extract an aggregate of primal values from an aggregate of active types.
Definition realTraits.hpp:374
bool constexpr isPassiveReal
Value entry of IsPassiveReal.
Definition realTraits.hpp:432
inlinevoid setValue(Type &v, typename DataExtraction< Type >::Real const &value)
Set the primal values of an aggregate of active types.
Definition realTraits.hpp:386
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
inlinevoid registerOutput(Type &v)
Register all active types of an aggregated type as tape outputs.
Definition realTraits.hpp:398
typename std::enable_if<!IsPassiveReal< Type >::value >::type EnableIfNotPassiveReal
Negated enable if wrapper for IsPassiveReal.
Definition realTraits.hpp:436
inlinevoid registerInput(Type &v)
Register all active types of an aggregated type as tape inputs.
Definition realTraits.hpp:392
std::is_same< Type, PassiveReal< Type > > IsPassiveReal
If the real type is not handled by CoDiPack.
Definition realTraits.hpp:428
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
std::enable_if< std::is_base_of< Base, Impl >::value, R > enable_if_base_of
Enable if abbreviation for std::is_base_of.
Definition enableIfHelpers.hpp:47
inlinevoid static_for(F func, Args &&... args)
Static for with i = 0 .. (N - 1). See CompileTimeLoop for details.
Definition compileTimeLoop.hpp:110
inlineauto real(ExpressionInterface< std::complex< Real >, Arg > const &arg)
Function overload for FUNCTION.
Definition allOperators.hpp:75
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
inlinebool isfinite(ExpressionInterface< Real, Arg > const &arg)
Function overload for isfinite.
Definition unaryOperators.hpp:787
Defines an aggregated type via an array and implements the ExpressionInterface.
Definition aggregatedActiveType.hpp:59
If the expression inherits from LhsExpressionInterface. Is either std::false_type or std::true_type.
Definition expressionTraits.hpp:164
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
int InnerType
Definition realTraits.hpp:229
static int constexpr Elements
Definition realTraits.hpp:232
int Real
Definition realTraits.hpp:230
Real Type
Definition realTraits.hpp:228
inlinestatic InnerType & arrayAccess(Type &v)
Definition realTraits.hpp:270
static Type arrayConstructor(InnerType const *v)
Definition realTraits.hpp:236
static InnerType adjointOfConstructor(Type const &w, Type const &w_b)
Definition realTraits.hpp:249
static InnerType arrayAccess(Type const &w)
Definition realTraits.hpp:260
static Type adjointOfArrayAccess(Type const &w, InnerType const &v_b)
Definition realTraits.hpp:282
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition realTraits.hpp:291
Base implementation of AggregatedTypeTraits for aggregated types that can be defined as an array.
Definition realTraits.hpp:301
inlinestatic Type adjointOfArrayAccess(Type const &w, InnerType const &v_b)
Definition realTraits.hpp:356
T_Real Real
See AggregatedTypeTraits.
Definition realTraits.hpp:310
inlinestatic Type arrayConstructor(InnerType const *v)
Definition realTraits.hpp:315
static int constexpr Elements
See AggregatedTypeTraits.
Definition realTraits.hpp:312
T_InnerType InnerType
See AggregatedTypeTraits.
Definition realTraits.hpp:309
inlinestatic InnerType & arrayAccess(Type &w)
Definition realTraits.hpp:340
inlinestatic InnerType const & arrayAccess(Type const &w)
Definition realTraits.hpp:348
T_Type Type
See AggregatedTypeTraits.
Definition realTraits.hpp:308
inlinestatic InnerType adjointOfConstructor(Type const &w, Type const &w_b)
Definition realTraits.hpp:329
Data handling methods for aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:164
inlinestatic void setValue(Type &v, Real const &value)
Set the primal values of an aggregate of active types.
inlinestatic Identifier getIdentifier(Type const &v)
Extract an aggregate of identifiers from an aggregate of active types.
typename Type::Real Real
Type of a corresponding aggregate of primal values.
Definition realTraits.hpp:171
inlinestatic Real getValue(Type const &v)
Extract an aggregate of primal values from an aggregate of active types.
typename Type::Identifier Identifier
Type of a corresponding aggregate of identifiers.
Definition realTraits.hpp:172
inlinestatic void setIdentifier(Type &v, Identifier const &identifier)
Set the identifiers of a type of aggregated active types.
T_Type Type
See DataExtraction.
Definition realTraits.hpp:169
Function for checking if all values of the type are finite.
Definition realTraits.hpp:88
T_Type Type
See IsTotalFinite.
Definition realTraits.hpp:91
static inlinebool isTotalFinite(Type const &v)
Checks if the values are all finite.
Definition realTraits.hpp:94
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:106
T_Type Type
See IsTotalZero.
Definition realTraits.hpp:109
static inlinebool isTotalZero(Type const &v)
Checks if the values are completely zero.
Definition realTraits.hpp:112
Tape registration methods for aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:197
T_Type Type
See TapeRegistration.
Definition realTraits.hpp:202
inlinestatic void registerInput(Type &v)
Register all active types of an aggregated type as tape inputs.
inlinestatic void registerOutput(Type &v)
Register all active types of an aggregated type as tape outputs.
typename DataExtraction< Type >::Real Real
See DataExtraction::Real.
Definition realTraits.hpp:204
inlinestatic Real registerExternalFunctionOutput(Type &v)
Register all active types of an aggregated type as external function outputs.
Common traits for all types used as real values.
Definition realTraits.hpp:66
static int constexpr MaxDerivativeOrder
CoDiPack derivative order of the type.
Definition realTraits.hpp:74
T_Type Type
See TraitsImplementation.
Definition realTraits.hpp:69
static inlinePassiveReal getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition realTraits.hpp:77
Type PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:72
Type Real
Inner type of the real value.
Definition realTraits.hpp:71