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

Spherical #203

Closed
wants to merge 13 commits into from
4 changes: 4 additions & 0 deletions stardis/config_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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
4 changes: 3 additions & 1 deletion stardis/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def parse_config_to_model(config_fname, add_config_keys=None, add_config_vals=No
logging.info("Reading model")
if config.model.type == "marcs":
raw_marcs_model = read_marcs_model(
Path(config.model.fname), gzipped=config.model.gzipped
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
93 changes: 83 additions & 10 deletions stardis/io/model/marcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MARCSModel(object):

metadata: dict
data: pd.DataFrame
spherical: bool

def to_geometry(self):
"""
Expand All @@ -31,10 +32,15 @@ def to_geometry(self):
-------
stardis.model.geometry.radial1d.Radial1DGeometry
"""

reference_r = None
r = (
-self.data.depth.values[::-1] * u.cm
) # Flip data to move from innermost stellar point to surface
return Radial1DGeometry(r)
if self.spherical:
r += self.metadata["radius"]
reference_r = self.metadata["radius"]
return Radial1DGeometry(r, reference_r)

def to_composition(self, atom_data, final_atomic_number):
"""
Expand Down Expand Up @@ -147,7 +153,7 @@ def to_stellar_model(self, atom_data, final_atomic_number=118):
return StellarModel(temperatures, marcs_geometry, marcs_composition)


def read_marcs_metadata(fpath, gzipped=True):
def read_marcs_metadata(fpath, gzipped=True, spherical=False):
"""
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 All @@ -159,14 +165,16 @@ def read_marcs_metadata(fpath, gzipped=True):
Path to model file
gzipped : Bool
Whether or not the file is gzipped
spherical : Bool
Whether or not the model is spherical

Returns
-------
dict : dictionary
metadata parameters of file
"""

METADATA_RE_STR = [
METADATA_PLANE_PARALLEL_RE_STR = [
(r"(.+)\n", "fname"),
(
r" (\d+\.)\s+Teff \[(.+)\]\.\s+Last iteration; yyyymmdd=\d+",
Expand Down Expand Up @@ -210,11 +218,73 @@ def read_marcs_metadata(fpath, gzipped=True):
"12C/13C",
),
]

METADATA_SPHERICAL_RE_STR = [
(r"(.+)\n", "fname"),
(
r" (\d+\.)\s+Teff \[(.+)\]\.\s+Last iteration; yyyymmdd=\d+",
"teff",
"teff_units",
),
(r" (\d+\.\d+E\+\d+) Flux \[(.+)\]", "flux", "flux_units"),
(
r" (\d+.\d+E\+\d+) Surface gravity \[(.+)\]",
"surface_grav",
"surface_grav_units",
),
(
r" (\d+\.\d+)\W+Microturbulence parameter \[(.+)\]",
"microturbulence",
"microturbulence_units",
),
(
r"\s+(\d+\.\d+)\s+Mass \[(.+)\]",
"mass",
"mass_units",
),
(
r" (\+?\-?\d+.\d+) (\+?\-?\d+.\d+) Metallicity \[Fe\/H] and \[alpha\/Fe\]",
"feh",
"afe",
),
(
r" (\d+.\d+E\+\d\d) Radius \[(.+)\] at Tau",
"radius",
"radius_units",
),
(
r"\s+(\d+\.\d+(?:E[+-]?\d+)?) Luminosity \[(.+)\]",
"luminosity",
"luminosity_units",
),
(
r" (\d+.\d+) (\d+.\d+) (\d+.\d+) (\d+.\d+) are the convection parameters: alpha, nu, y and beta",
"conv_alpha",
"conv_nu",
"conv_y",
"conv_beta",
),
(
r" (0.\d+) (0.\d+) (\d.\d+E-\d+) are X, Y and Z, 12C\/13C=(\d+.?\d+)",
"x",
"y",
"z",
"12C/13C",
),
]
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.
metadata_re = [re.compile(re_str[0]) for re_str in METADATA_RE_STR]
# 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:
Expand All @@ -227,10 +297,11 @@ def read_marcs_metadata(fpath, gzipped=True):

lines = list(contents)

for i, line in enumerate(lines):
# 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]
metadata_re_match = metadata_re[i].match(line)

for j, metadata_name in enumerate(METADATA_RE_STR[i][1:]):
for j, metadata_name in enumerate(metadata_re_str[i][1:]):
metadata[metadata_name] = metadata_re_match.group(j + 1)

# clean up metadata dictionary by changing strings of numbers to floats and attaching parsed units where appropriate
Expand Down Expand Up @@ -316,21 +387,23 @@ def read_marcs_data(fpath, gzipped=True):
return marcs_model_data


def read_marcs_model(fpath, gzipped=True):
def read_marcs_model(fpath, gzipped=True, spherical=False):
"""
Parameters
----------
fpath : str
Path to model file
gzipped : Bool
Whether or not the file is gzipped
spherical : Bool
Whether or not the model is spherical

Returns
-------
model : MARCSModel
Assembled metadata and data pair of a MARCS model
"""
metadata = read_marcs_metadata(fpath, gzipped=gzipped)
metadata = read_marcs_metadata(fpath, gzipped=gzipped, spherical=spherical)
data = read_marcs_data(fpath, gzipped=gzipped)

return MARCSModel(metadata, data)
return MARCSModel(metadata, data, spherical=spherical)
4 changes: 2 additions & 2 deletions stardis/model/geometry/radial1d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Radial1DGeometry:

"""
Holds information about model geometry (distribution of depth points) for radial 1D models.

Expand All @@ -15,8 +14,9 @@ class Radial1DGeometry:
distance to the next depth point
"""

def __init__(self, r):
def __init__(self, r, reference_r=None):
self.r = r
self.reference_r = reference_r

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

return stellar_radiation_field
Loading
Loading