Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gautam-404 committed Nov 15, 2023
2 parents b908f9c + 239e235 commit e5b2d95
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 45 deletions.
60 changes: 30 additions & 30 deletions mesaport/Access/gyre_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from . import access_helper
from .support.utils import cwd

from threading import Lock
file_operation_lock = Lock()

"""
This module defines the `GyreAccess` class, which handles GYRE input file operations.
Expand All @@ -20,19 +23,15 @@
"""

class GyreAccess:
def __init__(self, path, binary=False, target=''):
def __init__(self):
"""
Args:
path (str): The path to the GYRE input file.
binary (str): The name of the binary file to be used.
target (str): The target to be used.
"""
self.check_env()
self.path = path
self.binary = binary
self.target = target
self.projectDir = os.path.abspath(path)
self.gyre_in = None
self.default_sections = self.gyreDefaults()

def check_env(self):
"""
Expand Down Expand Up @@ -89,35 +88,36 @@ def writetoGyreFile(self, wdir, parameter, value, default_section=None, gyre_in=
sections (list): A list with the sections of the inlist file.
"""
if default_section is None:
for section, values in self.gyreDefaults().items():
for section, values in self.default_sections.items():
if parameter in values:
default_section = section
if default_section is None:
raise(f"Parameter {parameter} not found in any GYRE input files.")
this_section = False
with cwd(wdir):
with open(gyre_in, "r") as file:
lines = file.readlines()
with open(gyre_in, "w+") as f:
indent = " "
for line in lines:
edited = False
if default_section in line:
this_section = True
if this_section:
if parameter in line:
if parameter == line.split("=")[0].strip():
f.write(line.replace(line.split("=")[1], f" {value} ! Changed\n"))
with file_operation_lock:
with open(gyre_in, "r") as file:
lines = file.readlines()
with open(gyre_in, "w+") as f:
indent = " "
for line in lines:
edited = False
if default_section in line:
this_section = True
if this_section:
if parameter in line:
if parameter == line.split("=")[0].strip():
f.write(line.replace(line.split("=")[1], f" {value} ! Changed\n"))
edited = True
this_section = False
elif line[0] == "/":
f.write(indent)
f.write(f"{parameter} = {value} ! Added\n")
f.write("/")
edited = True
this_section = False
elif line[0] == "/":
f.write(indent)
f.write(f"{parameter} = {value} ! Added\n")
f.write("/")
edited = True
this_section = False
if not edited:
f.write(line)
if not edited:
f.write(line)



Expand All @@ -135,19 +135,19 @@ def modify_gyre_params(self, wdir, filename, data_format, gyre_in="gyre.in", dif
self.writetoGyreFile(wdir, parameter="summary_file", value=f"'{filename.split('.')[0]}-freqs-nad.dat'", default_section="&nad_output", gyre_in=gyre_in)


def set(self, arg, gyre_in="gyre.in"):
def set(self, arg, wdir, gyre_in="gyre.in"):
"""Sets the value of a parameter in the inlist file.
Args:
arg (dict or list of dicts): A dictionary or a list of dictionaries with the parameters to be set.
"""
if isinstance(arg, dict):
for key, value in arg.items():
self.writetoGyreFile(self.projectDir, key, access_helper.toFortranType(value), gyre_in=gyre_in)
self.writetoGyreFile(wdir, key, access_helper.toFortranType(value), gyre_in=gyre_in)
elif isinstance(arg, list):
for item in arg:
for key, value in item.items():
self.writetoGyreFile(self.projectDir, key, access_helper.toFortranType(value), gyre_in=gyre_in)
self.writetoGyreFile(wdir, key, access_helper.toFortranType(value), gyre_in=gyre_in)
elif arg is None:
pass
else:
Expand Down
38 changes: 24 additions & 14 deletions mesaport/ProjectOps/ops_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import subprocess
import shlex
import sys, os, glob
import sys, os, glob, time
import numpy as np
import shutil
from rich import print

Expand Down Expand Up @@ -38,16 +39,26 @@ def run_subprocess(commands, wdir, silent=True, runlog='', status=None,
Pass os.environ.copy() to use the current environment. Or pass a dictionary with the environment variables to be used.
"""
if gyre_in is not None:
gyre_obj = GyreAccess(wdir)
if not os.path.exists(os.join(wdir, "gyre.in")):
gyre_obj.load(gyre_in=gyre_in, dest=wdir)
gyre_obj = GyreAccess()
if parallel:
num = filename.split(".")[0]
shutil.copy(gyre_in, os.path.join(wdir, f"gyre{num}.in"))
gyre_in = os.path.join(wdir, f"gyre{num}.in")
new_gyre_in = os.path.join(wdir, f"gyre{num}.in")
gyre_obj.modify_gyre_params(wdir, filename, data_format, gyre_in=new_gyre_in)
gyre_obj.set(arg=gyre_input_params, wdir=wdir, gyre_in=new_gyre_in)
time.sleep(1)

# Update gyre_in to the new file
gyre_in = new_gyre_in
commands = commands.replace("gyre.in", f"gyre{num}.in")
gyre_obj.modify_gyre_params(wdir, filename, data_format, gyre_in=gyre_in)
gyre_obj.set(gyre_input_params)
runlog = runlog.replace("gyre.log", f"gyre{num}.log")
else:
new_gyre_in = os.path.join(wdir, "gyre.in")
shutil.copyfile(gyre_in, new_gyre_in)
gyre_in = new_gyre_in
gyre_obj.modify_gyre_params(wdir, filename, data_format, gyre_in=gyre_in)
gyre_obj.set(arg=gyre_input_params, wdir=wdir, gyre_in=gyre_in)



evo_terminated = False
termination_code = None
Expand Down Expand Up @@ -100,12 +111,6 @@ def run_subprocess(commands, wdir, silent=True, runlog='', status=None,
sys.stdout.write(errline)
logfile.write( "\n\n"+("*"*100)+"\n\n" )
_data, error = proc.communicate()
if gyre_in is not None:
working_dir = wdir.replace("LOGS", "")
with open(f'{working_dir}/gyre.log', 'a+') as f:
f.write(f"Done with {filename}.\n")
if parallel:
os.remove(gyre_in)
if proc.returncode or error:
print('The process raised an error:', proc.returncode, error)
return False
Expand All @@ -121,6 +126,11 @@ def run_subprocess(commands, wdir, silent=True, runlog='', status=None,
age = age_
return termination_code, age
else:
working_dir = wdir.replace("LOGS", "")
with open(f'{working_dir}/gyre.log', 'a+') as f:
f.write(f"Done with {filename}.\n")
if parallel:
os.remove(gyre_in)
return True

def process_outline(outline):
Expand Down
21 changes: 20 additions & 1 deletion mesaport/ProjectOps/project_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ def runGyre(self, gyre_in, files='all', wdir=None, data_format="GYRE", silent=Tr
if parallel:
gyre_input_params = gyre_input_params if gyre_input_params is not None else repeat(None)
os.environ['HDF5_USE_FILE_LOCKING'] = 'FALSE'
## copy gyre.in to gyre1.in, gyre2.in, etc. for parallel runs
for i, file in enumerate(files):
num = file.split(".")[0]
new_gyre_in = os.path.join(wdir, f"gyre{num}.in")
shutil.copyfile(gyre_in, new_gyre_in)
# commands, wdir,
# silent=True, runlog='',
# status=None, filename="",
Expand All @@ -381,7 +386,7 @@ def runGyre(self, gyre_in, files='all', wdir=None, data_format="GYRE", silent=Tr
repeat(silent), repeat(runlog),
repeat(None), files, repeat(data_format),
repeat(True), repeat(gyre_in),
gyre_input_params, repeat(None), repeat(env))
gyre_input_params, repeat(None), repeat(os.environ.copy()))
if n_cores is None:
n_cores = psutil.cpu_count(logical=True)
Pool = mp.Pool
Expand All @@ -401,7 +406,21 @@ def runGyre(self, gyre_in, files='all', wdir=None, data_format="GYRE", silent=Tr
executor.map(ops_helper.run_subprocess, *args)

except Exception as e:
filenames = glob.glob(os.path.join(LOGS_dir, f"gyre*.log"))
with open(runlog, 'a+') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
os.remove(fname)
raise e
filenames = glob.glob(os.path.join(LOGS_dir, f"gyre*.log"))
with open(runlog, 'a+') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
os.remove(fname)
res = True
else:
for i, file in enumerate(files):
Expand Down

0 comments on commit e5b2d95

Please sign in to comment.