Skip to content

Commit

Permalink
Refactor fixtures and test_rpacket_doppler_factor.
Browse files Browse the repository at this point in the history
  • Loading branch information
karandesai-96 committed Apr 11, 2016
1 parent ab52169 commit f837c80
Showing 1 changed file with 69 additions and 73 deletions.
142 changes: 69 additions & 73 deletions tardis/montecarlo/tests/test_cmontecarlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
- `packet_params`
- `model_params`
- `expected_params` (`expected` if only one value to be asserted.)
- Suggested variable names can be compromised if readability of the test
increases.
2. Test Method body:
- Keep name as `test_` + `(name of C method)`.
Expand Down Expand Up @@ -62,112 +64,106 @@
@pytest.fixture(scope="function")
def packet():
"""Fixture to return `RPacket` object with default params initialized."""
packet_default = {
'nu': 0.4,
'mu': 0.3,
'energy': 0.9,
'r': 7.5e14,
'tau_event': 2.9e13,
'nu_line': 0.2,
'current_shell_id': 0,
'next_line_id': 1,
'last_line': 0,
'close_line': 0,
'current_continuum_id': 1,
'virtual_packet_flag': 1,
'virtual_packet': 0,
'status': 0,
'id': 0
}
return RPacket(**packet_default)
return RPacket(
nu=0.4,
mu=0.3,
energy=0.9,
r=7.5e14,
tau_event=2.9e13,
nu_line=0.2,
current_shell_id=0,
next_line_id=1,
last_line=0,
close_line=0,
current_continuum_id=1,
virtual_packet_flag=1,
virtual_packet=0,
status=0,
id=0
)


@pytest.fixture(scope="function")
def model():
"""Fixture to return `StorageModel` object with default params initialized."""
model_default = {
'last_line_interaction_in_id': (c_int64 * 2)(*([0] * 2)),
'last_line_interaction_shell_id': (c_int64 * 2)(*([0] * 2)),
'last_line_interaction_type': (c_int64 * 2)(*([2])),
return StorageModel(
last_line_interaction_in_id=(c_int64 * 2)(*([0] * 2)),
last_line_interaction_shell_id=(c_int64 * 2)(*([0] * 2)),
last_line_interaction_type=(c_int64 * 2)(*([2])),

'no_of_shells': 2,
no_of_shells=2,

'r_inner': (c_double * 2)(*[6.912e14, 8.64e14]),
'r_outer': (c_double * 2)(*[8.64e14, 1.0368e15]),
r_inner=(c_double * 2)(*[6.912e14, 8.64e14]),
r_outer=(c_double * 2)(*[8.64e14, 1.0368e15]),

'time_explosion': 5.2e7,
'inverse_time_explosion': 1 / 5.2e7,
time_explosion=5.2e7,
inverse_time_explosion=1 / 5.2e7,

'electron_densities': (c_double * 2)(*[1.0e9] * 2),
'inverse_electron_densities': (c_double * 2)(*[1.0e-9] * 2),
electron_densities=(c_double * 2)(*[1.0e9] * 2),
inverse_electron_densities=(c_double * 2)(*[1.0e-9] * 2),

'line_list_nu': (c_double * 5)(*[1.26318289e+16, 1.26318289e+16,
line_list_nu=(c_double * 5)(*[1.26318289e+16, 1.26318289e+16,
1.23357675e+16, 1.23357675e+16,
1.16961598e+16]),

'continuum_list_nu': (c_double * 20000)(*([1.e13] * 20000)),
continuum_list_nu=(c_double * 20000)(*([1.e13] * 20000)),

'line_lists_tau_sobolevs': (c_double * 1000)(*([1.e-5] * 1000)),
'line_lists_j_blues': (c_double * 2)(*([1.e-10] * 2)),
'line_lists_j_blues_nd': 0,
line_lists_tau_sobolevs=(c_double * 1000)(*([1.e-5] * 1000)),
line_lists_j_blues=(c_double * 2)(*([1.e-10] * 2)),
line_lists_j_blues_nd=0,

'no_of_lines': 2,
'no_of_edges': 100,
no_of_lines=2,
no_of_edges=100,

'line_interaction_id': 0,
'line2macro_level_upper': (c_int64 * 2)(*([0] * 2)),
line_interaction_id=0,
line2macro_level_upper=(c_int64 * 2)(*([0] * 2)),

'js': (c_double * 2)(*([0.0] * 2)),
'nubars': (c_double * 2)(*([0.0] * 2)),
js=(c_double * 2)(*([0.0] * 2)),
nubars=(c_double * 2)(*([0.0] * 2)),

'spectrum_start_nu': 1.e14,
'spectrum_delta_nu': 293796608840.0,
'spectrum_end_nu': 6.e15,
spectrum_start_nu=1.e14,
spectrum_delta_nu=293796608840.0,
spectrum_end_nu=6.e15,

'spectrum_virt_start_nu': 1e14,
'spectrum_virt_end_nu': 6e15,
'spectrum_virt_nu': (c_double * 20000)(*([0.0] * 20000)),
spectrum_virt_start_nu=1e14,
spectrum_virt_end_nu=6e15,
spectrum_virt_nu=(c_double * 20000)(*([0.0] * 20000)),

'sigma_thomson': 6.652486e-25,
'inverse_sigma_thomson': 1 / 6.652486e-25,
sigma_thomson=6.652486e-25,
inverse_sigma_thomson=1 / 6.652486e-25,

'inner_boundary_albedo': 0.0,
'reflective_inner_boundary': 0,
inner_boundary_albedo=0.0,
reflective_inner_boundary=0,

'chi_bf_tmp_partial': (c_double * 20000)(*([160.0] * 20000)),
't_electrons': (c_double * 2)(*([0.0] * 2)),
chi_bf_tmp_partial=(c_double * 20000)(*([160.0] * 20000)),
t_electrons=(c_double * 2)(*([0.0] * 2)),

'l_pop': (c_double * 20000)(*([2.0] * 20000)),
'l_pop_r': (c_double * 20000)(*([3.0] * 20000)),
}
return StorageModel(**model_default)
l_pop=(c_double * 20000)(*([2.0] * 20000)),
l_pop_r=(c_double * 20000)(*([3.0] * 20000)),
)


@pytest.fixture(scope="function")
def mt_state():
"""Fixture to return `RKState` object with default params initialized."""
mt_state_default = {
'key': (c_ulong * 624)(*([0] * 624)),
'pos': 0,
'has_gauss': 0,
'gauss': 0.0
}
return RKState(**mt_state_default)
return RKState(
key=(c_ulong * 624)(*([0] * 624)),
pos=0,
has_gauss=0,
gauss=0.0
)


@pytest.mark.parametrize(
['packet_params', 'model_params', 'expected'],
[({'mu': 0.3, 'r': 7.5e14}, {'inverse_time_explosion': 1 / 5.2e7},
0.9998556693818854),
({'mu': -.3, 'r': 8.1e14}, {'inverse_time_explosion': 1 / 2.6e7},
1.0003117541351274)]
['mu', 'r', 'inv_t_exp', 'expected'],
[(0.3, 7.5e14, 1 / 5.2e7, 0.9998556693818854),
(-.3, 8.1e14, 1 / 2.6e7, 1.0003117541351274)]
)
def test_rpacket_doppler_factor(packet_params, model_params, expected, packet, model):
def test_rpacket_doppler_factor(mu, r, inv_t_exp, expected, packet, model):
# Set the params from test cases here
packet.mu = packet_params['mu']
packet.r = packet_params['r']
model.inverse_time_explosion = model_params['inverse_time_explosion']
packet.mu = mu
packet.r = r
model.inverse_time_explosion = inv_t_exp

# Perform any other setups just before this, they can be additional calls
# to other methods or introduction of some temporary variables
Expand Down

0 comments on commit f837c80

Please sign in to comment.