CoDiPack  2.2.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-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 <cmath>
38#include <complex>
39
40#include "../config.h"
41#include "../misc/macros.hpp"
42#include "expressionTraits.hpp"
43
45namespace codi {
46
48 namespace RealTraits {
49
50 /*******************************************************************************/
53
59 template<typename T_Type, typename = void>
61 public:
62
63 using Type = CODI_DD(T_Type, double);
64
65 using Real = Type;
66 using PassiveReal = Type;
67
68 static int constexpr MaxDerivativeOrder = 0;
69
71 static CODI_INLINE PassiveReal const& getPassiveValue(Type const& v) {
72 return v;
73 }
74 };
75
81 template<typename T_Type, typename = void>
83 public:
84
85 using Type = CODI_DD(T_Type, double);
86
88 static CODI_INLINE bool isTotalFinite(Type const& v) {
89 using std::isfinite;
90 return isfinite(v);
91 }
92 };
93
99 template<typename T_Type, typename = void>
100 struct IsTotalZero {
101 public:
102
103 using Type = CODI_DD(T_Type, double);
104
106 static CODI_INLINE bool isTotalZero(Type const& v) {
107 return Type() == v;
108 }
109 };
110
112 template<typename Type>
114
116 template<typename Type>
118
120 template<typename Type>
121 CODI_INLINE size_t constexpr MaxDerivativeOrder() {
123 }
124
126 template<typename Type>
129 }
130
132 template<typename Type>
133 CODI_INLINE bool isTotalFinite(Type const& v) {
135 }
136
138 template<typename Type>
139 CODI_INLINE bool isTotalZero(Type const& v) {
141 }
142
144 /*******************************************************************************/
147
157 template<typename T_Type, typename = void>
159 public:
160 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
161 "Instantiation of unspecialized RealTraits::DataExtraction.");
162
163 using Type = CODI_DD(T_Type, ActiveTypeProxy);
164
165 using Real = typename Type::Real;
167 typename Type::Identifier;
168
170 CODI_INLINE static Real getValue(Type const& v);
171
174
176 CODI_INLINE static void setValue(Type& v, Real const& value);
177 };
178
188 template<typename T_Type, typename = void>
190 public:
191 CODI_STATIC_ASSERT(false && std::is_void<T_Type>::value,
192 "Instantiation of unspecialized RealTraits::TapeRegistration.");
193
194 using Type = CODI_DD(T_Type, CODI_ANY);
195
197
200
203
206 };
207
209 template<typename Type>
210 typename DataExtraction<Type>::Real getValue(Type const& v) {
212 }
213
215 template<typename Type>
218 }
219
221 template<typename Type>
222 void setValue(Type& v, typename DataExtraction<Type>::Real const& value) {
223 return DataExtraction<Type>::setValue(v, value);
224 }
225
227 template<typename Type>
228 void registerInput(Type& v) {
230 }
231
233 template<typename Type>
234 void registerOutput(Type& v) {
236 }
237
239 template<typename Type>
242 }
243
244#ifndef DOXYGEN_DISABLE
245
247 template<typename T_Type>
248 struct DataExtraction<T_Type, typename std::enable_if<std::is_floating_point<T_Type>::value>::type> {
249 public:
250 using Type = CODI_DD(T_Type, double);
251
252 using Real = double;
253 using Identifier = int;
254
256 CODI_INLINE static Real getValue(Type const& v) {
257 return v;
258 }
259
261 CODI_INLINE static Identifier getIdentifier(Type const& v) {
262 CODI_UNUSED(v);
263
264 return 0;
265 }
266
268 CODI_INLINE static void setValue(Type& v, Real const& value) {
269 v = value;
270 }
271 };
272
274 template<typename T_InnerType>
275 struct DataExtraction<std::complex<T_InnerType>> {
276 public:
277
278 using InnerType = CODI_DD(T_InnerType, ActiveTypeProxy);
279 using Type = std::complex<T_InnerType>;
280
281 using InnerExtraction = DataExtraction<InnerType>;
282
283 using Real = std::complex<typename InnerExtraction::Real>;
284 using Identifier = std::complex<typename InnerExtraction::Identifier>;
285
287 CODI_INLINE static Real getValue(Type const& v) {
288 InnerType const* vArray = reinterpret_cast<InnerType const*>(&v);
289
290 return Real(InnerExtraction::getValue(vArray[0]), InnerExtraction::getValue(vArray[1]));
291 }
292
294 CODI_INLINE static Identifier getIdentifier(Type const& v) {
295 InnerType const* vArray = reinterpret_cast<InnerType const*>(&v);
296
297 return Identifier(InnerExtraction::getIdentifier(vArray[0]), InnerExtraction::getIdentifier(vArray[1]));
298 }
299
301 CODI_INLINE static void setValue(Type& v, Real const& value) {
302 InnerType* vArray = reinterpret_cast<InnerType*>(&v);
303
304 InnerExtraction::setValue(vArray[0], std::real(value));
305 InnerExtraction::setValue(vArray[1], std::imag(value));
306 }
307 };
308
310 template<typename T_InnerType>
311 struct TapeRegistration<std::complex<T_InnerType>> {
312 public:
313
314 using InnerType = CODI_DD(T_InnerType, CODI_ANY);
315 using Type = std::complex<T_InnerType>;
316
317 using InnerRegistration = TapeRegistration<InnerType>;
318
319 using Real = typename DataExtraction<Type>::Real;
320
322 CODI_INLINE static void registerInput(Type& v) {
323 InnerType* vArray = reinterpret_cast<InnerType*>(&v);
324
325 InnerRegistration::registerInput(vArray[0]);
326 InnerRegistration::registerInput(vArray[1]);
327 }
328
330 CODI_INLINE static void registerOutput(Type& v) {
331 InnerType* vArray = reinterpret_cast<InnerType*>(&v);
332
333 InnerRegistration::registerOutput(vArray[0]);
334 InnerRegistration::registerOutput(vArray[1]);
335 }
336
339 InnerType* vArray = reinterpret_cast<InnerType*>(&v);
340
341 return Real(InnerRegistration::registerExternalFunctionOutput(vArray[0]),
342 InnerRegistration::registerExternalFunctionOutput(vArray[1]));
343 }
344 };
345#endif
346
348 /*******************************************************************************/
351
353 template<typename Type>
354 using IsPassiveReal = std::is_same<Type, PassiveReal<Type>>;
355
356#if CODI_IS_CPP14
358 template<typename Expr>
359 bool constexpr isPassiveReal = IsPassiveReal<Expr>::value;
360#endif
361
363 template<typename Type>
364 using EnableIfNotPassiveReal = typename std::enable_if<!IsPassiveReal<Type>::value>::type;
365
367 template<typename Type>
368 using EnableIfPassiveReal = typename std::enable_if<IsPassiveReal<Type>::value>::type;
369
371
372 }
373}
#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_ANY
Used in default declarations of expression templates.
Definition: macros.hpp:98
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition: macros.hpp:123
PassiveReal< Type > const & getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition: realTraits.hpp:127
typename std::enable_if<!IsPassiveReal< Type >::value >::type EnableIfNotPassiveReal
Negated enable if wrapper for IsPassiveReal.
Definition: realTraits.hpp:364
DataExtraction< Type >::Identifier registerExternalFunctionOutput(Type &v)
Register all active types of a aggregated type as external function outputs.
Definition: realTraits.hpp:240
DataExtraction< Type >::Identifier getIdentifier(Type const &v)
Extract the identifiers from a type of aggregated active types.
Definition: realTraits.hpp:216
size_t constexpr MaxDerivativeOrder()
CoDiPack derivative order of the type.
Definition: realTraits.hpp:121
void setValue(Type &v, typename DataExtraction< Type >::Real const &value)
Set the primal values of a type of aggregated active types.
Definition: realTraits.hpp:222
void registerInput(Type &v)
Register all active types of a aggregated type as tape input.
Definition: realTraits.hpp:228
std::is_same< Type, PassiveReal< Type > > IsPassiveReal
If the real type is not handled by CoDiPack.
Definition: realTraits.hpp:354
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition: realTraits.hpp:117
void registerOutput(Type &v)
Register all active types of a aggregated type as tape output.
Definition: realTraits.hpp:234
typename TraitsImplementation< Type >::Real Real
Inner type of the real value.
Definition: realTraits.hpp:113
bool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition: realTraits.hpp:133
DataExtraction< Type >::Real getValue(Type const &v)
Extract the primal values from a type of aggregated active types.
Definition: realTraits.hpp:210
bool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition: realTraits.hpp:139
typename std::enable_if< IsPassiveReal< Type >::value >::type EnableIfPassiveReal
Enable if wrapper for IsPassiveReal.
Definition: realTraits.hpp:368
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
bool isfinite(ExpressionInterface< Real, Arg > const &arg)
Function overload for isfinite.
Definition: unaryOperators.hpp:539
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
Data handling methods for aggregated types that contain CoDiPack active types.
Definition: realTraits.hpp:158
typename Type::Real Real
Type of primal values extracted from the type with AD values.
Definition: realTraits.hpp:165
static Real getValue(Type const &v)
Extract the primal values from a type of aggregated active types.
static void setValue(Type &v, Real const &value)
Set the primal values of a type of aggregated active types.
typename Type::Identifier Identifier
Type of identifier values extracted from the type with AD values.
Definition: realTraits.hpp:167
T_Type Type
See DataExtraction.
Definition: realTraits.hpp:163
static Identifier getIdentifier(Type const &v)
Extract the identifiers from a type of aggregated active types.
Function for checking if all values of the type are finite.
Definition: realTraits.hpp:82
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
Function for checking if the value of the type is completely zero.
Definition: realTraits.hpp:100
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
Tape registration methods for aggregated types that contain CoDiPack active types.
Definition: realTraits.hpp:189
static Real registerExternalFunctionOutput(Type &v)
Register all active types of a aggregated type as external function outputs.
static void registerOutput(Type &v)
Register all active types of a aggregated type as tape output.
static void registerInput(Type &v)
Register all active types of a aggregated type as tape input.
typename DataExtraction< Type >::Real Real
See DataExtraction::Real.
Definition: realTraits.hpp:196
T_Type Type
See TapeRegistration.
Definition: realTraits.hpp:194
Common traits for all types used as real values.
Definition: realTraits.hpp:60
static PassiveReal const & getPassiveValue(Type const &v)
Get the basic primal value of the type.
Definition: realTraits.hpp:71
static int constexpr MaxDerivativeOrder
CoDiPack derivative order of the type.
Definition: realTraits.hpp:68
T_Type Type
See TraitsImplementation.
Definition: realTraits.hpp:63
Type PassiveReal
The original computation type, that was used in the application.
Definition: realTraits.hpp:66
Type Real
Inner type of the real value.
Definition: realTraits.hpp:65