GGEMS  1.1
GPU GEant4-based Monte Carlo Simulations
GGEMSSystemOfUnits.hh
Go to the documentation of this file.
1 #ifndef GUARD_GGEMS_TOOLS_GGEMSSYSTEMOFUNITS_HH
2 #define GUARD_GGEMS_TOOLS_GGEMSSYSTEMOFUNITS_HH
3 
4 // ************************************************************************
5 // * This file is part of GGEMS. *
6 // * *
7 // * GGEMS is free software: you can redistribute it and/or modify *
8 // * it under the terms of the GNU General Public License as published by *
9 // * the Free Software Foundation, either version 3 of the License, or *
10 // * (at your option) any later version. *
11 // * *
12 // * GGEMS is distributed in the hope that it will be useful, *
13 // * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 // * GNU General Public License for more details. *
16 // * *
17 // * You should have received a copy of the GNU General Public License *
18 // * along with GGEMS. If not, see <https://www.gnu.org/licenses/>. *
19 // * *
20 // ************************************************************************
21 
34 #ifdef PASCAL
35 #undef PASCAL
36 #endif
37 
38 #ifndef __OPENCL_C_VERSION__
39 
43 #define __constant inline static constexpr
44 #include <algorithm>
45 #include <sstream>
46 #include <cfloat>
47 #include <iostream>
49 #endif
50 
51 // Lengths [L] (mm)
52 __constant GGfloat nm = 1.e-6f;
53 __constant GGfloat um = 1.e-3f;
60 __constant GGfloat m = 1.e3f;
66 __constant GGfloat pc = 3.0856775807e+19f;
68 // Cross section unit (mm2)
69 __constant GGfloat b = 1.e-22f;
70 __constant GGfloat mb = 1.e-25f;
71 __constant GGfloat ub = 1.e-28f;
72 __constant GGfloat nb = 1.e-31f;
73 __constant GGfloat pb = 1.e-34f;
75 // Angles
78 __constant GGfloat deg = 3.141592653589793238463f/180.0f;
80 // Solid angle
83 // Time [T] (ns)
85 __constant GGfloat s = 1.e+9f;
86 __constant GGfloat ms = 1.e+6f;
87 __constant GGfloat us = 1.e+3f;
88 __constant GGfloat ps = 1.e-3f;
90 // Frequency [T^-1] (ns-1)
91 __constant GGfloat Hz = 1.f/(1.e+9f);
95 // Electric charge [Q] (eplus)
97 __constant GGfloat qe = 1.602176487e-19f;
98 __constant GGfloat C = 1.f/1.602176487e-19f;
100 // Energy [E] (MeV)
107 __constant GGfloat J = 1.e-6f/1.602176487e-19f;
109 // Mass [E][T^2][L^-2] (MeV.ns2.mm-2)
110 __constant GGfloat kg = 6.241509704e+24f;
111 __constant GGfloat g = 6.241509704e+21f;
112 __constant GGfloat mg = 6.241509704e+18f;
114 // Power [E][T^-1] (MeV.ns-1)
115 __constant GGfloat W = 6.241509766e+3f;
117 // Force [E][L^-1] (MeV.mm-1)
118 __constant GGfloat N = 6.241509766e+9f;
120 // Pressure [E][L^-3] (MeV.mm-3)
121 __constant GGfloat Pa = 6.241509766e+3f;
122 __constant GGfloat bar = 100000.0f*6.241509766e+3f;
123 __constant GGfloat atm = 101325.0f*6.241509766e+3f;
125 // Electric current [Q][T^-1] (C.ns-1)
126 __constant GGfloat A = 6.241509696e+9f;
127 __constant GGfloat mA = 6.241509696e+6f;
128 __constant GGfloat uA = 6.241509696e+3f;
129 __constant GGfloat nA = 6.241509696f;
131 // Electric potential [E][Q^-1]
134 __constant GGfloat V = 1.e-6f;
136 // Electric resistance [E][T][Q^-2] (MeV.ns.C-2)
137 __constant GGfloat OHM = 1.602176452e-16f;
139 // Electric capacitance [Q^2][E^-1] (C.MV-1)
140 __constant GGfloat F = 6.241509468e+24f;
141 __constant GGfloat mF = 6.241509468e+21f;
142 __constant GGfloat uF = 6.241509468e+18f;
143 __constant GGfloat nF = 6.241509468e+15f;
144 __constant GGfloat pF = 6.241509468e+12f;
146 // Magnetic Flux [T][E][Q^-1] (ns.MV)
147 __constant GGfloat Wb = 1000.0f;
149 // Magnetic Field [T][E][Q^-1][L^-2] (MV.ns.mm2)
150 __constant GGfloat T = 0.001f;
151 __constant GGfloat G = 1.e-7f;
154 // Inductance [T^2][E][Q^-2] (MeV.ns2.C-2)
155 __constant GGfloat H = 1.602176383e-07f;
157 // Temperature (K)
160 // Amount of substance (mol)
163 // Activity [T^-1] (ns-1)
168 __constant GGfloat Ci = 3.7e+10f/1.e+9f;
169 __constant GGfloat mCi = 3.7e-2f;
170 __constant GGfloat uCi = 3.7e-5f;
172 // Absorbed dose [L^2][T^-2] (mm2.ns-2)
173 __constant GGfloat Gy = 1.0e-12f;
174 __constant GGfloat kGy = 1.0e-9f;
175 __constant GGfloat mGy = 1.0e-15f;
176 __constant GGfloat uGy = 1.0e-18f;
178 // Luminous intensity [I] (cd)
181 // Luminous flux [I] (cd.sr)
184 // Illuminance [I][L^-2] (cd.sr.mm-2)
187 // Miscellaneous
192 #ifndef __OPENCL_C_VERSION__
193 
197 namespace
198 {
199  std::string prefix_unit[17] = {
200  // yocto, zepto, atto, femto, pico, nano, micro, milli,
201  "y", "z", "a", "f", "p", "n", "u", "m",
202  // N/A,
203  "",
204  // kilo, mega, giga, tera, peta, exa, zetta, yotta
205  "k", "M", "G", "T", "P", "E", "Z", "Y"
206  };
207 }
208 
216 template <typename T>
217 inline std::string BestDistanceUnit(T const& value)
218 {
219  std::string base_unit = "m"; // Base unit is meter (m)
220 
221  // Find index of prefix, milli is the reference
222  GGint index = static_cast<GGint>(std::log(std::fabs(static_cast<GGdouble>(value)))/std::log(1000.0)) + 7;
223  index = (index < 0) ? 0 : index;
224  index = (index > 16) ? 16 : index;
225 
226  if (static_cast<GGdouble>(value) == 0.0) index = 7;
227 
228  std::ostringstream oss(std::stringstream::out);
229  oss << static_cast<GGdouble>(value)/std::pow(1000, index - 7) << " " << ::prefix_unit[index]+base_unit;
230 
231  return oss.str();
232 }
233 
242 template <typename T>
243 inline T DistanceUnit(T const& value, std::string const& unit)
244 {
245  T new_value = static_cast<T>(0);
246  if (unit == "nm") {
247  new_value = static_cast<T>(value * nm);
248  }
249  else if (unit == "um") {
250  new_value = static_cast<T>(value * um);
251  }
252  else if (unit == "mm") {
253  new_value = static_cast<T>(value * mm);
254  }
255  else if (unit == "cm") {
256  new_value = static_cast<T>(value * cm);
257  }
258  else if (unit == "m") {
259  new_value = static_cast<T>(value * m);
260  }
261  else if (unit == "km") {
262  new_value = static_cast<T>(value * km);
263  }
264  else if (unit == "pc") {
265  new_value = static_cast<T>(value * pc);
266  }
267  else {
268  std::ostringstream oss(std::ostringstream::out);
269  oss << "Unknown unit!!! You have choice between:" << std::endl;
270  oss << " - \"nm\": nanometer" << std::endl;
271  oss << " - \"um\": micrometer" << std::endl;
272  oss << " - \"mm\": millimeter" << std::endl;
273  oss << " - \"cm\": centimeter" << std::endl;
274  oss << " - \"m\": meter" << std::endl;
275  oss << " - \"km\": kilometer" << std::endl;
276  oss << " - \"pc\": parsec";
277  GGEMSMisc::ThrowException("", "DistanceUnit", oss.str());
278  }
279  return new_value;
280 }
281 
289 template <typename T>
290 inline std::string BestEnergyUnit(T const& value)
291 {
292  std::string base_unit = "eV"; // Base unit is electron volt (eV)
293 
294  // Find index of prefix
295  GGint index = static_cast<GGint>(std::log(std::fabs(static_cast<GGdouble>(value)))/std::log(1000.0)) + 10;
296  index = (index < 0) ? 0 : index;
297  index = (index > 16) ? 16 : index;
298 
299  if (static_cast<GGdouble>(value) == 0.0) index = 8;
300 
301  std::ostringstream oss(std::stringstream::out);
302  oss << static_cast<GGdouble>(value)/std::pow(1000, index - 10) << " " << ::prefix_unit[index]+base_unit;
303 
304  return oss.str();
305 }
306 
315 template <typename T>
316 inline T EnergyUnit(T const& value, std::string const& unit)
317 {
318  T new_value = static_cast<T>(0);
319  if (unit == "eV") {
320  new_value = static_cast<T>(value * eV);
321  }
322  else if (unit == "keV") {
323  new_value = static_cast<T>(value * keV);
324  }
325  else if (unit == "MeV") {
326  new_value = static_cast<T>(value * MeV);
327  }
328  else if (unit == "GeV") {
329  new_value = static_cast<T>(value * GeV);
330  }
331  else if (unit == "TeV") {
332  new_value = static_cast<T>(value * TeV);
333  }
334  else if (unit == "PeV") {
335  new_value = static_cast<T>(value * PeV);
336  }
337  else {
338  std::ostringstream oss(std::ostringstream::out);
339  oss << "Unknown unit!!! You have choice between:" << std::endl;
340  oss << " - \"eV\": electronvolt" << std::endl;
341  oss << " - \"keV\": kiloelectronvolt" << std::endl;
342  oss << " - \"MeV\": megaelectronvolt" << std::endl;
343  oss << " - \"GeV\": gigaelectronvolt" << std::endl;
344  oss << " - \"TeV\": teraelectronvolt" << std::endl;
345  oss << " - \"PeV\": petaelectronvolt" << std::endl;
346  GGEMSMisc::ThrowException("", "EnergyUnit", oss.str());
347  }
348  return new_value;
349 }
350 
359 template <typename T>
360 inline T AngleUnit(T const& value, std::string const& unit)
361 {
362  T new_value = static_cast<T>(0);
363  if (unit == "rad") {
364  new_value = static_cast<T>(value * rad);
365  }
366  else if (unit == "mrad") {
367  new_value = static_cast<T>(value * mrad);
368  }
369  else if (unit == "deg") {
370  new_value = static_cast<T>(value * deg);
371  }
372  else if (unit == "sr") {
373  new_value = static_cast<T>(value * sr);
374  }
375  else {
376  std::ostringstream oss(std::ostringstream::out);
377  oss << "Unknown unit!!! You have choice between:" << std::endl;
378  oss << " - \"rad\": radian" << std::endl;
379  oss << " - \"mrad\": milliradian" << std::endl;
380  oss << " - \"deg\": degree" << std::endl;
381  oss << " - \"sr\": steradian" << std::endl;
382  GGEMSMisc::ThrowException("", "AngleUnit", oss.str());
383  }
384  return new_value;
385 }
386 
395 template <typename T>
396 inline T DensityUnit(T const& value, std::string const& unit)
397 {
398  T new_value = static_cast<T>(0);
399  if (unit == "g/cm3") {
400  new_value = static_cast<T>(value * g/cm3);
401  }
402  else if (unit == "kg/m3") {
403  new_value = static_cast<T>(value * kg/m3);
404  }
405  else if (unit == "mg/cm3") {
406  new_value = static_cast<T>(value * mg/cm3);
407  }
408  else {
409  std::ostringstream oss(std::ostringstream::out);
410  oss << "Unknown unit!!! You have choice between:" << std::endl;
411  oss << " - \"g/cm3\": gram per cubic centimeter" << std::endl;
412  oss << " - \"kg/m3\": kilogram per cubic meter" << std::endl;
413  oss << " - \"mg/cm3\": milligram per cubic centimeter" << std::endl;
414  GGEMSMisc::ThrowException("", "DensityUnit", oss.str());
415  }
416  return new_value;
417 }
418 
425 inline std::string BestDigitalUnit(GGulong const& value)
426 {
427  std::string base_unit = "B"; // Base unit is byte(B)
428 
429  // Find index of prefix
430  GGint index = static_cast<GGint>(std::log(static_cast<GGdouble>(value))/std::log(1000.0)) + 8;
431  index = (index < 8) ? 8 : index;
432  index = (index > 16) ? 16 : index;
433 
434  if (value == 0) index = 8;
435 
436  std::ostringstream oss(std::stringstream::out);
437  oss << static_cast<GGdouble>(value)/std::pow(1000, index - 8) << " " << ::prefix_unit[index]+base_unit;
438 
439  return oss.str();
440 }
441 #endif
442 
443 #endif // End of GUARD_GGEMS_TOOLS_GGEMSSYSTEMOFUNITS_HH
kGy
__constant GGfloat kGy
Definition: GGEMSSystemOfUnits.hh:174
OHM
__constant GGfloat OHM
Definition: GGEMSSystemOfUnits.hh:137
anonymous_namespace{GGEMSSystemOfUnits.hh}::prefix_unit
std::string prefix_unit[17]
Definition: GGEMSSystemOfUnits.hh:199
km3
__constant GGfloat km3
Definition: GGEMSSystemOfUnits.hh:65
K
__constant GGfloat K
Definition: GGEMSSystemOfUnits.hh:158
nF
__constant GGfloat nF
Definition: GGEMSSystemOfUnits.hh:143
Wb
__constant GGfloat Wb
Definition: GGEMSSystemOfUnits.hh:147
MBq
__constant GGfloat MBq
Definition: GGEMSSystemOfUnits.hh:166
N
__constant GGfloat N
Definition: GGEMSSystemOfUnits.hh:118
GGulong
#define GGulong
Definition: GGEMSTypes.hh:245
eplus
__constant GGfloat eplus
Definition: GGEMSSystemOfUnits.hh:96
percent
__constant GGfloat percent
Definition: GGEMSSystemOfUnits.hh:188
PeV
__constant GGfloat PeV
Definition: GGEMSSystemOfUnits.hh:106
BestDistanceUnit
std::string BestDistanceUnit(T const &value)
Choose best distance unit.
Definition: GGEMSSystemOfUnits.hh:217
ms
__constant GGfloat ms
Definition: GGEMSSystemOfUnits.hh:86
AngleUnit
T AngleUnit(T const &value, std::string const &unit)
Choose best angle unit.
Definition: GGEMSSystemOfUnits.hh:360
H
__constant GGfloat H
Definition: GGEMSSystemOfUnits.hh:155
Pa
__constant GGfloat Pa
Definition: GGEMSSystemOfUnits.hh:121
permillion
__constant GGfloat permillion
Definition: GGEMSSystemOfUnits.hh:190
lm
__constant GGfloat lm
Definition: GGEMSSystemOfUnits.hh:182
qe
__constant GGfloat qe
Definition: GGEMSSystemOfUnits.hh:97
us
__constant GGfloat us
Definition: GGEMSSystemOfUnits.hh:87
cd
__constant GGfloat cd
Definition: GGEMSSystemOfUnits.hh:179
DensityUnit
T DensityUnit(T const &value, std::string const &unit)
Choose best density unit.
Definition: GGEMSSystemOfUnits.hh:396
V
__constant GGfloat V
Definition: GGEMSSystemOfUnits.hh:134
MeV
__constant GGfloat MeV
Definition: GGEMSSystemOfUnits.hh:103
mF
__constant GGfloat mF
Definition: GGEMSSystemOfUnits.hh:141
kHz
__constant GGfloat kHz
Definition: GGEMSSystemOfUnits.hh:92
perthousant
__constant GGfloat perthousant
Definition: GGEMSSystemOfUnits.hh:189
GGEMSTools.hh
Namespaces for different useful fonctions.
eV
__constant GGfloat eV
Definition: GGEMSSystemOfUnits.hh:101
mrad
__constant GGfloat mrad
Definition: GGEMSSystemOfUnits.hh:77
atm
__constant GGfloat atm
Definition: GGEMSSystemOfUnits.hh:123
mm
__constant GGfloat mm
Definition: GGEMSSystemOfUnits.hh:54
pb
__constant GGfloat pb
Definition: GGEMSSystemOfUnits.hh:73
A
__constant GGfloat A
Definition: GGEMSSystemOfUnits.hh:126
BestEnergyUnit
std::string BestEnergyUnit(T const &value)
Choose best energy unit, mega is the reference.
Definition: GGEMSSystemOfUnits.hh:290
nb
__constant GGfloat nb
Definition: GGEMSSystemOfUnits.hh:72
ps
__constant GGfloat ps
Definition: GGEMSSystemOfUnits.hh:88
kV
__constant GGfloat kV
Definition: GGEMSSystemOfUnits.hh:133
mCi
__constant GGfloat mCi
Definition: GGEMSSystemOfUnits.hh:169
GGint
#define GGint
Definition: GGEMSTypes.hh:224
keV
__constant GGfloat keV
Definition: GGEMSSystemOfUnits.hh:102
MV
__constant GGfloat MV
Definition: GGEMSSystemOfUnits.hh:132
cm3
__constant GGfloat cm3
Definition: GGEMSSystemOfUnits.hh:59
cm
__constant GGfloat cm
Definition: GGEMSSystemOfUnits.hh:57
TeV
__constant GGfloat TeV
Definition: GGEMSSystemOfUnits.hh:105
cm2
__constant GGfloat cm2
Definition: GGEMSSystemOfUnits.hh:58
sr
__constant GGfloat sr
Definition: GGEMSSystemOfUnits.hh:81
mm3
__constant GGfloat mm3
Definition: GGEMSSystemOfUnits.hh:56
G
__constant GGfloat G
Definition: GGEMSSystemOfUnits.hh:151
GeV
__constant GGfloat GeV
Definition: GGEMSSystemOfUnits.hh:104
__constant
#define __constant
__constant is known for OpenCL, but for C++ we define __constant as a constexpr
Definition: GGEMSSystemOfUnits.hh:43
uCi
__constant GGfloat uCi
Definition: GGEMSSystemOfUnits.hh:170
W
__constant GGfloat W
Definition: GGEMSSystemOfUnits.hh:115
ns
__constant GGfloat ns
Definition: GGEMSSystemOfUnits.hh:84
Hz
__constant GGfloat Hz
Definition: GGEMSSystemOfUnits.hh:91
km2
__constant GGfloat km2
Definition: GGEMSSystemOfUnits.hh:64
mGy
__constant GGfloat mGy
Definition: GGEMSSystemOfUnits.hh:175
kBq
__constant GGfloat kBq
Definition: GGEMSSystemOfUnits.hh:165
km
__constant GGfloat km
Definition: GGEMSSystemOfUnits.hh:63
nA
__constant GGfloat nA
Definition: GGEMSSystemOfUnits.hh:129
MHz
__constant GGfloat MHz
Definition: GGEMSSystemOfUnits.hh:93
deg
__constant GGfloat deg
Definition: GGEMSSystemOfUnits.hh:78
m2
__constant GGfloat m2
Definition: GGEMSSystemOfUnits.hh:61
m3
__constant GGfloat m3
Definition: GGEMSSystemOfUnits.hh:62
Gy
__constant GGfloat Gy
Definition: GGEMSSystemOfUnits.hh:173
mb
__constant GGfloat mb
Definition: GGEMSSystemOfUnits.hh:70
mg
__constant GGfloat mg
Definition: GGEMSSystemOfUnits.hh:112
g
__constant GGfloat g
Definition: GGEMSSystemOfUnits.hh:111
uA
__constant GGfloat uA
Definition: GGEMSSystemOfUnits.hh:128
GGdouble
#define GGdouble
Definition: GGEMSTypes.hh:280
ub
__constant GGfloat ub
Definition: GGEMSSystemOfUnits.hh:71
mol
__constant GGfloat mol
Definition: GGEMSSystemOfUnits.hh:161
kG
__constant GGfloat kG
Definition: GGEMSSystemOfUnits.hh:152
s
__constant GGfloat s
Definition: GGEMSSystemOfUnits.hh:85
rad
__constant GGfloat rad
Definition: GGEMSSystemOfUnits.hh:76
pc
__constant GGfloat pc
Definition: GGEMSSystemOfUnits.hh:66
nm
__constant GGfloat nm
Definition: GGEMSSystemOfUnits.hh:52
Ci
__constant GGfloat Ci
Definition: GGEMSSystemOfUnits.hh:168
EnergyUnit
T EnergyUnit(T const &value, std::string const &unit)
Choose best energy unit.
Definition: GGEMSSystemOfUnits.hh:316
GBq
__constant GGfloat GBq
Definition: GGEMSSystemOfUnits.hh:167
J
__constant GGfloat J
Definition: GGEMSSystemOfUnits.hh:107
lx
__constant GGfloat lx
Definition: GGEMSSystemOfUnits.hh:185
m
__constant GGfloat m
Definition: GGEMSSystemOfUnits.hh:60
mm2
__constant GGfloat mm2
Definition: GGEMSSystemOfUnits.hh:55
um
__constant GGfloat um
Definition: GGEMSSystemOfUnits.hh:53
Bq
__constant GGfloat Bq
Definition: GGEMSSystemOfUnits.hh:164
b
__constant GGfloat b
Definition: GGEMSSystemOfUnits.hh:69
uGy
__constant GGfloat uGy
Definition: GGEMSSystemOfUnits.hh:176
uF
__constant GGfloat uF
Definition: GGEMSSystemOfUnits.hh:142
mA
__constant GGfloat mA
Definition: GGEMSSystemOfUnits.hh:127
F
__constant GGfloat F
Definition: GGEMSSystemOfUnits.hh:140
bar
__constant GGfloat bar
Definition: GGEMSSystemOfUnits.hh:122
pF
__constant GGfloat pF
Definition: GGEMSSystemOfUnits.hh:144
T
__constant GGfloat T
Definition: GGEMSSystemOfUnits.hh:150
kg
__constant GGfloat kg
Definition: GGEMSSystemOfUnits.hh:110
GGEMSMisc::ThrowException
void ThrowException(std::string const &class_name, std::string const &method_name, std::string const &message)
Throw a C++ exception.
Definition: GGEMSTools.cc:61
BestDigitalUnit
std::string BestDigitalUnit(GGulong const &value)
Choose best digital unit.
Definition: GGEMSSystemOfUnits.hh:425
DistanceUnit
T DistanceUnit(T const &value, std::string const &unit)
Choose best distance unit.
Definition: GGEMSSystemOfUnits.hh:243
GGfloat
#define GGfloat
Definition: GGEMSTypes.hh:273
C
__constant GGfloat C
Definition: GGEMSSystemOfUnits.hh:98