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

Commit

Permalink
Merge branch 'Simulation_API_improvements' into debug
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Nov 23, 2022
2 parents b058169 + 2bcff51 commit 962363f
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Simulation:
This is a class that defines the basic Swift/Swifter/Swiftest simulation object
"""

def __init__(self,read_param: bool = True, **kwargs: Any):
def __init__(self,read_param: bool = False, **kwargs: Any):
"""
Parameters
Expand Down Expand Up @@ -330,6 +330,7 @@ def __init__(self,read_param: bool = True, **kwargs: Any):
# overriding everything with defaults when there are no arguments passed to Simulation()
kwargs['param_file'] = self.param_file
param_file_found = True

else:
param_file_found = False

Expand Down Expand Up @@ -379,32 +380,36 @@ def _type_scrub(output_data):
output_data[k] = float(v)
return output_data

process_output = False
noutput = int((self.param['TSTOP'] - self.param['T0']) / self.param['DT'])
iloop = int((self.param['TSTART'] - self.param['T0']) / self.param['DT'])
post_message = f"Time: {self.param['TSTART']} / {self.param['TSTOP']} {self.TU_name} "
post_message += f"npl: {self.data['npl'].values[0]} ntp: {self.data['ntp'].values[0]}"
if "nplm" in self.data:
post_message += f" nplm: {self.data['nplm'].values[0]}"
pbar = tqdm(total=noutput, postfix=post_message, bar_format='{l_bar}{bar}{postfix}')
try:
with subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True) as p:
process_output = False
noutput = int((self.param['TSTOP'] - self.param['T0']) / self.param['DT'])
iloop = int((self.param['TSTART'] - self.param['T0']) / self.param['DT'])
pbar = tqdm(total=noutput)

for line in p.stdout:
if "SWIFTEST STOP" in line:
process_output = False

if process_output:
kvstream=line.replace('\n','').strip().split(';') # Removes the newline character,
output_data = _type_scrub({kv.split()[0]: kv.split()[1] for kv in kvstream[:-1]})
pre_message = f"Time: {output_data['T']} / {self.param['TSTOP']} {self.TU_name}"
post_message = f"npl: {output_data['NPL']} ntp: {output_data['NTP']}"
post_message = f"Time: {output_data['T']} / {self.param['TSTOP']} {self.TU_name}"
post_message += f" npl: {output_data['NPL']} ntp: {output_data['NTP']}"
if "NPLM" in output_data:
post_message = post_message + f" nplm: {output_data['NPLM']}"
pbar.set_description_str(pre_message)
pbar.set_postfix_str(post_message)
post_message += f" nplm: {output_data['NPLM']}"
interval = output_data['ILOOP'] - iloop
if interval > 0:
pbar.update(interval)
pbar.set_postfix_str(post_message)
iloop = output_data['ILOOP']

if "SWIFTEST START" in line:
Expand Down Expand Up @@ -2431,9 +2436,13 @@ def get_nvals(ds):
if "Gmass" in ds:
ds['ntp'] = ds[count_dim].where(np.isnan(ds['Gmass'])).count(dim=count_dim)
ds['npl'] = ds[count_dim].where(~(np.isnan(ds['Gmass']))).count(dim=count_dim) - 1
if self.integrator == "symba" and "GMTINY" in self.param and self.param['GMTINY'] is not None:
ds['nplm'] = ds[count_dim].where(ds['Gmass'] > self.param['GMTINY']).count(dim=count_dim) - 1
else:
ds['ntp'] = ds[count_dim].count(dim=count_dim)
ds['npl'] = xr.full_like(ds['ntp'],0)
if self.integrator == "symba" and "GMTINY" in self.param and self.param['GMTINY'] is not None:
ds['nplm'] = xr.full_like(ds['ntp'],0)
return ds

dsnew = get_nvals(dsnew)
Expand Down Expand Up @@ -2477,6 +2486,14 @@ def read_param(self,

if codename == "Swiftest":
self.param = io.read_swiftest_param(param_file, self.param, verbose=verbose)
if "NETCDF" in self.param['IN_TYPE']:
init_cond_file = self.sim_dir / self.param['NC_IN']
if os.path.exists(init_cond_file):
param_tmp = self.param.copy()
param_tmp['BIN_OUT'] = init_cond_file
self.data = io.swiftest2xr(param_tmp, verbose=self.verbose)
else:
warnings.warn(f"Initial conditions file file {init_cond_file} not found.", stacklevel=2)
elif codename == "Swifter":
self.param = io.read_swifter_param(param_file, verbose=verbose)
elif codename == "Swift":
Expand Down

0 comments on commit 962363f

Please sign in to comment.