Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into add_individual_inte…
Browse files Browse the repository at this point in the history
…nsities
  • Loading branch information
jvshields committed Oct 14, 2024
2 parents 1cd79ae + b921279 commit 2d22a75
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
14 changes: 4 additions & 10 deletions stardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
logger = logging.getLogger(__name__)


def run_stardis(
config_fname, tracing_lambdas_or_nus, add_config_keys=None, add_config_vals=None
):
def run_stardis(config_fname, tracing_lambdas_or_nus, add_config_dict=None):
"""
Runs a STARDIS simulation.
Expand All @@ -24,10 +22,8 @@ def run_stardis(
Numpy array of the frequencies or wavelengths to calculate the
spectrum for. Must have units attached to it, with dimensions
of either length or inverse time.
add_config_keys : list, optional
List of additional keys to add or overwrite for the configuration file.
add_config_vals : list, optional
List of corresponding additional values to add to the configuration file.
add_config_dict : dict, optional
Dictionary containing the the keys and values of the configuration to add or overwrite.
Returns
-------
Expand All @@ -37,9 +33,7 @@ def run_stardis(

tracing_nus = tracing_lambdas_or_nus.to(u.Hz, u.spectral())

config, adata, stellar_model = parse_config_to_model(
config_fname, add_config_keys, add_config_vals
)
config, adata, stellar_model = parse_config_to_model(config_fname, add_config_dict)
set_num_threads(config.n_threads)
stellar_plasma = create_stellar_plasma(stellar_model, adata, config)
stellar_radiation_field = create_stellar_radiation_field(
Expand Down
20 changes: 5 additions & 15 deletions stardis/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
logger = logging.getLogger(__name__)


def parse_config_to_model(config_fname, add_config_keys=None, add_config_vals=None):
def parse_config_to_model(config_fname, add_config_dict):
"""
Parses the config and model files and outputs python objects to be passed into run stardis so they can be individually modified in python.
Expand Down Expand Up @@ -47,28 +47,18 @@ def parse_config_to_model(config_fname, add_config_keys=None, add_config_vals=No
raise ValueError("Config failed to validate. Check the config file.")

if (
not add_config_keys
not add_config_dict
): # If a dictionary was passed, update the config with the dictionary
pass
else:
logger.info("Updating config with additional keys and values")
if isinstance(add_config_keys, str):
# Directly set the config item if add_config_keys is a string
config.set_config_item(add_config_keys, add_config_vals)
else:
# Proceed with iteration if add_config_keys is not a string
if len(add_config_keys) != len(add_config_vals):
raise ValueError(
"Length of additional config keys and values do not match."
)
for key, val in add_config_dict.items():
try:
for key, val in zip(add_config_keys, add_config_vals):
config.set_config_item(key, val)
config.set_config_item(key, val)
except:
raise ValueError(
f"{add_config_keys} not a valid type. Should be a single string or a list of strings for keys."
f"{key} not a valid type. Should be a string for keys."
)

try:
config_dict = validate_dict(config, schemapath=SCHEMA_PATH)
except:
Expand Down
7 changes: 4 additions & 3 deletions stardis/plasma/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging

from astropy import constants as const, units as u
from scipy.interpolate import interp1d

from tardis.plasma.base import BasePlasma
from tardis.plasma.properties.base import (
Expand Down Expand Up @@ -118,11 +117,13 @@ class H2PlusDensity(ProcessingPlasmaProperty):
outputs = ("h2_plus_density",)

def calculate(self, ion_number_density, t_rad):
interp_Ks = interp1d(H2_PLUS_K_SAMPLE_TEMPS, H2_PLUS_K_EQUILIBRIUM_CONSTANT)
h_neutral_density = ion_number_density.loc[1, 0]
h_plus_density = ion_number_density.loc[1, 1]
resampled_Ks = np.interp(
t_rad, H2_PLUS_K_SAMPLE_TEMPS, H2_PLUS_K_EQUILIBRIUM_CONSTANT
)
return (
h_neutral_density * h_plus_density / interp_Ks(t_rad) * 1e-19
h_neutral_density * h_plus_density / resampled_Ks * 1e-19
) # scale factor from Stancil 1994 table 1


Expand Down
11 changes: 3 additions & 8 deletions stardis/radiation_field/opacities/opacities_solvers/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from astropy import units as u, constants as const
from tardis.util.base import species_string_to_tuple

from scipy.interpolate import interp1d, LinearNDInterpolator
from scipy.interpolate import LinearNDInterpolator


def sigma_file(tracing_lambdas, temperatures, fpath, opacity_source=None):
Expand Down Expand Up @@ -83,16 +83,11 @@ def sigma_file(tracing_lambdas, temperatures, fpath, opacity_source=None):
h_minus_bf_table = pd.read_csv(
fpath, header=None, comment="#", names=["wavelength", "cross_section"]
)
linear_interp_1d_from_file = interp1d(
sigmas = np.interp(
tracing_lambdas,
h_minus_bf_table.wavelength.values,
h_minus_bf_table.cross_section.values,
bounds_error=False,
fill_value=(
h_minus_bf_table.cross_section.iloc[0],
h_minus_bf_table.cross_section.iloc[-1],
),
)
sigmas = linear_interp_1d_from_file(tracing_lambdas)

else:
raise ValueError(f"Unknown opacity_source: {opacity_source}")
Expand Down

0 comments on commit 2d22a75

Please sign in to comment.