Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Added new simdir argument to make it easier to create individual simu…
Browse files Browse the repository at this point in the history
…lations in specific directories without having to change the name of every file
  • Loading branch information
daminton committed Dec 4, 2022
1 parent 9f0294f commit c509479
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Simulation:
This is a class that defines the basic Swift/Swifter/Swiftest simulation object
"""

def __init__(self,read_param: bool = False, read_old_output_file: bool = False, **kwargs: Any):
def __init__(self,read_param: bool = False, read_old_output_file: bool = False, simdir: os.PathLike | str = "simdata",**kwargs: Any):
"""
Parameters
Expand All @@ -66,6 +66,9 @@ def __init__(self,read_param: bool = False, read_old_output_file: bool = False,
read_old_output_file : bool, default False
If true, read in a pre-existing binary input file given by the argument `output_file_name` if it exists.
Parameter input file equivalent: None
simdir : PathLike, default `"simdir"`
Directory where simulation data will be stored, including the parameter file, initial conditions file, output file,
dump files, and log files.
**kwargs : See list of valid parameters and their defaults below
Expand Down Expand Up @@ -308,6 +311,8 @@ def __init__(self,read_param: bool = False, read_old_output_file: bool = False,
param_file = kwargs.pop("param_file",Path.cwd() / "simdata" / "param.in")
self.verbose = kwargs.pop("verbose",True)

self.simdir = simdir

# Parameters are set in reverse priority order. First the defaults, then values from a pre-existing input file,
# then using the arguments passed via **kwargs.

Expand Down Expand Up @@ -346,7 +351,7 @@ def __init__(self,read_param: bool = False, read_old_output_file: bool = False,

# Read in an old simulation file if requested
if read_old_output_file:
binpath = os.path.join(self.sim_dir, self.param['BIN_OUT'])
binpath = os.path.join(self.simdir, self.param['BIN_OUT'])
if os.path.exists(binpath):
self.read_output_file()
else:
Expand All @@ -365,7 +370,7 @@ def _run_swiftest_driver(self):
with open(driver_script, 'w') as f:
f.write(f"#{self._shell_full} -l\n")
f.write(f"source ~/.{self._shell}rc\n")
f.write(f"cd {self.sim_dir}\n")
f.write(f"cd {self.simdir}\n")
f.write(f"{str(self.driver_executable)} {self.integrator} {str(self.param_file)} compact\n")

cmd = f"{env['SHELL']} -l {driver_script}"
Expand Down Expand Up @@ -767,14 +772,13 @@ def set_parameter(self, verbose: bool = True, **kwargs):
# Extract the simulation directory and create it if it doesn't exist
if param_file is not None:
self.param_file = Path.cwd() / param_file
self.sim_dir = self.param_file.parent
if self.sim_dir.exists():
if not self.sim_dir.is_dir():
msg = f"Cannot create the {self.sim_dir} directory: File exists."
if self.simdir.exists():
if not self.simdir.is_dir():
msg = f"Cannot create the {self.simdir} directory: File exists."
msg += "\nDelete the file or change the location of param_file"
warnings.warn(msg,stacklevel=2)
else:
self.sim_dir.mkdir(parents=True, exist_ok=False)
self.simdir.mkdir(parents=True, exist_ok=False)

# If no arguments (other than, possibly, verbose) are requested, use defaults
if len(kwargs) == 0:
Expand Down Expand Up @@ -2517,7 +2521,7 @@ def read_param(self,
self.param = io.read_swiftest_param(param_file, self.param, verbose=verbose)
if read_init_cond:
if "NETCDF" in self.param['IN_TYPE']:
init_cond_file = self.sim_dir / self.param['NC_IN']
init_cond_file = self.simdir / self.param['NC_IN']
if os.path.exists(init_cond_file):
param_tmp = self.param.copy()
param_tmp['BIN_OUT'] = init_cond_file
Expand Down Expand Up @@ -2665,13 +2669,13 @@ def read_output_file(self,read_init_cond : bool = True):
# This is done to handle cases where the method is called from a different working directory than the simulation
# results
param_tmp = self.param.copy()
param_tmp['BIN_OUT'] = os.path.join(self.sim_dir, self.param['BIN_OUT'])
param_tmp['BIN_OUT'] = os.path.join(self.simdir, self.param['BIN_OUT'])
if self.codename == "Swiftest":
self.data = io.swiftest2xr(param_tmp, verbose=self.verbose)
if self.verbose: print('Swiftest simulation data stored as xarray DataSet .data')
if read_init_cond:
if "NETCDF" in self.param['IN_TYPE']:
param_tmp['BIN_OUT'] = os.path.join(self.sim_dir, self.param['NC_IN'])
param_tmp['BIN_OUT'] = os.path.join(self.simdir, self.param['NC_IN'])
self.ic = io.swiftest2xr(param_tmp, verbose=self.verbose)
else:
self.ic = self.data.isel(time=0)
Expand Down Expand Up @@ -2764,7 +2768,7 @@ def save(self,
param = self.param

if codename == "Swiftest":
infile_name = Path(self.sim_dir) / param['NC_IN']
infile_name = Path(self.simdir) / param['NC_IN']
io.swiftest_xr2infile(ds=self.data, param=param, in_type=self.param['IN_TYPE'], infile_name=infile_name, framenum=framenum, verbose=verbose)
self.write_param(param_file=param_file,**kwargs)
elif codename == "Swifter":
Expand Down

0 comments on commit c509479

Please sign in to comment.