From b482e1bde71b1a9e3c11021294e0cf7975e59260 Mon Sep 17 00:00:00 2001 From: Andrew Fullard <20341051+andrewfullard@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:22:40 -0500 Subject: [PATCH] Allow workflows to specify n_e tolerance and fix it for the IIP workflow --- tardis/plasma/assembly/base.py | 38 ++++++++--- tardis/plasma/properties/ion_population.py | 73 ++++++++++++++++++--- tardis/workflows/tests/test_iip_workflow.py | 16 +++++ tardis/workflows/type_iip_workflow.py | 6 ++ 4 files changed, 114 insertions(+), 19 deletions(-) diff --git a/tardis/plasma/assembly/base.py b/tardis/plasma/assembly/base.py index 7eb78cc608f..44c93db7c5a 100644 --- a/tardis/plasma/assembly/base.py +++ b/tardis/plasma/assembly/base.py @@ -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 @@ -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 @@ -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, diff --git a/tardis/plasma/properties/ion_population.py b/tardis/plasma/properties/ion_population.py index a5993e58664..8455bb69878 100644 --- a/tardis/plasma/properties/ion_population.py +++ b/tardis/plasma/properties/ion_population.py @@ -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", @@ -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( @@ -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 @@ -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) @@ -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 @@ -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: @@ -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) diff --git a/tardis/workflows/tests/test_iip_workflow.py b/tardis/workflows/tests/test_iip_workflow.py index ac7f70c80b4..c0869df0c48 100644 --- a/tardis/workflows/tests/test_iip_workflow.py +++ b/tardis/workflows/tests/test_iip_workflow.py @@ -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, diff --git a/tardis/workflows/type_iip_workflow.py b/tardis/workflows/type_iip_workflow.py index 9c07494900e..cd5cc888e59 100644 --- a/tardis/workflows/type_iip_workflow.py +++ b/tardis/workflows/type_iip_workflow.py @@ -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.""" @@ -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)]) )