Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHls committed Nov 27, 2023
1 parent b114908 commit fdf9399
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ plasma:
initial_t_inner: 12000 K
link_t_rad_t_electron: 1.0
nlte_ionization_species: [H I, He II, Ti II]
nlte_solver: lu

continuum_interaction:
species:
Expand Down
71 changes: 71 additions & 0 deletions tardis/io/configuration/tests/data/tardis_configv1_nlte_root.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
tardis_config_version: v1.0

supernova:
luminosity_requested: 9.05 log_lsun
time_explosion: 16 day

atom_data: TestNLTE_Ti.h5

model:
structure:
type: specific
velocity:
start: 6395 km/s
stop: 12500 km/s
num: 5
density:
type : power_law
time_0: 16.0 day
rho_0: 1.948e-14 g/cm^3
v_0: 8000 km/s
exponent: -10

abundances:
type: uniform
H: 0.9
He: 0.099
Ti: 1e-3

plasma:
disable_electron_scattering: no
ionization: lte
excitation: lte
radiative_rates_type: dilute-blackbody
line_interaction_type: macroatom
initial_t_inner: 12000 K
link_t_rad_t_electron: 1.0
nlte_ionization_species: [H I, He II, Ti II]
nlte_solver: root

continuum_interaction:
species:
- H I
- Ti II
- He II

enable_adiabatic_cooling: True



montecarlo:
seed: 23111963
no_of_packets: 100000
iterations: 1
nthreads: 1

last_no_of_packets: 1000
no_of_virtual_packets: 0

convergence_strategy:
type: damped
damping_constant: 0.5
threshold: 0.05
fraction: 0.8
hold_iterations: 3

spectrum:
start: 250 angstrom
stop: 10000 angstrom
num: 10000
method: integrated
compute: Automatic
156 changes: 107 additions & 49 deletions tardis/plasma/tests/test_nlte_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,41 +217,78 @@ def test_jacobian_matrix(


@pytest.fixture
def nlte_raw_plasma_w1(
tardis_model_config_nlte, nlte_raw_model, nlte_atom_data
def nlte_raw_plasma_w1_root(
tardis_model_config_nlte_root, nlte_raw_model, nlte_atom_data
):
"""
Plasma assembled with dilution factors set to 1.0.
"""
new_w = np.ones_like(nlte_raw_model.dilution_factor)
nlte_raw_model.dilution_factor = new_w
plasma = assemble_plasma(
tardis_model_config_nlte, nlte_raw_model, nlte_atom_data
tardis_model_config_nlte_root, nlte_raw_model, nlte_atom_data
)
return plasma


@pytest.fixture
def nlte_raw_plasma_w0(
tardis_model_config_nlte, nlte_raw_model, nlte_atom_data
def nlte_raw_plasma_w1_lu(
tardis_model_config_nlte_lu, nlte_raw_model, nlte_atom_data
):
"""
Plasma assembled with dilution factors set to 1.0.
"""
new_w = np.ones_like(nlte_raw_model.dilution_factor)
nlte_raw_model.dilution_factor = new_w
plasma = assemble_plasma(
tardis_model_config_nlte_lu, nlte_raw_model, nlte_atom_data
)
return plasma


@pytest.fixture
def nlte_raw_plasma_w0_root(
tardis_model_config_nlte_root, nlte_raw_model, nlte_atom_data
):
"""
Plasma assembled with dilution factors set to 0.0.
"""
new_w = np.zeros_like(nlte_raw_model.dilution_factor)
nlte_raw_model.dilution_factor = new_w
plasma = assemble_plasma(
tardis_model_config_nlte, nlte_raw_model, nlte_atom_data
tardis_model_config_nlte_root, nlte_raw_model, nlte_atom_data
)
return plasma


@pytest.fixture
def nlte_raw_plasma_w0_lu(
tardis_model_config_nlte_lu, nlte_raw_model, nlte_atom_data
):
"""
Plasma assembled with dilution factors set to 0.0.
"""
new_w = np.zeros_like(nlte_raw_model.dilution_factor)
nlte_raw_model.dilution_factor = new_w
plasma = assemble_plasma(
tardis_model_config_nlte_lu, nlte_raw_model, nlte_atom_data
)
return plasma


###############################################################################
# These tests should not fail! Currently there seems to be a problem with the #
# NLTE solver that yields negative populations. The xfail should be removed #
# once the problem is fixed. #
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv #
###############################################################################
@pytest.mark.xfail
def test_critical_case_w1_root(nlte_raw_plasma_w1):
"""Check that the LTE and NLTE solution agree for w=1.0."""
ion_number_density_nlte = nlte_raw_plasma_w1.ion_number_density.values
ion_number_density_nlte[ion_number_density_nlte < 1e-10] = 0.0
ion_number_density_nlte = nlte_raw_plasma_w1_root.ion_number_density.values
ion_number_density_nlte[np.abs(ion_number_density_nlte) < 1e-10] = 0.0

ind = IonNumberDensity(nlte_raw_plasma_w1)
ind = IonNumberDensity(nlte_raw_plasma_w1_root)
ion_number_density_lte = ind.calculate(
nlte_raw_plasma_w1.thermal_phi_lte,
nlte_raw_plasma_w1.partition_function,
Expand All @@ -269,41 +306,61 @@ def test_critical_case_w1_root(nlte_raw_plasma_w1):
)


@pytest.mark.xfail
def test_critical_case_w1_lu(nlte_raw_plasma_w1):
# TODO: implement test for LU solver
pass
"""Check that the LTE and NLTE solution agree for w=1.0."""
ion_number_density_nlte = nlte_raw_plasma_w1_lu.ion_number_density.values
ion_number_density_nlte[np.abs(ion_number_density_nlte) < 1e-10] = 0.0

ind = IonNumberDensity(nlte_raw_plasma_w1_lu)
ion_number_density_lte = ind.calculate(
nlte_raw_plasma_w1.thermal_phi_lte,
nlte_raw_plasma_w1.partition_function,
nlte_raw_plasma_w1.number_density,
)[0]

def test_critical_case_w0_root(nlte_raw_plasma_w0):
ion_number_density_lte = ion_number_density_lte.values
ion_number_density_lte[
ion_number_density_lte < 1e-10
] = 0.0 # getting rid of small numbers.
assert_allclose(
ion_number_density_lte,
ion_number_density_nlte,
rtol=1e-2,
)


@pytest.mark.xfail
def test_critical_case_w0_root(nlte_raw_plasma_w0_root):
"""Check that the LTE and NLTE solution agree for w=0.0."""
nlte_solver = NLTEPopulationSolverRoot(nlte_raw_plasma_w0)
nlte_solver = NLTEPopulationSolverRoot(nlte_raw_plasma_w0_root)
ion_number_density_nlte = nlte_solver.calculate(
nlte_raw_plasma_w0.gamma,
nlte_raw_plasma_w0_root.gamma,
0.0, # to test collisions only, we set the radiative recombination rate to 0
nlte_raw_plasma_w0.alpha_stim,
nlte_raw_plasma_w0.coll_ion_coeff,
nlte_raw_plasma_w0.coll_recomb_coeff,
nlte_raw_plasma_w0.partition_function,
nlte_raw_plasma_w0.levels,
nlte_raw_plasma_w0.level_boltzmann_factor,
nlte_raw_plasma_w0.phi,
nlte_raw_plasma_w0.rate_matrix_index,
nlte_raw_plasma_w0.number_density,
nlte_raw_plasma_w0.nlte_excitation_species,
nlte_raw_plasma_w0_root.alpha_stim,
nlte_raw_plasma_w0_root.coll_ion_coeff,
nlte_raw_plasma_w0_root.coll_recomb_coeff,
nlte_raw_plasma_w0_root.partition_function,
nlte_raw_plasma_w0_root.levels,
nlte_raw_plasma_w0_root.level_boltzmann_factor,
nlte_raw_plasma_w0_root.phi,
nlte_raw_plasma_w0_root.rate_matrix_index,
nlte_raw_plasma_w0_root.number_density,
nlte_raw_plasma_w0_root.nlte_excitation_species,
)[0]
ion_number_density_nlte = ion_number_density_nlte.values
ion_number_density_nlte[ion_number_density_nlte < 1e-10] = 0.0
ion_number_density_nlte[np.abs(ion_number_density_nlte) < 1e-10] = 0.0

ind = IonNumberDensity(nlte_raw_plasma_w0)
ind = IonNumberDensity(nlte_raw_plasma_w0_root)
ion_number_density_lte = ind.calculate(
nlte_raw_plasma_w0.thermal_phi_lte,
nlte_raw_plasma_w0.partition_function,
nlte_raw_plasma_w0.number_density,
nlte_raw_plasma_w0_root.thermal_phi_lte,
nlte_raw_plasma_w0_root.partition_function,
nlte_raw_plasma_w0_root.number_density,
)[0]

ion_number_density_lte = ion_number_density_lte.values
ion_number_density_lte[
ion_number_density_lte < 1e-10
np.abs(ion_number_density_lte) < 1e-10
] = 0.0 # getting rid of small numbers.
assert_allclose(
ion_number_density_lte,
Expand All @@ -312,36 +369,37 @@ def test_critical_case_w0_root(nlte_raw_plasma_w0):
)


def test_critical_case_w0_lu(nlte_raw_plasma_w0):
@pytest.mark.xfail
def test_critical_case_w0_lu(nlte_raw_plasma_w0_lu):
"""Check that the LTE and NLTE solution agree for w=0.0."""
nlte_solver = NLTEPopulationSolverLU(nlte_raw_plasma_w0)
nlte_solver = NLTEPopulationSolverLU(nlte_raw_plasma_w0_lu)
ion_number_density_nlte = nlte_solver.calculate(
nlte_raw_plasma_w0.gamma,
nlte_raw_plasma_w0_lu.gamma,
0.0, # to test collisions only, we set the radiative recombination rate to 0
nlte_raw_plasma_w0.alpha_stim,
nlte_raw_plasma_w0.coll_ion_coeff,
nlte_raw_plasma_w0.coll_recomb_coeff,
nlte_raw_plasma_w0.partition_function,
nlte_raw_plasma_w0.levels,
nlte_raw_plasma_w0.level_boltzmann_factor,
nlte_raw_plasma_w0.phi,
nlte_raw_plasma_w0.rate_matrix_index,
nlte_raw_plasma_w0.number_density,
nlte_raw_plasma_w0.nlte_excitation_species,
nlte_raw_plasma_w0_lu.alpha_stim,
nlte_raw_plasma_w0_lu.coll_ion_coeff,
nlte_raw_plasma_w0_lu.coll_recomb_coeff,
nlte_raw_plasma_w0_lu.partition_function,
nlte_raw_plasma_w0_lu.levels,
nlte_raw_plasma_w0_lu.level_boltzmann_factor,
nlte_raw_plasma_w0_lu.phi,
nlte_raw_plasma_w0_lu.rate_matrix_index,
nlte_raw_plasma_w0_lu.number_density,
nlte_raw_plasma_w0_lu.nlte_excitation_species,
)[0]
ion_number_density_nlte = ion_number_density_nlte.values
ion_number_density_nlte[ion_number_density_nlte < 1e-10] = 0.0
ion_number_density_nlte[np.abs(ion_number_density_nlte) < 1e-10] = 0.0

ind = IonNumberDensity(nlte_raw_plasma_w0)
ind = IonNumberDensity(nlte_raw_plasma_w0_lu)
ion_number_density_lte = ind.calculate(
nlte_raw_plasma_w0.thermal_phi_lte,
nlte_raw_plasma_w0.partition_function,
nlte_raw_plasma_w0.number_density,
nlte_raw_plasma_w0_lu.thermal_phi_lte,
nlte_raw_plasma_w0_lu.partition_function,
nlte_raw_plasma_w0_lu.number_density,
)[0]

ion_number_density_lte = ion_number_density_lte.values
ion_number_density_lte[
ion_number_density_lte < 1e-10
np.abs(ion_number_density_lte) < 1e-10
] = 0.0 # getting rid of small numbers.
assert_allclose(
ion_number_density_lte,
Expand Down
20 changes: 16 additions & 4 deletions tardis/tests/fixtures/atom_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ def nlte_atom_data(nlte_atomic_dataset):


@pytest.fixture # (scope="session")
def tardis_model_config_nlte(example_configuration_dir):
def tardis_model_config_nlte_root(example_configuration_dir):
return Configuration.from_yaml(
example_configuration_dir / "tardis_configv1_nlte.yml"
example_configuration_dir / "tardis_configv1_nlte_root.yml"
)


@pytest.fixture # (scope="session")
def nlte_raw_model(tardis_model_config_nlte):
return SimulationState.from_config(tardis_model_config_nlte)
def tardis_model_config_nlte_lu(example_configuration_dir):
return Configuration.from_yaml(
example_configuration_dir / "tardis_configv1_nlte_lu.yml"
)


@pytest.fixture # (scope="session")
def nlte_raw_model_root(tardis_model_config_nlte_root):
return SimulationState.from_config(tardis_model_config_nlte_root)


@pytest.fixture # (scope="session")
def nlte_raw_model_lu(tardis_model_config_nlte_lu):
return SimulationState.from_config(tardis_model_config_nlte_lu)

0 comments on commit fdf9399

Please sign in to comment.