Skip to content
Snippets Groups Projects
SP_Relativity.py 2.91 KiB
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
'''
def beta(energy: float, mass=938.2720813):
    '''
    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=938.2720813):
    '''
    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=938.2720813):
    '''
    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=938.2720813):
    '''
    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

def c():
    '''
    speed of light, 299792458 m/s
    '''
    return 299792458