forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTypes.hh
335 lines (288 loc) · 7.98 KB
/
UnitTypes.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//----------------------------------*-C++-*----------------------------------//
// Copyright 2023-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/UnitTypes.hh
//! \brief Annotated unit struct definitions for use with Quantity
//---------------------------------------------------------------------------//
#pragma once
#include <utility>
#include "celeritas_config.h"
#include "corecel/math/UnitUtils.hh"
#include "Constants.hh"
#include "Types.hh"
#include "Units.hh"
namespace celeritas
{
namespace units
{
//---------------------------------------------------------------------------//
//!@{
//! \name Natural units
struct CLight
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::c_light;
}
static char const* label() { return "c"; }
};
//! "Natural units" for energy
struct Mev
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
#if CELERITAS_UNITS == CELERITAS_UNITS_CLHEP
return units::megaelectronvolt;
#else
return real_type(1e6) * constants::e_electron * units::volt;
#endif
}
static char const* label() { return "MeV"; }
};
//! "Natural units" for mass
struct MevPerCsq : UnitDivide<Mev, UnitProduct<CLight, CLight>>
{
static char const* label() { return "MeV/c^2"; }
};
//! "Natural units" for momentum
struct MevPerC : UnitDivide<Mev, CLight>
{
static char const* label() { return "MeV/c"; }
};
//! Magnitude of electron charge (positive sign)
struct EElectron
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::e_electron;
}
static char const* label() { return "e"; }
};
//! Quantity of substance
struct Mol
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::na_avogadro;
}
static char const* label() { return "mol"; }
};
//!@}
//---------------------------------------------------------------------------//
//!@{
//! \name Atomic units
struct Amu
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return constants::atomic_mass;
}
static char const* label() { return "amu"; }
};
struct Barn
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::barn; }
static char const* label() { return "b"; }
};
struct Millibarn
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return real_type(1e-3) * units::barn;
}
static char const* label() { return "mb"; }
};
//!@}
//---------------------------------------------------------------------------//
//!@{
//! \name Gaussian units
struct Centimeter
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return units::centimeter;
}
static char const* label() { return "cm"; }
};
struct Gram
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::gram; }
static char const* label() { return "g"; }
};
struct Gauss
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::gauss; }
static char const* label() { return "G"; }
};
//! Inverse cubic centimeter for number densities
struct InvCentimeterCubed
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return 1 / (units::centimeter * units::centimeter * units::centimeter);
}
static char const* label() { return "1/cm^3"; }
};
//! Molar density
struct MolPerCentimeterCubed : UnitProduct<Mol, InvCentimeterCubed>
{
static char const* label() { return "mol/cm^3"; }
};
//! Mass density
struct GramPerCentimeterCubed : UnitProduct<Gram, InvCentimeterCubed>
{
static char const* label() { return "g/cm^3"; }
};
//!@}
//---------------------------------------------------------------------------//
//!@{
//! \name SI units
struct Meter
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::meter; }
static char const* label() { return "m"; }
};
struct Kilogram
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return units::kilogram;
}
static char const* label() { return "kg"; }
};
struct Second
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::second; }
static char const* label() { return "s"; }
};
struct Tesla
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return units::tesla; }
static char const* label() { return "T"; }
};
//!@}
//---------------------------------------------------------------------------//
//!@{
//! \name CLHEP units
struct Millimeter
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return units::millimeter;
}
static char const* label() { return "mm"; }
};
struct Nanosecond
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
return units::nanosecond;
}
static char const* label() { return "ns"; }
};
//! Unit mass in CLHEP system
struct ClhepUnitMass
{
static CELER_CONSTEXPR_FUNCTION real_type value()
{
if constexpr (CELERITAS_UNITS == CELERITAS_UNITS_CLHEP)
{
// Floating point errors make the true expression below difficult
// to be exactly unity
return real_type(1);
}
else
{
return constants::e_electron / coulomb * kilogram * real_type(1e-6);
}
}
static char const* label() { return "mass_clhep"; }
};
//! Unit magnetic flux density in CLHEP system
struct ClhepUnitBField
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return 1e3 * tesla; }
static char const* label() { return "field_clhep"; }
};
//!@}
//---------------------------------------------------------------------------//
//!@{
//! \name Annotation-only units
//! Mark as being in the native/builtin unit system
struct Native
{
static CELER_CONSTEXPR_FUNCTION real_type value() { return 1; }
};
//! Annotate a quantity represented the logarithm of (E/MeV)
struct LogMev
{
//! Conversion factor is not multiplicative
static CELER_CONSTEXPR_FUNCTION real_type value() { return 0; }
};
//!@}
//---------------------------------------------------------------------------//
//! Traits class for units
template<UnitSystem>
struct UnitSystemTraits;
//! CGS unit traits
template<>
struct UnitSystemTraits<UnitSystem::cgs>
{
using Length = Centimeter;
using Mass = Gram;
using Time = Second;
using BField = Gauss; //!< Magnetic flux density
static char const* label() { return "cgs"; }
};
//! SI unit traits
template<>
struct UnitSystemTraits<UnitSystem::si>
{
using Length = Meter;
using Mass = Kilogram;
using Time = Second;
using BField = Tesla;
static char const* label() { return "si"; }
};
//! CLHEP unit traits
template<>
struct UnitSystemTraits<UnitSystem::clhep>
{
using Length = Millimeter;
using Mass = ClhepUnitMass;
using Time = Nanosecond;
using BField = ClhepUnitBField;
static char const* label() { return "clhep"; }
};
using CgsTraits = UnitSystemTraits<UnitSystem::cgs>;
using SiTraits = UnitSystemTraits<UnitSystem::si>;
using ClhepTraits = UnitSystemTraits<UnitSystem::clhep>;
using NativeTraits = UnitSystemTraits<UnitSystem::native>;
//---------------------------------------------------------------------------//
/*!
* Expand a macro to a switch statement over all possible unit system types.
*
* This is *not* a \c CELER_FUNCTION because unit conversion should be done
* only during preprocessing on the CPU.
*/
template<class F>
constexpr decltype(auto) visit_unit_system(F&& func, UnitSystem sys)
{
#define CELER_US_VISIT_CASE(TYPE) \
case UnitSystem::TYPE: \
return std::forward<F>(func)(UnitSystemTraits<UnitSystem::TYPE>{});
switch (sys)
{
CELER_US_VISIT_CASE(cgs);
CELER_US_VISIT_CASE(si);
CELER_US_VISIT_CASE(clhep);
default:
CELER_ASSERT_UNREACHABLE();
}
#undef CELER_US_VISIT_CASE
}
//---------------------------------------------------------------------------//
} // namespace units
} // namespace celeritas