Stellar Model Calibration using Python

This Python script is part of a larger project focusing on the calibration of stellar models using opacity data from various sources (e.g., Ferguson and Aesopus). It demonstrates the process of calibrating solar models, updating model controls, evolving stellar models in parallel, and saving the calibration results with the pickle module. The script handles file operations, utilizes custom libraries for stellar physics, and highlights techniques for optimizing stellar models based on low-temperature opacity files.

from pysep.dsep.calibrate import LRZX_calib_solar, calibrate_Z_post_XML
 
from pysep.api.starting import get_basic_stellar_model
from pysep.dm.filetypes import opacf_file
 
from pysep.api.parallel import pStellarModel
 
import os
import pickle
 
FergLowTempRoot = "./inputs/opac/low/ferg04"
AesoLowTempRoot1 = "./inputs/opac/low/aesopusExtend"
AesoLowTempRoot2 = "./inputs/opac/low/aesopus"
 
FergFiles = filter(lambda x: x.endswith(".tron"), os.listdir(FergLowTempRoot))
AesoFiles1 = filter(lambda x: x.endswith(".tron"), os.listdir(AesoLowTempRoot1))
AesoFiles2 = filter(lambda x: x.endswith(".tron"), os.listdir(AesoLowTempRoot2))
 
FergPaths = [os.path.abspath(os.path.join(FergLowTempRoot, x)) for x in FergFiles]
AesoPaths1 = [os.path.abspath(os.path.join(AesoLowTempRoot1, x)) for x in AesoFiles1]
AesoPaths2 = [os.path.abspath(os.path.join(AesoLowTempRoot2, x)) for x in AesoFiles2]
 
# if not os.path.exists("./fergOutput"):
#     os.mkdir("./fergOutput")
# if not os.path.exists("./aesopusExtendOutput"):
#     os.mkdir("./aesopusExtendOutput")
# if not os.path.exists("./aesopusOutput"):
#     os.mkdir("./aesopusOutput")
#
# FergModel = get_basic_stellar_model("./fergOutput")
# AesoModel1 = get_basic_stellar_model("./aesopusExtendOutput")
# AesoModel2 = get_basic_stellar_model("./aesopusOutput")
#
# FergModel.control.update_low_temp_opacity_files(FergPaths)
# AesoModel1.control.update_low_temp_opacity_files(AesoPaths1)
# AesoModel2.control.update_low_temp_opacity_files(AesoPaths2)
#
# FergCalibration = LRZX_calib_solar(FergModel, 0.7, 0.02, 1.9)
# # AesoCalibration1 = LRZX_calib_solar(AesoModel1, 0.7, 0.02, 1.9)
# AesoCalibration = LRZX_calib_solar(AesoModel2, 0.7, 0.02, 1.9)
#
# # FergCalibration = calibrate_Z_post_XML(FergModel)
# # AesoCalibration1 = calibrate_Z_post_XML(AesoModel1)
# # AesoCalibration2 = calibrate_Z_post_XML(AesoModel2)
#
#
# print(f"Ferguson Calibration: {FergCalibration}")
# # print(f"Aesopus Extended Calibration: {AesoCalibration1}")
# print(f"Aesopus Glued Calibration: {AesoCalibration}")
 
FergCalibration = {
        'x' : [0.71104982, 0.01807735, 1.89857451]
        # 'x': [0.6771655226243738, 0.023578646794921738, 1.941]
        }
AesoCalibration = {
        'x' : [0.70692733, 0.01877248, 1.89937269]
        # 'x' : [0.6771997223238397, 0.023574373280217716, 1.941]
        }
 
if not os.path.exists("./fergCalibOutput"):
    os.mkdir("./fergCalibOutput")
if not os.path.exists("./aesopusCalibOutput"):
    os.mkdir("./aesopusCalibOutput")
 
 
FergCalibModel = get_basic_stellar_model("./fergCalibOutput")
AesoCalibModel = get_basic_stellar_model("./aesopusCalibOutput")
 
FergCalibModel.control.add_key("LBNOUT", True)
FergCalibModel.control.add_key("NBN", 1)
AesoCalibModel.control.add_key("LBNOUT", True)
AesoCalibModel.control.add_key("NBN", 1)
 
FergCalibModel.control['lpulse'] = True
AesoCalibModel.control['lpulse'] = True
 
FergCalibModel.control.update_low_temp_opacity_files(FergPaths)
AesoCalibModel.control.update_low_temp_opacity_files(AesoPaths2)
 
FergCalibModel.update_composition(X=FergCalibration['x'][0],
        Z=FergCalibration['x'][1])
FergCalibModel.control[0]['cmixla'] = FergCalibration['x'][2]
FergCalibModel.control[1]['cmixla'] = FergCalibration['x'][2]
 
 
AesoCalibModel.update_composition(X=AesoCalibration['x'][0],
        Z=AesoCalibration['x'][1])
AesoCalibModel.control[0]['cmixla'] = AesoCalibration['x'][2]
AesoCalibModel.control[1]['cmixla'] = AesoCalibration['x'][2]
 
FergCalibModel.control[1]['endage'] = 12.1e9
AesoCalibModel.control[1]['endage'] = 12.1e9
 
 
pMod = pStellarModel([FergCalibModel, AesoCalibModel], name="Calibration Results")
pMod.pEvolve(autoStash=True)
 
with open("FergCalibration.pkl", 'wb') as f:
    pickle.dump(FergCalibration, f)
 
with open("AesoCalibration.pkl", "wb") as f:
    pickle.dump(AesoCalibration, f)