Athena++/Atmosphere
Planetary Atmosphere Simulator
athena.hpp
Go to the documentation of this file.
1 #ifndef ATHENA_HPP_
2 #define ATHENA_HPP_
3 //========================================================================================
4 // Athena++ astrophysical MHD code
5 // Copyright(C) 2014 James M. Stone <jmstone@princeton.edu> and other code contributors
6 // Licensed under the 3-clause BSD License, see LICENSE file for details
7 //========================================================================================
9 // \brief contains Athena++ general purpose types, structures, enums, etc.
10 
11 // C headers
12 
13 // C++ headers
14 #include <cmath>
15 #include <cstdint> // std::int64_t
16 
17 // Athena++ headers
18 #include "athena_arrays.hpp"
19 #include "defs.hpp"
20 
21 // primitive type alias that allows code to run with either floats or doubles
22 #if SINGLE_PRECISION_ENABLED
23 using Real = float;
24 #ifdef MPI_PARALLEL
25 #define MPI_ATHENA_REAL MPI_FLOAT
26 #define MPI_ATHENA_REAL_INT MPI_FLOAT_INT
27 #endif
28 #else
29 using Real = double;
30 #ifdef MPI_PARALLEL
31 #define MPI_ATHENA_REAL MPI_DOUBLE
32 #define MPI_ATHENA_REAL_INT MPI_DOUBLE_INT
33 #endif
34 #endif
35 
36 // for OpenMP 4.0 SIMD vectorization, control width of SIMD lanes
37 #if defined(__AVX512F__)
38 #define SIMD_WIDTH 8
39 #elif defined(__AVX__)
40 #define SIMD_WIDTH 4
41 #elif defined(__SSE2__)
42 #define SIMD_WIDTH 2
43 #else
44 #define SIMD_WIDTH 4
45 #endif
46 
47 #define CACHELINE_BYTES 64
48 
49 // forward declarations needed for function pointer type aliases
50 class MeshBlock;
51 class Coordinates;
52 class ParameterInput;
53 class HydroDiffusion;
54 class FieldDiffusion;
55 
56 //--------------------------------------------------------------------------------------
58 // \brief stores logical location and level of MeshBlock
59 
60 struct LogicalLocation { // aggregate and POD type
61  // These values can exceed the range of std::int32_t even if the root grid has only a
62  // single MeshBlock if >30 levels of AMR are used, since the corresponding max index =
63  // 1*2^31 > INT_MAX = 2^31 -1 for most 32-bit signed integer type impelementations
64  std::int64_t lx1, lx2, lx3;
65  int level;
66 
67  // operators useful for sorting
69  return ((ll.level == level) && (ll.lx1 == lx1) && (ll.lx2 == lx2) && (ll.lx3 == lx3));
70  }
71  static bool Lesser(const LogicalLocation &left, const LogicalLocation &right) {
72  return left.level < right.level;
73  }
74  static bool Greater(const LogicalLocation & left, const LogicalLocation &right) {
75  return left.level > right.level;
76  }
77 };
78 
79 //----------------------------------------------------------------------------------------
81 // \brief physical size and number of cells in a Mesh or a MeshBlock
82 
83 struct RegionSize { // aggregate and POD type; do NOT reorder member declarations:
86  Real x1rat, x2rat, x3rat; // ratio of dxf(i)/dxf(i-1)
87  // the size of the root grid or a MeshBlock should not exceed std::int32_t limits
88  int nx1, nx2, nx3; // number of active cells (not including ghost zones)
89 };
90 
91 //---------------------------------------------------------------------------------------
93 // \brief container for face-centered fields
94 
95 struct FaceField {
97  FaceField() = default;
98  FaceField(int ncells3, int ncells2, int ncells1,
100  x1f(ncells3, ncells2, ncells1+1, init), x2f(ncells3, ncells2+1, ncells1, init),
101  x3f(ncells3+1, ncells2, ncells1, init) {}
102 };
103 
104 //----------------------------------------------------------------------------------------
106 // \brief container for edge-centered fields
107 
108 struct EdgeField {
110  EdgeField() = default;
111  EdgeField(int ncells3, int ncells2, int ncells1,
113  x1e(ncells3+1, ncells2+1, ncells1, init), x2e(ncells3+1, ncells2, ncells1+1, init),
114  x3e(ncells3, ncells2+1, ncells1+1, init) {}
115 };
116 
117 //----------------------------------------------------------------------------------------
118 // enums used everywhere
119 // (not specifying underlying integral type (C++11) for portability & performance)
120 
121 // TODO(felker): C++ Core Guidelines Enum.5: Don’t use ALL_CAPS for enumerators
122 // (avoid clashes with preprocessor macros). Enumerated type definitions in this file and:
123 // athena_fft.hpp, io_wrapper.hpp, bvals.hpp, hydro_diffusion.hpp, field_diffusion.hpp,
124 // task_list.hpp, ???
125 
126 //------------------
127 // named, weakly typed / unscoped enums:
128 //------------------
129 
130 // enumerators only used for indexing AthenaArray and regular arrays; typename and
131 // explicitly specified enumerator values aare unnecessary, but provided for clarity:
132 
133 // array indices for conserved: density, momemtum, total energy, face-centered field
134 //enum {NMASS = 1+NPHASE*NVAPOR};
135 enum ConsIndex {IDN=0, IM1=1+NVAPOR, IM2=2+NVAPOR, IM3=3+NVAPOR, IEN=4+NVAPOR};
136 enum MagneticIndex {IB1=0, IB2=1, IB3=2};
137 
138 // array indices for 1D primitives: velocity, transverse components of field
139 enum PrimIndex {IVX=1+NVAPOR, IVY=2+NVAPOR, IVZ=3+NVAPOR, IPR=4+NVAPOR,
140  IBY=(NHYDRO), IBZ=((NHYDRO)+1), IV1=IVX, IV2=IVY, IV3=IVZ};
141 
142 // array indices for face-centered electric fields returned by Riemann solver
143 enum ElectricIndex {X1E2=0, X1E3=1, X2E3=0, X2E1=1, X3E1=0, X3E2=1};
144 
145 // array indices for metric and triangular matrices in GR
146 enum MetricIndex {I00=0, I01=1, I02=2, I03=3, I11=4, I12=5, I13=6, I22=7, I23=8, I33=9,
147  NMETRIC=10};
148 enum TriangleIndex {T00=0, T10=1, T11=2, T20=3, T21=4, T22=5, T30=6, T31=7, T32=8, T33=9,
150 
151 // enumerator types that are used for variables and function parameters:
152 
153 // needed for arrays dimensioned over grid directions
154 // enumerator type only used in Mesh::EnrollUserMeshGenerator()
156 
157 //------------------
158 // strongly typed / scoped enums (C++11):
159 //------------------
160 // KGF: Except for the 2x MG* enums, these may be unnessary w/ the new class inheritance
161 // Now, only passed to BoundaryVariable::InitBoundaryData(); could replace w/ bool switch
165 // TODO(felker): consider generalizing/renaming to QuantityFormulation
166 enum class FluidFormulation {evolve, background, disabled}; // rename background -> fixed?
168 enum class VariableType {prim, cons, chem};
169 
170 //----------------------------------------------------------------------------------------
171 // function pointer prototypes for user-defined modules set at runtime
172 
173 using BValFunc = void (*)(
175  Real time, Real dt,
176  int is, int ie, int js, int je, int ks, int ke, int ngh);
177 using AMRFlagFunc = int (*)(MeshBlock *pmb);
178 using MeshGenFunc = Real (*)(Real x, RegionSize rs);
179 using SrcTermFunc = void (*)(
180  MeshBlock *pmb, const Real time, const Real dt,
182 using TimeStepFunc = Real (*)(MeshBlock *pmb);
183 using HistoryOutputFunc = Real (*)(MeshBlock *pmb, int iout);
184 using MetricFunc = void (*)(
185  Real x1, Real x2, Real x3, ParameterInput *pin,
187  AthenaArray<Real> &dg_dx1, AthenaArray<Real> &dg_dx2, AthenaArray<Real> &dg_dx3);
188 using MGBoundaryFunc = void (*)(
189  AthenaArray<Real> &dst,Real time, int nvar,
190  int is, int ie, int js, int je, int ks, int ke, int ngh,
191  Real x0, Real y0, Real z0, Real dx, Real dy, Real dz);
192 using ViscosityCoeffFunc = void (*)(
193  HydroDiffusion *phdif, MeshBlock *pmb,
194  const AthenaArray<Real> &w, const AthenaArray<Real> &bc,
195  int is, int ie, int js, int je, int ks, int ke);
196 using ConductionCoeffFunc = void (*)(
197  HydroDiffusion *phdif, MeshBlock *pmb,
198  const AthenaArray<Real> &w, const AthenaArray<Real> &bc,
199  int is, int ie, int js, int je, int ks, int ke);
200 using FieldDiffusionCoeffFunc = void (*)(
201  FieldDiffusion *pfdif, MeshBlock *pmb,
202  const AthenaArray<Real> &w,
203  const AthenaArray<Real> &bmag,
204  int is, int ie, int js, int je, int ks, int ke);
205 
206 #endif // ATHENA_HPP_
void(*)(FieldDiffusion *pfdif, MeshBlock *pmb, const AthenaArray< Real > &w, const AthenaArray< Real > &bmag, int is, int ie, int js, int je, int ks, int ke) FieldDiffusionCoeffFunc
Definition: athena.hpp:204
TriangleIndex
Definition: athena.hpp:148
@ T32
Definition: athena.hpp:148
@ T11
Definition: athena.hpp:148
@ T10
Definition: athena.hpp:148
@ T00
Definition: athena.hpp:148
@ T21
Definition: athena.hpp:148
@ T22
Definition: athena.hpp:148
@ T33
Definition: athena.hpp:148
@ T20
Definition: athena.hpp:148
@ NTRIANGULAR
Definition: athena.hpp:149
@ T31
Definition: athena.hpp:148
@ T30
Definition: athena.hpp:148
PrimIndex
Definition: athena.hpp:139
@ IV2
Definition: athena.hpp:140
@ IPR
Definition: athena.hpp:139
@ IVZ
Definition: athena.hpp:139
@ IV3
Definition: athena.hpp:140
@ IV1
Definition: athena.hpp:140
@ IBZ
Definition: athena.hpp:140
@ IVY
Definition: athena.hpp:139
@ IVX
Definition: athena.hpp:139
@ IBY
Definition: athena.hpp:140
UserHistoryOperation
Definition: athena.hpp:167
void(*)(AthenaArray< Real > &dst, Real time, int nvar, int is, int ie, int js, int je, int ks, int ke, int ngh, Real x0, Real y0, Real z0, Real dx, Real dy, Real dz) MGBoundaryFunc
Definition: athena.hpp:191
double Real
Definition: athena.hpp:29
Real(*)(Real x, RegionSize rs) MeshGenFunc
Definition: athena.hpp:178
CoordinateDirection
Definition: athena.hpp:155
@ X2DIR
Definition: athena.hpp:155
@ X3DIR
Definition: athena.hpp:155
@ X1DIR
Definition: athena.hpp:155
void(*)(Real x1, Real x2, Real x3, ParameterInput *pin, AthenaArray< Real > &g, AthenaArray< Real > &g_inv, AthenaArray< Real > &dg_dx1, AthenaArray< Real > &dg_dx2, AthenaArray< Real > &dg_dx3) MetricFunc
Definition: athena.hpp:187
HydroBoundaryQuantity
Definition: athena.hpp:163
void(*)(HydroDiffusion *phdif, MeshBlock *pmb, const AthenaArray< Real > &w, const AthenaArray< Real > &bc, int is, int ie, int js, int je, int ks, int ke) ViscosityCoeffFunc
Definition: athena.hpp:195
FluidFormulation
Definition: athena.hpp:166
ConsIndex
Definition: athena.hpp:135
@ IM3
Definition: athena.hpp:135
@ IEN
Definition: athena.hpp:135
@ IM1
Definition: athena.hpp:135
@ IDN
Definition: athena.hpp:135
@ IM2
Definition: athena.hpp:135
BoundaryCommSubset
Definition: athena.hpp:164
int(*)(MeshBlock *pmb) AMRFlagFunc
Definition: athena.hpp:177
Real(*)(MeshBlock *pmb) TimeStepFunc
Definition: athena.hpp:182
BoundaryQuantity
Definition: athena.hpp:162
ElectricIndex
Definition: athena.hpp:143
@ X3E2
Definition: athena.hpp:143
@ X2E1
Definition: athena.hpp:143
@ X3E1
Definition: athena.hpp:143
@ X1E2
Definition: athena.hpp:143
@ X2E3
Definition: athena.hpp:143
@ X1E3
Definition: athena.hpp:143
MetricIndex
Definition: athena.hpp:146
@ I12
Definition: athena.hpp:146
@ I02
Definition: athena.hpp:146
@ I11
Definition: athena.hpp:146
@ I13
Definition: athena.hpp:146
@ I00
Definition: athena.hpp:146
@ I23
Definition: athena.hpp:146
@ I03
Definition: athena.hpp:146
@ NMETRIC
Definition: athena.hpp:147
@ I22
Definition: athena.hpp:146
@ I01
Definition: athena.hpp:146
@ I33
Definition: athena.hpp:146
void(*)(MeshBlock *pmb, Coordinates *pco, AthenaArray< Real > &prim, FaceField &b, Real time, Real dt, int is, int ie, int js, int je, int ks, int ke, int ngh) BValFunc
Definition: athena.hpp:176
VariableType
Definition: athena.hpp:168
void(*)(MeshBlock *pmb, const Real time, const Real dt, const AthenaArray< Real > &prim, const AthenaArray< Real > &bcc, AthenaArray< Real > &cons) SrcTermFunc
Definition: athena.hpp:181
MagneticIndex
Definition: athena.hpp:136
@ IB1
Definition: athena.hpp:136
@ IB3
Definition: athena.hpp:136
@ IB2
Definition: athena.hpp:136
Real(*)(MeshBlock *pmb, int iout) HistoryOutputFunc
Definition: athena.hpp:183
void(*)(HydroDiffusion *phdif, MeshBlock *pmb, const AthenaArray< Real > &w, const AthenaArray< Real > &bc, int is, int ie, int js, int je, int ks, int ke) ConductionCoeffFunc
Definition: athena.hpp:199
AthenaArray< Real > x1e
Definition: athena.hpp:109
EdgeField()=default
AthenaArray< Real > x2e
Definition: athena.hpp:109
AthenaArray< Real > x3e
Definition: athena.hpp:109
EdgeField(int ncells3, int ncells2, int ncells1, AthenaArray< Real >::DataStatus init=AthenaArray< Real >::DataStatus::allocated)
Definition: athena.hpp:111
AthenaArray< Real > x3f
Definition: athena.hpp:96
AthenaArray< Real > x1f
Definition: athena.hpp:96
FaceField(int ncells3, int ncells2, int ncells1, AthenaArray< Real >::DataStatus init=AthenaArray< Real >::DataStatus::allocated)
Definition: athena.hpp:98
AthenaArray< Real > x2f
Definition: athena.hpp:96
FaceField()=default
std::int64_t lx3
Definition: athena.hpp:64
bool operator==(LogicalLocation &ll)
Definition: athena.hpp:68
static bool Greater(const LogicalLocation &left, const LogicalLocation &right)
Definition: athena.hpp:74
std::int64_t lx2
Definition: athena.hpp:64
std::int64_t lx1
Definition: athena.hpp:64
static bool Lesser(const LogicalLocation &left, const LogicalLocation &right)
Definition: athena.hpp:71
Real x3rat
Definition: athena.hpp:86
int nx3
Definition: athena.hpp:88
Real x1max
Definition: athena.hpp:85
Real x1min
Definition: athena.hpp:84
Real x2rat
Definition: athena.hpp:86
Real x3min
Definition: athena.hpp:84
Real x1rat
Definition: athena.hpp:86
int nx2
Definition: athena.hpp:88
Real x3max
Definition: athena.hpp:85
Real x2min
Definition: athena.hpp:84
int nx1
Definition: athena.hpp:88
Real x2max
Definition: athena.hpp:85