Ejemplo n.º 1
0
def test_compute_distance2line(packet_params, expected_params):
    r = 7.5e14
    mu = 0.3
    nu = 0.4
    energy = 0.9
    packet = r_packet.RPacket(r, mu, nu, energy)
    nu_line = packet_params["nu_line"]
    # packet.next_line_id = packet_params['next_line_id']
    # packet.last_line = packet_params['last_line']

    time_explosion = 5.2e7

    doppler_factor = get_doppler_factor(packet.r, packet.mu, time_explosion)
    comov_nu = packet.nu * doppler_factor

    d_line = 0
    obtained_tardis_error = None
    try:
        d_line = r_packet.calculate_distance_line(packet, comov_nu, nu_line,
                                                  time_explosion)
    except utils.MonteCarloException:
        obtained_tardis_error = utils.MonteCarloException

    assert_almost_equal(d_line, expected_params["d_line"])
    assert obtained_tardis_error == expected_params["tardis_error"]
Ejemplo n.º 2
0
def test_move_packet_across_shell_boundary_emitted(packet, current_shell_id,
                                                   delta_shell, no_of_shells):
    packet = r_packet.RPacket(packet.r, packet.mu, packet.nu, packet.energy)
    packet.current_shell_id = current_shell_id
    r_packet.move_packet_across_shell_boundary(packet, delta_shell,
                                               no_of_shells)
    assert packet.status == r_packet.PacketStatus.EMITTED
Ejemplo n.º 3
0
def test_packet_energy_limit_one(distance_trace, time_explosion, mu, r):
    initial_energy = 0.9
    nu = 0.4
    packet = r_packet.RPacket(r, mu, nu, initial_energy)
    new_energy = r_packet.calc_packet_energy(packet, distance_trace,
                                             time_explosion)
    assert new_energy == initial_energy
Ejemplo n.º 4
0
def test_rpacket_tracking(index, seed, r, nu, mu, energy):
    # Setup Montecarlo_Configuration.INITIAL_TRACKING_ARRAY_LENGTH
    mc.INITIAL_TRACKING_ARRAY_LENGTH = 10

    tracked_rpacket_properties = RPacketTracker()
    test_rpacket = r_packet.RPacket(
        index=index,
        seed=seed,
        r=r,
        nu=nu,
        mu=mu,
        energy=energy,
    )

    # TearDown Montecarlo_Configuration.INITIAL_TRACKING_ARRAY_LENGTH
    mc.INITIAL_TRACKING_ARRAY_LENGTH = None

    tracked_rpacket_properties.track(test_rpacket)
    tracked_rpacket_properties.finalize_array()

    assert test_rpacket.index == tracked_rpacket_properties.index
    assert test_rpacket.seed == tracked_rpacket_properties.seed
    assert test_rpacket.r == tracked_rpacket_properties.r
    assert test_rpacket.nu == tracked_rpacket_properties.nu
    assert test_rpacket.mu == tracked_rpacket_properties.mu
    assert test_rpacket.energy == tracked_rpacket_properties.energy
    assert tracked_rpacket_properties.interact_id == 1
Ejemplo n.º 5
0
def test_move_packet_across_shell_boundary_increment(packet, current_shell_id,
                                                     delta_shell,
                                                     no_of_shells):
    packet = r_packet.RPacket(packet.r, packet.mu, packet.nu, packet.energy)
    packet.current_shell_id = current_shell_id
    r_packet.move_packet_across_shell_boundary(packet, delta_shell,
                                               no_of_shells)
    assert packet.current_shell_id == current_shell_id + delta_shell
Ejemplo n.º 6
0
def test_angle_transformation_invariance(mu, r, inv_t_exp):
    packet = r_packet.RPacket(r, mu, 0.4, 0.9)
    mc.full_relativity = True

    mu1 = angle_aberration_CMF_to_LF(packet, 1 / inv_t_exp, mu)
    mu_obtained = angle_aberration_LF_to_CMF(packet, 1 / inv_t_exp, mu1)

    mc.full_relativity = False
    assert_almost_equal(mu_obtained, mu)
Ejemplo n.º 7
0
def test_angle_transformation_invariance(packet, model, mu, r, inv_t_exp):
    packet = r_packet.RPacket(r, mu, packet.nu, packet.energy)
    model.full_relativity = 1

    mu1 = r_packet.angle_aberration_CMF_to_LF(packet, 1 / inv_t_exp, mu)
    mu_obtained = r_packet.angle_aberration_LF_to_CMF(packet, 1 / inv_t_exp,
                                                      mu1)

    assert_almost_equal(mu_obtained, mu)
Ejemplo n.º 8
0
def test_angle_ab_LF_to_CMF_diverge(mu, r, time_explosion):
    """
    This equation should diverage as costheta --> 1 and beta --> 1
    """
    nu = 0.4
    energy = 0.9
    packet = r_packet.RPacket(r, mu, nu, energy)
    with pytest.raises(ZeroDivisionError):
        obtained = r_packet.angle_aberration_LF_to_CMF(packet, time_explosion,
                                                       mu)
Ejemplo n.º 9
0
def test_angle_ab_LF_to_CMF_diverge(mu, r, time_explosion, packet):
    """
    This equation should diverage as costheta --> 1 and beta --> 1
    """
    packet = r_packet.RPacket(packet.r, packet.mu, packet.nu, packet.energy)
    packet.r = r
    packet.mu = mu
    with pytest.raises(ZeroDivisionError):
        obtained = r_packet.angle_aberration_LF_to_CMF(packet, time_explosion,
                                                       mu)
Ejemplo n.º 10
0
def test_move_packet_across_shell_boundary_emitted(current_shell_id,
                                                   delta_shell, no_of_shells):
    r = 7.5e14
    mu = 0.3
    nu = 0.4
    energy = 0.9
    packet = r_packet.RPacket(r, mu, nu, energy)
    packet.current_shell_id = current_shell_id
    r_packet.move_packet_across_shell_boundary(packet, delta_shell,
                                               no_of_shells)
    assert packet.status == r_packet.PacketStatus.EMITTED
Ejemplo n.º 11
0
def test_move_packet_across_shell_boundary_increment(current_shell_id,
                                                     delta_shell,
                                                     no_of_shells):
    r = 7.5e14
    mu = 0.3
    nu = 0.4
    energy = 0.9
    packet = r_packet.RPacket(r, mu, nu, energy)
    packet.current_shell_id = current_shell_id
    r_packet.move_packet_across_shell_boundary(packet, delta_shell,
                                               no_of_shells)
    assert packet.current_shell_id == current_shell_id + delta_shell
Ejemplo n.º 12
0
def test_both_angle_aberrations_inverse(mu, r, time_explosion, packet):
    """
    The angle aberration functions should be the functional inverse of one
    another.
    """
    packet = r_packet.RPacket(r, packet.mu, packet.nu, packet.energy)
    packet.r = r
    obtained_mu = r_packet.angle_aberration_CMF_to_LF(packet, time_explosion,
                                                      mu)
    inverse_obtained_mu = r_packet.angle_aberration_LF_to_CMF(
        packet, time_explosion, obtained_mu)
    assert_almost_equal(inverse_obtained_mu, mu)
Ejemplo n.º 13
0
def test_move_packet_across_shell_boundary_reabsorbed(current_shell_id,
                                                      delta_shell,
                                                      no_of_shells):
    r = 7.5e14
    mu = 0.3
    nu = 0.4
    energy = 0.9
    packet = r_packet.RPacket(r, mu, nu, energy)
    packet.current_shell_id = current_shell_id
    r_packet_transport.move_packet_across_shell_boundary(
        packet, delta_shell, no_of_shells)
    assert packet.status == r_packet.PacketStatus.REABSORBED
Ejemplo n.º 14
0
def test_macro_atom(packet, z_random, packet_params, get_rkstate, expected):
    packet.macro_atom_activation_level = packet_params["activation_level"]
    packet = r_packet.RPacket(r=packet.r,
                              mu=packet.mu,
                              nu=packet.nu,
                              energy=packet.energy)
    packet.current_shell_id = packet_params["shell_id"]
    rkstate = get_rkstate(z_random)

    macro_atom.macro_atom(packet, numba_plasma)
    obtained_line_id = model_3lvlatom.last_line_interaction_out_id[packet.id]

    assert_equal(obtained_line_id, expected)
Ejemplo n.º 15
0
def test_frame_transformations(mu, r, inv_t_exp, full_relativity):
    packet = r_packet.RPacket(r=r, mu=mu, energy=0.9, nu=0.4)
    mc.full_relativity = bool(full_relativity)
    mc.full_relativity = full_relativity

    inverse_doppler_factor = r_packet.get_inverse_doppler_factor(
        r, mu, 1 / inv_t_exp)
    r_packet.angle_aberration_CMF_to_LF(packet, 1 / inv_t_exp, packet.mu)

    doppler_factor = get_doppler_factor(r, mu, 1 / inv_t_exp)
    mc.full_relativity = False

    assert_almost_equal(doppler_factor * inverse_doppler_factor, 1.0)
Ejemplo n.º 16
0
def test_move_packet(packet_params, expected_params, full_relativity):
    distance = 1e13
    r, mu, nu, energy = 7.5e14, 0.3, 0.4, 0.9
    time_explosion = 5.2e7
    packet = r_packet.RPacket(r, mu, nu, energy)
    packet.nu = packet_params["nu"]
    packet.mu = packet_params["mu"]
    packet.energy = packet_params["energy"]
    packet.r = packet_params["r"]
    # model.full_relativity = full_relativity
    mc.full_relativity = full_relativity

    doppler_factor = get_doppler_factor(packet.r, packet.mu, time_explosion)
    numba_estimator = Estimators(packet_params["j"], packet_params["nu_bar"],
                                 0, 0)
    r_packet_transport.move_r_packet(packet, distance, time_explosion,
                                     numba_estimator)

    assert_almost_equal(packet.mu, expected_params["mu"])
    assert_almost_equal(packet.r, expected_params["r"])

    expected_j = expected_params["j"]
    expected_nubar = expected_params["nubar"]
    if full_relativity:
        expected_j *= doppler_factor
        expected_nubar *= doppler_factor

    mc.full_relativity = False

    assert_allclose(
        numba_estimator.j_estimator[packet.current_shell_id],
        expected_j,
        rtol=5e-7,
    )
    assert_allclose(
        numba_estimator.nu_bar_estimator[packet.current_shell_id],
        expected_nubar,
        rtol=5e-7,
    )
Ejemplo n.º 17
0
def test_compute_distance2line(packet_params, expected_params, packet, model):
    packet = r_packet.RPacket(packet.r, packet.mu, packet.nu, packet.energy)
    nu_line = packet_params["nu_line"]
    # packet.next_line_id = packet_params['next_line_id']
    # packet.last_line = packet_params['last_line']

    time_explosion = model.time_explosion

    doppler_factor = r_packet.get_doppler_factor(packet.r, packet.mu,
                                                 time_explosion)
    comov_nu = packet.nu * doppler_factor

    d_line = 0
    obtained_tardis_error = None
    try:
        d_line = r_packet.calculate_distance_line(packet, comov_nu, nu_line,
                                                  time_explosion)
    except r_packet.MonteCarloException:
        obtained_tardis_error = r_packet.MonteCarloException

    assert_almost_equal(d_line, expected_params["d_line"])
    assert obtained_tardis_error == expected_params["tardis_error"]
Ejemplo n.º 18
0
def test_compute_distance2line_relativistic(mu, r, t_exp, nu, nu_line,
                                            full_relativity):
    packet = r_packet.RPacket(r=r, nu=nu, mu=mu, energy=0.9)
    # packet.nu_line = nu_line
    numba_estimator = Estimators(
        runner.j_estimator,
        runner.nu_bar_estimator,
        runner.j_blue_estimator,
        runner.Edotlu_estimator,
    )
    mc.full_relativity = bool(full_relativity)

    doppler_factor = get_doppler_factor(r, mu, t_exp)
    comov_nu = packet.nu * doppler_factor
    distance = r_packet.calculate_distance_line(packet, comov_nu, nu_line,
                                                t_exp)
    r_packet.move_r_packet(packet, distance, t_exp, numba_estimator)

    doppler_factor = get_doppler_factor(r, mu, t_exp)
    comov_nu = packet.nu * doppler_factor
    mc.full_relativity = False

    assert_allclose(comov_nu, nu_line, rtol=1e-14)
Ejemplo n.º 19
0
def test_montecarlo_main_loop(
    config_verysimple,
    atomic_dataset,
    tardis_ref_path,
    tmpdir,
    set_seed_fixture,
    random_call_fixture,
):

    montecarlo_configuration.LEGACY_MODE_ENABLED = True

    # Load C data from refdata
    C_fname = os.path.join(tardis_ref_path, "montecarlo_1e5_compare_data.h5")
    expected_nu = pd.read_hdf(C_fname,
                              key="/simulation/runner/output_nu").values
    expected_energy = pd.read_hdf(
        C_fname, key="/simulation/runner/output_energy").values
    expected_nu_bar_estimator = pd.read_hdf(
        C_fname, key="/simulation/runner/nu_bar_estimator").values
    expected_j_estimator = pd.read_hdf(
        C_fname, key="/simulation/runner/j_estimator").values

    # Setup model config from verysimple
    atomic_data = deepcopy(atomic_dataset)
    config_verysimple.montecarlo.last_no_of_packets = 1e5
    config_verysimple.montecarlo.no_of_virtual_packets = 0
    config_verysimple.montecarlo.iterations = 1
    config_verysimple.montecarlo.single_packet_seed = 0
    del config_verysimple["config_dirname"]

    sim = Simulation.from_config(config_verysimple, atom_data=atomic_data)

    # Init model
    numba_plasma = numba_plasma_initialize(sim.plasma,
                                           line_interaction_type="macroatom")

    runner = sim.runner
    model = sim.model

    runner._initialize_geometry_arrays(model)
    runner._initialize_estimator_arrays(numba_plasma.tau_sobolev.shape)
    runner._initialize_packets(model.t_inner.value, 100000, 0)

    # Init parameters
    montecarlo_configuration.v_packet_spawn_start_frequency = (
        runner.virtual_spectrum_spawn_range.end.to(
            u.Hz, equivalencies=u.spectral()).value)
    montecarlo_configuration.v_packet_spawn_end_frequency = (
        runner.virtual_spectrum_spawn_range.start.to(
            u.Hz, equivalencies=u.spectral()).value)
    montecarlo_configuration.temporary_v_packet_bins = 20000
    montecarlo_configuration.full_relativity = runner.enable_full_relativity
    montecarlo_configuration.single_packet_seed = 0

    # Init packet collection from runner
    packet_collection = PacketCollection(
        runner.input_nu,
        runner.input_mu,
        runner.input_energy,
        runner._output_nu,
        runner._output_energy,
    )

    # Init model from runner
    numba_model = NumbaModel(
        runner.r_inner_cgs,
        runner.r_outer_cgs,
        model.time_explosion.to("s").value,
    )

    # Init estimators from runner
    estimators = Estimators(
        runner.j_estimator,
        runner.nu_bar_estimator,
        runner.j_blue_estimator,
        runner.Edotlu_estimator,
    )

    # Empty vpacket collection
    vpacket_collection = VPacketCollection(0, np.array([0, 0],
                                                       dtype=np.float64), 0,
                                           np.inf, 0, 0)

    # output arrays
    output_nus = np.empty_like(packet_collection.packets_output_nu)
    output_energies = np.empty_like(packet_collection.packets_output_nu)

    # IMPORTANT: seeds RNG state within JIT
    seed = 23111963
    set_seed_fixture(seed)
    for i in range(len(packet_collection.packets_input_nu)):
        # Generate packet
        packet = r_packet.RPacket(
            numba_model.r_inner[0],
            packet_collection.packets_input_mu[i],
            packet_collection.packets_input_nu[i],
            packet_collection.packets_input_energy[i],
            seed,
            i,
            0,
        )

        # Loop packet
        spl.single_packet_loop(packet, numba_model, numba_plasma, estimators,
                               vpacket_collection)
        output_nus[i] = packet.nu
        if packet.status == r_packet.PacketStatus.REABSORBED:
            output_energies[i] = -packet.energy
        elif packet.status == r_packet.PacketStatus.EMITTED:
            output_energies[i] = packet.energy

        # RNG to match C
        random_call_fixture()

    packet_collection.packets_output_energy[:] = output_energies[:]
    packet_collection.packets_output_nu[:] = output_nus[:]

    actual_energy = packet_collection.packets_output_energy
    actual_nu = packet_collection.packets_output_nu
    actual_nu_bar_estimator = estimators.nu_bar_estimator
    actual_j_estimator = estimators.j_estimator

    # Compare
    npt.assert_allclose(actual_nu_bar_estimator,
                        expected_nu_bar_estimator,
                        rtol=1e-13)
    npt.assert_allclose(actual_j_estimator, expected_j_estimator, rtol=1e-13)
    npt.assert_allclose(actual_energy, expected_energy, rtol=1e-13)
    npt.assert_allclose(actual_nu, expected_nu, rtol=1e-13)