Skip to content

Commit

Permalink
make marcs reader read whether the model is plane parallel or spherical
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshields committed Oct 4, 2024
1 parent 57d7375 commit b2dd7b0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
4 changes: 0 additions & 4 deletions stardis/config_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ properties:
type: boolean
default: false
description: Whether the file is gzipped. Only used for marcs models.
spherical:
type: boolean
default: false
description: Whether the model is spherical. Only used for marcs models.
final_atomic_number:
type: number
multipleOf: 1
Expand Down
1 change: 0 additions & 1 deletion stardis/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def parse_config_to_model(config_fname, add_config_keys=None, add_config_vals=No
raw_marcs_model = read_marcs_model(
Path(config.model.fname),
gzipped=config.model.gzipped,
spherical=config.model.spherical,
)
stellar_model = raw_marcs_model.to_stellar_model(
adata, final_atomic_number=config.model.final_atomic_number
Expand Down
45 changes: 27 additions & 18 deletions stardis/io/model/marcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from stardis.model.base import StellarModel
from tardis.model.matter.composition import Composition

logger = logging.getLogger(__name__)


@dataclass
class MARCSModel(object):
Expand Down Expand Up @@ -150,10 +152,12 @@ def to_stellar_model(self, atom_data, final_atomic_number=118):
self.data.t.values[::-1] * u.K
) # Flip data to move from innermost stellar point to surface
# First two none values are old fv_geometry and abundances which are replaced by the new structures.
return StellarModel(temperatures, marcs_geometry, marcs_composition)
return StellarModel(
temperatures, marcs_geometry, marcs_composition, spherical=self.spherical
)


def read_marcs_metadata(fpath, gzipped=True, spherical=False):
def read_marcs_metadata(fpath, gzipped=True):
"""
Grabs the metadata information from a gzipped MARCS model file and returns it in a python dictionary.
Matches the metadata information and units using regex. Assumes file line structure of plane-parallel models.
Expand Down Expand Up @@ -274,19 +278,6 @@ def read_marcs_metadata(fpath, gzipped=True, spherical=False):
]
BYTES_THROUGH_METADATA = 550

# Compile each of the regex pattern strings then open the file and match each of the patterns by line.
# Then add each of the matched patterns as a key:value pair to the metadata dict.
# Files are formatted a little differently depending on if the MARCS model is spherical or plane-parallel
if spherical:
metadata_re = [re.compile(re_str[0]) for re_str in METADATA_SPHERICAL_RE_STR]
metadata_re_str = METADATA_SPHERICAL_RE_STR
else:
metadata_re = [
re.compile(re_str[0]) for re_str in METADATA_PLANE_PARALLEL_RE_STR
]
metadata_re_str = METADATA_PLANE_PARALLEL_RE_STR
metadata = {}

if gzipped:
with gzip.open(fpath, "rt") as file:
contents = file.readlines(BYTES_THROUGH_METADATA)
Expand All @@ -297,6 +288,24 @@ def read_marcs_metadata(fpath, gzipped=True, spherical=False):

lines = list(contents)

# Compile each of the regex pattern strings then open the file and match each of the patterns by line.
# Then add each of the matched patterns as a key:value pair to the metadata dict.
# Files are formatted a little differently depending on if the MARCS model is spherical or plane-parallel
if "plane-parallel" in lines[5]:
logger.info("Plane-parallel model detected.")
spherical = False
metadata_re = [
re.compile(re_str[0]) for re_str in METADATA_PLANE_PARALLEL_RE_STR
]
metadata_re_str = METADATA_PLANE_PARALLEL_RE_STR
else:
logger.info("Spherical model detected.")
spherical = True
metadata_re = [re.compile(re_str[0]) for re_str in METADATA_SPHERICAL_RE_STR]
metadata_re_str = METADATA_SPHERICAL_RE_STR

metadata = {}

# Check each line against the regex patterns and add the matched values to the metadata dictionary
for i in range(len(metadata_re_str)):
line = lines[i]
Expand All @@ -315,7 +324,7 @@ def read_marcs_metadata(fpath, gzipped=True, spherical=False):
metadata[key] = float(metadata[key])
metadata = {key: metadata[key] for key in metadata if key not in keys_to_remove}

return metadata
return metadata, spherical


def read_marcs_data(fpath, gzipped=True):
Expand Down Expand Up @@ -387,7 +396,7 @@ def read_marcs_data(fpath, gzipped=True):
return marcs_model_data


def read_marcs_model(fpath, gzipped=True, spherical=False):
def read_marcs_model(fpath, gzipped=True):
"""
Parameters
----------
Expand All @@ -404,7 +413,7 @@ def read_marcs_model(fpath, gzipped=True, spherical=False):
Assembled metadata and data pair of a MARCS model
"""
try:
metadata = read_marcs_metadata(fpath, gzipped=gzipped, spherical=spherical)
metadata, spherical = read_marcs_metadata(fpath, gzipped=gzipped)
except:
raise ValueError(
"Failed to read metadata from MARCS model file. Make sure that you are specifying if the file is gzipped, and whether the model is spherical or plane-parallel appropriately."
Expand Down
5 changes: 4 additions & 1 deletion stardis/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class StellarModel:
Composition of the model. Includes density and atomic mass fractions.
no_of_depth_points : int
Class attribute to be easily accessible for initializing arrays that need to match the shape of the model.
spherical : bool
Flag for spherical geometry.
"""

def __init__(self, temperatures, geometry, composition):
def __init__(self, temperatures, geometry, composition, spherical=False):
self.temperatures = temperatures
self.geometry = geometry
self.composition = composition
self.spherical = spherical

@property
def no_of_depth_points(self):
Expand Down
1 change: 0 additions & 1 deletion stardis/radiation_field/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def create_stellar_radiation_field(tracing_nus, stellar_model, stellar_plasma, c
stellar_model,
stellar_radiation_field,
n_threads=config.n_threads,
spherical=config.model.spherical,
)

return stellar_radiation_field
5 changes: 2 additions & 3 deletions stardis/radiation_field/radiation_field_solvers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ def raytrace(
stellar_model,
stellar_radiation_field,
n_threads=1,
spherical=False,
):
"""
Raytraces over many angles and integrates to get flux using the midpoint
Expand All @@ -395,7 +394,7 @@ def raytrace(
each depth_point for each frequency in tracing_nus.
"""

if spherical:
if stellar_model.spherical:
ray_distances = calculate_spherical_ray(
stellar_radiation_field.thetas, stellar_model.geometry.r
)
Expand Down Expand Up @@ -438,7 +437,7 @@ def raytrace(
I_nu * stellar_radiation_field.I_nus_weights[theta_index]
)

if spherical:
if stellar_model.spherical:
photospheric_correction = (
stellar_model.geometry.r[-1] / stellar_model.geometry.reference_r
) ** 2
Expand Down

0 comments on commit b2dd7b0

Please sign in to comment.