Documentation 6b54b78464
Loading...
Searching...
No Matches
datatype_structure.hpp
Go to the documentation of this file.
1/*!
2 * \file datatype_structure.hpp
3 * \brief Headers for generalized datatypes, defines an interface for AD types.
4 * \author T. Albring
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
30#include <iostream>
31#include <complex>
32#include <cstdio>
33
34#include "../code_config.hpp"
35#include "ad_structure.hpp"
36
37/*!
38 * \namespace SU2_TYPE
39 * \brief Namespace for defining the datatype wrapper routines, this acts as a base
40 * class for type interfaces for non-primitive dataypes e.g. used by AD, complex etc.
41 * \author T. Albring
42 */
43namespace SU2_TYPE {
44/*!
45 * \brief Set the (primitive) value of the datatype (needs to be implemented for each new type).
46 * \param[in] data - The non-primitive datatype.
47 * \param[in] val - The primitive value.
48 */
49void SetValue(su2double& data, const passivedouble& val);
50
51/*!
52 * \brief Set the secondary value of the datatype (needs to be implemented for each new type).
53 * \param[in] data - The non-primitive datatype.
54 * \param[in] val - The primitive value.
55 */
56void SetSecondary(su2double& data, const passivedouble& val);
57
58/*!
59 * \brief Get the (primitive) value of the datatype (needs to be specialized for active types).
60 * \param[in] data - The non-primitive datatype.
61 * \return The primitive value.
62 */
64
65/*!
66 * \brief Get the secondary value of the datatype (needs to be implemented for each new type).
67 * \param[in] data - The non-primitive datatype.
68 * \return The primitive value.
69 */
71
72/*!
73 * \brief Get the derivative value of the datatype (needs to be implemented for each new type).
74 * \param[in] data - The non-primitive datatype.
75 * \return The derivative value.
76 */
78
79/*!
80 * \brief Set the derivative value of the datatype (needs to be implemented for each new type).
81 * \param[in] data - The non-primitive datatype.
82 * \param[in] val - The value of the derivative.
83 */
84void SetDerivative(su2double& data, const passivedouble& val);
85
86/*--- Implementation of the above for the different types. ---*/
87
88#if defined(CODI_REVERSE_TYPE) || defined(CODI_FORWARD_TYPE)
89
90FORCEINLINE void SetValue(su2double& data, const passivedouble& val) { data.setValue(val); }
91
92FORCEINLINE passivedouble GetValue(const double& data) { return data; }
93
94FORCEINLINE passivedouble GetValue(const su2double& data) { return data.getValue(); }
95
96template <typename Expr>
97FORCEINLINE passivedouble GetValue(const codi::ExpressionInterface<double, Expr>& data) {
98 return data.cast().getValue();
99}
100
101FORCEINLINE void SetSecondary(su2double& data, const passivedouble& val) { data.setGradient(val); }
102
103FORCEINLINE void SetDerivative(su2double& data, const passivedouble& val) { data.setGradient(val); }
104
105FORCEINLINE passivedouble GetSecondary(const su2double& data) { return data.getGradient(); }
106
107FORCEINLINE passivedouble GetDerivative(const su2double& data) { return data.getGradient(); }
108
109#else // passive type, no AD
110
111FORCEINLINE void SetValue(su2double& data, const passivedouble& val) { data = val; }
112
113FORCEINLINE passivedouble GetValue(const su2double& data) { return data; }
114
116
118
120
122#endif
123
124/*!
125 * \brief Get the passive value of any variable. For most types return directly,
126 * specialize for su2double to call GetValue.
127 * \note This is a struct instead of a function because the return type of the
128 * su2double specialization changes.
129 */
130template <class T>
131struct Passive {
132 FORCEINLINE static T Value(const T& val) { return val; }
133};
134template <>
136 FORCEINLINE static passivedouble Value(const su2double& val) { return GetValue(val); }
137};
138template <class T>
139FORCEINLINE auto PassiveValue(const T& val) {
140 return Passive<T>::Value(val);
141}
142
143/*!
144 * \brief Casts the primitive value to int (uses GetValue, already implemented for each type).
145 * \param[in] data - The non-primitive datatype.
146 * \return - The primary value casted to int.
147 */
148FORCEINLINE int Int(const su2double& data) { return static_cast<int>(SU2_TYPE::GetValue(data)); }
149
150/*!
151 * \brief Casts the primitive value to short (uses GetValue, already implemented for each type).
152 * \param[in] data - The non-primitive datatype.
153 * \return - The primary value casted to short.
154 */
155FORCEINLINE short Short(const su2double& data) { return static_cast<short>(SU2_TYPE::GetValue(data)); }
156
157/*--- Special handling of the sprintf routine for non-primitive types. ---*/
158/*--- Pass-through for built-in types. ---*/
159template <class T, su2enable_if<std::is_trivial<T>::value> = 0>
160FORCEINLINE const T& _printGetValue(const T& val) {
161 return val;
162}
163/*--- Overload for expressions of active types. ---*/
164template <class T, su2enable_if<!std::is_trivial<T>::value> = 0>
166 return val.getValue();
167}
168
169/*!
170 * \brief Wrapper to sprintf to be able to print active types and AD expressions.
171 * \note This is for compatibility with old code, stringstreams should be the preferred way to build strings.
172 * \param[in] str - Target char buffer.
173 * \param[in] format - Format string.
174 * \param[in] args - Values to be printed to the string.
175 */
176template <class... Ts>
177FORCEINLINE void sprintf(char* str, const char* format, Ts&&... args) {
178 ::sprintf(str, format, SU2_TYPE::_printGetValue(args)...);
179}
180FORCEINLINE void sprintf(char* str, const char* literal) { ::sprintf(str, "%s", literal); }
181#define SPRINTF SU2_TYPE::sprintf
182
183} // namespace SU2_TYPE
simd::Array< unsigned long, Double::Size > Int
Definition CNumericsSIMD.hpp:37
Main routines for the algorithmic differentiation (AD) structure.
double passivedouble
Definition code_config.hpp:129
#define FORCEINLINE
Definition code_config.hpp:44
passivedouble su2double
Definition code_config.hpp:181
Namespace for defining the datatype wrapper routines, this acts as a base class for type interfaces f...
void sprintf(char *str, const char *format, Ts &&... args)
Wrapper to sprintf to be able to print active types and AD expressions.
Definition datatype_structure.hpp:177
void SetSecondary(su2double &data, const passivedouble &val)
Set the secondary value of the datatype (needs to be implemented for each new type).
Definition datatype_structure.hpp:115
short Short(const su2double &data)
Casts the primitive value to short (uses GetValue, already implemented for each type).
Definition datatype_structure.hpp:155
passivedouble GetDerivative(const su2double &data)
Get the derivative value of the datatype (needs to be implemented for each new type).
Definition datatype_structure.hpp:117
passivedouble GetSecondary(const su2double &data)
Get the secondary value of the datatype (needs to be implemented for each new type).
Definition datatype_structure.hpp:119
auto PassiveValue(const T &val)
Definition datatype_structure.hpp:139
void SetDerivative(su2double &data, const passivedouble &val)
Set the derivative value of the datatype (needs to be implemented for each new type).
Definition datatype_structure.hpp:121
void SetValue(su2double &data, const passivedouble &val)
Set the (primitive) value of the datatype (needs to be implemented for each new type).
Definition datatype_structure.hpp:111
const T & _printGetValue(const T &val)
Definition datatype_structure.hpp:160
passivedouble GetValue(const su2double &data)
Get the (primitive) value of the datatype (needs to be specialized for active types).
Definition datatype_structure.hpp:113
static passivedouble Value(const su2double &val)
Definition datatype_structure.hpp:136
Get the passive value of any variable. For most types return directly, specialize for su2double to ca...
Definition datatype_structure.hpp:131
static T Value(const T &val)
Definition datatype_structure.hpp:132