Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
print_units_physical_constants.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file print_units_physical_constants.py
1 #!/usr/bin/env python3
2 #
3 # Compute physical constants
4 
5 from decimal import Decimal
6 
7 
8 # circle constant beyond double precision floating point
9 # from https://oeis.org/A000796
10 pi_digits = (
11  3,
12  1,
13  4,
14  1,
15  5,
16  9,
17  2,
18  6,
19  5,
20  3,
21  5,
22  8,
23  9,
24  7,
25  9,
26  3,
27  2,
28  3,
29  8,
30  4,
31  6,
32  2,
33  6,
34  4,
35  3,
36  3,
37  8,
38  3,
39  2,
40  7,
41  9,
42  5,
43  0,
44  2,
45  8,
46  8,
47  4,
48  1,
49  9,
50  7,
51  1,
52  6,
53  9,
54  3,
55  9,
56  9,
57  3,
58  7,
59  5,
60  1,
61  0,
62  5,
63  8,
64  2,
65  0,
66  9,
67  7,
68  4,
69  9,
70  4,
71  4,
72  5,
73  9,
74  2,
75  3,
76  0,
77  7,
78  8,
79  1,
80  6,
81  4,
82  0,
83  6,
84  2,
85  8,
86  6,
87  2,
88  0,
89  8,
90  9,
91  9,
92  8,
93  6,
94  2,
95  8,
96  0,
97  3,
98  4,
99  8,
100  2,
101  5,
102  3,
103  4,
104  2,
105  1,
106  1,
107  7,
108  0,
109  6,
110  7,
111  9,
112  8,
113  2,
114  1,
115  4,
116 )
117 pi = Decimal((0, pi_digits, 1 - len(pi_digits)))
118 
119 # values are taken from Review of Particle Physics 2020
120 # see https://pdg.lbl.gov/2020/reviews/rpp2020-rev-phys-constants.pdf
121 
122 # vacuum speed-of-light, exact
123 c_m_s = Decimal((0, (2, 9, 9, 7, 9, 2, 4, 5, 8), 0))
124 # electron charge, exact
125 e_C = Decimal((0, (1, 6, 0, 2, 1, 7, 6, 6, 3, 4), -28))
126 # Planck constant, exact
127 h_Js = Decimal((0, (6, 6, 2, 6, 0, 7, 0, 1, 5), -42))
128 h_eVs = h_Js / e_C
129 h_MeVs = h_eVs / Decimal((0, (1,), 6))
130 h_GeVs = h_eVs / Decimal((0, (1,), 9))
131 # reduced Planck constant
132 hbar_Js = h_Js / (2 * pi)
133 hbar_eVs = h_eVs / (2 * pi)
134 hbar_MeVs = h_MeVs / (2 * pi)
135 hbar_GeVs = h_GeVs / (2 * pi)
136 
137 # unit conversions
138 degree_radian = pi / Decimal((0, (1, 8, 0), 0))
139 J_eV = 1 / e_C
140 J_GeV = J_eV / Decimal((0, (1,), 9))
141 
142 full_constants = [
143  ("pi", pi, ""),
144  ("speed of light in vacuum", c_m_s, "m/s"),
145  ("electron charge", e_C, "C"),
146  ("Planck constant", h_Js, "J*s"),
147  ("Planck constant", h_eVs, "eV*s"),
148  ("Planck constant", h_MeVs, "MeV*s"),
149  ("Planck constant", h_GeVs, "GeV*s"),
150  ("reduced Planck constant", hbar_Js, "J*s"),
151  ("reduced Planck constant", hbar_eVs, "eV*s"),
152  ("reduced Planck constant", hbar_MeVs, "MeV*s"),
153  ("reduced Planck constant", hbar_GeVs, "GeV*s"),
154  ("degree", degree_radian, "radian"),
155  ("Joule", J_eV, "eV"),
156  ("Joule", J_GeV, "GeV"),
157 ]
158 float_constants = [(n, float(v), u) for n, v, u in full_constants]
159 
160 
161 def print_constants(constants):
162  # find the largest name first for consistent formatting
163  max_len_name = max((len(n) for n, *_ in constants))
164  line_format = f"{{:>{max_len_name}}}: {{}} {{}}"
165  for name, value, unit in constants:
166  print(line_format.format(name, value, unit))
167 
168 
169 if __name__:
170  print("=== high precision values:")
171  print_constants(full_constants)
172  print("=== double precision values:")
173  print_constants(float_constants)