CEPS  24.01
Cardiac ElectroPhysiology Simulator
CepsTypes.hpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
34 #pragma once
35 
36 // Standard and library includes
37 #include <array>
38 #include <cassert>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <iostream>
42 #include <iterator>
43 #include <list>
44 #include <map>
45 #include <mpi.h>
46 #include <ostream>
47 #include <set>
48 #include <span>
49 #include <sstream>
50 #include <tuple>
51 #include <typeinfo>
52 #include <unordered_map>
53 #include <vector>
54 #include <concepts>
55 #include <memory>
56 // Bug in order of includes...
57 #include <vtkPointLocator.h>
58 #ifdef CEPS_USE_PETSC
59  #include <petsc.h>
60 #endif // CEPS_USE_PETSC
61 #include <Eigen/Dense>
62 
63 // =========================================================================
64 // Select int and floating point types
65 
66 // If Ceps is using Petsc, types are deduced from Petsc configuration
67 #ifdef CEPS_USE_PETSC
68 
69  #if not (PETSC_VERSION_MAJOR >= 3 && PETSC_VERSION_MINOR >= 2)
70  #error "Ceps need a Petsc version higher or equal than 3.2."
71  #endif
72 
73 // Only check if petsc use real as scalar, we don't want extra memory allocation !
74 static_assert (
75  sizeof (PetscReal) == sizeof (PetscScalar),
76  "You need to compile Petsc with option '--with-scalar-type=real'"
77 );
78 
79 using CepsReal = PetscReal;
80 using CepsInt = PetscInt;
81 using CepsUInt = std::make_unsigned_t<CepsInt>;
82 using CepsShort = PetscShort;
83 using CepsIndex = PetscInt;
84 
85  // If Petsc is using double precision and Ceps is not using double precision, make the change
86  #if defined(PETSC_USE_REAL_DOUBLE) && not defined(CEPS_WITH_DOUBLE_PRECISION)
87  #define CEPS_WITH_DOUBLE_PRECISION
88  #elif defined(PETSC_USE_REAL_SINGLE) && defined(CEPS_WITH_DOUBLE_PRECISION)
89  #undef CEPS_WITH_DOUBLE_PRECISION
90  #endif
91 
92 // If Ceps is not using Petsc, types are deduced from Ceps configuration
93 #else
94  #include <inttypes.h>
95  #include <type_traits>
96 
97  #ifdef CEPS_WITH_DOUBLE_PRECISION
98 using CepsReal = double;
99  #else
100 using CepsReal = float;
101  #endif
102 
103  #ifdef CEPS_WITH_64_BIT_INTEGERS
104 using CepsInt = int64_t;
105  #else
106 using CepsInt = int32_t;
107  #endif
108 
109 using CepsUInt = std::make_unsigned_t<CepsInt>;
110 using CepsShort = short;
112 #endif
113 
114 #ifdef CEPS_WITH_DOUBLE_PRECISION
115  #define FLOATING_POINT_EPSILON 1E-15
116 #else
117  #define FLOATING_POINT_EPSILON 1E-6
118 #endif
119 
120 // =========================================================================
121 // Define types with same prefix
122 
124 using CepsBool = bool;
125 using CepsChar = char;
126 using CepsSize = size_t;
127 using CepsCString = const CepsChar *;
128 using CepsString = std::basic_string<CepsChar>;
129 
130 // --------------------------------------------------------------
131 // Definition of math types
134 using CepsMathScalars = Eigen::Matrix<CepsScalar, 1, Eigen::Dynamic>;
135 using CepsMathVertex = Eigen::Matrix<CepsScalar, 3, 1>;
136 using CepsMathVertices = Eigen::Matrix<CepsScalar, 3, Eigen::Dynamic>;
137 using CepsMathTensor = Eigen::Matrix<CepsScalar, 3, 3>;
138 using CepsMathTensors = Eigen::Matrix<CepsScalar, 3, Eigen::Dynamic>;
139 using CepsMathDynamic1D = Eigen::Matrix<CepsScalar, Eigen::Dynamic, 1>;
140 using CepsMathDynamic2D = Eigen::Matrix<CepsScalar, Eigen::Dynamic, Eigen::Dynamic>;
141 
142 namespace ceps {
144  template<typename _Result>
145  _Result
147 }
148 
149 
150 // --------------------------------------------------------------
151 // Definition of containers,
152 // check the corresponding header files in common/containers
154 template <class _Type, class _Alloc = std::allocator<_Type>>
155 using CepsVector = std::vector<_Type, _Alloc>;
156 
158 template <class _Type, CepsSize _N>
159 using CepsArray = std::array<_Type, _N>;
161 template <class _Type>
164 template <class _Type>
167 template <class _Type>
170 template <class _Type>
172 
180 constexpr CepsReal1D CepsZero1D = {0.0};
182 constexpr CepsReal2D CepsZero2D = {0.0, 0.0};
184 constexpr CepsReal3D CepsZero3D = {0.0, 0.0, 0.0};
185 
187 template <class... _Args>
188 using CepsTuple = std::tuple<_Args...>;
189 
191 template <
192  typename _Key,
193  typename _Tp,
194  typename _Compare = std::less<_Key>,
195  typename _Alloc = std::allocator<std::pair<const _Key, _Tp>>>
196 using CepsMap = std::map<_Key, _Tp, _Compare, _Alloc>;
197 
199 template <
200  typename _Key,
201  typename _Tp,
202  typename _Hash = std::hash<_Key>,
203  typename _KeyEqual = std::equal_to<_Key>,
204  typename _Alloc = std::allocator<std::pair<const _Key, _Tp>>>
205 using CepsMultiMap = std::unordered_multimap<_Key, _Tp, _Hash, _KeyEqual, _Alloc>;
206 
208 template <class _Type, class _Compare = std::less<_Type>, class _Alloc = std::allocator<_Type>>
209 using CepsSet = std::set<_Type, _Compare, _Alloc>;
210 
211 // -----------------
212 // Types for indices
213 // These typedefs are mostly to discriminate what is indexed when writing code
214 
216 using CepsEnum = int;
221 using CepsSHash = int64_t;
228 
229 constexpr const CepsChar CepsNoName[] = "no-name";
230 constexpr const CepsChar CepsNoUnit[] = "no-unit";
231 
232 constexpr CepsAttribute CepsUniversal = -1;
233 
239 {
240  CepsReal t = 0.0;
242  CepsReal u = 0.0;
248 };
249 
250 // =========================================================================
251 // Utilities to retrieve the name of the MPI type to be used as argument
252 // in communications methods
253 
261 template <typename _Type>
262 struct MpiType;
263 
265 #define CEPS_DECL_PARALLEL_TYPE(_type, _mpi_type) \
266  template <> \
267  struct MpiType<_type> \
268  { \
269  \
270  static MPI_Datatype \
271  getType() \
272  { \
273  return static_cast<MPI_Datatype>(static_cast<void *>(&_mpi_type)); \
274  } \
275  }
276 
278 CEPS_DECL_PARALLEL_TYPE(void, ompi_mpi_datatype_null);
279 
281 CEPS_DECL_PARALLEL_TYPE(float, ompi_mpi_float);
282 
284 CEPS_DECL_PARALLEL_TYPE(double, ompi_mpi_double);
285 
287 CEPS_DECL_PARALLEL_TYPE(int8_t, ompi_mpi_int8_t);
288 
290 CEPS_DECL_PARALLEL_TYPE(uint8_t, ompi_mpi_uint8_t);
291 
293 CEPS_DECL_PARALLEL_TYPE(int16_t, ompi_mpi_int16_t);
294 
296 CEPS_DECL_PARALLEL_TYPE(uint16_t, ompi_mpi_uint16_t);
297 
299 CEPS_DECL_PARALLEL_TYPE(int32_t, ompi_mpi_int32_t);
300 
302 CEPS_DECL_PARALLEL_TYPE(uint32_t, ompi_mpi_uint32_t);
303 
305 CEPS_DECL_PARALLEL_TYPE(int64_t, ompi_mpi_int64_t);
306 
308 CEPS_DECL_PARALLEL_TYPE (uint64_t, ompi_mpi_uint64_t);
309 
310 // Define MPI_TYPE used in Ceps
311 
315 #define CEPS_MPI_REAL MpiType<CepsReal>::getType()
316 
320 #define CEPS_MPI_INT MpiType<CepsInt>::getType ()
321 
325 #define CEPS_MPI_UINT MpiType<CepsUInt>::getType ()
326 
330 #define CEPS_MPI_SHORT MpiType<CepsShort>::getType ()
331 
335 #define CEPS_MPI_INDEX MpiType<CepsIndex>::getType ()
336 
340 #define CEPS_MPI_GEOM_INDEX MpiType<CepsIndex>::getType ()
341 
345 #define CEPS_MPI_GLOBAL_INDEX MpiType<CepsGlobalIndex>::getType ()
346 
350 #define CEPS_MPI_LOCAL_INDEX MpiType<CepsLocalIndex>::getType ()
351 
355 #define CEPS_MPI_ELEMENT_INDEX MpiType<CepsIndex>::getType ()
356 
360 #define CEPS_MPI_HASH MpiType<CepsHash>::getType ()
361 
365 #define CEPS_MPI_SHASH MpiType<CepsSHash>::getType()
366 
367 // undefine CEPS_DECL_PARALLEL_TYPE
368 #undef CEPS_DECL_PARALLEL_TYPE
369 
374 #define CEPS_MAKE_ALIGNED_OP_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
Eigen::Matrix< CepsScalar, Eigen::Dynamic, 1 > CepsMathDynamic1D
Dynamic 1D array, eigen format.
Definition: CepsTypes.hpp:139
CepsIndex CepsUnknownIndex
For unknowns.
Definition: CepsTypes.hpp:217
std::array< _Type, _N > CepsArray
C++ arrays.
Definition: CepsTypes.hpp:159
constexpr const CepsChar CepsNoName[]
Default string for things without defined name.
Definition: CepsTypes.hpp:229
CepsArray2< CepsReal > CepsReal2D
Two real scalars, used like this for compatibility in polynomials.
Definition: CepsTypes.hpp:176
CepsScalar CepsMathScalar
Real numbers.
Definition: CepsTypes.hpp:133
CepsGlobalIndex CepsCellGlobalIndex
Indices of cells.
Definition: CepsTypes.hpp:222
std::map< _Key, _Tp, _Compare, _Alloc > CepsMap
C++ map.
Definition: CepsTypes.hpp:196
constexpr CepsReal2D CepsZero2D
Zero of CepsReal2D.
Definition: CepsTypes.hpp:182
int CepsEnum
Enum type.
Definition: CepsTypes.hpp:216
CepsSize CepsHash
Hashes for distributed data.
Definition: CepsTypes.hpp:220
char CepsChar
Char.
Definition: CepsTypes.hpp:125
std::set< _Type, _Compare, _Alloc > CepsSet
C++ set.
Definition: CepsTypes.hpp:209
std::tuple< _Args... > CepsTuple
C++ tuple.
Definition: CepsTypes.hpp:188
CepsLocalIndex CepsDofLocalIndex
Indices of degrees of freedom.
Definition: CepsTypes.hpp:227
CepsArray< CepsReal, 1u > CepsReal1D
A real scalar, used like this for compatibility in polynomials.
Definition: CepsTypes.hpp:174
CepsReal CepsScalar
Real numbers.
Definition: CepsTypes.hpp:132
Eigen::Matrix< CepsScalar, 3, 3 > CepsMathTensor
Tensor, eigen format.
Definition: CepsTypes.hpp:137
CepsArray< _Type, 2U > CepsArray2
C++ array, 2 elements.
Definition: CepsTypes.hpp:162
short CepsShort
Short type for custom purposes.
Definition: CepsTypes.hpp:110
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
const CepsChar * CepsCString
C format string.
Definition: CepsTypes.hpp:127
Eigen::Matrix< CepsScalar, 3, Eigen::Dynamic > CepsMathTensors
Array of tensors, eigen format.
Definition: CepsTypes.hpp:138
#define CEPS_DECL_PARALLEL_TYPE(_type, _mpi_type)
Definition: CepsTypes.hpp:265
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
size_t CepsSize
Size unsigned.
Definition: CepsTypes.hpp:126
CepsGlobalIndex CepsDofGlobalIndex
Indices of degrees of freedom.
Definition: CepsTypes.hpp:226
CepsInt CepsAttribute
Used to define regions.
Definition: CepsTypes.hpp:215
CepsIndex CepsGlobalIndex
Many uses. Has to be signed for PETSc.
Definition: CepsTypes.hpp:218
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
Eigen::Matrix< CepsScalar, 3, 1 > CepsMathVertex
Vertex, eigen format.
Definition: CepsTypes.hpp:135
CepsGlobalIndex CepsNodeGlobalIndex
Indices of nodes.
Definition: CepsTypes.hpp:224
int64_t CepsSHash
Hashes for coordinates.
Definition: CepsTypes.hpp:221
CepsIndex CepsLocalIndex
To use when the indexing is only on one process.
Definition: CepsTypes.hpp:219
constexpr CepsReal1D CepsZero1D
Zero of CepsReal1D.
Definition: CepsTypes.hpp:180
CepsArray< _Type, 3U > CepsArray3
C++ array, 3 elements.
Definition: CepsTypes.hpp:165
CepsUInt CepsProcId
For CPU indices.
Definition: CepsTypes.hpp:123
CepsArray< _Type, 9U > CepsArray9
C++ array, 9 elements.
Definition: CepsTypes.hpp:171
constexpr const CepsChar CepsNoUnit[]
Default string for things without defined unit.
Definition: CepsTypes.hpp:230
Eigen::Matrix< CepsScalar, 3, Eigen::Dynamic > CepsMathVertices
Array of vertices, eigen format.
Definition: CepsTypes.hpp:136
Eigen::Matrix< CepsScalar, 1, Eigen::Dynamic > CepsMathScalars
Array of reals, eigen format.
Definition: CepsTypes.hpp:134
CepsLocalIndex CepsCellLocalIndex
Indices of cells.
Definition: CepsTypes.hpp:223
CepsArray3< CepsReal > CepsReal3D
Three real scalars, used like this for compatibility in polynomials.
Definition: CepsTypes.hpp:178
constexpr CepsReal3D CepsZero3D
Zero of CepsReal3D.
Definition: CepsTypes.hpp:184
int32_t CepsInt
Need 32 bit integer.
Definition: CepsTypes.hpp:106
CepsArray< _Type, 4U > CepsArray4
C++ array, 4 elements.
Definition: CepsTypes.hpp:168
constexpr CepsAttribute CepsUniversal
This attribute means "everywhere".
Definition: CepsTypes.hpp:232
Eigen::Matrix< CepsScalar, Eigen::Dynamic, Eigen::Dynamic > CepsMathDynamic2D
Dynamic 2D array, eigen format.
Definition: CepsTypes.hpp:140
CepsInt CepsIndex
Index rowid etc.
Definition: CepsTypes.hpp:111
std::unordered_multimap< _Key, _Tp, _Hash, _KeyEqual, _Alloc > CepsMultiMap
C++ multimap.
Definition: CepsTypes.hpp:205
CepsLocalIndex CepsNodeLocalIndex
Indices of nodes.
Definition: CepsTypes.hpp:225
A namespace for all utility methods.
_Result convertReal(CepsReal *data)
A small utility to convert a vector of reals into the appropriate (scalar,vector,tensor)
Structure used to pass arguments to SAFunc (see pde directory) The flags of the SAFunc allows extract...
Definition: CepsTypes.hpp:239
CepsSet< CepsAttribute > attr
attributes
Definition: CepsTypes.hpp:243
CepsNodeGlobalIndex nodeId
node index
Definition: CepsTypes.hpp:246
CepsReal u
solution u
Definition: CepsTypes.hpp:242
CepsCellGlobalIndex cellId
cell index
Definition: CepsTypes.hpp:245
CepsReal3D x
space
Definition: CepsTypes.hpp:241
CepsReal t
time
Definition: CepsTypes.hpp:240
CepsDofGlobalIndex dofId
dof index
Definition: CepsTypes.hpp:244
CepsUnknownIndex unknownId
unknown index
Definition: CepsTypes.hpp:247
Used to retrieve the MPI "type" in function of the true type.
Definition: CepsTypes.hpp:262