Skip to content
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
38 changes: 29 additions & 9 deletions tardis/plasma/assembly/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
RadiationFieldCorrection,
StimulatedEmissionFactor,
)
from tardis.plasma.properties.ion_population import (
ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD,
)
from tardis.util.base import species_string_to_tuple


Expand All @@ -36,6 +39,13 @@ class PlasmaSolverFactory:

radiative_rates_type: str = "dilute-blackbody"

## Ionization solve
# Relative change of n_e between iterations below which the ionization
# solve stops; it bounds the charge mismatch of the solved n_e.
electron_number_density_convergence_threshold: float = (
ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD
)

delta_treatment = None

## Statistical Balance Solver
Expand Down Expand Up @@ -323,15 +333,25 @@ def set_nlte_species_from_string(self, nlte_species: list[object]) -> None:
self.legacy_nlte_species = map_species_from_string(nlte_species)

def setup_electron_densities(self, electron_densities: object) -> None:
"""Configure the electron-density input for the helium treatment."""
if self.helium_treatment == "numerical-nlte":
self.property_kwargs[IonNumberDensityHeNLTE] = dict(
electron_densities=electron_densities
)
else:
self.property_kwargs[IonNumberDensity] = dict(
electron_densities=electron_densities
)
"""Configure the electron-number-density inputs of the ionization solve.

Parameters
----------
electron_densities : object
Fixed electron number densities, or None to solve them with the ion
populations to ``electron_number_density_convergence_threshold``.
"""
ion_number_density_property = (
IonNumberDensityHeNLTE
if self.helium_treatment == "numerical-nlte"
else IonNumberDensity
)
self.property_kwargs[ion_number_density_property] = dict(
electron_densities=electron_densities,
electron_number_density_convergence_threshold=(
self.electron_number_density_convergence_threshold
),
)

def assemble(
self,
Expand Down
73 changes: 63 additions & 10 deletions tardis/plasma/properties/ion_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
logger = logging.getLogger(__name__)

ION_ZERO_THRESHOLD = 1e-20
# The ionization solve stops once n_e changes by less than this fraction
# between iterations, so the returned n_e can differ from the free-electron
# number density of the returned ions by up to this fraction.
ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD = 0.05

__all__ = [
"PhiSahaNebular",
Expand Down Expand Up @@ -284,14 +288,38 @@ class IonNumberDensity(ProcessingPlasmaProperty):

def __init__(
self,
plasma_parent,
ion_zero_threshold=ION_ZERO_THRESHOLD,
electron_densities=None,
):
plasma_parent: object,
ion_zero_threshold: float = ION_ZERO_THRESHOLD,
electron_densities: pd.Series | None = None,
electron_number_density_convergence_threshold: float = (
ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD
),
) -> None:
"""Initialize the ion number density property.

Parameters
----------
plasma_parent : object
Plasma graph that owns this property.
ion_zero_threshold : float, optional
Ion number density below which populations are set to zero
[cm^-3].
electron_densities : pandas.Series or None, optional
Fixed electron number densities [cm^-3]. If None, they are solved
together with the ion populations.
electron_number_density_convergence_threshold : float, optional
Relative change of the electron number density between iterations
below which the solve stops. It bounds the relative difference
between the returned electron number density and the
free-electron number density of the returned ions.
"""
super().__init__(plasma_parent)
self.ion_zero_threshold = ion_zero_threshold
self.block_ids = None
self._electron_densities = electron_densities
self.electron_number_density_convergence_threshold = (
electron_number_density_convergence_threshold
)

@staticmethod
def calculate_with_n_electron(
Expand Down Expand Up @@ -337,7 +365,6 @@ def _calculate_block_ids(phi):

def calculate(self, phi, partition_function, number_density):
if self._electron_densities is None:
n_e_convergence_threshold = 0.05
n_electron = number_density.sum(axis=0)
n_electron_iterations = 0

Expand Down Expand Up @@ -372,7 +399,7 @@ def calculate(self, phi, partition_function, number_density):
)
if np.all(
np.abs(new_n_electron - n_electron) / n_electron
< n_e_convergence_threshold
< self.electron_number_density_convergence_threshold
):
break
n_electron = 0.5 * (new_n_electron + n_electron)
Expand Down Expand Up @@ -419,12 +446,39 @@ class IonNumberDensityHeNLTE(ProcessingPlasmaProperty):
)

def __init__(
self, plasma_parent, ion_zero_threshold=1e-20, electron_densities=None
):
self,
plasma_parent: object,
ion_zero_threshold: float = 1e-20,
electron_densities: pd.Series | None = None,
electron_number_density_convergence_threshold: float = (
ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD
),
) -> None:
"""Initialize the helium-NLTE ion number density property.

Parameters
----------
plasma_parent : object
Plasma graph that owns this property.
ion_zero_threshold : float, optional
Ion number density below which populations are set to zero
[cm^-3].
electron_densities : pandas.Series or None, optional
Fixed electron number densities [cm^-3]. If None, they are solved
together with the ion populations.
electron_number_density_convergence_threshold : float, optional
Relative change of the electron number density between iterations
below which the solve stops. It bounds the relative difference
between the returned electron number density and the
free-electron number density of the returned ions.
"""
super().__init__(plasma_parent)
self.ion_zero_threshold = ion_zero_threshold
self.block_ids = None
self._electron_densities = electron_densities
self.electron_number_density_convergence_threshold = (
electron_number_density_convergence_threshold
)

def update_he_population(
self, helium_population, n_electron, number_density
Expand All @@ -451,7 +505,6 @@ def calculate(
self, phi, partition_function, number_density, helium_population
):
if self._electron_densities is None:
n_e_convergence_threshold = 0.05
n_electron = number_density.sum(axis=0)
n_electron_iterations = 0
while True:
Expand Down Expand Up @@ -497,7 +550,7 @@ def calculate(
)
if np.all(
np.abs(new_n_electron - n_electron) / n_electron
< n_e_convergence_threshold
< self.electron_number_density_convergence_threshold
):
break
n_electron = 0.5 * (new_n_electron + n_electron)
Expand Down
16 changes: 16 additions & 0 deletions tardis/workflows/tests/test_iip_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ def test_workflow_initial_populations_match_ctardis(
)


def test_workflow_initial_electron_number_density_conserves_charge(
type_iip_workflow: TypeIIPWorkflow,
) -> None:
"""The initial n_e equals the free-electron number density of the ions."""
ion_number_density = type_iip_workflow.plasma_solver.ion_number_density
ion_charge = ion_number_density.index.get_level_values("ion_number")
free_electron_number_density = ion_number_density.multiply(
ion_charge, axis=0
).sum()
np.testing.assert_allclose(
type_iip_workflow.plasma_solver.electron_densities,
free_electron_number_density,
rtol=1e-9,
)


def test_workflow_initial_opacity_matches_ctardis(
type_iip_workflow: TypeIIPWorkflow,
ctardis_reference_path: Path,
Expand Down
6 changes: 6 additions & 0 deletions tardis/workflows/type_iip_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
# logging support
logger = logging.getLogger(__name__)

# Relative n_e convergence threshold of the initial ionization solve.
INITIAL_ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD = 1e-10


class TypeIIPWorkflow:
"""Coordinate Type IIP plasma, transport, and thermal-balance solves."""
Expand Down Expand Up @@ -156,6 +159,9 @@ def __init__(

factory = PlasmaSolverFactory(self.atom_data, configuration)
factory.helium_treatment = "none"
factory.electron_number_density_convergence_threshold = (
INITIAL_ELECTRON_NUMBER_DENSITY_CONVERGENCE_THRESHOLD
)
factory.legacy_nlte_species = list(
dict.fromkeys([*factory.legacy_nlte_species, (1, 0)])
)
Expand Down
Loading