-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unit tests for the spectrum solver
- Loading branch information
1 parent
7d5a340
commit 7d03b2e
Showing
2 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from pathlib import Path | ||
|
||
import astropy.units as u | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
from astropy.tests.helper import assert_quantity_allclose | ||
|
||
from tardis.io.configuration.config_reader import Configuration | ||
from tardis.simulation.base import Simulation | ||
from tardis.spectrum.base import SpectrumSolver | ||
from tardis.tests.fixtures.regression_data import RegressionData | ||
|
||
|
||
class TestSpectrumSolver: | ||
regression_data: RegressionData = None | ||
|
||
@pytest.fixture(scope="class") | ||
def simulation( | ||
self, | ||
request, | ||
atomic_data_fname, | ||
generate_reference, | ||
example_configuration_dir: Path, | ||
): | ||
config = Configuration.from_yaml( | ||
str(example_configuration_dir / "tardis_configv1_verysimple.yml") | ||
) | ||
config["atom_data"] = atomic_data_fname | ||
|
||
simulation = Simulation.from_config(config) | ||
simulation.run_final() | ||
|
||
request.cls.regression_data = RegressionData(request) | ||
request.cls.regression_data.sync_hdf_store(simulation) | ||
|
||
return simulation | ||
|
||
def get_expected_data(self, key: str): | ||
return pd.read_hdf(self.regression_data.fpath, key) | ||
|
||
def test_initialization(self, simulation): | ||
transport_state = simulation.transport.transport_state | ||
spectrum_frequency = simulation.transport.spectrum_frequency | ||
|
||
solver = SpectrumSolver(transport_state, spectrum_frequency) | ||
assert solver.transport_state == transport_state | ||
assert np.array_equal( | ||
solver.spectrum_frequency.value, spectrum_frequency.value | ||
) | ||
assert np.array_equal( | ||
solver._montecarlo_virtual_luminosity.value, | ||
np.zeros_like(spectrum_frequency.value), | ||
) | ||
assert solver._integrator is None | ||
assert solver.integrator_settings is None | ||
assert solver._spectrum_integrated is None | ||
|
||
def test_spectrum_real_packets(self, simulation): | ||
transport_state = simulation.transport.transport_state | ||
spectrum_frequency = simulation.transport.spectrum_frequency | ||
|
||
solver = SpectrumSolver(transport_state, spectrum_frequency) | ||
result = solver.spectrum_real_packets.luminosity | ||
key = "simulation/spectrum_solver/spectrum_real_packets/luminosity" | ||
expected = self.get_expected_data(key) | ||
|
||
luminosity = u.Quantity(expected, "erg /s") | ||
|
||
assert_quantity_allclose( | ||
result, | ||
luminosity, | ||
) | ||
|
||
def test_spectrum_real_packets_reabsorbed(self, simulation): | ||
transport_state = simulation.transport.transport_state | ||
spectrum_frequency = simulation.transport.spectrum_frequency | ||
|
||
solver = SpectrumSolver(transport_state, spectrum_frequency) | ||
result = solver.spectrum_real_packets_reabsorbed.luminosity | ||
key = "simulation/spectrum_solver/spectrum_real_packets_reabsorbed/luminosity" | ||
expected = self.get_expected_data(key) | ||
|
||
luminosity = u.Quantity(expected, "erg /s") | ||
|
||
assert_quantity_allclose( | ||
result, | ||
luminosity, | ||
) |