Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solve method to SpectrumSolver #2735

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def __init__(
convergence_plots_kwargs,
show_progress_bars,
spectrum_solver,
integrator_settings,
):
super(Simulation, self).__init__(
iterations, simulation_state.no_of_shells
Expand All @@ -159,7 +158,6 @@ def __init__(
self.luminosity_nu_end = luminosity_nu_end
self.luminosity_requested = luminosity_requested
self.spectrum_solver = spectrum_solver
self.integrator_settings = integrator_settings
self.show_progress_bars = show_progress_bars
self.version = tardis.__version__

Expand Down Expand Up @@ -477,7 +475,6 @@ def run_final(self):
self.iterate(self.last_no_of_packets, self.no_of_virtual_packets)

# Set up spectrum solver integrator
self.spectrum_solver.integrator_settings = self.integrator_settings
self.spectrum_solver._integrator = FormalIntegrator(
self.simulation_state, self.plasma, self.transport
)
Expand Down Expand Up @@ -771,6 +768,5 @@ def from_config(
convergence_strategy=config.montecarlo.convergence_strategy,
convergence_plots_kwargs=convergence_plots_kwargs,
show_progress_bars=show_progress_bars,
integrator_settings=config.spectrum.integrated,
spectrum_solver=spectrum_solver,
)
34 changes: 30 additions & 4 deletions tardis/spectrum/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

hdf_name = "spectrum"

def __init__(self, transport_state, spectrum_frequency_grid):
def __init__(
self, transport_state, spectrum_frequency_grid, integrator_settings=None
):
self.transport_state = transport_state
self.spectrum_frequency_grid = spectrum_frequency_grid
self._montecarlo_virtual_luminosity = u.Quantity(
np.zeros_like(self.spectrum_frequency_grid.value), "erg / s"
) # should be init with v_packets_energy_hist
self._integrator = None
self.integrator_settings = None
self.integrator_settings = integrator_settings

Check warning on line 34 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L34

Added line #L34 was not covered by tests
self._spectrum_integrated = None

@property
Expand Down Expand Up @@ -60,7 +62,7 @@

@property
def spectrum_integrated(self):
if self._spectrum_integrated is None:
if self._spectrum_integrated is None and self.integrator is not None:

Check warning on line 65 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L65

Added line #L65 was not covered by tests
# This was changed from unpacking to specific attributes as compute
# is not used in calculate_spectrum
try:
Expand All @@ -83,13 +85,15 @@
np.array([np.nan, np.nan]) * u.Hz,
np.array([np.nan]) * u.erg / u.s,
)
else:
self._spectrum_integrated = None

Check warning on line 89 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L89

Added line #L89 was not covered by tests
return self._spectrum_integrated

@property
def integrator(self):
if self._integrator is None:
warnings.warn(
"MontecarloTransport.integrator: "
"SpectrumSolver.integrator: "
"The FormalIntegrator is not yet available."
"Please run the montecarlo simulation at least once.",
UserWarning,
Expand Down Expand Up @@ -178,6 +182,27 @@
luminosity_wavelength_filter
].sum()

def solve(self, transport_state):
Rodot- marked this conversation as resolved.
Show resolved Hide resolved
"""Solve the spectra

Parameters
----------
transport_state: MonteCarloTransportState
The transport state to be used to compute the spectra

Returns
-------
tuple(TARDISSpectrum)
Real, virtual and integrated spectra, if available
"""
self.transport_state = transport_state

Check warning on line 198 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L198

Added line #L198 was not covered by tests

return (

Check warning on line 200 in tardis/spectrum/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/base.py#L200

Added line #L200 was not covered by tests
self.spectrum_real_packets,
self.spectrum_virtual_packets,
self.spectrum_integrated,
)

@classmethod
def from_config(cls, config):
spectrum_frequency_grid = quantity_linspace(
Expand All @@ -189,4 +214,5 @@
return cls(
transport_state=None,
spectrum_frequency_grid=spectrum_frequency_grid,
integrator_settings=config.spectrum.integrated,
)
27 changes: 27 additions & 0 deletions tardis/spectrum/tests/test_spectrum_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,30 @@
result,
luminosity,
)

def test_solve(self, simulation):
transport_state = simulation.transport.transport_state
spectrum_frequency_grid = simulation.transport.spectrum_frequency_grid

Check warning on line 93 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L92-L93

Added lines #L92 - L93 were not covered by tests

solver = SpectrumSolver(transport_state, spectrum_frequency_grid)
result_real, result_virtual, result_integrated = solver.solve(

Check warning on line 96 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L95-L96

Added lines #L95 - L96 were not covered by tests
transport_state
)
key_real = "simulation/spectrum_solver/spectrum_real_packets/luminosity"
expected_real = self.get_expected_data(key_real)

Check warning on line 100 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L99-L100

Added lines #L99 - L100 were not covered by tests

luminosity_real = u.Quantity(expected_real, "erg /s")

Check warning on line 102 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L102

Added line #L102 was not covered by tests

assert_quantity_allclose(

Check warning on line 104 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L104

Added line #L104 was not covered by tests
result_real.luminosity,
luminosity_real,
)

assert_quantity_allclose(

Check warning on line 109 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L109

Added line #L109 was not covered by tests
result_virtual.luminosity,
u.Quantity(
np.zeros_like(spectrum_frequency_grid.value)[:-1], "erg / s"
),
)

assert result_integrated is None

Check warning on line 116 in tardis/spectrum/tests/test_spectrum_solver.py

View check run for this annotation

Codecov / codecov/patch

tardis/spectrum/tests/test_spectrum_solver.py#L116

Added line #L116 was not covered by tests
Loading