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

Opacity state slice #2758

Merged
merged 17 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
35 changes: 35 additions & 0 deletions tardis/opacities/opacity_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@
self.photo_ion_activation_idx = photo_ion_activation_idx
self.k_packet_idx = k_packet_idx

def __getitem__(self, i):
"""Get a shell or slice of shells of the attributes of the opacity state

Args:
i (int, slice): shell index or slice

Returns:
OpacityState : a shallow copy of the current instance
"""
# NOTE: This currently will not work with continuum processes since it does not slice those arrays
return OpacityState(

Check warning on line 119 in tardis/opacities/opacity_state.py

View check run for this annotation

Codecov / codecov/patch

tardis/opacities/opacity_state.py#L119

Added line #L119 was not covered by tests
self.electron_density[i],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if i is an int and then raise an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't allow one to slice the opacity state then

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, Wolfgang is concerned about what happens if somebody requests OpacityState[5] and passes that to something else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. OpacityState[5] should return either a length 1 OPacity state or fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which do you prefer?

self.t_electrons[i],
self.line_list_nu,
self.tau_sobolev[:, i],
self.transition_probabilities[:, i],
self.line2macro_level_upper,
self.macro_block_references,
self.transition_type,
self.destination_level_id,
self.transition_line_id,
self.bf_threshold_list_nu,
self.p_fb_deactivation,
self.photo_ion_nu_threshold_mins,
self.photo_ion_nu_threshold_maxs,
self.photo_ion_block_references,
self.chi_bf,
self.x_sect,
self.phot_nus,
self.ff_opacity_factor,
self.emissivities,
self.photo_ion_activation_idx,
self.k_packet_idx,
)


def opacity_state_initialize(
plasma,
Expand Down
11 changes: 7 additions & 4 deletions tardis/transport/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tardis.transport.montecarlo.montecarlo_transport_state import (
MonteCarloTransportState,
)
from tardis.transport.montecarlo.numba_interface import (
from tardis.opacities.opacity_state import (
opacity_state_initialize,
)
from tardis.transport.montecarlo.packet_trackers import (
Expand Down Expand Up @@ -103,16 +103,19 @@ def initialize_transport_state(
packet_collection = self.packet_source.create_packets(
no_of_packets, seed_offset=iteration
)
estimators = initialize_estimator_statistics(
plasma.tau_sobolevs.shape, gamma_shape
)

geometry_state = simulation_state.geometry.to_numba()
opacity_state = opacity_state_initialize(
plasma,
self.line_interaction_type,
self.montecarlo_configuration.DISABLE_LINE_SCATTERING,
)
opacity_state = opacity_state[simulation_state.geometry.v_inner_boundary_index: simulation_state.geometry.v_outer_boundary_index]

estimators = initialize_estimator_statistics(
opacity_state.tau_sobolev.shape, gamma_shape
)

transport_state = MonteCarloTransportState(
packet_collection,
estimators,
Expand Down
35 changes: 35 additions & 0 deletions tardis/transport/montecarlo/numba_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@
self.photo_ion_activation_idx = photo_ion_activation_idx
self.k_packet_idx = k_packet_idx

def __getitem__(self, i):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we can't make new classes in a jitclass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mistaken

"""Get a shell or slice of shells of the attributes of the opacity state

Args:
i (int, slice): shell index or slice

Returns:
OpacityState : a shallow copy of the current instance
"""
# NOTE: This currently will not work with continuum processes since it does not slice those arrays
return OpacityState(

Check warning on line 125 in tardis/transport/montecarlo/numba_interface.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/numba_interface.py#L125

Added line #L125 was not covered by tests
self.electron_density[i],
self.t_electrons[i],
self.line_list_nu,
self.tau_sobolev[:, i],
self.transition_probabilities[:, i],
self.line2macro_level_upper,
self.macro_block_references,
self.transition_type,
self.destination_level_id,
self.transition_line_id,
self.bf_threshold_list_nu,
self.p_fb_deactivation,
self.photo_ion_nu_threshold_mins,
self.photo_ion_nu_threshold_maxs,
self.photo_ion_block_references,
self.chi_bf,
self.x_sect,
self.phot_nus,
self.ff_opacity_factor,
self.emissivities,
self.photo_ion_activation_idx,
self.k_packet_idx,
)


def opacity_state_initialize(
plasma,
Expand Down
30 changes: 25 additions & 5 deletions tardis/transport/montecarlo/tests/test_numba_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
import numpy as np


@pytest.mark.parametrize("input_params", ["scatter", "macroatom", "downbranch"])
def test_opacity_state_initialize(nb_simulation_verysimple, input_params):
@pytest.mark.parametrize(
"input_params,sliced",
[
("scatter", False),
("macroatom", False),
("macroatom", True),
("downbranch", False),
("downbranch", True),
],
)
def test_opacity_state_initialize(
nb_simulation_verysimple, input_params, sliced
):
line_interaction_type = input_params
plasma = nb_simulation_verysimple.plasma
actual = numba_interface.opacity_state_initialize(
Expand All @@ -14,11 +25,20 @@ def test_opacity_state_initialize(nb_simulation_verysimple, input_params):
disable_line_scattering=False,
)

if sliced:
index = slice(2, 5)
actual = actual[index]
else:
index = ...

npt.assert_allclose(
actual.electron_density, plasma.electron_densities.values
actual.electron_density, plasma.electron_densities.values[index]
)
npt.assert_allclose(actual.line_list_nu, plasma.atomic_data.lines.nu.values)
npt.assert_allclose(actual.tau_sobolev, plasma.tau_sobolevs.values)
print(actual.tau_sobolev.shape, plasma.tau_sobolevs.values[:, index].shape)
npt.assert_allclose(
actual.tau_sobolev, plasma.tau_sobolevs.values[:, index]
)
if line_interaction_type == "scatter":
empty = np.zeros(1, dtype=np.int64)
npt.assert_allclose(
Expand All @@ -32,7 +52,7 @@ def test_opacity_state_initialize(nb_simulation_verysimple, input_params):
else:
npt.assert_allclose(
actual.transition_probabilities,
plasma.transition_probabilities.values,
plasma.transition_probabilities.values[:, index],
)
npt.assert_allclose(
actual.line2macro_level_upper,
Expand Down
Loading