def test_AdvectivePropagation(self): ConstMagVec = crpropa.Vector3d(0.) BField = crpropa.UniformMagneticField(ConstMagVec) ConstAdvVec = crpropa.Vector3d(0., 1e6, 0.) AdvField = crpropa.UniformAdvectionField(ConstAdvVec) precision = 1e-4 minStep = 1*pc maxStep = 10*kpc epsilon = 0. Dif = crpropa.DiffusionSDE(BField, AdvField, precision, minStep, maxStep, epsilon) c = crpropa.Candidate() c.current.setId(crpropa.nucleusId(1,1)) c.current.setEnergy(10*TeV) c.current.setDirection(crpropa.Vector3d(1,0,0)) # check for position Dif.process(c) pos = c.current.getPosition() self.assertEqual(pos.x, 0.) self.assertAlmostEqual(pos.y, minStep/c_light*1e6) self.assertEqual(pos.z, 0.) # Step size is increased to maxStep self.assertAlmostEqual(c.getNextStep()/minStep, 5.) #AlmostEqual due to rounding error
def test_NeutralPropagation(self): c = crpropa.Candidate() c.current.setId(crpropa.nucleusId(1,0)) c.current.setEnergy(10*TeV) c.current.setDirection(crpropa.Vector3d(1,0,0)) # check for position self.Dif.process(c) pos = c.current.getPosition() self.assertAlmostEqual(pos.x/pc, 1.) #AlmostEqual due to rounding error self.assertEqual(pos.y, 0.) self.assertEqual(pos.z, 0.) # Step size is increased to maxStep self.assertAlmostEqual(c.getNextStep()/self.maxStep, 1.) #AlmostEqual due to rounding error
def initial_directions(M, source_l, source_b, weights, d, NN, energies=None): lons0 = [] lats0 = [] for isource in range(len(source_l)): l = source_l[isource] * np.pi / 180.0 - (2.0 * np.pi) b = Lat2CoLat(source_b[isource] * np.pi / 180.0) #M.addParticles(Id, E, l, b, E0**-1) print(isource, weights[isource], l, b) meanDir = crpropa.Vector3d(-1, 0, 0) meanDir.setRThetaPhi(-1, b, l) kappa = 50.0 ncrs = int((weights[isource] / total) * NN) #if ncrs>0: ncrs = 500 #Emin = 10.0 energies = powerlaw.Power_Law(xmin=10.0, xmax=100.0, parameters=[2.2]).generate_random(ncrs) #print (energies) for i in xrange(ncrs): particleId = crpropa.nucleusId(1, 1) energy = (energies[i]) * crpropa.EeV energy = 10.0 * crpropa.EeV galCenter = crpropa.Vector3d(-1, 0, 0) theta_k = 0.8 * (100.0 * crpropa.EeV / energy) * np.sqrt( d[isource] / 10.0) kappa = (81.0 / theta_k)**2 #print (kappa) momentumVector = crpropa.Random.instance().randFisherVector( meanDir, kappa) M.addParticle(particleId, energy, momentumVector) lons0.append(momentumVector.getPhi()) lats0.append(momentumVector.getTheta()) lons0 = np.array(lons0) - np.pi lats0 = -CoLat2Lat(np.array(lats0)) return (lons0, lats0)
def test_DiffusionEnergy100PeV(self): x, y, z = [], [], [] E = 10 * PeV D = self.Dif.getScale()*6.1e24*(E/(4*GeV))**self.Dif.getAlpha() L_max = 50 * kpc std_exp = np.sqrt(2*D*L_max/c_light) mean_exp = 0. N = 10**4 maxTra = crpropa.MaximumTrajectoryLength(L_max) for i in range(N): c = crpropa.Candidate() c.current.setId(crpropa.nucleusId(1,1)) c.current.setEnergy(E) while c.getTrajectoryLength() < L_max: maxTra.process(c) self.Dif.process(c) x.append(c.current.getPosition().x) y.append(c.current.getPosition().y) z.append(c.current.getPosition().z) A2 = Anderson(z)['testStatistic'] self.assertLess(A2, 2.274, msg=self.msg1) meanX, meanY, meanZ = np.mean(x), np.mean(y), np.mean(z) stdZ = np.std(z) # no diffusion in perpendicular direction self.assertAlmostEqual(meanX, 0.) self.assertAlmostEqual(meanY, 0.) # diffusion in parallel direction # compare the mean and std of the z-positions with expected values # z_mean = 0. (no advection) # z_ std = (2*D_parallel*t)^0.5 # Take 4 sigma errors due to limited number of candidates into account stdOfMeans = std_exp/np.sqrt(N) stdOfStds = std_exp/np.sqrt(N)/np.sqrt(2.) self.assertLess(abs((meanZ-mean_exp)/4./stdOfMeans), 1., msg=self.msg1) self.assertLess(abs((stdZ-std_exp)/4./stdOfStds), 1., msg=self.msg1)
# You could also phrase: vertical incident angles are more frequent due to the # larger visible size of the area of the surface element than flat angles. # In[2]: get_ipython().magic(u'matplotlib inline') import matplotlib.pyplot as plt # source setup source = crpropa.Source() # inward=True for inwards directed emission and False for outwards directed emission center, radius, inward = crpropa.Vector3d( 0, 0, 0) * crpropa.kpc, 20 * crpropa.kpc, True source.add(crpropa.SourceLambertDistributionOnSphere(center, radius, inward)) source.add(crpropa.SourceParticleType(-crpropa.nucleusId(1, 1))) source.add(crpropa.SourceEnergy(100 * crpropa.EeV)) sim.run(source, n) print("Number of hits: %i" % len(output)) lons = [] for c in output: v = c.current.getDirection() lons.append(v.getPhi()) plt.hist(np.array(lons), bins=30, color='k', histtype='step') plt.xlabel('lon', fontsize=20) plt.ylabel('counts', fontsize=20) plt.savefig('lon_distribution_lamberts.png', bbox_inches='tight') plt.close()
crdata = np.genfromtxt("crpropa_output.txt") id = crdata[:, 0] E = crdata[:, 3] * crpropa.EeV E0 = crdata[:, 4] * crpropa.EeV px = crdata[:, 11] py = crdata[:, 12] pz = crdata[:, 13] galactic_longitude = np.arctan2(-1. * py, -1. * px) galactic_latitude = np.pi / 2 - np.arccos(-pz / (px * px + py * py + pz * pz)) M.addParticles(id, E, galactic_longitude, galactic_latitude, E0**-1) # Alternatively, data can be added manually. # This provides freedom to adapt to customized weights used in the simulation for i in xrange(1000): particleId = crpropa.nucleusId(1, 1) energy = 10 * crpropa.EeV galCenter = crpropa.Vector3d(-1, 0, 0) momentumVector = crpropa.Random.instance().randFisherVector(galCenter, 200) M.addParticle(particleId, energy, momentumVector) # ## Plot Maps # # The probability maps for the individual particles can be accessed directly and plotted with healpy. # # In[2]: get_ipython().magic('matplotlib inline') import healpy
import sys import healpy as hp a1, b1 = (12.6 - 18.25) / 2.75, (19.5 - 18.25) / 2.75 a2, b2 = (0. - 0.2) / 0.12, (1 - 0.2) / 0.12 a3, b3 = (0 - 10.97) / 3.80, (25. - 10.97) / 3.8 a4, b4 = (0 - 2.84) / 1.3, (8 - 2.84) / 1.3 E = float(sys.argv[1]) lon = float(sys.argv[2]) lat = float(sys.argv[3]) s1 = int(sys.argv[4]) s2 = int(sys.argv[5]) s3 = int(sys.argv[6]) pid = -crp.nucleusId(1, 1) sun = crp.Vector3d(-8.5, 0, 0) * crp.kpc E = E * crp.EeV nhat = hp.dir2vec(lon, lat, lonlat=True) direc = crp.Vector3d() direc.setXYZ(nhat[0], nhat[1], nhat[2]) ps = crp.ParticleState(pid, E, sun, direc) cr = crp.Candidate(ps) sim = crp.ModuleList() sim.add(crp.Redshift()) sim.add(crp.PhotoPionProduction(crp.CMB)) sim.add(crp.PhotoPionProduction(crp.IRB)) sim.add(crp.PhotoDisintegration(crp.CMB)) sim.add(crp.PhotoDisintegration(crp.IRB))
Id = [int(x) for x in crdata[:,0]] E = crdata[:,3] * crpropa.EeV E0 = crdata[:,4] * crpropa.EeV px = crdata[:,11] py = crdata[:,12] pz = crdata[:,13] galactic_longitude = np.arctan2(-1. * py, -1. *px) galactic_latitude = np.pi / 2 - np.arccos( -pz / (px*px + py*py+ pz*pz) ) M.addParticles(Id, E, galactic_longitude, galactic_latitude, E0**-1) # Alternatively, data can be added manually. # This provides freedom to adapt to customized weights used in the simulation for i in xrange(1000): particleId = crpropa.nucleusId(1,1) energy = 10 * crpropa.EeV galCenter = crpropa.Vector3d(-1,0,0) momentumVector = crpropa.Random.instance().randFisherVector(galCenter, 200) M.addParticle(particleId, energy, momentumVector) # ## Plot Maps # # The probability maps for the individual particles can be accessed directly and plotted with healpy. # # In[3]: get_ipython().magic(u'matplotlib inline')
# crpropa backtracking # set up GMF B = crp.JF12Field() B.randomStriated(seed) B.randomTurbulent(seed) # set up simulation sim = crp.ModuleList() sim.add(crp.PropagationCK(B, 1e-3, 0.1 * crp.parsec, 100 * crp.parsec)) obs = crp.Observer() obs.add(crp.ObserverLargeSphere(crp.Vector3d(0), gal_size * crp.kpc)) sim.add(obs) position = crp.Vector3d(-8.5, 0, 0) * crp.kpc # start at the earth pid = crp.nucleusId(1, 1) # proton pid *= -1 # antiparticle, for backtracking if read_in_galaxy_map: galaxy_map = np.genfromtxt("maps/galaxy_map.txt") else: galaxy_map = np.zeros(hp.nside2npix(nside)) for i in xrange(len(events)): if i % 10 == 0: print i, len(events) event = events[i] if event["is_Auger"]: mean_energy = event["E"] * crp.EeV omega = CR.omega_one_exp(event["dec"], True, ff) sigma = Auger_angular_sigma else: mean_energy = event["E"] * crp.EeV / E_scale
def test_FullTransport(self): x, y, z = [], [], [] E = 10 * TeV D = self.DifAdv.getScale()*6.1e24*(E/(4*GeV))**self.DifAdv.getAlpha() L_max = 50 * kpc epsilon = 0.1 advSpeed = 1e6 std_exp_x = np.sqrt(2*epsilon*D*L_max/c_light) std_exp_y = np.sqrt(2*epsilon*D*L_max/c_light) std_exp_z = np.sqrt(2*D*L_max/c_light) mean_exp_x = advSpeed * L_max / c_light mean_exp_y = 0. mean_exp_z = 0. N = 10**4 maxTra = crpropa.MaximumTrajectoryLength(L_max) ConstMagVec = crpropa.Vector3d(0*nG,0*nG,1*nG) BField = crpropa.UniformMagneticField(ConstMagVec) ConstAdvVec = crpropa.Vector3d(advSpeed, 0., 0.) AdvField = crpropa.UniformAdvectionField(ConstAdvVec) precision = 1e-4 minStep = 1*pc maxStep = 10*kpc DifAdv = crpropa.DiffusionSDE(BField, AdvField, precision, minStep, maxStep, epsilon) for i in range(N): c = crpropa.Candidate() c.current.setId(crpropa.nucleusId(1,1)) c.current.setEnergy(E) while c.getTrajectoryLength() < L_max: maxTra.process(c) DifAdv.process(c) x.append(c.current.getPosition().x) y.append(c.current.getPosition().y) z.append(c.current.getPosition().z) # test for normality A2 = Anderson(x)['testStatistic'] self.assertLess(A2, 2.274, msg=self.msg1) A2 = Anderson(y)['testStatistic'] self.assertLess(A2, 2.274, msg=self.msg1) A2 = Anderson(z)['testStatistic'] self.assertLess(A2, 2.274, msg=self.msg1) meanX, meanY, meanZ = np.mean(x), np.mean(y), np.mean(z) stdX, stdY, stdZ = np.std(x), np.std(y), np.std(z) # diffusion in parallel direction # compare the mean and std of the z-positions with expected values # z_mean = 0. (no advection) # z_ std = (2*D_parallel*t)^0.5 # Take 4 sigma errors due to limited number of candidates into account stdOfMeans_x = std_exp_x/np.sqrt(N) stdOfStds_x = std_exp_x/np.sqrt(N)/np.sqrt(2.) self.assertLess(abs((meanX-mean_exp_x)/4./stdOfMeans_x), 1., msg=self.msg1) self.assertLess(abs((stdX-std_exp_x)/4./stdOfStds_x), 1., msg=self.msg1) stdOfMeans_y = std_exp_y/np.sqrt(N) stdOfStds_y = std_exp_y/np.sqrt(N)/np.sqrt(2.) self.assertLess(abs((meanY-mean_exp_y)/4./stdOfMeans_y), 1., msg=self.msg1) self.assertLess(abs((stdY-std_exp_y)/4./stdOfStds_y), 1., msg=self.msg1) stdOfMeans_z = std_exp_z/np.sqrt(N) stdOfStds_z = std_exp_z/np.sqrt(N)/np.sqrt(2.) self.assertLess(abs((meanZ-mean_exp_z)/4./stdOfMeans_z), 1., msg=self.msg1) self.assertLess(abs((stdZ-std_exp_z)/4./stdOfStds_z), 1., msg=self.msg1)