def test_BunchFunction(): """ Check that a bunch containing a single particle returns a list of length one and the particle is given the maximum velocity vector """ ListofOne = BunchFunction(1) assert len(ListofOne) == 1 assert np.all(ListofOne[0].velocity == [0.4*constants.speed_of_light*np.cos(2*math.pi*(0)), -(0.43 + (0.48))*constants.speed_of_light, 0.4*constants.speed_of_light*np.sin(2*math.pi*(0))]) #CosmicRay([0,8E4,0], , [0,0,0],'Proton 1',constants.proton_mass, constants.proton_mass, 1, 0, 1.0)
def Atmosphere(RunTime, NumberofCosmicRays): Bunch = BunchFunction(NumberofCosmicRays) for j in range(NumberofCosmicRays): time = 0.0 deltaT = 0.01*RunTime TimeList = [] PositionList = [[],[],[]] VelocityList = [[],[],[]] AccelerationList = [[],[],[]] ListofDirections = Direction(Bunch[j].velocity) for _ in np.arange(0, RunTime, deltaT): TimeList.append(time) Bunch[j].CosmicRayUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) time += deltaT CosmicRayData = {'Particle Number':j+1, 'Time':TimeList, 'X Position':PositionList[0], 'Y Position':PositionList[1],'Z Position':PositionList[2], 'X Velocity':VelocityList[0], 'Y Velocity':VelocityList[1], 'Z Velocity':VelocityList[2], 'X Acceleration':AccelerationList[0], 'Y Acceleration':AccelerationList[1], 'Z Acceleration':AccelerationList[2]} CosmicRayDataFrame = pd.DataFrame(CosmicRayData) CosmicRayDataFrame.to_pickle('Data/Cosmic_Ray_Data_%s.csv' %(j+1)) percent = round((j+1)/(len(Bunch))*100, 1) print('Simulation is %s %% complete' %(percent)) return(Bunch)
def Atmosphere(RunTime, NumberofCosmicRays, InteractionOnOff): Bunch = BunchFunction(NumberofCosmicRays) interactionOnOff = InteractionOnOff if interactionOnOff == 'I' or interactionOnOff == 'i': Update = 1 else: Update = 0 for j in range(NumberofCosmicRays): time = 0.0 deltaT = 0.01 * RunTime TimeList = [] InteractList = [] ParticleEnergyList = [] PositionList = [[], [], []] VelocityList = [[], [], []] AccelerationList = [[], [], []] ListofDirections = Direction(Bunch[j].velocity) for _ in np.arange(0, RunTime, deltaT): TimeList.append(time) Bunch[j].GammaUpdate() Bunch[j].MassUpdate() ParticleEnergyList.append(Bunch[j].ParticleEnergyUpdate()) if Update == 1: InteractList.append(Interacted(Bunch[j].interact)) Bunch[j].CosmicRayInteractionUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) Bunch[j].InteractionCheck() else: Bunch[j].CosmicRayUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) InteractList.append('N/A') time += deltaT CosmicRayData = { 'Particle Number': j + 1, 'Time [s]': TimeList, 'Interacted?': InteractList, 'Particle Energy [J]': ParticleEnergyList, 'X Position [m]': PositionList[0], 'Y Position [m]': PositionList[1], 'Z Position [m]': PositionList[2], 'X Velocity [m/s]': VelocityList[0], 'Y Velocity [m/s]': VelocityList[1], 'Z Velocity [m/s]': VelocityList[2], 'X Acceleration [m/s^2]': AccelerationList[0], 'Y Acceleration [m/s^2]': AccelerationList[1], 'Z Acceleration [m/s^2]': AccelerationList[2] } CosmicRayDataFrame = pd.DataFrame(CosmicRayData) CosmicRayDataFrame.to_pickle('Data/Cosmic_Ray_Data_%s.csv' % (j + 1)) percent = round((j + 1) / (len(Bunch)) * 100, 1) print('Simulation is %s %% complete' % (percent)) return (Bunch)
def Atmosphere(RunTime, NumberofCosmicRays): Bunch = BunchFunction(NumberofCosmicRays) for j in range(NumberofCosmicRays): time = 0.0 deltaT = 0.01*RunTime PositionList = [[],[],[]] VelocityList = [[],[],[]] AccerlerationList = [[],[],[]] TimeList = [] ListofDirections = Direction(Bunch[j].velocity) # print(Bunch[j]) for _ in np.arange(0, RunTime, deltaT): TimeList.append(time) beta = Bunch[j].beta() for i in range(len(Bunch[j].velocity)): PositionList[i].append(Bunch[j].position[i]) VelocityList[i].append(Bunch[j].velocity[i]) if np.abs(Bunch[j].velocity[i]) <= 2.74E6: # minimum speed before bethe stopping power eqn breaks due to -ve ln. AccerlerationList[i].append(Bunch[j].acceleration[i]) else: if np.abs(Bunch[j].velocity[i]) >= 1E7: Force = StoppingForce(5.3E25,Bunch[j].charge,beta[i],1.36E-17) # using stratosphere eDensity for now. else: Force = NonRelativisticStoppingForce(5.3E25,Bunch[j].charge,Bunch[j].velocity[i],1.36E-17) # using stratosphere eDensity for now. AccelerationCalc = (Force/Bunch[j].mass) Bunch[j].acceleration[i] = ForceDirectionCheck(ListofDirections[i],AccelerationCalc) AccerlerationList[i].append(Bunch[j].acceleration[i]) Bunch[j].Velocityupdate(i,deltaT) if ListofDirections[i] == 1 and Bunch[j].velocity[i] < 0: Bunch[j].velocity[i] = 0 Bunch[j].acceleration[i] = 0 Bunch[j].Positionupdate(i,deltaT) elif ListofDirections[i] == -1 and Bunch[j].velocity[i] > 0: Bunch[j].velocity[i] = 0 Bunch[j].acceleration[i] = 0 Bunch[j].Positionupdate(i,deltaT) else: Bunch[j].Positionupdate(i,deltaT) time += deltaT CosmicRayData = {'Particle Number':j+1, 'Time':TimeList, 'X Position':PositionList[0], 'Y Position':PositionList[1],'Z Position':PositionList[2], 'X Velocity':VelocityList[0], 'Y Velocity':VelocityList[1], 'Z Velocity':VelocityList[2], 'X Acceleration':AccerlerationList[0], 'Y Acceleration':AccerlerationList[1], 'Z Acceleration':AccerlerationList[2]} CosmicRayDataFrame = pd.DataFrame(CosmicRayData) CosmicRayDataFrame.to_pickle('Data/Cosmic_Ray_Data_%s.csv' %(j+1)) percent = (j+1)/(len(Bunch))*100 print ('Simulation is %s %% complete' %(percent)) return(Bunch)
def Atmosphere(RunTime, NumberofCosmicRays): Bunch = BunchFunction(NumberofCosmicRays) InteractionOnOff = str( input( 'Would you like: \n - Non-Interacting Primary Cosmic Rays? [Input N] \n - Interacting Cosmic Rays [Input I] \n' )) # \n Both Types [Input B] ')) if InteractionOnOff == 'I' or InteractionOnOff == 'i': Update = 1 else: Update = 0 for j in range(NumberofCosmicRays): time = 0.0 deltaT = 0.01 * RunTime TimeList = [] InteractList = [] PositionList = [[], [], []] VelocityList = [[], [], []] AccelerationList = [[], [], []] ListofDirections = Direction(Bunch[j].velocity) for _ in np.arange(0, RunTime, deltaT): TimeList.append(time) if Update == 1: InteractList.append(Interacted(Bunch[j].interact)) Bunch[j].CosmicRayInteractionUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) Bunch[j].InteractionCheck() else: Bunch[j].CosmicRayUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) InteractList.append('N/A') time += deltaT CosmicRayData = { 'Particle Number': j + 1, 'Time': TimeList, 'Interaction Occurred?': InteractList, 'X Position': PositionList[0], 'Y Position': PositionList[1], 'Z Position': PositionList[2], 'X Velocity': VelocityList[0], 'Y Velocity': VelocityList[1], 'Z Velocity': VelocityList[2], 'X Acceleration': AccelerationList[0], 'Y Acceleration': AccelerationList[1], 'Z Acceleration': AccelerationList[2] } CosmicRayDataFrame = pd.DataFrame(CosmicRayData) CosmicRayDataFrame.to_pickle('Data/Cosmic_Ray_Data_%s.csv' % (j + 1)) percent = round((j + 1) / (len(Bunch)) * 100, 1) print('Simulation is %s %% complete' % (percent)) return (Bunch)
def Atmosphere(RunTime, NumberofCosmicRays, InteractionOnOff): Bunch = BunchFunction(NumberofCosmicRays) # Initialise the bunch of cosmic rays. interactionOnOff = InteractionOnOff # The if-else statement selects whether interactions will or won't be simulated. if interactionOnOff == 'I' or interactionOnOff == 'i': Update = 1 else: Update = 0 for j in range(NumberofCosmicRays): # As cosmic rays do not interact with each other, the simulation is run sequentially for each cosmic ray in the bunch. time = 0.0 # Initialise all data members. deltaT = 0.001*RunTime TimeList = [] InteractList = [] ParticleEnergyList = [] PositionList = [[],[],[]] VelocityList = [[],[],[]] AccelerationList = [[],[],[]] ListofDirections = Direction(Bunch[j].velocity) for _ in np.arange(0, RunTime, deltaT): # Run the simulation for the given cosmic ray over the RunTime, updating every timestep - deltaT. TimeList.append(time) # Append values for this time-step to each data member. These are done before updating the cosmic ray parameters so that intial parameters are recorded. Bunch[j].GammaUpdate() # Calculate gamma for cosmic ray at its current speed. Bunch[j].MassUpdate() # Use gamma to calculate relativistic mass of cosmic ray. ParticleEnergyList.append(Bunch[j].ParticleEnergyUpdate()) if Update == 1: # If statment runs if cosmic rays can interact. InteractList.append(Interacted(Bunch[j].interact)) Bunch[j].CosmicRayUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) # Run the CosmicRay class CosmicRayInteractionUpdate Function. Bunch[j].InteractionCheck() # Run the CosmicRay class InteractionCheck function. else: # Else statement runs if cosmic rays cannot interact. Bunch[j].CosmicRayUpdate(deltaT, PositionList, VelocityList, AccelerationList, ListofDirections) # Run the CosmicRay class CosmicRayUpdate function. InteractList.append('N/A') time += deltaT # Add one timestep to the time before repeating the loop. CosmicRayData = {'Particle Number':j+1, 'Time [s]':TimeList, 'Interacted?':InteractList, 'Particle Energy [J]':ParticleEnergyList, # Create a dictionary with the parameters for the cosmic ray at each timestep. 'X Position [m]':PositionList[0], 'Y Position [m]':PositionList[1],'Z Position [m]':PositionList[2], 'X Velocity [m/s]':VelocityList[0], 'Y Velocity [m/s]':VelocityList[1], 'Z Velocity [m/s]':VelocityList[2], 'X Acceleration [m/s^2]':AccelerationList[0], 'Y Acceleration [m/s^2]':AccelerationList[1], 'Z Acceleration [m/s^2]':AccelerationList[2]} CosmicRayDataFrame = pd.DataFrame(CosmicRayData) # Convert the dictionary to a dataframe for the cosmic ray. CosmicRayDataFrame.to_pickle('Data/Cosmic_Ray_Data_%s.csv' %(j+1)) # Pickle and save the dataframe to the Data folder. The data frames are saved with the cosmic ray's number in the bunch. percent = round((j+1)/(len(Bunch))*100, 1) # A calculation to print percentage completion of the simulation. This prints after each cosmic ray has been simulated. print('Simulation is %s %% complete' %(percent)) return(Bunch) # Return the bunch of cosmic rays, now with their final parameters.
import numpy as np import pandas as pd import math import copy from scipy import constants from Particle import Particle from Particle import Charticle from AtmosphereSupportFunctions import StoppingForce, NonRelativisticStoppingForce, Direction, ForceDirectionCheck, BunchFunction from ParticleBunchClass import ParticleBunch particles = BunchFunction(10) tester = ParticleBunch(particles) print(tester)