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

[Restructure/plasma] estimator cleanup #2675

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
d583510
change numberdensity to input
wkerzendorf Apr 30, 2024
7aaba5d
fixed number density
wkerzendorf May 4, 2024
32b22ea
some fixes
wkerzendorf May 5, 2024
261bbad
removing density
wkerzendorf May 6, 2024
df3c406
remove atomic and isotope mass
wkerzendorf May 6, 2024
d05e819
add isotopic_number_density
wkerzendorf May 6, 2024
61f3db5
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf May 6, 2024
e804942
add opacities package
wkerzendorf May 6, 2024
e573f16
Update imports in property_collections.py, base.py, test_numba_interf…
wkerzendorf May 7, 2024
73f2ddc
Add calculate_transition_probabilities function to util.py in macro_a…
wkerzendorf May 7, 2024
52d5523
Add calculate_transition_probabilities function to util.py in macro_a…
wkerzendorf May 9, 2024
9530fee
Remove unused imports and update plasma properties
wkerzendorf May 9, 2024
2fb7f92
add __init__ to macroatom
wkerzendorf May 9, 2024
6ac3e88
blackify tardis
wkerzendorf May 10, 2024
801c92b
blackified
wkerzendorf May 10, 2024
ae0650d
chore: Update imports and remove unused code
wkerzendorf May 10, 2024
69dc1b5
chore: Add PlanckRadiationField and DilutePlanckRadiationField classes
wkerzendorf May 10, 2024
448d5c4
chore: Update imports and remove unused code
wkerzendorf May 10, 2024
c1dc52d
removed density
wkerzendorf May 13, 2024
f053967
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf May 20, 2024
17c033c
Merge branch 'restructure/plasma_prepare' into restructure/plasma_opa…
wkerzendorf May 23, 2024
5e02054
ruff output
wkerzendorf Jun 6, 2024
28c4348
cleanup and adding object mode
wkerzendorf Jun 6, 2024
cfffe5b
starting to make radiation_field a thing
wkerzendorf Jun 7, 2024
4e68723
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf Jun 7, 2024
41779e2
switched over to old tau_sobolev calculation
wkerzendorf Jun 9, 2024
0d1bc4d
renamed function to indicate numba use
wkerzendorf Jun 9, 2024
7cc84a2
Merge branch 'restructure/plasma_opacities' into restructure/plasma_r…
wkerzendorf Jun 9, 2024
c924749
address comments
wkerzendorf Jun 10, 2024
ad5a314
Merge branch 'restructure/plasma_opacities' into restructure/plasma_r…
wkerzendorf Jun 10, 2024
af7e39e
added dilute planckian radiation field
wkerzendorf Jun 11, 2024
36fa4b6
refactor: Convert species lists to proper format in assemble_plasma f…
wkerzendorf Jun 13, 2024
4bc0dd7
moved radiation field into plasma. Resulting in some renames
wkerzendorf Jun 14, 2024
863f281
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf Jun 19, 2024
2f042f2
some fixes
wkerzendorf Jun 19, 2024
d849f85
black montecarlo
wkerzendorf Jun 19, 2024
3379062
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf Jun 19, 2024
6feefbe
Merge branch 'restructure/plasma_opacities' into restructure/plasma_r…
wkerzendorf Jun 19, 2024
1786436
chore: Initialize atom data and simulation state in `initialization.py`
wkerzendorf Jun 19, 2024
5740708
updating the documentation
wkerzendorf Jun 19, 2024
92f4e90
feat: Add EstimatedRadiationFieldProperties class for Monte Carlo est…
wkerzendorf Jun 20, 2024
e3f1819
trying to get rid of j_blues in plasma with MC restructure
wkerzendorf Jun 21, 2024
a99fbb8
completely restructure j_blues. Estimators and all.
wkerzendorf Jun 22, 2024
2d7e1c5
cleanup for the restructure
wkerzendorf Jun 22, 2024
63e0e0c
Merge remote-tracking branch 'upstream/master' into restructure/plasm…
wkerzendorf Jul 23, 2024
818c6d4
remove parse_input.py
wkerzendorf Jul 23, 2024
3ec64f1
chore: Refactor radiation field configuration parsing and state creation
wkerzendorf Jul 23, 2024
f1f0cdd
revert astropy_helpers
wkerzendorf Jul 23, 2024
2a7d460
remove astropy_helpers
wkerzendorf Jul 23, 2024
dbc34ac
removed test.txt
wkerzendorf Jul 23, 2024
251d1cb
blackified code
wkerzendorf Jul 23, 2024
e80b15c
Merge branch 'restructure/plasma_radiation_field' into restructure/si…
wkerzendorf Jul 23, 2024
8581bed
Merge branch 'restructure/simulation_from_config_cleanup' into restru…
wkerzendorf Jul 23, 2024
d49a49c
cleanup simulation from merges
wkerzendorf Jul 23, 2024
adbaf7f
fix
wkerzendorf Jul 24, 2024
bb19fdb
remove unused Input
wkerzendorf Jul 24, 2024
aca1099
added description
wkerzendorf Jul 24, 2024
44ffcf9
Merge branch 'restructure/plasma_radiation_field' into restructure/si…
wkerzendorf Jul 24, 2024
54ffe68
blackiefied codebase
wkerzendorf Jul 24, 2024
71a2b13
Merge branch 'restructure/simulation_from_config_cleanup' into restru…
wkerzendorf Jul 24, 2024
2ea3122
cleanup of branch
wkerzendorf Jul 24, 2024
9800fc8
blackify code
wkerzendorf Jul 24, 2024
5914618
Merge branch 'restructure/plasma_radiation_field' into restructure/si…
wkerzendorf Jul 24, 2024
d19e423
chore: Refactor atom data parsing and simulation state initialization
wkerzendorf Jul 24, 2024
49b9819
Merge branch 'restructure/simulation_from_config_cleanup' into restru…
wkerzendorf Jul 24, 2024
b3bf356
restructure logger
wkerzendorf Jul 24, 2024
a22223e
Merge branch 'restructure/simulation_from_config_cleanup' into restru…
wkerzendorf Jul 24, 2024
734ffc8
working on continuum radfield properties
wkerzendorf Jul 24, 2024
d69533a
Refactor continuum processes module structure
wkerzendorf Jul 24, 2024
98f8340
Refactor opacities module structure
wkerzendorf Jul 24, 2024
84671ca
cleanup from restructure
wkerzendorf Jul 25, 2024
bf25313
Merge remote-tracking branch 'upstream/master' into restructure/base_…
wkerzendorf Jul 25, 2024
7782d07
cleanup
wkerzendorf Jul 25, 2024
cfc5078
clean up
wkerzendorf Jul 25, 2024
ee864bb
more cleanup
wkerzendorf Jul 25, 2024
ad4f1b1
Merge remote-tracking branch 'upstream/master' into restructure/base_…
wkerzendorf Jul 25, 2024
0ebcef8
cleanup standard_plasmas.py
wkerzendorf Jul 25, 2024
cf1396a
working nlte ionizations
wkerzendorf Jul 26, 2024
557dde9
Merge remote-tracking branch 'upstream/master' into restructure/base_…
wkerzendorf Jul 26, 2024
5e4902a
some more cleanup
wkerzendorf Jul 26, 2024
9f92256
fix benchmarks
wkerzendorf Jul 26, 2024
cd1badc
Merge remote-tracking branch 'upstream/master' into restructure/base_…
wkerzendorf Jul 26, 2024
bd04642
Merge remote-tracking branch 'upstream/master' into restructure/base_…
wkerzendorf Jul 26, 2024
930462f
blackify
wkerzendorf Jul 27, 2024
9e47b81
reverse the import pygraphviz
wkerzendorf Jul 27, 2024
8d67db8
fix docstrings
wkerzendorf Jul 27, 2024
7d8e6e0
Refactor code to address comments
wkerzendorf Jul 29, 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
5 changes: 2 additions & 3 deletions benchmarks/opacities_opacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import tardis.opacities.opacities as calculate_opacity
from benchmarks.benchmark_base import BenchmarkBase
from tardis.opacities.opacities import compton_opacity_calculation


class BenchmarkMontecarloMontecarloNumbaOpacities(BenchmarkBase):
Expand All @@ -28,9 +29,7 @@ class BenchmarkMontecarloMontecarloNumbaOpacities(BenchmarkBase):
}
)
def time_compton_opacity_calculation(self, electron_number_density, energy):
calculate_opacity.compton_opacity_calculation(
energy, electron_number_density
)
compton_opacity_calculation(energy, electron_number_density)

@parameterize(
{
Expand Down
2 changes: 1 addition & 1 deletion tardis/io/atom_data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from tardis import constants as const
from tardis.io.atom_data.util import resolve_atom_data_fname
from tardis.plasma.properties.continuum_processes import (
from tardis.plasma.properties.continuum_processes.rates import (
get_ground_state_multi_index,
)

Expand Down
Empty file.
36 changes: 36 additions & 0 deletions tardis/opacities/continuum/bound_free.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from tardis.plasma.exceptions import PlasmaException
from tardis.plasma.properties.base import ProcessingPlasmaProperty


class BoundFreeOpacity(ProcessingPlasmaProperty):
"""
Attributes
----------
chi_bf : pandas.DataFrame, dtype float
Bound-free opacity corrected for stimulated emission.
"""

outputs = ("chi_bf",)
latex_name = (r"\chi^{\textrm{bf}}",)

def calculate(
self,
photo_ion_cross_sections,
t_electrons,
phi_ik,
level_number_density,
lte_level_number_density,
boltzmann_factor_photo_ion,
):
cross_section = photo_ion_cross_sections["x_sect"].values

n_i = level_number_density.loc[photo_ion_cross_sections.index]
lte_n_i = lte_level_number_density.loc[photo_ion_cross_sections.index]
chi_bf = (n_i - lte_n_i * boltzmann_factor_photo_ion).multiply(
cross_section, axis=0
)

num_neg_elements = (chi_bf < 0).sum().sum()
if num_neg_elements:
raise PlasmaException("Negative values in bound-free opacity.")

Check warning on line 35 in tardis/opacities/continuum/bound_free.py

View check run for this annotation

Codecov / codecov/patch

tardis/opacities/continuum/bound_free.py#L35

Added line #L35 was not covered by tests
return chi_bf
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import pandas as pd

from tardis import constants as const
from tardis.plasma.properties.base import (
TransitionProbabilitiesProperty,
)
from tardis.plasma.properties.continuum_processes.rates import (
IndexSetterMixin,
)

H = const.h.cgs.value
wkerzendorf marked this conversation as resolved.
Show resolved Hide resolved


class RawCollIonTransProbs(TransitionProbabilitiesProperty, IndexSetterMixin):
"""
Attributes
----------
p_coll_ion : pandas.DataFrame, dtype float
The unnormalized transition probabilities for
collisional ionization.
p_coll_recomb : pandas.DataFrame, dtype float
The unnormalized transition probabilities for
collisional recombination.
cool_rate_coll_ion : pandas.DataFrame, dtype float
The collisional ionization cooling rates of the electron gas.
"""

outputs = ("p_coll_ion", "p_coll_recomb", "cool_rate_coll_ion")
transition_probabilities_outputs = (
"p_coll_ion",
"p_coll_recomb",
"cool_rate_coll_ion",
)
latex_name = (
r"p^{\textrm{coll ion}}",
r"p^{\textrm{coll recomb}}",
r"C^{\textrm{ion}}",
)

def calculate(
self,
coll_ion_coeff,
coll_recomb_coeff,
nu_i,
photo_ion_idx,
electron_densities,
energy_i,
level_number_density,
):
p_coll_ion = coll_ion_coeff.multiply(energy_i, axis=0)
p_coll_ion = p_coll_ion.multiply(electron_densities, axis=1)
p_coll_ion = self.set_index(p_coll_ion, photo_ion_idx, reverse=False)

coll_recomb_rate = coll_recomb_coeff.multiply(
electron_densities, axis=1
) # The full rate is obtained from this by multiplying by the
# electron density and ion number density.
p_recomb_deactivation = coll_recomb_rate.multiply(nu_i, axis=0) * H
p_recomb_deactivation = self.set_index(
p_recomb_deactivation, photo_ion_idx, transition_type=-1
)
p_recomb_deactivation = p_recomb_deactivation.groupby(level=[0]).sum()
index_dd = pd.MultiIndex.from_product(
[p_recomb_deactivation.index.values, ["k"], [0]],
names=list(photo_ion_idx.columns) + ["transition_type"],
)
p_recomb_deactivation = p_recomb_deactivation.set_index(index_dd)

p_recomb_internal = coll_recomb_rate.multiply(energy_i, axis=0)
p_recomb_internal = self.set_index(
p_recomb_internal, photo_ion_idx, transition_type=0
)
p_coll_recomb = pd.concat([p_recomb_deactivation, p_recomb_internal])

cool_rate_coll_ion = (coll_ion_coeff * electron_densities).multiply(
nu_i * H, axis=0
)
level_lower_index = coll_ion_coeff.index
cool_rate_coll_ion = (
cool_rate_coll_ion
* level_number_density.loc[level_lower_index].values
)
cool_rate_coll_ion = self.set_index(
cool_rate_coll_ion, photo_ion_idx, reverse=False
)
cool_rate_coll_ion = cool_rate_coll_ion.groupby(
level="destination_level_idx"
).sum()
ion_cool_index = pd.MultiIndex.from_product(
[["k"], cool_rate_coll_ion.index.values, [0]],
names=list(photo_ion_idx.columns) + ["transition_type"],
)
cool_rate_coll_ion = cool_rate_coll_ion.set_index(ion_cool_index)
return p_coll_ion, p_coll_recomb, cool_rate_coll_ion
2 changes: 1 addition & 1 deletion tardis/opacities/macro_atom/transition_probabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from scipy import sparse as sp

from tardis.plasma.properties.base import ProcessingPlasmaProperty
from tardis.plasma.properties.continuum_processes import (
from tardis.plasma.properties.continuum_processes.rates import (
get_ground_state_multi_index,
)
from tardis.transport.montecarlo.macro_atom import (
Expand Down
6 changes: 4 additions & 2 deletions tardis/opacities/opacities.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ def compton_opacity_partial(energy, fraction):

@njit(**njit_dict_no_parallel)
def compton_opacity_calculation(energy, electron_density):
andrewfullard marked this conversation as resolved.
Show resolved Hide resolved
"""Calculate the Compton scattering opacity for a given energy
"""
Calculate the Compton scattering opacity for a given energy
(Rybicki & Lightman, 1979)

$
Expand Down Expand Up @@ -364,7 +365,8 @@ def photoabsorption_opacity_calculation(
def photoabsorption_opacity_calculation_kasen(
energy, number_density, proton_count
):
"""Calculates photoabsorption opacity for a given energy
"""
Calculates photoabsorption opacity for a given energy
Approximate treatment from Kasen et al. (2006)

Parameters
Expand Down
43 changes: 25 additions & 18 deletions tardis/plasma/base.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import os
import re
import dataclasses
import fileinput
import logging
import re
import tempfile
import fileinput

import networkx as nx

from tardis.plasma.exceptions import PlasmaMissingModule, NotInitializedModule
from tardis.plasma.properties.base import *
from tardis.io.util import PlasmaWriterMixin
from tardis.plasma.exceptions import NotInitializedModule, PlasmaMissingModule
from tardis.plasma.properties.base import *

logger = logging.getLogger(__name__)


@dataclasses.dataclass(frozen=True)
class PlasmaSolverSettings:
RADIATIVE_RATES_TYPE: str = "blackbody"


class BasePlasma(PlasmaWriterMixin):

outputs_dict = {}
hdf_name = "plasma"

def __init__(self, plasma_properties, property_kwargs=None, **kwargs):
def __init__(
self,
plasma_properties,
plasma_solver_settings,
property_kwargs=None,
**kwargs,
):
self.plasma_solver_settings = plasma_solver_settings
self.outputs_dict = {}
self.input_properties = []
self.plasma_properties = self._init_properties(
Expand Down Expand Up @@ -63,7 +75,6 @@
:param plasma_modules:
:return:
"""

self.graph = nx.DiGraph()
# Adding all nodes
self.graph.add_nodes_from(
Expand Down Expand Up @@ -200,8 +211,7 @@
for key in args:
if key not in self.outputs_dict:
raise PlasmaMissingModule(
"Trying to freeze property {0}"
" that is unavailable".format(key)
f"Trying to freeze property {key}" " that is unavailable"
)
self.outputs_dict[key].frozen = True

Expand All @@ -224,8 +234,7 @@
for key in args:
if key not in self.outputs_dict:
raise PlasmaMissingModule(
"Trying to thaw property {0}"
" that is unavailable".format(key)
f"Trying to thaw property {key}" " that is unavailable"
)
self.outputs_dict[key].frozen = False

Expand All @@ -249,7 +258,6 @@
: list
all affected modules.
"""

descendants_ob = []

for plasma_property in changed_properties:
Expand Down Expand Up @@ -284,11 +292,10 @@
enables/disables writing LaTeX equations and
edge labels into the file.
"""

try:
import pygraphviz
except:
logger.warn(
logger.warning(

Check warning on line 298 in tardis/plasma/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/plasma/base.py#L298

Added line #L298 was not covered by tests
"pygraphviz missing. Plasma graph will not be " "generated."
)
return
Expand All @@ -303,7 +310,7 @@
] = f"\\\\textrm{{{node}: }}"
node_list = self.plasma_properties_dict[node]
formulae = node_list.latex_formula
for output in range(0, len(formulae)):
for output in range(len(formulae)):

Check warning on line 313 in tardis/plasma/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/plasma/base.py#L313

Added line #L313 was not covered by tests
formula = formulae[output]
label = formula.replace("\\", "\\\\")
print_graph.nodes[str(node)]["label"] += label
Expand Down Expand Up @@ -341,7 +348,7 @@
)

if args is not None:
with open(fname, "r") as file:
with open(fname) as file:

Check warning on line 351 in tardis/plasma/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/plasma/base.py#L351

Added line #L351 was not covered by tests
lines = file.readlines()

for newline in args:
Expand Down Expand Up @@ -374,7 +381,7 @@
try:
import dot2tex
except:
logger.warn(
logger.warning(

Check warning on line 384 in tardis/plasma/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/plasma/base.py#L384

Added line #L384 was not covered by tests
"dot2tex missing. Plasma graph will not be " "generated."
)
return
Expand All @@ -383,7 +390,7 @@

self.write_to_dot(temp_fname, args=args, latex_label=latex_label)

with open(temp_fname, "r") as file:
with open(temp_fname) as file:

Check warning on line 393 in tardis/plasma/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/plasma/base.py#L393

Added line #L393 was not covered by tests
dot_string = file.read().replace("\\\\", "\\")

texcode = dot2tex.dot2tex(
Expand Down
1 change: 0 additions & 1 deletion tardis/plasma/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from tardis.plasma.properties.general import *
from tardis.plasma.properties.helium_nlte import *
from tardis.plasma.properties.ion_population import *
from tardis.plasma.properties.j_blues import *
from tardis.plasma.properties.level_population import *
from tardis.plasma.properties.nlte import *
from tardis.plasma.properties.nlte_rate_equation_solver import *
Expand Down
2 changes: 1 addition & 1 deletion tardis/plasma/properties/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
HiddenPlasmaProperty,
ProcessingPlasmaProperty,
)
from tardis.plasma.properties.continuum_processes import (
from tardis.plasma.properties.continuum_processes.rates import (
A0,
BETA_COLL,
K_B,
Expand Down
9 changes: 9 additions & 0 deletions tardis/plasma/properties/continuum_processes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from tardis.plasma.properties.continuum_processes.photo_ion_rate_coeff import (
PhotoIonRateCoeff,
)
from tardis.plasma.properties.continuum_processes.rates import *
from tardis.plasma.properties.continuum_processes.recomb_rate_coeff import (
StimRecombRateFactor,
SpontRecombRateCoeff,
StimRecombRateCoeff,
)
Loading
Loading