Skip to content

Commit ecf6483

Browse files
authored
append dummy index to references and fix docs (tardis-sn#3315)
* append dummy index to references * remove fill for source since they should should never be empty * remove whitespace change * change solve in montecarlo tutorial * add hydrogen only test * add tolerances * rtol 1e-12
1 parent 47738f1 commit ecf6483

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

tardis/conftest.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from astropy.version import version as astropy_version
7+
from astropy import units as u
78

89
from tardis import run_tardis
910
from tardis.io.configuration.config_reader import Configuration
@@ -168,11 +169,17 @@ def generate_reference(request):
168169

169170
@pytest.fixture(scope="session")
170171
def tardis_regression_path(request):
171-
tardis_regression_path = request.config.getoption("--tardis-regression-data")
172+
tardis_regression_path = request.config.getoption(
173+
"--tardis-regression-data"
174+
)
172175
if tardis_regression_path is None:
173176
pytest.skip("--tardis-regression-data was not specified")
174177
else:
175-
return Path(os.path.expandvars(tardis_regression_path)).expanduser().resolve()
178+
return (
179+
Path(os.path.expandvars(tardis_regression_path))
180+
.expanduser()
181+
.resolve()
182+
)
176183

177184

178185
@pytest.fixture(scope="function")
@@ -182,6 +189,7 @@ def tardis_config_verysimple():
182189
YAMLLoader,
183190
)
184191

192+
185193
@pytest.fixture(scope="session")
186194
def config_verysimple_for_simulation_one_loop(
187195
config_verysimple, atomic_data_fname
@@ -194,6 +202,17 @@ def config_verysimple_for_simulation_one_loop(
194202
return config
195203

196204

205+
@pytest.fixture(scope="function")
206+
def config_verysimple_hydrogen_only(config_verysimple):
207+
config = deepcopy(config_verysimple)
208+
config.model.abundances = {
209+
"type": "uniform",
210+
"H": 1.0,
211+
"model_isotope_time_0": 0.0 * u.s,
212+
}
213+
return config
214+
215+
197216
@pytest.fixture(scope="function")
198217
def tardis_config_verysimple_nlte():
199218
return yaml_load_file(

tardis/opacities/macro_atom/macroatom_solver.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,8 @@ def _solve_first_macroatom_iteration(
441441
)
442442

443443
macro_atom_transition_metadata["source_level_idx"] = (
444-
(macro_atom_transition_metadata.source.map(source_to_index))
445-
.fillna(0)
446-
.astype(np.int64)
447-
)
444+
macro_atom_transition_metadata.source.map(source_to_index)
445+
).astype(np.int64)
448446

449447
macro_block_references = create_macro_block_references(
450448
macro_atom_transition_metadata
@@ -596,8 +594,21 @@ def create_macro_block_references(macro_atom_transition_metadata):
596594
.groupby("source")
597595
.apply(lambda x: x.index[0])
598596
)
597+
598+
# Append a dummy index so that the interactions can access a "block end" if a packet activates the macroatom highest level of the heaviest element in the montecarlo.
599+
# Without this the kernel will crash trying to access an index that doesn't exist.
600+
macro_data = np.append(
601+
macro_data.values, len(macro_atom_transition_metadata)
602+
)
603+
unique_source_multi_index = unique_source_multi_index.append(
604+
pd.MultiIndex.from_tuples(
605+
[(-99, -99, -99)],
606+
names=["atomic_number", "ion_number", "level_number"],
607+
)
608+
)
609+
599610
macro_block_references = pd.Series(
600-
data=macro_data.values,
611+
data=macro_data,
601612
index=unique_source_multi_index,
602613
name="macro_block_references",
603614
)

tardis/transport/montecarlo/tests/test_montecarlo_main_loop.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,23 @@ def test_montecarlo_main_loop_vpacket_log(
137137
rtol=1e-12,
138138
atol=1e-15,
139139
)
140+
141+
142+
def test_montecarlo_main_loop_hydrogen_only(
143+
config_verysimple_hydrogen_only, atomic_dataset, regression_data
144+
):
145+
atomic_dataset = deepcopy(atomic_dataset)
146+
montecarlo_main_loop_simulation = Simulation.from_config(
147+
config_verysimple_hydrogen_only,
148+
atom_data=atomic_dataset,
149+
virtual_packet_logging=False,
150+
)
151+
montecarlo_main_loop_simulation.run_convergence()
152+
montecarlo_main_loop_simulation.run_final()
153+
154+
transport = montecarlo_main_loop_simulation.transport.transport_state
155+
assert transport.j_estimator is not None
156+
expected_j_estimator = regression_data.sync_ndarray(transport.j_estimator)
157+
npt.assert_allclose(
158+
transport.j_estimator, expected_j_estimator, atol=0, rtol=1e-12
159+
)

0 commit comments

Comments
 (0)