示例#1
0
 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)
示例#2
0
 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)
示例#3
0
    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)
示例#4
0
import crpropa
import CRPropaROOTOutput

print("My Simulation\n")

ml = crpropa.ModuleList()

ml.add(crpropa.SimplePropagation(1 * crpropa.parsec, 100 * crpropa.parsec))
ml.add(crpropa.MaximumTrajectoryLength(1000 * crpropa.parsec))

print("+++ List of modules")
print(ml.getDescription())

print("+++ Preparing source")
source = crpropa.Source()
print(source.getDescription())

print("+++ Starting Simulation")
ml.run(source, 1)

print("+++ Done")

# As test, we propagate some particels in a random field with a sheet observer:

# In[2]:

from crpropa import Mpc, nG, EeV
import numpy as np

vgrid = crpropa.VectorGrid(crpropa.Vector3d(0), 128, 1 * Mpc)
crpropa.initTurbulence(vgrid, 1 * nG, 2 * Mpc, 5 * Mpc, -11. / 3.)
BField = crpropa.MagneticFieldGrid(vgrid)

m = crpropa.ModuleList()
m.add(crpropa.PropagationCK(BField, 1e-4, 0.1 * Mpc, 5 * Mpc))
m.add(crpropa.MaximumTrajectoryLength(25 * Mpc))

# Observer
out = crpropa.TextOutput("sheet.txt")
o = crpropa.Observer()
# The Observer feature has to be created outside of the class attribute
# o.add(ObserverPlane(...)) will not work for custom python modules
plo = ObserverPlane(
    np.asarray([0., 0, 0]) * Mpc,
    np.asarray([0., 1., 0.]) * Mpc,
    np.asarray([0., 0., 1.]) * Mpc)
o.add(plo)
o.setDeactivateOnDetection(False)
o.onDetection(out)
m.add(o)
示例#6
0
    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)