def testParticleCollectorAsModuleListInput(self): sim = crp.ModuleList() sim.add(crp.MaximumTrajectoryLength(3.14)) sim.add(crp.SimplePropagation(0.001, 0.001)) collector = crp.ParticleCollector() c1 = crp.Candidate() c2 = crp.Candidate() collector.process(c1) collector.process(c2) sim.run(collector.getContainer()) for c in collector: self.assertAlmostEqual(c.getTrajectoryLength(), 3.14, places=2)
def test_ParticleCollector(self): c = crp.Candidate() p = crp.ParticleCollector() p.process(c) c_out = p[0] for c_i in p: c_out = c_i
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 testParticleCollectorAsModuleListOutput(self): sim = crp.ModuleList() sim.add(crp.MaximumTrajectoryLength(3.14)) sim.add(crp.SimplePropagation(0.001, 0.001)) collector = crp.ParticleCollector() sim.add(collector) c = crp.Candidate() sim.run(c) self.assertAlmostEqual(collector[0].getTrajectoryLength(), 3.14, places=2)
def testParticleCollectorIterator(self): collector = crp.ParticleCollector() lengths = [1 * crp.pc, 10 * crp.pc, 100 * crp.pc] for l in lengths: c = crp.Candidate() c.setTrajectoryLength(l) collector.process(c) self.assertEqual(len(collector), len(lengths)) for c, l in zip(collector, lengths): self.assertEqual(c.getTrajectoryLength(), l)
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 test_ObserverFeature(self): class CountingFeature(crp.ObserverFeature): def __init__(self): crp.ObserverFeature.__init__(self) self.value = 0 def checkDetection(self, candidate): self.value += 1 return crp.DETECTED obs = crp.Observer() counter = CountingFeature() obs.add(counter) for i in range(5): candidate = crp.Candidate() obs.process(candidate) self.assertEqual(i + 1, counter.value)
def test_module(self): class CountingModule(crp.Module): def __init__(self): crp.Module.__init__(self) self.count = 0 def process(self, c): self.count += 1 count_accept = CountingModule() count_reject = CountingModule() filter = crp.ParticleFilter([-1, 1]) filter.onAccept(count_accept) filter.onReject(count_reject) c = crp.Candidate() for id in [-1, 1, 6, 9, -19, 23, 100010001]: c.current.setId(id) filter.process(c)
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)
def setUp(self): self.candidate = crp.Candidate()
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)) sim.add(crp.NuclearDecay()) sim.add(crp.ElectronPairProduction(crp.CMB)) sim.add(crp.ElectronPairProduction(crp.IRB)) np.random.seed(s1) p1 = ss.truncnorm.rvs(a1, b1, 18.25, 2.75, size=1)[0] p2 = ss.truncnorm.rvs(a2, b2, 0.2, 0.12, size=1)[0] p3 = ss.truncnorm.rvs(a3, b3, 10.97, 3.80, size=1)[0] p4 = ss.truncnorm.rvs(a4, b4, 2.84, 1.30, size=1)[0] Bfield = crp.JF12Field(s1, p1, p2, p3, p4)
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 omega = CR.omega_one_exp(event["dec"], False, ff) sigma = TA_angular_sigma sigma_energy = 0.2 * mean_energy # see fig 19 in 1407.3214 for Auger, bottom of page 5 of 1404.5890 for TA mean_direction = crp.Vector3d() mean_direction.setRThetaPhi(1, np.pi / 2 - event["b"], 1.0 * event["l"]) for i in xrange(int(nrepeat_backtrack)): energy = rng.randNorm(mean_energy, sigma_energy) initial_direction = rng.randVectorAroundMean(mean_direction, sigma) c = crp.Candidate( crp.ParticleState(pid, energy, position, initial_direction)) sim.run(c) final_direction = c.current.getDirection() galaxy_map[hp.pixelfunc.ang2pix( nside, final_direction.getTheta(), final_direction.getPhi())] += 1. / (nrepeat_backtrack * omega) np.savetxt("maps/galaxy_map.txt", galaxy_map) hp.visufunc.mollview(galaxy_map, cbar=False, title=r"${\rm Data\ Outside\ the\ Galaxy}$") draw_map_lines() plt.savefig("fig/galaxy_data.eps", bbox_inches="tight") plt.clf()
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)