Exemple #1
0
def test_trajectories_unvectorized():
    '''
    Test the final times of the trajectory evaluation in the igrf field for 
    the unvectorized Runge Kutta version
    '''

    expected_times = [
        2e-05, 0.2999300000001592, 0.19221000000005145, 0.20289000000006213,
        0.21144000000007068, 0.2024600000000617, 0.19869000000005793,
        0.2169600000000762, 0.19499000000005423, 0.23231000000009155,
        0.007359999999999868, 0.019439999999999378, 0.19331000000005255
    ]

    dt = 1e-5
    max_time = 1.

    for iexp, initial_variables in enumerate(initial_variable_list):

        (plabel, zenith, azimuth, palt, lat, lng, dalt, rig,
         en) = initial_variables

        traj = Trajectory(plabel=plabel,
                          zenith_angle=zenith,
                          azimuth_angle=azimuth,
                          particle_altitude=palt,
                          latitude=lat,
                          longitude=lng,
                          detector_altitude=dalt,
                          rigidity=rig,
                          energy=en,
                          bfield_type="igrf")

        traj.get_trajectory(dt=dt, max_time=max_time, use_unvectorized=True)

        assert np.allclose(traj.final_time, expected_times[iexp])
Exemple #2
0
def test_trajectories_igrf():
    '''
    Test the final times of the trajectory evaluation in the IGRF field.
    '''

    expected_times = [
        1e-05, 0.29990000000015915, 0.19221000000005145, 0.20288000000006212,
        0.21143000000007067, 0.2024600000000617, 0.19868000000005792,
        0.2169700000000762, 0.19499000000005423, 0.23231000000009155,
        0.007349999999999869, 0.01941999999999938, 0.19331000000005255
    ]

    dt = 1e-5
    max_time = 1.

    for iexp, initial_variables in enumerate(initial_variable_list):

        (plabel, zenith, azimuth, palt, lat, lng, dalt, rig,
         en) = initial_variables

        traj = Trajectory(plabel=plabel,
                          zenith_angle=zenith,
                          azimuth_angle=azimuth,
                          particle_altitude=palt,
                          latitude=lat,
                          longitude=lng,
                          detector_altitude=dalt,
                          rigidity=rig,
                          energy=en,
                          bfield_type="igrf")

        traj.get_trajectory(dt=dt, max_time=max_time)

        assert np.allclose(traj.final_time, expected_times[iexp])
Exemple #3
0
def test_trajectories_maxtimes():
    '''
    Test the final times of the trajectory evaluation in the igrf field for 
    different maximal times
    '''

    expected_times = [
        0.00999999999999976, 0.027829999999999036, 0.07743000000000268,
        0.2154500000000747, 0.22074000000007998, 0.22074000000007998,
        0.22074000000007998, 0.22074000000007998, 0.22074000000007998,
        0.22074000000007998
    ]

    dt = 1e-5
    max_times = np.logspace(-2, 2, 10)

    for iexp, max_time in enumerate(max_times):

        (plabel, zenith, azimuth, palt, lat, lng, dalt, rig,
         en) = ("p+", 90., 0., 100., 0., 0., 0., 50., None)

        traj = Trajectory(plabel=plabel,
                          zenith_angle=zenith,
                          azimuth_angle=azimuth,
                          particle_altitude=palt,
                          latitude=lat,
                          longitude=lng,
                          detector_altitude=dalt,
                          rigidity=rig,
                          energy=en,
                          bfield_type="igrf")

        traj.get_trajectory(dt=dt, max_time=max_time)

        assert np.allclose(traj.final_time, expected_times[iexp])
Exemple #4
0
def test_trajectories_stepsize():
    '''
    Test the final times of the trajectory evaluation in the igrf field for 
    different step sizes
    '''

    expected_times = [
        0.22073792992447885, 0.22073800000531751, 0.22073800000020008,
        0.22074000000007998, 0.220799999999992, 0.22100000000000017,
        0.23000000000000007, 0.30000000000000004
    ]

    dt_arr = [1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1]
    max_time = 1.

    (plabel, zenith, azimuth, palt, lat, lng, dalt, rig,
     en) = ("p+", 90., 0., 100., 0., 0., 0., 50., None)

    for iexp, dt in enumerate(dt_arr):

        traj = Trajectory(plabel=plabel,
                          zenith_angle=zenith,
                          azimuth_angle=azimuth,
                          particle_altitude=palt,
                          latitude=lat,
                          longitude=lng,
                          detector_altitude=dalt,
                          rigidity=rig,
                          energy=en,
                          bfield_type="igrf")

        traj.get_trajectory(dt=dt, max_time=max_time)

        assert np.allclose(traj.final_time, expected_times[iexp])
Exemple #5
0
def get_evaltime(iter_num,
                 initial_variables,
                 bfield_type,
                 use_unvec=False,
                 use_python=False):
    '''
    Evaluates iter_num number of iterations of the same trajectory calculation and return the average evaluation time for those number of iterations

    Parameters
    ----------

    - iter_num : int
        the number of iterations to evaluate for 
    - initial_variables : tuple
        initial parameters to initialize trajectory
    - bfield_type : str
        type of magnetic field model to use
    - use_unvec : bool
        whether to use unvectorized version of Trajectory evaluator or not (default False)
    - use_python : bool
        whether to use Python version of Trajectory evaluator or not (default False)
    '''

    plabel, zenith, azimuth, part_alt, lat, lng, dec_alt, rig = initial_variables

    trajectory = Trajectory(plabel=plabel,
                            latitude=lat,
                            longitude=lng,
                            detector_altitude=dec_alt,
                            zenith_angle=zenith,
                            azimuth_angle=azimuth,
                            particle_altitude=part_alt,
                            rigidity=rig,
                            bfield_type=bfield_type)

    eval_time = 0.  # initialize evaluation time
    dt = 1e-5  # step size in integration
    max_step = 10000  # max steps in integration

    for i in range(int(np.floor(iter_num))):
        # start counter
        # perf counter for more precise time evaluations
        start_time = time.perf_counter()
        trajectory.get_trajectory(dt=dt,
                                  max_step=max_step,
                                  use_python=use_python,
                                  use_unvectorized=use_unvec)
        stop_time = time.perf_counter()
        eval_time += stop_time - start_time

    # evaluate average
    avg_evaltime = eval_time / iter_num

    return avg_evaltime
Exemple #6
0
    def evaluate(self, dt=1e-5, max_time=1):
        '''
        Evaluate the rigidity cutoff value at some provided location
        on Earth for a given cosmic ray particle.

        Parameters
        ----------

        - dt : float
            The stepsize of each trajectory evaluation (default = 1e-5)
        - max_time : float
            The maximal time of each trajectory evaluation (default = 1.).

        '''

        # perform Monte Carlo integration to get cutoff rigidity
        for i in tqdm(range(self.iter_num)):
            # get a random zenith and azimuth angle
            # zenith angles range from 0 to 180
            # azimuth angles range from 0 to 360
            [azimuth, zenith] = np.random.rand(2)
            azimuth *= 360.
            zenith *= 180.

            # iterate through each rigidity, and break the loop
            # when particle is able to escape earth
            for rigidity in self.rigidity_list:

                traj = Trajectory(plabel=self.plabel,
                                  location_name=self.location,
                                  zenith_angle=zenith,
                                  azimuth_angle=azimuth,
                                  particle_altitude=100.,
                                  rigidity=rigidity,
                                  bfield_type=self.bfield_type,
                                  date=self.date)

                traj.get_trajectory(dt=dt, max_time=max_time)
                # break loop and append direction and current rigidity if particle has escaped

                if traj.particle_escaped == True:
                    # self.azimuth_arr[i] = azimuth
                    # self.zenith_arr[i] = zenith
                    # self.rcutoff_arr[i] = rigidity
                    self.data_dict["azimuth"][i] = azimuth
                    self.data_dict["zenith"][i] = zenith
                    self.data_dict["rcutoff"][i] = rigidity
                    # self.rcutoff_arr.append((azimuth, zenith, rig))
                    break
Exemple #7
0
def test_trajectories_dates():
    '''
    Test the final times of the trajectory evaluation in the igrf field for 
    different dates
    '''

    expected_times = [
        0.22074000000007998, 0.22074000000007998, 0.22074000000007998,
        0.22074000000007998, 0.22074000000007998, 0.22074000000007998,
        0.22074000000007998, 0.22074000000007998, 0.22074000000007998,
        0.22074000000007998
    ]

    dt = 1e-5
    max_time = 1.

    dates = [
        "1900-01-01", "1909-01-01", "1900-10-31", "2020-09-12", "2004-03-08",
        "2000-02-28", "1970-03-26", "1952-04-31", "1999-03-08", "2024-03-09"
    ]
    for iexp, date in enumerate(dates):

        (plabel, zenith, azimuth, palt, lat, lng, dalt, rig,
         en) = ("p+", 90., 0., 100., 0., 0., 0., 50., None)

        traj = Trajectory(plabel=plabel,
                          zenith_angle=zenith,
                          azimuth_angle=azimuth,
                          particle_altitude=palt,
                          latitude=lat,
                          longitude=lng,
                          detector_altitude=dalt,
                          rigidity=rig,
                          energy=en,
                          bfield_type="igrf",
                          date=date)

        traj.get_trajectory(dt=dt, max_time=max_time)

        assert np.allclose(traj.final_time, expected_times[iexp])
Exemple #8
0
def get_trajectory():
    # set parameters

    # parameters for trajectory
    # particle is assumed to be proton
    q = 1
    plabel = "p+"

    # initial momentum
    p0 = 30.
    rigidity = p0 / np.abs(q)  # convert to rigidity

    # location of detector
    lat = 10.
    lng = 40.
    detector_alt = 0.

    # 3-vector of particle
    zenith = 90.
    azimuth = 0.
    particle_alt = 100.

    # set integration parameters
    dt = 1e-5
    max_time = 1.
    max_step = 10000

    # control variables for the code
    check_pmag = True  # if we want to check the momentum magnitude
    check_3dtraj = True  # if we want to check the 3d trajectory or not
    show_plot = False  # if we want to show the plot on some GUI or not

    # first create plot directory if it doesnt exist
    if not os.path.exists(PLOT_DIR):
        os.mkdir(PLOT_DIR)

    # initialize trajectory
    traj = Trajectory(plabel=plabel,
                      zenith_angle=zenith,
                      azimuth_angle=azimuth,
                      particle_altitude=particle_alt,
                      latitude=lat,
                      longitude=lng,
                      detector_altitude=detector_alt,
                      rigidity=rigidity,
                      bfield_type="igrf")

    # obtain the trajectory result
    traj_datadict = traj.get_trajectory(dt=dt,
                                        max_time=max_time,
                                        get_data=True,
                                        max_step=max_step,
                                        use_python=False,
                                        use_unvectorized=False)

    # convert lat, long in decimal notation to dms
    lat_dms, lng_dms = dec_to_dms(lat, lng)

    title = "Particle Trajectory at {:s}, {:s} with Zenith Angle {:.1f}°, \
           \n Azimuth Angle {:.1f}° and Rigidity R = {:.1f}GV".format(
        lat_dms, lng_dms, zenith, azimuth, rigidity)

    # get momentum only if check_pmag is true
    if check_pmag:
        plot_traj_momentum(traj_datadict, p0, show_plot)

    # plot the trajectory
    plot_trajectory(traj_datadict, title, check_3dtraj, show_plot)