Newer
Older
'''
renamed variables not to have a variable with the same name as the fuction the variable is used within
-- ME. 2018-07-23
'''
from scipy import constants
# Proton mass in MeV/c
proton_mass_in_MeV=constants.physical_constants['proton mass energy equivalent in MeV'][0]
# Speed of light in m/s
c=constants.c
def beta(energy: float, mass=proton_mass_in_MeV):
'''
Parameters
----------
energy: float
kinetic energy of the particle in MeV/c2
mass: float
mass of the moving particle, optional, if not given mass of proton is used
Returns
-------
relaivistic beta of the particle
'''
b=(1-gamma(energy, mass)**-2)**0.5
return b
def beta_c(energy, mass=proton_mass_in_MeV):
'''
Parameters
----------
energy: float
kinetic energy of the particle in MeV/c2
mass: float
mass of the moving particle, optional, if not given mass of proton is used
Returns
-------
relaivistic c*beta of the particle
'''
bc=c*(1-gamma(energy, mass)**-2)**0.5
return bc
def beta_from_gamma(gamma):
'''
Parameters
----------
gamma: float
relaivistic gamma of the particle
Returns
-------
relaivistic beta of the particle
raises
------
if given gamma is smaller than one raises a warning
'''
if gamma < 1:
print('gamma cannot be less than one')
beta = 0
else:
beta=(1-gamma**-2)**0.5
return beta
def gamma(energy, mass=proton_mass_in_MeV):
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
'''
Parameters
----------
energy: float
kinetic energy of the particle in MeV/c2
mass: float
mass of the moving particle, optional, if not given mass of proton is used
Returns
-------
relaivistic gamma of the particle
'''
#if mass>0:
g=1+(energy/mass)
return g
def gamma_from_beta(beta):
'''
Parameters
----------
beta: float
relaivistic beta of the particle
Returns
-------
relaivistic gamma of the particle
'''
if beta == 1:
from math import inf as math_inf
gamma = math_inf
else:
gamma = (1-beta**2)**-0.5
return gamma
def energy(gamma_beta, mass=proton_mass_in_MeV):
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
'''
Parameters
----------
gamma_beta: float
gamma or beta of the particle; if gamma_beta >= 1 the value is gamma, if gamma_beta < 1 the value is beta
mass: float
mass of the moving particle, optional, if not given mass of proton is used
Returns
-------
returns kinetic energy of the particle in MeV.
example:
sprel.energy(2.55, 938)
> 1453.8999999999999
if gamma_beta < 1 the value is beta
example:
sprel.energy(.55, 938)
> 185.13182200743233
'''
if gamma_beta >= 1: #assuming the value given is gamma
e = (gamma_beta - 1) * mass
elif gamma_beta < 1: #adduming the value given is beta
e = mass * (-1 + 1 / (1 - gamma_beta ** 2) ** 0.5)
return e