physkit 0.1.17__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
physkit/__init__.py ADDED
@@ -0,0 +1,45 @@
1
+ # Physkit - A Python toolkit for constants, unit conversions, and equations.
2
+ # Copyright (C) 2024 sapphimars
3
+ #
4
+ # This program 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
+ # This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+
18
+ """
19
+ physkit
20
+
21
+ A Python toolkit providing physical constants, unit conversions,
22
+ and plotting styles for scientific work.
23
+ """
24
+
25
+ __version__ = "0.1.0"
26
+
27
+ from .config import get_ureg, set_default
28
+ from .constants import constants
29
+ from .conversions import convert_unit, convert_to_base_units, convert_unit_system
30
+ from .plot_styler import plot_styler
31
+ from .equations import equations
32
+
33
+
34
+ __version__ = "0.1.17"
35
+
36
+ __all__ = [
37
+ "set_default",
38
+ "get_ureg",
39
+ "constants",
40
+ "convert_unit",
41
+ "convert_to_base_units",
42
+ "convert_unit_system",
43
+ "plot_styler",
44
+ "equations",
45
+ ]
physkit/cli.py ADDED
@@ -0,0 +1,97 @@
1
+ # Physkit - A Python toolkit for constants, unit conversions, and equations.
2
+ # Copyright (C) 2024 sapphimars
3
+ #
4
+ # This program 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
+ # This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+
18
+ import argparse
19
+ import sys
20
+ from .conversions import convert_unit
21
+ from .config import set_default
22
+ from .constants import constants
23
+
24
+
25
+ def main():
26
+ """
27
+ Entry point for the physkit CLI. Parses subcommands to then run the appropriate function.
28
+ """
29
+ parser = argparse.ArgumentParser(
30
+ prog="physkit",
31
+ description="A CLI for the physkit library to display constants or convert units",
32
+ )
33
+
34
+ subparsers = parser.add_subparsers(dest="command", help="Sub-command to run")
35
+
36
+ # `physkit convert <value> <from_unit> <to_unit>`
37
+ convert_parser = subparsers.add_parser("convert", help="Convert units")
38
+ convert_parser.add_argument("value", type=float, help="Numeric value to convert")
39
+ convert_parser.add_argument("from_unit", type=str, help="Current unit")
40
+ convert_parser.add_argument("to_unit", type=str, help="Target unit")
41
+
42
+ # `physkit constant <name> [--system ...]`
43
+ convert_parser = subparsers.add_parser("constant", help="Value of constant")
44
+ convert_parser.add_argument("name", type=str, help="Name of constant to display")
45
+ convert_parser.add_argument(
46
+ "--system",
47
+ type=str,
48
+ default=None,
49
+ help="Optionally set unit system before converting (SI, cgs, etc.)",
50
+ )
51
+ # Parse the CLI args
52
+ args = parser.parse_args()
53
+
54
+ # If no subcommand given, print help and exit
55
+ if not args.command:
56
+ parser.print_help()
57
+ sys.exit(1)
58
+
59
+ if args.command == "convert":
60
+ run_convert(args)
61
+ elif args.command == "constant":
62
+ run_constant(args)
63
+ else:
64
+ parser.print_help()
65
+
66
+
67
+ def run_convert(args):
68
+ """
69
+ Handle the 'physkit convert' subcommand.
70
+ """
71
+
72
+ # Do the conversion
73
+ result = convert_unit(args.value, args.from_unit, args.to_unit)
74
+ print(f"{args.value} {args.from_unit} = {result} {args.to_unit}")
75
+
76
+
77
+ def run_constant(args):
78
+ """
79
+ Handle the 'physkit constant' subcommand.
80
+ """
81
+ if args.system:
82
+ # Change the global default system if specified
83
+ set_default(args.system)
84
+
85
+ # Attempt to retrieve the constant by attribute name
86
+ try:
87
+ value = getattr(constants, args.name)
88
+ except AttributeError:
89
+ print(f"No such constant '{args.name}' in the 'constants' class.")
90
+ sys.exit(1)
91
+
92
+ val, unit = value
93
+ print(f"{args.name} = {val} {unit}")
94
+
95
+
96
+ # if __name__ == "__main__":
97
+ # main()
physkit/config.py ADDED
@@ -0,0 +1,41 @@
1
+ # Physkit - A Python toolkit for constants, unit conversions, and equations.
2
+ # Copyright (C) 2024 sapphimars
3
+ #
4
+ # This program 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
+ # This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+
18
+ import pint
19
+ from .custom_units import define_custom_units
20
+
21
+ ureg = pint.UnitRegistry(system="SI") # Set default to SI
22
+ define_custom_units(ureg) # Define units in SI
23
+
24
+
25
+ def set_default(unit_system: str) -> None:
26
+ """
27
+ Change global default unit system.
28
+
29
+ Args:
30
+ unit_system (str): unit system to change to
31
+
32
+ Example:
33
+ physkit.set_default("cgs") -> change to cgs unit system
34
+ """
35
+ global ureg
36
+ ureg = pint.UnitRegistry(system=unit_system)
37
+ define_custom_units(ureg)
38
+
39
+
40
+ def get_ureg():
41
+ return ureg
physkit/constants.py ADDED
@@ -0,0 +1,426 @@
1
+ # Physkit - A Python toolkit for constants, unit conversions, and equations.
2
+ # Copyright (C) 2024 sapphimars
3
+ #
4
+ # This program 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
+ # This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+
18
+ """
19
+ constants.py
20
+
21
+ Fundamental and derived physical constants, using 2022 CODATA (SI units),
22
+ plus selected particle masses from PDG, and astrophysical/cosmological
23
+ parameters from external references (Planck data, etc.).
24
+ """
25
+
26
+ from .config import get_ureg
27
+
28
+
29
+ class Constants:
30
+ ##########################################################################
31
+ # UNIVERSAL CONSTANTS
32
+ ##########################################################################
33
+ @property
34
+ def c(self):
35
+ # Speed of light in vacuum (exact by definition)
36
+ val = 299792458.0 * get_ureg()("m / s")
37
+ converted = val.to_base_units()
38
+ return converted.magnitude, f"{converted.units:~}"
39
+
40
+ @property
41
+ def G(self):
42
+ # Newtonian constant of gravitation
43
+ val = 6.67430e-11 * get_ureg()("m^3 / (kg * s^2)")
44
+ converted = val.to_base_units()
45
+ return converted.magnitude, f"{converted.units:~}"
46
+
47
+ @property
48
+ def h(self):
49
+ # Planck constant (exact by definition)
50
+ val = 6.62607015e-34 * get_ureg()("J * s")
51
+ converted = val.to_base_units()
52
+ return converted.magnitude, f"{converted.units:~}"
53
+
54
+ @property
55
+ def h_bar(self):
56
+ # Reduced Planck constant ħ = h / (2π)
57
+ val = 1.054571817e-34 * get_ureg()("J * s")
58
+ converted = val.to_base_units()
59
+ return converted.magnitude, f"{converted.units:~}"
60
+
61
+ ##########################################################################
62
+ # ELECTROMAGNETIC CONSTANTS
63
+ ##########################################################################
64
+
65
+ @property
66
+ def mu_0(self):
67
+ # Vacuum magnetic permeability µ0 (no longer exact post-2019)
68
+ val = 1.25663706212e-6 * get_ureg()("N / A^2")
69
+ converted = val.to_base_units()
70
+ return converted.magnitude, f"{converted.units:~}"
71
+
72
+ @property
73
+ def epsilon_0(self):
74
+ # Vacuum electric permittivity ε0 (no longer exact post-2019)
75
+ val = 8.8541878128e-12 * get_ureg()("F / m") # (F = farad)
76
+ converted = val.to_base_units()
77
+ return converted.magnitude, f"{converted.units:~}"
78
+
79
+ @property
80
+ def Z_0(self):
81
+ # Characteristic impedance of vacuum Z0
82
+ val = 376.730313412 * get_ureg()("ohm")
83
+ converted = val.to_base_units()
84
+ return converted.magnitude, f"{converted.units:~}"
85
+
86
+ @property
87
+ def e(self):
88
+ # Elementary charge (exact by definition)
89
+ val = 1.602176634e-19 * get_ureg()("C")
90
+ converted = val.to_base_units()
91
+ return converted.magnitude, f"{converted.units:~}"
92
+
93
+ ##########################################################################
94
+ # PARTICLE MASSES
95
+ ##########################################################################
96
+ @property
97
+ def m_e(self):
98
+ # Electron mass
99
+ val = 9.1093837015e-31 * get_ureg()("kg")
100
+ converted = val.to_base_units()
101
+ return converted.magnitude, f"{converted.units:~}"
102
+
103
+ @property
104
+ def m_p(self):
105
+ # Proton mass
106
+ val = 1.67262192369e-27 * get_ureg()("kg")
107
+ converted = val.to_base_units()
108
+ return converted.magnitude, f"{converted.units:~}"
109
+
110
+ @property
111
+ def m_n(self):
112
+ # Neutron mass
113
+ val = 1.67492749804e-27 * get_ureg()("kg")
114
+ converted = val.to_base_units()
115
+ return converted.magnitude, f"{converted.units:~}"
116
+
117
+ @property
118
+ def m_nu(self):
119
+ # Muon mass
120
+ val = 1.883531627e-28 * get_ureg()("kg")
121
+ converted = val.to_base_units()
122
+ return converted.magnitude, f"{converted.units:~}"
123
+
124
+ @property
125
+ def m_tau(self):
126
+ # Tau mass (approx)
127
+ val = 3.16754e-27 * get_ureg()("kg")
128
+ converted = val.to_base_units()
129
+ return converted.magnitude, f"{converted.units:~}"
130
+
131
+ @property
132
+ def m_u(self):
133
+ # Atomic mass constant (1 u in kg)
134
+ val = 1.66053906660e-27 * get_ureg()("kg")
135
+ converted = val.to_base_units()
136
+ return converted.magnitude, f"{converted.units:~}"
137
+
138
+ @property
139
+ def m_alpha(self):
140
+ # Alpha particle mass
141
+ val = 6.6446573450e-27 * get_ureg()("kg")
142
+ converted = val.to_base_units()
143
+ return converted.magnitude, f"{converted.units:~}"
144
+
145
+ @property
146
+ def m_h(self):
147
+ # Helion mass (helium-3 nucleus)
148
+ val = 5.0064127862e-27 * get_ureg()("kg")
149
+ converted = val.to_base_units()
150
+ return converted.magnitude, f"{converted.units:~}"
151
+
152
+ ##########################################################################
153
+ # OTHER FUNDAMENTAL CONSTANTS
154
+ ##########################################################################
155
+ @property
156
+ def N_A(self):
157
+ # Avogadro constant (exact by definition)
158
+ val = 6.02214076e23 * get_ureg()("1 / mol")
159
+ converted = val.to_base_units()
160
+ return converted.magnitude, f"{converted.units:~}"
161
+
162
+ @property
163
+ def k_b(self):
164
+ # Boltzmann constant (exact by definition)
165
+ val = 1.380649e-23 * get_ureg()("J / K")
166
+ converted = val.to_base_units()
167
+ return converted.magnitude, f"{converted.units:~}"
168
+
169
+ @property
170
+ def r_e(self):
171
+ # Classical electron radius (m)
172
+ val = 2.8179403262e-15 * get_ureg()("m")
173
+ converted = val.to_base_units()
174
+ return converted.magnitude, f"{converted.units:~}"
175
+
176
+ ##########################################################################
177
+ # DERIVED FUNDAMENTAL CONSTANTS
178
+ ##########################################################################
179
+ @property
180
+ def alpha(self):
181
+ # Fine-structure constant (dimensionless)
182
+ val = 0.0072973525693
183
+ return val, ""
184
+
185
+ @property
186
+ def mu_B(self):
187
+ # Bohr magneton (J/T)
188
+ val = 9.2740100783e-24 * get_ureg()("J/T")
189
+ converted = val.to_base_units()
190
+ return converted.magnitude, f"{converted.units:~}"
191
+
192
+ @property
193
+ def mu_N(self):
194
+ # Nuclear magneton (J/T)
195
+ val = 5.0507837461e-27 * get_ureg()("J/T")
196
+ converted = val.to_base_units()
197
+ return converted.magnitude, f"{converted.units:~}"
198
+
199
+ @property
200
+ def a0(self):
201
+ # Bohr radius (m)
202
+ val = 5.29177210903e-11 * get_ureg()("m")
203
+ converted = val.to_base_units()
204
+ return converted.magnitude, f"{converted.units:~}"
205
+
206
+ @property
207
+ def R_inf(self):
208
+ # Rydberg constant (1/m)
209
+ val = 10973731.568160 * get_ureg()("1/m")
210
+ converted = val.to_base_units()
211
+ return converted.magnitude, f"{converted.units:~}"
212
+
213
+ ##########################################################################
214
+ # MAGNETIC MOMENTS, G-FACTORS, MOLAR PLANCK CONSTANT
215
+ ##########################################################################
216
+ @property
217
+ def mu_e(self):
218
+ # Electron magnetic moment (magnitude) [J/T] (2022 CODATA)
219
+ val = 9.2847647043e-24 * get_ureg()("J/T")
220
+ converted = val.to_base_units()
221
+ return converted.magnitude, f"{converted.units:~}"
222
+
223
+ @property
224
+ def mu_p(self):
225
+ # Proton magnetic moment [J/T]
226
+ val = 1.41060679736e-26 * get_ureg()("J/T")
227
+ converted = val.to_base_units()
228
+ return converted.magnitude, f"{converted.units:~}"
229
+
230
+ @property
231
+ def mu_n(self):
232
+ # Neutron magnetic moment (magnitude) [J/T]
233
+ val = 9.6623651e-27 * get_ureg()("J/T")
234
+ converted = val.to_base_units()
235
+ return converted.magnitude, f"{converted.units:~}"
236
+
237
+ @property
238
+ def g_e(self):
239
+ # Electron g-factor (dimensionless)
240
+ val = 2.00231930436256
241
+ return val, ""
242
+
243
+ @property
244
+ def g_p(self):
245
+ # Proton g-factor (dimensionless)
246
+ val = 5.5856946893
247
+ return val, ""
248
+
249
+ @property
250
+ def g_n(self):
251
+ # Neutron g-factor (dimensionless)
252
+ val = -3.8260837 # negative sign is typical convention
253
+ return val, ""
254
+
255
+ @property
256
+ def mu_mu(self):
257
+ # Muon magnetic moment (magnitude) [J/T]
258
+ val = 4.49044830e-26 * get_ureg()("J/T")
259
+ converted = val.to_base_units()
260
+ return converted.magnitude, f"{converted.units:~}"
261
+
262
+ @property
263
+ def g_mu(self):
264
+ # Muon g-factor (dimensionless)
265
+ val = 2.0023318418
266
+ return val, ""
267
+
268
+ @property
269
+ def molar_planck(self):
270
+ # Molar Planck constant (N_A * h) [J·s/mol]
271
+ val = 3.990312717628e-10 * get_ureg()("J * s / mol")
272
+ converted = val.to_base_units()
273
+ return converted.magnitude, f"{converted.units:~}"
274
+
275
+ ##########################################################################
276
+ # THERMODYNAMIC
277
+ ##########################################################################
278
+ @property
279
+ def sigma_SB(self):
280
+ # Stefan-Boltzmann constant (sigma) [W/(m^2·K^4)]
281
+ val = 5.670374419e-8 * get_ureg()("W / (m^2 * K^4)")
282
+ converted = val.to_base_units()
283
+ return converted.magnitude, f"{converted.units:~}"
284
+
285
+ @property
286
+ def R_gas(self):
287
+ # Ideal gas constant (R) [J/(mol·K)]
288
+ val = 8.31446261815324 * get_ureg()("J / (mol * K)")
289
+ converted = val.to_base_units()
290
+ return converted.magnitude, f"{converted.units:~}"
291
+
292
+ @property
293
+ def Rydberg_energy(self):
294
+ # Rydberg energy (~13.605693122 eV in joules)
295
+ val = 2.1798723611035e-18 * get_ureg()("J")
296
+ converted = val.to_base_units()
297
+ return converted.magnitude, f"{converted.units:~}"
298
+
299
+ @property
300
+ def atm(self):
301
+ # 1 atm pressure (exact in SI)
302
+ val = 101325.0 * get_ureg()("Pa")
303
+ converted = val.to_base_units()
304
+ return converted.magnitude, f"{converted.units:~}"
305
+
306
+ ##########################################################################
307
+ # HADRON MASSES
308
+ ##########################################################################
309
+ @property
310
+ def m_pion_charged(self):
311
+ # Charged pion mass (pi±) ~139.57039 MeV/c^2 => ~2.49e-28 kg
312
+ val = 2.48832e-28 * get_ureg()("kg")
313
+ converted = val.to_base_units()
314
+ return converted.magnitude, f"{converted.units:~}"
315
+
316
+ @property
317
+ def m_pion_neutral(self):
318
+ # Neutral pion mass (pi0) ~134.9770 MeV/c^2 => ~2.41e-28 kg
319
+ val = 2.405e-28 * get_ureg()("kg")
320
+ converted = val.to_base_units()
321
+ return converted.magnitude, f"{converted.units:~}"
322
+
323
+ @property
324
+ def m_kaon_charged(self):
325
+ # Charged kaon mass (K±) ~493.677 MeV/c^2 => ~8.80e-28 kg
326
+ val = 8.80e-28 * get_ureg()("kg")
327
+ converted = val.to_base_units()
328
+ return converted.magnitude, f"{converted.units:~}"
329
+
330
+ @property
331
+ def m_kaon_neutral(self):
332
+ # Neutral kaon mass (K0) ~497.611 MeV/c^2 => ~8.87e-28 kg
333
+ val = 8.87e-28 * get_ureg()("kg")
334
+ converted = val.to_base_units()
335
+ return converted.magnitude, f"{converted.units:~}"
336
+
337
+ ##########################################################################
338
+ # COMPTON WAVELENGTHS
339
+ ##########################################################################
340
+ @property
341
+ def lambda_compton_e(self):
342
+ # Electron Compton wavelength ~2.42631023867e-12 m
343
+ val = 2.42631023867e-12 * get_ureg()("m")
344
+ converted = val.to_base_units()
345
+ return converted.magnitude, f"{converted.units:~}"
346
+
347
+ @property
348
+ def lambda_compton_p(self):
349
+ # Proton Compton wavelength ~1.32140985539e-15 m
350
+ val = 1.32140985539e-15 * get_ureg()("m")
351
+ converted = val.to_base_units()
352
+ return converted.magnitude, f"{converted.units:~}"
353
+
354
+ @property
355
+ def lambda_compton_n(self):
356
+ # Neutron Compton wavelength ~1.31959090581e-15 m
357
+ val = 1.31959090581e-15 * get_ureg()("m")
358
+ converted = val.to_base_units()
359
+ return converted.magnitude, f"{converted.units:~}"
360
+
361
+ ##########################################################################
362
+ # SCATTERING CROSS SECTIONS
363
+ ##########################################################################
364
+ @property
365
+ def sigma_T(self):
366
+ # Thomson cross section (2022 CODATA) [m^2]
367
+ val = 6.6524587321e-29 * get_ureg()("m^2")
368
+ converted = val.to_base_units()
369
+ return converted.magnitude, f"{converted.units:~}"
370
+
371
+ ##########################################################################
372
+ # ASTRONOMICAL / ASTROPHYSICAL CONSTANTS
373
+ ##########################################################################
374
+ @property
375
+ def M_earth(self):
376
+ # Earth mass (approx)
377
+ val = 5.97219e24 * get_ureg()("kg")
378
+ converted = val.to_base_units()
379
+ return converted.magnitude, f"{converted.units:~}"
380
+
381
+ @property
382
+ def M_sun(self):
383
+ # Solar mass (approx)
384
+ val = 1.98847e30 * get_ureg()("kg")
385
+ converted = val.to_base_units()
386
+ return converted.magnitude, f"{converted.units:~}"
387
+
388
+ @property
389
+ def L_sun(self):
390
+ # Solar luminosity (approx)
391
+ val = 3.828e26 * get_ureg()("W")
392
+ converted = val.to_base_units()
393
+ return converted.magnitude, f"{converted.units:~}"
394
+
395
+ @property
396
+ def AU(self):
397
+ # Astronomical Unit (exact, 2012 definition: 1 AU = 149,597,870,700 m)
398
+ val = 1.49597870700e11 * get_ureg()("m")
399
+ converted = val.to_base_units()
400
+ return converted.magnitude, f"{converted.units:~}"
401
+
402
+ ##########################################################################
403
+ # COSMOLOGICAL CONSTANTS
404
+ ##########################################################################
405
+ @property
406
+ def H_0(self):
407
+ # Hubble constant ~67.66 (km/s)/Mpc => ~2.192e-18 1/s
408
+ val = 2.192e-18 * get_ureg()("1/s")
409
+ converted = val.to_base_units()
410
+ return converted.magnitude, f"{converted.units:~}"
411
+
412
+ @property
413
+ def rho_crit(self):
414
+ # Critical density of the Universe (~8.6e-27 kg/m^3)
415
+ val = 8.6e-27 * get_ureg()("kg / m^3")
416
+ converted = val.to_base_units()
417
+ return converted.magnitude, f"{converted.units:~}"
418
+
419
+ @property
420
+ def pi(self):
421
+ # pi
422
+ val = 3.1415926535897932384626433
423
+ return val, ""
424
+
425
+
426
+ constants = Constants()