Documentation 6b54b78464
Loading...
Searching...
No Matches
CPrimitiveIndices.hpp
Go to the documentation of this file.
1/*!
2 * \file CPrimitiveIndices.hpp
3 * \brief Abstract representation of flow primitive variable indices that tries to be efficient.
4 * \version 8.5.0 "Harrier"
5 *
6 * SU2 Project Website: https://su2code.github.io
7 *
8 * The SU2 Project is maintained by the SU2 Foundation
9 * (http://su2foundation.org)
10 *
11 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
12 *
13 * SU2 is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * SU2 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27#pragma once
28
29#include <cstdint>
30#include <cstring>
31#include <map>
32#include <string>
33
34#include "CEulerVariable.hpp"
35#include "CIncEulerVariable.hpp"
37
38/*!
39 * \class CPrimitiveIndices
40 * \brief Abstract wrapper for the CIndices classes of CEulerVariable, CIncEulerVariable, etc.
41 * This should only be used when the concrete type of solver variable is not known.
42 * \note We are using some tricks to try to make this more efficient than virtual functions.
43 */
44template <class IndexType>
46 CPrimitiveIndices(bool incompressible, bool nemo, IndexType nDim, IndexType nSpecies) {
47 /*--- Instantiate the right type of CIndices, without heap allocations. ---*/
48 if (incompressible) {
49 type_ = 0;
50 Construct<CIncEulerVariable::template CIndices<IndexType>>(nDim, nSpecies);
51 } else if (nemo) {
52 type_ = 1;
53 Construct<CNEMOEulerVariable::template CIndices<IndexType>>(nDim, nSpecies);
54 } else {
55 type_ = 2;
56 Construct<CEulerVariable::template CIndices<IndexType>>(nDim, nSpecies);
57 }
58 }
59
60 /*--- To use the CIndices object we stored on the buffer, we cast to the
61 * concrete type using the same logic used during construction. ---*/
62#define GET_INDEX_IMPL(NAME) \
63 inline IndexType NAME() const { \
64 switch (type_) { \
65 case 0: \
66 return reinterpret_cast<const CIncEulerVariable::template CIndices<IndexType>*>(data_)->NAME(); \
67 case 1: \
68 return reinterpret_cast<const CNEMOEulerVariable::template CIndices<IndexType>*>(data_)->NAME(); \
69 default: \
70 return reinterpret_cast<const CEulerVariable::template CIndices<IndexType>*>(data_)->NAME(); \
71 } \
72 }
87#undef GET_INDEX_IMPL
88
89 private:
90 template <class ConcreteIndices>
91 void Construct(IndexType nDim, IndexType nSpecies) {
92 /*--- Build the indices object in the static buffer owned by this class. ---*/
93 static_assert(sizeof(ConcreteIndices) <= 2 * sizeof(IndexType));
94 new(data_) ConcreteIndices(nDim, nSpecies);
95 }
96
97 alignas(sizeof(IndexType)) char data_[2 * sizeof(IndexType)]{}; /*!< \brief Space for the largest CIndices class. */
98 uint8_t type_;
99};
100
101/*!
102 * \brief Maps primitive variable names to their indices based on a CPrimitiveIndices object.
103 * \note The variables names we try to map here must be kept in sync with CPrimitiveIndices.
104 * All index classes should use numeric_limits::max for variables that are not available.
105 */
106template <class IndexType>
107std::map<std::string, IndexType> PrimitiveNameToIndexMap(const CPrimitiveIndices<IndexType>& idx) {
108 std::map<std::string, IndexType> nameToIndex;
109 const auto notAvailable = std::numeric_limits<IndexType>::max();
110#define INSERT_VAR_INDEX_PAIR(STRING, FUNCTION) \
111 if (idx.FUNCTION() != notAvailable) nameToIndex[STRING] = idx.FUNCTION();
112 INSERT_VAR_INDEX_PAIR("TEMPERATURE", Temperature)
113 INSERT_VAR_INDEX_PAIR("TEMPERATURE_VE", Temperature_ve)
114 INSERT_VAR_INDEX_PAIR("PRESSURE", Pressure)
115 INSERT_VAR_INDEX_PAIR("DENSITY", Density)
116 INSERT_VAR_INDEX_PAIR("ENTHALPY", Enthalpy)
117 INSERT_VAR_INDEX_PAIR("SOUND_SPEED", SoundSpeed)
118 INSERT_VAR_INDEX_PAIR("LAMINAR_VISCOSITY", LaminarViscosity)
119 INSERT_VAR_INDEX_PAIR("EDDY_VISCOSITY", EddyViscosity)
120 INSERT_VAR_INDEX_PAIR("THERMAL_CONDUCTIVITY", ThermalConductivity)
121 INSERT_VAR_INDEX_PAIR("CP_TOTAL", CpTotal)
122#undef INSERT_VAR_INDEX_PAIR
123 nameToIndex["VELOCITY_X"] = idx.Velocity();
124 nameToIndex["VELOCITY_Y"] = idx.Velocity() + 1;
125 if (idx.NDim() == 3) {
126 nameToIndex["VELOCITY_Z"] = idx.Velocity() + 2;
127 }
128 for (IndexType iSpecies = 0; iSpecies < idx.NSpecies(); ++iSpecies) {
129 nameToIndex["DENSITY_" + std::to_string(iSpecies)] = idx.SpeciesDensities() + iSpecies;
130 }
131 return nameToIndex;
132}
Class for defining the variables of the compressible Euler solver.
Class for defining the variables of the incompressible Euler solver.
Class for defining the variables of the compressible NEMO Euler solver.
#define GET_INDEX_IMPL(NAME)
Definition CPrimitiveIndices.hpp:62
#define INSERT_VAR_INDEX_PAIR(STRING, FUNCTION)
std::map< std::string, IndexType > PrimitiveNameToIndexMap(const CPrimitiveIndices< IndexType > &idx)
Maps primitive variable names to their indices based on a CPrimitiveIndices object.
Definition CPrimitiveIndices.hpp:107
Abstract wrapper for the CIndices classes of CEulerVariable, CIncEulerVariable, etc....
Definition CPrimitiveIndices.hpp:45
IndexType Density() const
Definition CPrimitiveIndices.hpp:79
IndexType NSpecies() const
Definition CPrimitiveIndices.hpp:74
IndexType SoundSpeed() const
Definition CPrimitiveIndices.hpp:81
IndexType EddyViscosity() const
Definition CPrimitiveIndices.hpp:83
IndexType NDim() const
Definition CPrimitiveIndices.hpp:73
IndexType Temperature() const
Definition CPrimitiveIndices.hpp:76
IndexType LaminarViscosity() const
Definition CPrimitiveIndices.hpp:82
IndexType ThermalConductivity() const
Definition CPrimitiveIndices.hpp:84
CPrimitiveIndices(bool incompressible, bool nemo, IndexType nDim, IndexType nSpecies)
Definition CPrimitiveIndices.hpp:46
IndexType Pressure() const
Definition CPrimitiveIndices.hpp:78
IndexType CpTotal() const
Definition CPrimitiveIndices.hpp:85
IndexType Velocity() const
Definition CPrimitiveIndices.hpp:77
IndexType SpeciesDensities() const
Definition CPrimitiveIndices.hpp:75
IndexType Temperature_ve() const
Definition CPrimitiveIndices.hpp:86
IndexType Enthalpy() const
Definition CPrimitiveIndices.hpp:80