From 719d6b0597b1c07f7165461cfe998e591557e445 Mon Sep 17 00:00:00 2001 From: David Minton Date: Thu, 1 Sep 2022 12:36:20 -0400 Subject: [PATCH 1/5] Added verbose flags that can turn off non-error console messages if requested --- python/swiftest/swiftest/io.py | 29 +++++++++----------- python/swiftest/swiftest/simulation_class.py | 28 ++++++++++--------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/python/swiftest/swiftest/io.py b/python/swiftest/swiftest/io.py index 838b46b28..cfe598cfd 100644 --- a/python/swiftest/swiftest/io.py +++ b/python/swiftest/swiftest/io.py @@ -26,7 +26,7 @@ def real2float(realstr): return float(realstr.replace('d', 'E').replace('D', 'E')) -def read_swiftest_param(param_file_name, param): +def read_swiftest_param(param_file_name, param, verbose=True): """ Reads in a Swiftest param.in file and saves it as a dictionary @@ -43,7 +43,7 @@ def read_swiftest_param(param_file_name, param): param['! VERSION'] = f"Swiftest parameter input from file {param_file_name}" # Read param.in file - print(f'Reading Swiftest file {param_file_name}') + if verbose: print(f'Reading Swiftest file {param_file_name}') try: with open(param_file_name, 'r') as f: for line in f.readlines(): @@ -95,7 +95,7 @@ def read_swiftest_param(param_file_name, param): return param -def read_swifter_param(param_file_name): +def read_swifter_param(param_file_name, verbose=True): """ Reads in a Swifter param.in file and saves it as a dictionary @@ -140,7 +140,7 @@ def read_swifter_param(param_file_name): } # Read param.in file - print(f'Reading Swifter file {param_file_name}') + if verbose: print(f'Reading Swifter file {param_file_name}') try: with open(param_file_name, 'r') as f: for line in f.readlines(): @@ -179,7 +179,7 @@ def read_swifter_param(param_file_name): return param -def read_swift_param(param_file_name, startfile="swift.in"): +def read_swift_param(param_file_name, startfile="swift.in", verbose=True): """ Reads in a Swift param.in file and saves it as a dictionary @@ -228,7 +228,7 @@ def read_swift_param(param_file_name, startfile="swift.in"): param['TP_IN'] = tpname # Read param.in file - print(f'Reading Swift file {param_file_name}') + if verbose: print(f'Reading Swift file {param_file_name}') try: with open(param_file_name, 'r') as f: line = f.readline().split() @@ -603,7 +603,7 @@ def swiftest_stream(f, param): ntp, tpid, tpnames, tvec.T, tlab -def swifter2xr(param): +def swifter2xr(param, verbose=True): """ Converts a Swifter binary data file into an xarray DataSet. @@ -640,13 +640,13 @@ def swifter2xr(param): plds = plda.to_dataset(dim='vec') tpds = tpda.to_dataset(dim='vec') - print('\nCreating Dataset') + if verbose: print('\nCreating Dataset') ds = xr.combine_by_coords([plds, tpds]) - print(f"Successfully converted {ds.sizes['time']} output frames.") + if verbose: print(f"Successfully converted {ds.sizes['time']} output frames.") return ds -def swiftest2xr(param): +def swiftest2xr(param, verbose=True): """ Converts a Swiftest binary data file into an xarray DataSet. @@ -700,23 +700,20 @@ def swiftest2xr(param): cbds = cbda.to_dataset(dim='vec') plds = plda.to_dataset(dim='vec') tpds = tpda.to_dataset(dim='vec') - print('\nCreating Dataset') + if verbose: print('\nCreating Dataset') ds = xr.combine_by_coords([cbds, plds, tpds]) elif ((param['OUT_TYPE'] == 'NETCDF_DOUBLE') or (param['OUT_TYPE'] == 'NETCDF_FLOAT')): - print('\nCreating Dataset from NetCDF file') + if verbose: print('\nCreating Dataset from NetCDF file') ds = xr.open_dataset(param['BIN_OUT'], mask_and_scale=False) ds = clean_string_values(param, ds) else: print(f"Error encountered. OUT_TYPE {param['OUT_TYPE']} not recognized.") return None - print(f"Successfully converted {ds.sizes['time']} output frames.") + if verbose: print(f"Successfully converted {ds.sizes['time']} output frames.") return ds -def xstrip(a): - func = lambda x: np.char.strip(x) - return xr.apply_ufunc(func, a.str.decode(encoding='utf-8')) def clean_string_values(param, ds): """ diff --git a/python/swiftest/swiftest/simulation_class.py b/python/swiftest/swiftest/simulation_class.py index 846aba347..6a5f1728c 100644 --- a/python/swiftest/swiftest/simulation_class.py +++ b/python/swiftest/swiftest/simulation_class.py @@ -11,7 +11,7 @@ class Simulation: """ This is a class that defines the basic Swift/Swifter/Swiftest simulation object """ - def __init__(self, codename="Swiftest", param_file="", readbin=True): + def __init__(self, codename="Swiftest", param_file="param.in", readbin=True, verbose=True): self.ds = xr.Dataset() self.param = { '! VERSION': f"Swiftest parameter input", @@ -54,12 +54,14 @@ def __init__(self, codename="Swiftest", param_file="", readbin=True): 'ENCOUNTER_CHECK': "ADAPTIVE" } self.codename = codename + self.verbose = verbose if param_file != "" : dir_path = os.path.dirname(os.path.realpath(param_file)) - self.read_param(param_file, codename) + self.read_param(param_file, codename=codename, verbose=self.verbose) if readbin: - if os.path.exists(dir_path + '/' + self.param['BIN_OUT']): - self.param['BIN_OUT'] = dir_path + '/' + self.param['BIN_OUT'] + binpath = os.path.join(dir_path,self.param['BIN_OUT']) + if os.path.exists(binpath): + self.param['BIN_OUT'] = binpath self.bin2xr() else: print(f"BIN_OUT file {self.param['BIN_OUT']} not found.") @@ -114,15 +116,15 @@ def addp(self, idvals, namevals, t1, t2, t3, t4, t5, t6, GMpl=None, Rpl=None, rh return - def read_param(self, param_file, codename="Swiftest"): + def read_param(self, param_file, codename="Swiftest", verbose=True): if codename == "Swiftest": - self.param = io.read_swiftest_param(param_file, self.param) + self.param = io.read_swiftest_param(param_file, self.param, verbose=verbose) self.codename = "Swiftest" elif codename == "Swifter": - self.param = io.read_swifter_param(param_file) + self.param = io.read_swifter_param(param_file, verbose=verbose) self.codename = "Swifter" elif codename == "Swift": - self.param = io.read_swift_param(param_file) + self.param = io.read_swift_param(param_file, verbose=verbose) self.codename = "Swift" else: print(f'{codename} is not a recognized code name. Valid options are "Swiftest", "Swifter", or "Swift".') @@ -180,11 +182,11 @@ def convert(self, param_file, newcodename="Swiftest", plname="pl.swiftest.in", t def bin2xr(self): if self.codename == "Swiftest": - self.ds = io.swiftest2xr(self.param) - print('Swiftest simulation data stored as xarray DataSet .ds') + self.ds = io.swiftest2xr(self.param, verbose=self.verbose) + if self.verbose: print('Swiftest simulation data stored as xarray DataSet .ds') elif self.codename == "Swifter": - self.ds = io.swifter2xr(self.param) - print('Swifter simulation data stored as xarray DataSet .ds') + self.ds = io.swifter2xr(self.param, verbose=self.verbose) + if self.verbose: print('Swifter simulation data stored as xarray DataSet .ds') elif self.codename == "Swift": print("Reading Swift simulation data is not implemented yet") else: @@ -215,7 +217,7 @@ def follow(self, codestyle="Swifter"): else: fol = None - print('follow.out written') + if self.verbose: print('follow.out written') return fol From 374b0a779516de1b40b36cca3d6adc0a273b1863 Mon Sep 17 00:00:00 2001 From: David Minton Date: Thu, 1 Sep 2022 12:37:00 -0400 Subject: [PATCH 2/5] Improved handling of string encoding so that netcdf files saved by xarray can be read back in as if they were the original simulation data files. --- python/swiftest/swiftest/io.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/python/swiftest/swiftest/io.py b/python/swiftest/swiftest/io.py index cfe598cfd..f387492a4 100644 --- a/python/swiftest/swiftest/io.py +++ b/python/swiftest/swiftest/io.py @@ -727,18 +727,31 @@ def clean_string_values(param, ds): Returns ------- ds : xarray dataset with the strings cleaned up - """ + """ + + def xstrip(a): + func = lambda x: np.char.strip(x) + return xr.apply_ufunc(func, a.str.decode(encoding='utf-8')) + + def string_converter(da): + if da.dtype == np.dtype(object): + da = da.astype(' Date: Mon, 5 Sep 2022 16:28:13 -0400 Subject: [PATCH 3/5] Fixed integer type errors --- src/symba/symba_step.f90 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/symba/symba_step.f90 b/src/symba/symba_step.f90 index 368548832..c5e37c576 100644 --- a/src/symba/symba_step.f90 +++ b/src/symba/symba_step.f90 @@ -265,8 +265,8 @@ module subroutine symba_step_reset_system(self, param) class is (symba_tp) associate(npl => pl%nbody, ntp => tp%nbody) nenc_old = system%plplenc_list%nenc - call system%plplenc_list%setup(0) - call system%plplcollision_list%setup(0) + call system%plplenc_list%setup(0_I8B) + call system%plplcollision_list%setup(0_I8B) if (npl > 0) then pl%lcollision(1:npl) = .false. call pl%reset_kinship([(i, i=1, npl)]) @@ -284,7 +284,7 @@ module subroutine symba_step_reset_system(self, param) end if nenc_old = system%pltpenc_list%nenc - call system%pltpenc_list%setup(0) + call system%pltpenc_list%setup(0_I8B) if (ntp > 0) then tp%nplenc(1:ntp) = 0 tp%levelg(1:ntp) = -1 From 11ebb856017b09343815882cbd9ec8cbff168c9d Mon Sep 17 00:00:00 2001 From: David Minton Date: Mon, 5 Sep 2022 16:29:00 -0400 Subject: [PATCH 4/5] Can now save a NETCDF input file when calling .save on a Simulation object --- python/swiftest/swiftest/io.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/python/swiftest/swiftest/io.py b/python/swiftest/swiftest/io.py index f387492a4..ae12125b6 100644 --- a/python/swiftest/swiftest/io.py +++ b/python/swiftest/swiftest/io.py @@ -831,6 +831,13 @@ def swiftest_xr2infile(ds, param, framenum=-1): A set of three input files for a Swiftest run """ frame = ds.isel(time=framenum) + + # For NETCDF input file type, just do a dump of the system at the correct time frame + if param['IN_TYPE'] == "NETCDF_DOUBLE" or param['IN_TYPE'] == "NETCDF_FLOAT": + frame.to_netcdf(path=param['NC_IN']) + return + + # All other file types need seperate files for each of the inputs cb = frame.where(frame.id == 0, drop=True) pl = frame.where(frame.id > 0, drop=True) pl = pl.where(np.invert(np.isnan(pl['Gmass'])), drop=True).drop_vars(['J_2', 'J_4']) @@ -1015,6 +1022,7 @@ def swifter_xr2infile(ds, param, framenum=-1): A set of input files for a Swifter run """ frame = ds.isel(time=framenum) + cb = frame.where(frame.id == 0, drop=True) pl = frame.where(frame.id > 0, drop=True) pl = pl.where(np.invert(np.isnan(pl['Gmass'])), drop=True).drop_vars(['J_2', 'J_4']) @@ -1060,6 +1068,7 @@ def swifter_xr2infile(ds, param, framenum=-1): # Now make Swiftest files print(f"{param['IN_TYPE']} is an unknown input file type") + return def swift2swifter(swift_param, plname="", tpname="", conversion_questions={}): swifter_param = {} From 3e70bbd62a25d7ca5fff8142af69ebdb5debefa3 Mon Sep 17 00:00:00 2001 From: David Minton Date: Tue, 6 Sep 2022 16:54:11 -0400 Subject: [PATCH 5/5] Added ability to save initial conditions as NetCDF files extracted from a bin.nc file at a given timestep --- python/swiftest/swiftest/io.py | 74 +++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/python/swiftest/swiftest/io.py b/python/swiftest/swiftest/io.py index ae12125b6..5cd294246 100644 --- a/python/swiftest/swiftest/io.py +++ b/python/swiftest/swiftest/io.py @@ -7,6 +7,7 @@ import re newfeaturelist = ("FRAGMENTATION", "ROTATION", "TIDES", "ENERGY", "GR", "YARKOVSKY", "YORP", "IN_FORM") +string_varnames = ["name", "particle_type", "status", "origin_type"] def real2float(realstr): """ @@ -714,6 +715,16 @@ def swiftest2xr(param, verbose=True): return ds +def xstrip(a): + func = lambda x: np.char.strip(x) + return xr.apply_ufunc(func, a.str.decode(encoding='utf-8')) + +def string_converter(da): + if da.dtype == np.dtype(object): + da = da.astype('