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

Macroatom restructure #2786

Merged
merged 89 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
1056501
created file for opacity solver
Rodot- Aug 1, 2024
2dd9178
Added opacity states for the continuum and macroatom
Rodot- Aug 1, 2024
e0a9c02
Updated the opacity state and macroatom state to work with legacy plasma
Rodot- Aug 1, 2024
390c4d8
Added numba initialize from opacity state, added methods to solve opa…
Rodot- Aug 1, 2024
9f71324
moved line2macro_level_upper to the macroatom state
Rodot- Aug 1, 2024
7a5e864
renamed a macroatom variable, added solver to montecarlo solver
Rodot- Aug 1, 2024
378dbd5
ran black
Rodot- Aug 1, 2024
462a5e8
corrected an import
Rodot- Aug 1, 2024
89860f9
corrected an import
Rodot- Aug 1, 2024
8672a0c
ran black
Rodot- Aug 1, 2024
8941377
Added line_interaction_type to the to_numba function, should be reqor…
Rodot- Aug 1, 2024
2704633
fixed initialization of opacity state
Rodot- Aug 1, 2024
15b1565
Updated handling of continuum vs noncontinuum macroblock references
Rodot- Aug 1, 2024
80c9f85
Fixed a typo
Rodot- Aug 1, 2024
b3a4258
refactored the state objects to put them in the correct folders
Rodot- Aug 1, 2024
6010f40
another typo
Rodot- Aug 1, 2024
95fbc72
added basic docstrings to the continuum state
Rodot- Aug 1, 2024
5da0577
Added docstrings to macroatom state
Rodot- Aug 1, 2024
da72b58
Updated docstrings onithe opacity_state file
Rodot- Aug 1, 2024
81dde1b
Added docstrings to the solver
Rodot- Aug 1, 2024
e182bb8
Updated Docstring Style
Rodot- Aug 1, 2024
b8a527f
Merge branch 'master' of github.com:tardis-sn/tardis into opacity_sta…
Rodot- Aug 2, 2024
148672e
ran black
Rodot- Aug 2, 2024
204d6c9
changed Opacity solver to take kwargs
Rodot- Aug 2, 2024
c628eb6
ran black
Rodot- Aug 2, 2024
67e301f
Added class attributes to Opacity Solver
Rodot- Aug 2, 2024
939a9f8
Changed names of the opacity state and numba equivalent
Rodot- Aug 2, 2024
16d8db7
Merge branch 'master' into opacity_state_restructure
Rodot- Aug 2, 2024
f52d4ea
Added test for the opacity solver
Rodot- Aug 2, 2024
3131a6b
added conftest to the opacities module
Rodot- Aug 2, 2024
cc89f37
fixed up tests for the opacity_solver
Rodot- Aug 2, 2024
5b7bc95
ran black
Rodot- Aug 2, 2024
5d07c65
Added new test for the numba opacity state
Rodot- Aug 2, 2024
62371cb
ran black, updated name of the test
Rodot- Aug 2, 2024
acc02e8
Make a function to calculate markov chain indices
Rodot- Aug 2, 2024
f417cab
Extracted more functions
Rodot- Aug 2, 2024
b1c1b1d
Merged with master
Rodot- Aug 6, 2024
4121ff5
Moved more calculations over to functions (maybe this should be a sep…
Rodot- Aug 7, 2024
21f303f
Forgot an extra arg to the macroatom state
Rodot- Aug 7, 2024
131eead
fixed up continuum vs non-continuum initialization and handling of co…
Rodot- Aug 7, 2024
9213307
broke about the base transition probabilities properties into functions
Rodot- Aug 7, 2024
cec24ef
Added the default transition probabilities to the solver
Rodot- Aug 7, 2024
58d04f3
See previous commit, added a comment
Rodot- Aug 7, 2024
8d2428a
Improved code flow for continuum vs non-continuum
Rodot- Aug 7, 2024
c58f0bb
See previous commit
Rodot- Aug 7, 2024
39182b8
See previous commit
Rodot- Aug 7, 2024
240fabb
removed non-continuum interaction species from non-continuum solver path
Rodot- Aug 7, 2024
9aea0ad
broke up the solver to have a common method for both continuum and no…
Rodot- Aug 7, 2024
a9fcc6a
Made a separate solver for the macroatom when working with the contin…
Rodot- Aug 7, 2024
7587151
cleaned up default solver a bit
Rodot- Aug 7, 2024
e140196
ditto
Rodot- Aug 7, 2024
f9a1084
ran black, added personal comments
Rodot- Aug 7, 2024
add4d0f
first attempt to work basic macroatom solver into the opacity solver
Rodot- Aug 7, 2024
1e135d0
Fixed calculate_deactivation_channel_probs missing argument 'B'
Rodot- Aug 7, 2024
7c3b59f
Fixed calculate_deactivation_channel_probs missing argument 'B'
Rodot- Aug 7, 2024
0515200
Fixed accessign the macro_atom_data from the plasma
Rodot- Aug 7, 2024
0bc5362
Fixed typo
Rodot- Aug 7, 2024
ffdb82d
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
d4e2f0a
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
4b223ae
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
f0ad44e
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
feda179
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
ad0e132
Small fix to make tests pass, need to revisit later
Rodot- Aug 7, 2024
cad6d96
Added switch to swap our the macroatom solver when using the continuum
Rodot- Aug 7, 2024
f159c84
Made a passthrough when continuum processes are enabled in case we do…
Rodot- Aug 7, 2024
82a70d4
Removed switch to auto-build continuum macrostate using legacy mode
Rodot- Aug 8, 2024
3c9d4ed
Fixed initialization in default macroatom solver
Rodot- Aug 8, 2024
fb6ca3a
grab continuum interaction species from the legacy plasma
Rodot- Aug 8, 2024
a0adbb6
Added in the rest of the transition probability properties to the con…
Rodot- Aug 8, 2024
c3c573b
Removed old method of doing this
Rodot- Aug 8, 2024
69505c8
switch back to using legacy solve for continuum in case we don't want…
Rodot- Aug 8, 2024
a39f965
Moved the MacroAtomContinuumSolver to a new file (deleted from this P…
Rodot- Aug 8, 2024
aaef58f
moved 'calculate_' functions derived from transition_probabilities.py…
Rodot- Aug 8, 2024
7369c98
Moved MacroAtomState Solving out of the plasma state from_legacy_plasma
Rodot- Aug 8, 2024
a74e9ea
Added docstrings to MacroAtomSolver
Rodot- Aug 8, 2024
f29d317
Added docstrings to macro_atom base.py
Rodot- Aug 8, 2024
a64244f
Fixed up docstrings in macroatom_sovler
Rodot- Aug 8, 2024
30412b0
Added check to verify macroatom_state is None when line_interaction_t…
Rodot- Aug 8, 2024
cfff15f
removed some comments, fixed up OpacitySolver initialization
Rodot- Aug 8, 2024
8f9afcd
Moved handling of the opacity solver over to the simulation base
Rodot- Aug 8, 2024
5801932
Fixed typo
Rodot- Aug 8, 2024
d9b44b2
Grabbed the line interaction type from the right place (makes me thin…
Rodot- Aug 8, 2024
c03d1b0
Changed 'non_markov_transition...' to 'transtiition...'
Rodot- Aug 9, 2024
81f280a
decoupled macro_atom from the opacity solver
Rodot- Aug 9, 2024
67e3603
Fixed typo
Rodot- Aug 9, 2024
20ab5d8
Updated the to_numba tests
Rodot- Aug 9, 2024
7f24cec
UPdated docstrings/removed unused imports
Rodot- Aug 12, 2024
219ceb5
Merge branch 'master' of github.com:tardis-sn/tardis into macroatom_r…
Rodot- Aug 12, 2024
537562c
Merge branch 'master' into macroatom_restructure
Rodot- Aug 12, 2024
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
230 changes: 228 additions & 2 deletions tardis/opacities/macro_atom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,231 @@
logger = logging.getLogger(__name__)


class TransitionProbabilities(ProcessingPlasmaProperty):
def initialize_non_markov_transition_probabilities(
atomic_data,
):
"""Convienience Function for initializing the transition probabilities

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data

Returns
-------
dict
"transition_probability_coef" : np.ndarray
Reshaped macro atom transition probabilities
"block_references": np.ndarray
macro atom block references
"""
macro_atom_data = get_macro_atom_data(atomic_data)
(
transition_up_filter,
transition_up_line_filter,
block_references,
) = initialize_macro_atom_transition_type_filters(
atomic_data, macro_atom_data
)
transition_probability_coef = get_transition_probability_coefs(
macro_atom_data
)

return {
"transition_probability_coef": transition_probability_coef,
"block_references": block_references,
}


def calculate_non_markov_transition_probabilities(
atomic_data,
beta_sobolev,
j_blues,
stimulated_emission_factor,
tau_sobolevs,
transition_probability_coef,
block_references,
normalize=True,
):
"""Computes non-markov transition probabilities and provides them as a pd.DataFrame

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data
beta_sobolev : pd.DataFrame
Beta Sobolevs
j_blues : pd.DataFrame
mean intensity
stimulated_emission_factor : np.ndarray
Stimulated Emission Factors
tau_sobolev : pd.DataFrame
Expansion Optical Depths
transition_probability_coef : np.ndarray
Reshaped macro atom transition probabilities
block_references : np.ndarray
macro atom block references
normalize : bool
Whether or not to normalize the transition probabilities to unity

Returns
-------
pd.DataFrame
transition probabilities
"""
# I wonder why?
# Not sure who wrote this but the answer is that when the plasma is
# first initialised (before the first iteration, without temperature
# values etc.) there are no j_blues values so this just prevents
# an error. Aoife.
if len(j_blues) == 0:
return None

Check warning on line 90 in tardis/opacities/macro_atom/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/opacities/macro_atom/base.py#L90

Added line #L90 was not covered by tests
macro_atom_data = get_macro_atom_data(atomic_data)

transition_probabilities = calculate_non_markov_transition_probability(
macro_atom_data,
beta_sobolev,
j_blues,
stimulated_emission_factor,
transition_probability_coef,
block_references,
normalize,
)
transition_probabilities = pd.DataFrame(
transition_probabilities,
index=macro_atom_data.transition_line_id,
columns=tau_sobolevs.columns,
)
return transition_probabilities


def calculate_non_markov_transition_probability(
macro_atom_data,
beta_sobolev,
j_blues,
stimulated_emission_factor,
transition_probability_coef,
block_references,
normalize,
):
"""Calculate the non-markov transition probabilities using optimized functions
Parameters
----------
macro_atom_data : pd.DataFrame
Macro Atom Data
beta_sobolev : pd.DataFrame
Beta Sobolevs
j_blues : pd.DataFrame
mean intensity
stimulated_emission_factor : np.ndarray
Stimulated Emission Factors
transition_probability_coef : np.ndarray
Reshaped macro atom transition probabilities
block_references : np.ndarray
macro atom block references
normalize : bool
Whether or not to normalize the transition probabilities to unity

Returns
-------
np.ndarray
transition probabilities
"""
transition_probabilities = np.empty(
(transition_probability_coef.shape[0], beta_sobolev.shape[1])
)
# trans_old = self.calculate_transition_probabilities(macro_atom_data, beta_sobolev, j_blues, stimulated_emission_factor)
transition_type = macro_atom_data.transition_type.values
lines_idx = macro_atom_data.lines_idx.values
tpos = macro_atom_data.transition_probability.values
util.fast_calculate_transition_probabilities(
tpos,
beta_sobolev.values,
j_blues.values,
stimulated_emission_factor,
transition_type,
lines_idx,
block_references,
transition_probabilities,
normalize,
)
return transition_probabilities


def initialize_macro_atom_transition_type_filters(atomic_data, macro_atom_data):
"""Get the filters and block references from the macro atom

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data
macro_atom_data : pd.DataFrame
Macro Atom Data

Returns
-------
np.ndarray
Mask where the transition type is 1
np.ndarray
index of lines at these locations
pd.ndarray
macro atom block references
"""
transition_up_filter = macro_atom_data.transition_type.values == 1
transition_up_line_filter = macro_atom_data.lines_idx.values[
transition_up_filter
]
block_references = np.hstack(
(
atomic_data.macro_atom_references.block_references,
len(macro_atom_data),
)
)

return transition_up_filter, transition_up_line_filter, block_references


def get_transition_probability_coefs(macro_atom_data):
"""Coefficients of the transition probabilities

Parameters
----------
macro_atom_data : pd.DataFrame
Macro Atom Data

Returns
-------
np.ndarray
Reshaped macro atom transition probabilities
"""
return macro_atom_data.transition_probability.values[np.newaxis].T


def get_macro_atom_data(atomic_data):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this shows us that we don't need all the atomic data to be passed around all the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely not, some kind of MacroAtomData structure could be useful

"""Get the macro atom data from the atomic data

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data

Returns
-------
pd.DataFrame
The macro atom data in the plasma
"""
try:
return atomic_data.macro_atom_data
except:
logger.debug(

Check warning on line 228 in tardis/opacities/macro_atom/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/opacities/macro_atom/base.py#L227-L228

Added lines #L227 - L228 were not covered by tests
"Macro Atom Data was not found. Instead returning All Macro Atom Data"
)
return atomic_data.macro_atom_data_all

Check warning on line 231 in tardis/opacities/macro_atom/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/opacities/macro_atom/base.py#L231

Added line #L231 was not covered by tests


class TransitionProbabilities(
ProcessingPlasmaProperty
): # Base MacroAtom Property
"""
Attributes
----------
Expand Down Expand Up @@ -137,5 +361,7 @@
return atomic_data.macro_atom_data_all


class NonMarkovChainTransitionProbabilities(TransitionProbabilities):
class NonMarkovChainTransitionProbabilities(
DeerWhale marked this conversation as resolved.
Show resolved Hide resolved
TransitionProbabilities
): # Continuum Only
outputs = ("non_markov_transition_probabilities",)
133 changes: 133 additions & 0 deletions tardis/opacities/macro_atom/macroatom_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from tardis.opacities.macro_atom.base import (
calculate_non_markov_transition_probabilities,
initialize_non_markov_transition_probabilities,
)
from tardis.opacities.macro_atom.macroatom_state import MacroAtomState


class MacroAtomSolver(object):

initialize: bool = True
normalize: bool = True

def __init__(self, initialize=True, normalize=True):
"""Solver class for Macro Atom related opacities

Parameters
----------
initialize: bool
Whether or not to initialize the transition probabilitiy coefficients and block references when solving the first time (default True)
normalize: bool
Whether or not to normalize the transition probabilities to unity. Default True
"""

self.initialize = initialize
self.normalize = normalize

def initialize_non_markov_transition_probabilities(self, atomic_data):
"""initialize the transition probabilitiy coefficients and block references when solving the first time
Rodot- marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data
"""

coef_and_block_ref = initialize_non_markov_transition_probabilities(
atomic_data
)
self.transition_probability_coef = coef_and_block_ref[
"transition_probability_coef"
]
self.block_references = coef_and_block_ref["block_references"]
self.initialize = False

def solve_non_markov_transition_probabilities(
self,
atomic_data,
legacy_plasma,
tau_sobolev,
stimulated_emission_factor,
):
"""Solve the basic transition probabilities for the macroatom

Parameters
----------
atomic_data : tardis.io.atom_data.AtomData
Atomic Data
legacy_plasma : tarids.plasma.BasePlasma
legacy base plasma
tau_sobolev : pd.DataFrame
Expansion Optical Depths
stimulated_emission_factor : np.ndarray

Returns
-------
pd.DataFrame
Transition Probabilities
"""
if self.initialize:
self.initialize_non_markov_transition_probabilities(atomic_data)

non_markov_transition_probabilities = (
calculate_non_markov_transition_probabilities(
atomic_data,
legacy_plasma.beta_sobolev,
legacy_plasma.j_blues,
stimulated_emission_factor,
tau_sobolev,
self.transition_probability_coef,
self.block_references,
normalize=self.normalize,
)
)

return non_markov_transition_probabilities

def solve(
self,
legacy_plasma,
atomic_data,
tau_sobolev,
stimulated_emission_factor,
):
"""Solved the Macro Atom State

Parameters
----------
legacy_plasma : tarids.plasma.BasePlasma
legacy base plasma
atomic_data : tardis.io.atom_data.AtomData
Atomic Data
tau_sobolev : pd.DataFrame
Expansion Optical Depths
stimulated_emission_factor : pd.DataFrame

Returns
-------
tardis.opacities.macroatom_state.MacroAtomState
State of the macro atom ready to be placed into the OpacityState
"""

transition_probabilities = (
self.solve_non_markov_transition_probabilities(
atomic_data,
legacy_plasma,
tau_sobolev,
Copy link
Member

Choose a reason for hiding this comment

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

that is part of the opacity state - I presume?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you mean?

stimulated_emission_factor,
)
)

macro_block_references = atomic_data.macro_atom_references[
"block_references"
]
macro_atom_info = legacy_plasma.atomic_data.macro_atom_data

return MacroAtomState(
transition_probabilities,
macro_atom_info["transition_type"],
macro_atom_info["destination_level_idx"],
macro_atom_info["lines_idx"],
macro_block_references,
legacy_plasma.atomic_data.lines_upper2macro_reference_idx,
)
Loading
Loading