From 3f4eda23bcb272942a3d2ed49364897a267231fd Mon Sep 17 00:00:00 2001 From: carlnotsagan Date: Thu, 3 Oct 2024 13:22:17 -0700 Subject: [PATCH 1/3] a short script for taking mesa input and preparing it for the grsolver --- .../python/grsolver/read_MESA_for_grsolver.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 scripts/python/grsolver/read_MESA_for_grsolver.py diff --git a/scripts/python/grsolver/read_MESA_for_grsolver.py b/scripts/python/grsolver/read_MESA_for_grsolver.py new file mode 100644 index 00000000..cec1b4ad --- /dev/null +++ b/scripts/python/grsolver/read_MESA_for_grsolver.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +""" +This script retrieves raw MESA profile data variables, reverses them to be center to surface, and outputs them in the format needed for GRSOLVER for use in Phoebus. + +Usage: + python read_MESA_for_grsolver.py --file='20m_cc' --ext='.dat' + +Arguments: + input_file: filename and file extension of MESA profile +""" + +import numpy as np +import pandas as pd +from argparse import ArgumentParser + + +def load(file, ext): + df = pd.read_csv(file + ext, sep=r"\s+", header=4) + df = df.iloc[::-1].reset_index(drop=True) + nzones = len(df) + if nzones > 0: + print("loaded file:", file + ext, " with ", nzones, " zones.") + else: + print("failed to load file", file + ext) + exit() + return df + + +def retrieve_and_save(df, file): + radius = df["radius_cm"] + velocity = df["velocity"] + # if model non-rotating, pass array of zeros + try: + angular_velocity = df["omega"] + except: + angular_velocity = np.zeros_like((radius)) + density = df["density"] # g/cm^3 + pressure = df["pressure"] # g/cm^3 + ye = df["ye"] + temperature = df["temperature"] # K + sie = df["energy"] # ! internal energy (ergs/g) + + np.savetxt( + file + str("_raw_data.txt"), + np.column_stack( + [ + radius, + velocity, + angular_velocity, + density, + pressure, + ye, + temperature, + sie, + ] + ), + header="MESA output from model `" + str(file) + "` | \ " + "radius (cm) velocity (cm/s) angular_velocity (rad/s) density (g/cm^3) pressure (dyne/cm^2) ye temperature (K) specific internal energy (erg/g)", + ) + return + + +def main(): + parser = ArgumentParser( + description="Retrieve raw MESA profile data variables and output to format needed for GRSOLVER for use in Phoebus." + ) + + parser.add_argument( + "--file", + type=str, + default="file", + help="name of the input MESA file without extension", + ) + + parser.add_argument( + "--ext", + type=str, + default=".dat", + help="input MESA file extension", + ) + + args = parser.parse_args() + print(args.file, args.ext) + df = load(args.file, args.ext) + retrieve_and_save(df, args.file) + + return + + +if __name__ == "__main__": + main() From 0dbd0e148d837be571a254579d8c433502951ee3 Mon Sep 17 00:00:00 2001 From: carlnotsagan Date: Thu, 3 Oct 2024 14:45:43 -0700 Subject: [PATCH 2/3] detail exception, reduce extraneous variables for filename --- .../python/grsolver/read_MESA_for_grsolver.py | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/scripts/python/grsolver/read_MESA_for_grsolver.py b/scripts/python/grsolver/read_MESA_for_grsolver.py index cec1b4ad..c2d78876 100644 --- a/scripts/python/grsolver/read_MESA_for_grsolver.py +++ b/scripts/python/grsolver/read_MESA_for_grsolver.py @@ -4,10 +4,10 @@ This script retrieves raw MESA profile data variables, reverses them to be center to surface, and outputs them in the format needed for GRSOLVER for use in Phoebus. Usage: - python read_MESA_for_grsolver.py --file='20m_cc' --ext='.dat' + python read_MESA_for_grsolver.py --file='20m_cc.dat' Arguments: - input_file: filename and file extension of MESA profile + input_file: filename of MESA profile """ import numpy as np @@ -15,14 +15,14 @@ from argparse import ArgumentParser -def load(file, ext): - df = pd.read_csv(file + ext, sep=r"\s+", header=4) +def load(file): + df = pd.read_csv(file, sep=r"\s+", header=4) df = df.iloc[::-1].reset_index(drop=True) nzones = len(df) if nzones > 0: - print("loaded file:", file + ext, " with ", nzones, " zones.") + print("loaded file:", file, " with ", nzones, " zones.") else: - print("failed to load file", file + ext) + print("failed to load file", file) exit() return df @@ -33,7 +33,8 @@ def retrieve_and_save(df, file): # if model non-rotating, pass array of zeros try: angular_velocity = df["omega"] - except: + except KeyError: + print("angular velocity not found, setting to zero.") angular_velocity = np.zeros_like((radius)) density = df["density"] # g/cm^3 pressure = df["pressure"] # g/cm^3 @@ -42,7 +43,7 @@ def retrieve_and_save(df, file): sie = df["energy"] # ! internal energy (ergs/g) np.savetxt( - file + str("_raw_data.txt"), + file.split(".")[0] + str("_raw_data.txt"), np.column_stack( [ radius, @@ -55,7 +56,7 @@ def retrieve_and_save(df, file): sie, ] ), - header="MESA output from model `" + str(file) + "` | \ " + header="MESA output from model `" + str(file.split(".")[0]) + "` | \ " "radius (cm) velocity (cm/s) angular_velocity (rad/s) density (g/cm^3) pressure (dyne/cm^2) ye temperature (K) specific internal energy (erg/g)", ) return @@ -70,19 +71,12 @@ def main(): "--file", type=str, default="file", - help="name of the input MESA file without extension", - ) - - parser.add_argument( - "--ext", - type=str, - default=".dat", - help="input MESA file extension", + help="name of the input MESA file", ) args = parser.parse_args() - print(args.file, args.ext) - df = load(args.file, args.ext) + print(args.file) + df = load(args.file) retrieve_and_save(df, args.file) return From 94984ee76db25c11b22a2bb7853cdb0aba22d3ea Mon Sep 17 00:00:00 2001 From: carlnotsagan Date: Thu, 3 Oct 2024 17:24:17 -0700 Subject: [PATCH 3/3] positional and required filename --- scripts/python/grsolver/read_MESA_for_grsolver.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/scripts/python/grsolver/read_MESA_for_grsolver.py b/scripts/python/grsolver/read_MESA_for_grsolver.py index c2d78876..c6c64e32 100644 --- a/scripts/python/grsolver/read_MESA_for_grsolver.py +++ b/scripts/python/grsolver/read_MESA_for_grsolver.py @@ -4,7 +4,7 @@ This script retrieves raw MESA profile data variables, reverses them to be center to surface, and outputs them in the format needed for GRSOLVER for use in Phoebus. Usage: - python read_MESA_for_grsolver.py --file='20m_cc.dat' + python read_MESA_for_grsolver.py 20m_cc.dat Arguments: input_file: filename of MESA profile @@ -66,16 +66,14 @@ def main(): parser = ArgumentParser( description="Retrieve raw MESA profile data variables and output to format needed for GRSOLVER for use in Phoebus." ) - parser.add_argument( - "--file", + "file", type=str, default="file", help="name of the input MESA file", ) - args = parser.parse_args() - print(args.file) + df = load(args.file) retrieve_and_save(df, args.file)