CoDiPack  3.1.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-2026 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 using TapeData = typename Type::TapeData;
174
176 CODI_INLINE static Real getValue(Type const& v);
177
180
183
185 CODI_INLINE static void setValue(Type& v, Real const& value);
186
188 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier);
189
191 CODI_INLINE static void setTapeData(Type& v, TapeData const& data);
192 };
193
203 template<typename T_Type, typename = void>
205 public:
206 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
207 "Instantiation of unspecialized RealTraits::TapeRegistration.");
208
209 using Type = CODI_DD(T_Type, CODI_ANY);
210
212
215
218
221 };
222
232 template<typename T_Type, typename = void>
234 public:
235 using Type = CODI_DD(T_Type, double);
237 using Real = CODI_ANY;
238
239 static int constexpr Elements = 0;
240
243 static Type arrayConstructor(InnerType const* v) {
244 CODI_UNUSED(v);
245 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
246 "Instantiation of unspecialized AggregatedTypeTraits.");
247
248 return Type{};
249 }
250
255 template<size_t element>
256 static InnerType adjointOfConstructor(Type const& w, Type const& w_b) {
257 CODI_UNUSED(w, w_b);
258 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
259 "Instantiation of unspecialized AggregatedTypeTraits.");
260
261 return InnerType{};
262 }
263
266 template<size_t element>
267 static InnerType arrayAccess(Type const& w) {
268 CODI_UNUSED(w);
269 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
270 "Instantiation of unspecialized AggregatedTypeTraits.");
271
272 return InnerType{};
273 }
274
276 template<size_t element>
278 CODI_UNUSED(v);
279 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
280 "Instantiation of unspecialized AggregatedTypeTraits.");
281
282 static InnerType temp = {};
283 return temp;
284 }
285
288 template<size_t element>
289 static Type adjointOfArrayAccess(Type const& w, InnerType const& v_b) {
290 CODI_UNUSED(w, v_b);
291 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
292 "Instantiation of unspecialized AggregatedTypeTraits.");
293
294 return Type{};
295 }
296
298 static CODI_INLINE std::string getMathRep() {
299 CODI_STATIC_ASSERT(false && std::is_void<Type>::value,
300 "Instantiation of unspecialized AggregatedTypeTraits.");
301
302 return "";
303 }
304 };
305
307 template<typename T_Type, typename T_InnerType, typename T_Real, int T_Elements>
309 public:
310 static_assert(
311 sizeof(T_Type) == T_Elements * sizeof(T_InnerType),
312 "ArrayAggregatedTypeTraitsBase is designed for aggregated types that resemble arrays of their inner type."
313 " The sizes in the given instantiation do not match this use case.");
314
315 using Type = CODI_DD(T_Type, CODI_ANY);
316 using InnerType = CODI_DD(T_InnerType, CODI_ANY);
317 using Real = CODI_DD(T_Real, CODI_ANY);
318
319 static int constexpr Elements = CODI_DD(T_Elements, 1);
320
323 Type w{};
324
325 InnerType* wArray = reinterpret_cast<InnerType*>(&w);
326
328 wArray[i.value] = v[i.value];
329 });
330
331 return w;
332 }
333
335 template<size_t element>
336 CODI_INLINE static InnerType adjointOfConstructor(Type const& w, Type const& w_b) {
337 CODI_UNUSED(w);
338
339 // We assume that dT/dv is the identity and is not holomorphic. That means no one is changed to a minus one on
340 // a transpose.
341 InnerType const* w_bArray = reinterpret_cast<InnerType const*>(&w_b);
342 return ComputationTraits::transpose(w_bArray[element]);
343 }
344
346 template<size_t element>
348 InnerType* wArray = reinterpret_cast<InnerType*>(&w);
349
350 return wArray[element];
351 }
352
354 template<size_t element>
355 CODI_INLINE static InnerType const& arrayAccess(Type const& w) {
356 InnerType const* wArray = reinterpret_cast<InnerType const*>(&w);
357
358 return wArray[element];
359 }
360
362 template<size_t element>
363 CODI_INLINE static Type adjointOfArrayAccess(Type const& w, InnerType const& v_b) {
364 CODI_UNUSED(w);
365
366 // We assume that dw[element]/w is the identity and is not holomorphic. That means no one is changed to a
367 // minus one on a transpose.
368 Type w_b{};
369 InnerType* w_bArray = reinterpret_cast<InnerType*>(&w_b);
371 w_bArray[i.value] = InnerType();
372 });
373 w_bArray[element] = v_b;
374
375 return w_b;
376 }
377 };
378
380 template<typename Type>
384
386 template<typename Type>
390
392 template<typename Type>
393 CODI_INLINE void setValue(Type& v, typename DataExtraction<Type>::Real const& value) {
394 return DataExtraction<Type>::setValue(v, value);
395 }
396
398 template<typename Type>
402
404 template<typename Type>
408
410 template<typename Type>
414
416 /*******************************************************************************/
419
421 template<typename Type>
423 typename std::enable_if<(AggregatedTypeTraits<Type>::Elements != 0) &
425
427 template<typename Expr>
429 typename enable_if_base_of<AggregatedActiveTypeBase<typename Expr::Real, typename Expr::InnerActiveType,
430 typename Expr::Impl, Expr::isStatic>,
431 Expr>::type;
432
434 template<typename Type>
435 using IsPassiveReal = std::is_same<Type, PassiveReal<Type>>;
436
438 template<typename Expr>
440
442 template<typename Type>
443 using EnableIfNotPassiveReal = typename std::enable_if<!IsPassiveReal<Type>::value>::type;
444
446 template<typename Type>
447 using EnableIfPassiveReal = typename std::enable_if<IsPassiveReal<Type>::value>::type;
448
449#ifndef DOXYGEN_DISABLE
450
452 /*******************************************************************************/
455
457 template<typename T_Type>
458 struct DataExtraction<T_Type, typename std::enable_if<std::is_floating_point<T_Type>::value>::type> {
459 public:
460 using Type = CODI_DD(T_Type, double);
461
462 using Real = double;
463 using Identifier = int;
464 using TapeData = int;
465
467 CODI_INLINE static Real getValue(Type const& v) {
468 return v;
469 }
470
472 CODI_INLINE static Identifier getIdentifier(Type const& v) {
473 CODI_UNUSED(v);
474
475 return 0;
476 }
477
479 CODI_INLINE static TapeData getTapeData(Type const& v) {
480 CODI_UNUSED(v);
481
482 return 0;
483 }
484
486 CODI_INLINE static void setValue(Type& v, Real const& value) {
487 v = value;
488 }
489
491 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier) {
492 CODI_UNUSED(v, identifier);
493 }
494
496 CODI_INLINE static void setTapeData(Type& v, TapeData const& data) {
497 CODI_UNUSED(v, data);
498 }
499 };
500
502 template<typename T_Type>
503 struct DataExtraction<T_Type, EnableIfAggregatedTypeTraitsIsSpecialized<T_Type>> {
504 public:
505
506 using Type = CODI_DD(T_Type, double);
507
508 using TypeTraits = AggregatedTypeTraits<Type>;
509 using InnerType = typename TypeTraits::InnerType;
510 static int constexpr Elements = TypeTraits::Elements;
511
512 using InnerDataExtraction = DataExtraction<InnerType>;
513
514 using Real = typename TypeTraits::Real;
515 using Identifier = std::array<CODI_DD(typename InnerType::Identifier, int), Elements>;
516 using TapeData = std::array<CODI_DD(typename InnerType::TapeData, int), Elements>;
517
518 using RealTypeTraits = AggregatedTypeTraits<Real>;
519
521 CODI_INLINE static Real getValue(Type const& v) {
522 Real real{};
523
525 RealTypeTraits::template arrayAccess<i.value>(real) =
526 InnerDataExtraction::getValue(TypeTraits::template arrayAccess<i.value>(v));
527 });
528
529 return real;
530 }
531
533 CODI_INLINE static Identifier getIdentifier(Type const& v) {
534 Identifier res;
535
537 res[i.value] = InnerDataExtraction::getIdentifier(TypeTraits::template arrayAccess<i.value>(v));
538 });
539
540 return res;
541 }
542
544 CODI_INLINE static TapeData getTapeData(Type const& v) {
545 TapeData res;
546
548 res[i.value] = InnerDataExtraction::getTapeData(TypeTraits::template arrayAccess<i.value>(v));
549 });
550
551 return res;
552 }
553
555 CODI_INLINE static void setValue(Type& v, Real const& value) {
557 InnerDataExtraction::setValue(TypeTraits::template arrayAccess<i.value>(v),
558 RealTypeTraits::template arrayAccess<i.value>(value));
559 });
560 }
561
563 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier) {
565 InnerDataExtraction::setIdentifier(TypeTraits::template arrayAccess<i.value>(v), identifier[i.value]);
566 });
567 }
568
570 CODI_INLINE static void setTapeData(Type& v, TapeData const& data) {
572 InnerDataExtraction::setTapeData(TypeTraits::template arrayAccess<i.value>(v), data[i.value]);
573 });
574 }
575 };
576
578 template<typename T_Type>
580 public:
581
582 using Type = CODI_DD(T_Type, CODI_ANY);
583
584 using TypeTraits = AggregatedTypeTraits<Type>;
585 using InnerType = typename TypeTraits::InnerType;
586 static int constexpr Elements = TypeTraits::Elements;
587
588 using Real = typename TypeTraits::Real;
589
590 using RealTypeTraits = RealTraits::AggregatedTypeTraits<Real>;
591 using InnerTraits = TapeRegistration<InnerType>;
592
594 CODI_INLINE static void registerInput(Type& v) {
596 InnerTraits::registerInput(TypeTraits::template arrayAccess<i.value>(v));
597 });
598 }
599
601 CODI_INLINE static void registerOutput(Type& v) {
603 InnerTraits::registerOutput(TypeTraits::template arrayAccess<i.value>(v));
604 });
605 }
606
609 Real res{};
610
612 RealTypeTraits::template arrayAccess<i.value>(res) =
613 InnerTraits::registerExternalFunctionOutput(TypeTraits::template arrayAccess<i.value>(v));
614 });
615
616 return res;
617 }
618 };
619 template<typename T_Type>
620 struct AggregatedTypeTraits<T_Type, typename std::enable_if<std::is_floating_point<T_Type>::value>::type>
621 : ArrayAggregatedTypeTraitsBase<T_Type, T_Type, T_Type, 1> {};
622
623 template<typename T_Type>
624 struct AggregatedTypeTraits<T_Type, typename std::enable_if<std::is_integral<T_Type>::value>::type>
625 : ArrayAggregatedTypeTraitsBase<T_Type, T_Type, T_Type, 1> {};
626
627 template<>
629
630#endif
631
633
634 }
635}
#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:97
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:101
#define CODI_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:130
Traits for values that can be used as real values, e.g. double, float, codi::RealReverse etc....
Definition stdComplex.hpp:242
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:422
typename std::enable_if< IsPassiveReal< Type >::value >::type EnableIfPassiveReal
Enable if wrapper for IsPassiveReal.
Definition realTraits.hpp:447
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:411
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:387
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:428
inlinetypename DataExtraction< Type >::Real getValue(Type const &v)
Extract an aggregate of primal values from an aggregate of active types.
Definition realTraits.hpp:381
bool constexpr isPassiveReal
Value entry of IsPassiveReal.
Definition realTraits.hpp:439
inlinevoid setValue(Type &v, typename DataExtraction< Type >::Real const &value)
Set the primal values of an aggregate of active types.
Definition realTraits.hpp:393
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:405
typename std::enable_if<!IsPassiveReal< Type >::value >::type EnableIfNotPassiveReal
Negated enable if wrapper for IsPassiveReal.
Definition realTraits.hpp:443
inlinevoid registerInput(Type &v)
Register all active types of an aggregated type as tape inputs.
Definition realTraits.hpp:399
std::is_same< Type, PassiveReal< Type > > IsPassiveReal
If the real type is not handled by CoDiPack.
Definition realTraits.hpp:435
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
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:55
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:179
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:233
int InnerType
Definition realTraits.hpp:236
static int constexpr Elements
Definition realTraits.hpp:239
int Real
Definition realTraits.hpp:237
Real Type
Definition realTraits.hpp:235
inlinestatic InnerType & arrayAccess(Type &v)
Definition realTraits.hpp:277
static Type arrayConstructor(InnerType const *v)
Definition realTraits.hpp:243
static InnerType adjointOfConstructor(Type const &w, Type const &w_b)
Definition realTraits.hpp:256
static InnerType arrayAccess(Type const &w)
Definition realTraits.hpp:267
static Type adjointOfArrayAccess(Type const &w, InnerType const &v_b)
Definition realTraits.hpp:289
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition realTraits.hpp:298
Base implementation of AggregatedTypeTraits for aggregated types that can be defined as an array.
Definition realTraits.hpp:308
inlinestatic Type adjointOfArrayAccess(Type const &w, InnerType const &v_b)
Definition realTraits.hpp:363
T_Real Real
See AggregatedTypeTraits.
Definition realTraits.hpp:317
inlinestatic Type arrayConstructor(InnerType const *v)
Definition realTraits.hpp:322
static int constexpr Elements
See AggregatedTypeTraits.
Definition realTraits.hpp:319
T_InnerType InnerType
See AggregatedTypeTraits.
Definition realTraits.hpp:316
inlinestatic InnerType & arrayAccess(Type &w)
Definition realTraits.hpp:347
inlinestatic InnerType const & arrayAccess(Type const &w)
Definition realTraits.hpp:355
T_Type Type
See AggregatedTypeTraits.
Definition realTraits.hpp:315
inlinestatic InnerType adjointOfConstructor(Type const &w, Type const &w_b)
Definition realTraits.hpp:336
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 TapeData getTapeData(Type const &v)
Extract an aggregate of tape data from 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 void setTapeData(Type &v, TapeData const &data)
Set the tape data of a type of aggregated active types.
typename Type::TapeData TapeData
Type of a corresponding aggregate of tape data.
Definition realTraits.hpp:173
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:204
T_Type Type
See TapeRegistration.
Definition realTraits.hpp:209
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:211
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