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

Commit

Permalink
Merge branch 'master' into debug
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Oct 12, 2022
2 parents 47bf9a6 + a33169d commit 1bd4d53
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile.Defines
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ COLLRESOLVE_HOME = $(ROOT_DIR)/collresolve/
IDEBUG = -O0 -init=snan,arrays -nogen-interfaces -no-pie -no-ftz -fpe-all=0 -g -traceback -mp1 -qopt-matmul -fp-model strict -fpe0 -debug all -align all -pad -ip -prec-div -prec-sqrt -assume protect-parens -CB -no-wrap-margin
STRICTREAL = -fp-model=precise -prec-div -prec-sqrt -assume protect-parens
SIMDVEC = -simd -xhost -align all -assume contiguous_assumed_shape -vecabi=cmdtarget -fp-model no-except -fma
PAR = -qopenmp -parallel
PAR = -qopenmp
HEAPARR = -heap-arrays 4194304
OPTREPORT = -qopt-report=5
IPRODUCTION = -no-wrap-margin -O3 -qopt-prefetch=0 -qopt-matmul -sox $(PAR) $(SIMDVEC) #$(HEAPARR)
Expand Down
50 changes: 48 additions & 2 deletions python/swiftest/swiftest/init_cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def solar_system_horizons(plname, idval, param, ephemerides_start_date, ds):
Returns
-------
xarray dataset
ds : xarray dataset
"""
# Planet ids
planetid = {
Expand Down Expand Up @@ -255,7 +255,53 @@ def solar_system_horizons(plname, idval, param, ephemerides_start_date, ds):
return ds

def vec2xr(param, idvals, namevals, v1, v2, v3, v4, v5, v6, GMpl=None, Rpl=None, rhill=None, Ip1=None, Ip2=None, Ip3=None, rotx=None, roty=None, rotz=None, t=0.0):

"""
Converts and stores the variables of all bodies in an xarray dataset.
Parameters
----------
param : dict
Swiftest paramuration parameters.
idvals : integer
Array of body index values.
namevals :
v1 : array of floats
xh
v2 : array of floats
yh
v3 : array of floats
zh
v4 : array of floats
vhxh
v5 : array of floats
vhyh
v6 : array of floats
vhzh
GMpl : array of floats
G*mass
Rpl : array of floats
radius
rhill : array of floats
Hill Radius
Ip1 : array of floats
Principal axes moments of inertia
Ip2 : array of floats
Principal axes moments of inertia
Ip3 : array of floats
Principal axes moments of inertia
rox : array of floats
Rotation rate vector
roty : array of floats
Rotation rate vector
rotz : array of floats
Rotation rate vector
t : array of floats
Time at start of simulation
Returns
-------
ds : xarray dataset
"""
if param['ROTATION'] == 'YES':
if Ip1 is None:
Ip1 = np.full_like(v1, 0.4)
Expand Down
226 changes: 200 additions & 26 deletions python/swiftest/swiftest/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ def read_swift_param(param_file_name, startfile="swift.in", verbose=True):


def write_swift_param(param, param_file_name):
"""
Writes a Swift param.in file.
Parameters
----------
param : dictionary
The entries in the user parameter file
param_file_name : string
File name of the input parameter file
Returns
-------
Prints a text file containing the parameter information.
"""
outfile = open(param_file_name, 'w')
print(param['T0'], param['TSTOP'], param['DT'], file=outfile)
print(param['DTOUT'], param['DTDUMP'], file=outfile)
Expand All @@ -283,6 +297,20 @@ def write_swift_param(param, param_file_name):


def write_labeled_param(param, param_file_name):
"""
Writes a Swifter/Swiftest param.in file.
Parameters
----------
param : dictionary
The entries in the user parameter file
param_file_name : string
File name of the input parameter file
Returns
-------
Prints a text file containing the parameter information.
"""
outfile = open(param_file_name, 'w')
keylist = ['! VERSION',
'T0',
Expand Down Expand Up @@ -404,6 +432,29 @@ def swifter_stream(f, param):


def make_swiftest_labels(param):
"""
Creates the lables for the variables to be included in the output file.
Parameters
----------
param : dictionary
The entries in the user parameter file
Returns
-------
clab : string list
Labels for the cvec data
plab : string list
Labels for the pvec data
tlab : string list
Labels for the tvec data
infolab_float : string list
Labels for floating point data
infolab_int :
Labels for integer data
infolab_str :
Labels for string data
"""
tlab = []
if param['OUT_FORM'] == 'XV' or param['OUT_FORM'] == 'XVEL':
tlab.append('xhx')
Expand Down Expand Up @@ -746,15 +797,37 @@ def swiftest2xr(param, verbose=True):
return ds

def xstrip(a):
"""
Cleans up the string values in the DataSet to remove extra white space
Parameters
----------
a : xarray dataset
Returns
-------
da : xarray dataset with the strings cleaned up
"""
func = lambda x: np.char.strip(x)
return xr.apply_ufunc(func, a.str.decode(encoding='utf-8'),dask='parallelized')

def string_converter(da):
if da.dtype == np.dtype(object):
da = da.astype('<U32')
elif da.dtype != np.dtype('<U32'):
da = xstrip(da)
return da
"""
Converts a string to a unicode string
Parameters
----------
da : xarray dataset
Returns
-------
da : xarray dataset with the strings cleaned up
"""
if da.dtype == np.dtype(object):
da = da.astype('<U32')
elif da.dtype != np.dtype('<U32'):
da = xstrip(da)
return da

def clean_string_values(ds):
"""
Expand Down Expand Up @@ -827,32 +900,43 @@ def swiftest_particle_stream(f):


def swiftest_particle_2xr(param):
"""Reads in the Swiftest SyMBA-generated PARTICLE_OUT and converts it to an xarray Dataset"""
veclab = ['time_origin', 'xhx_origin', 'py_origin', 'pz_origin', 'vhx_origin', 'vhy_origin', 'vhz_origin']
id_list = []
origin_type_list = []
origin_vec_list = []
"""
Reads in the Swiftest SyMBA-generated PARTICLE_OUT and converts it to an xarray Dataset
Parameters
----------
param : dict
Swiftest parameters
try:
with FortranFile(param['PARTICLE_OUT'], 'r') as f:
for id, origin_type, origin_vec in swiftest_particle_stream(f):
id_list.append(id)
origin_type_list.append(origin_type)
origin_vec_list.append(origin_vec)
except IOError:
print(f"Error reading in {param['PARTICLE_OUT']} ")
Returns
-------
infoxr : xarray dataset
"""
veclab = ['time_origin', 'xhx_origin', 'py_origin', 'pz_origin', 'vhx_origin', 'vhy_origin', 'vhz_origin']
id_list = []
origin_type_list = []
origin_vec_list = []

id_list = np.asarray(id_list)[:,0]
origin_type_list = np.asarray(origin_type_list)
origin_vec_list = np.vstack(origin_vec_list)
try:
with FortranFile(param['PARTICLE_OUT'], 'r') as f:
for id, origin_type, origin_vec in swiftest_particle_stream(f):
id_list.append(id)
origin_type_list.append(origin_type)
origin_vec_list.append(origin_vec)
except IOError:
print(f"Error reading in {param['PARTICLE_OUT']} ")

id_list = np.asarray(id_list)[:,0]
origin_type_list = np.asarray(origin_type_list)
origin_vec_list = np.vstack(origin_vec_list)

typeda = xr.DataArray(origin_type_list, dims=['id'], coords={'id' : id_list})
vecda = xr.DataArray(origin_vec_list, dims=['id', 'vec'], coords={'id' : id_list, 'vec' : veclab})
typeda = xr.DataArray(origin_type_list, dims=['id'], coords={'id' : id_list})
vecda = xr.DataArray(origin_vec_list, dims=['id', 'vec'], coords={'id' : id_list, 'vec' : veclab})

infoxr = vecda.to_dataset(dim='vec')
infoxr['origin_type'] = typeda
infoxr = vecda.to_dataset(dim='vec')
infoxr['origin_type'] = typeda

return infoxr
return infoxr

def select_active_from_frame(ds, param, framenum=-1):
"""
Expand Down Expand Up @@ -1148,6 +1232,25 @@ def swifter_xr2infile(ds, param, framenum=-1):
return

def swift2swifter(swift_param, plname="", tpname="", conversion_questions={}):
"""
Converts from a Swift run to a Swifter run
Parameters
----------
swift_param : dictionary
Swift input parameters.
plname : string
Name of massive body input file
tpname : string
Name of test particle input file
conversion_questions : dictronary
Dictionary of additional parameters required to convert between formats
Returns
-------
swifter_param : A set of input files for a new Swifter run
"""

swifter_param = {}
intxt = conversion_questions.get('RHILL', None)
if not intxt:
Expand Down Expand Up @@ -1349,6 +1452,27 @@ def swift2swifter(swift_param, plname="", tpname="", conversion_questions={}):
return swifter_param

def swifter2swiftest(swifter_param, plname="", tpname="", cbname="", conversion_questions={}):
"""
Converts from a Swifter run to a Swiftest run
Parameters
----------
swifter_param : dictionary
Swifter input parameters.
plname : string
Name of massive body input file
tpname : string
Name of test particle input file
cbname : string
Name of central body input file
conversion_questions : dictronary
Dictionary of additional parameters required to convert between formats
Returns
-------
swiftest_param : A set of input files for a new Swiftest run
"""

swiftest_param = swifter_param.copy()
# Pull additional feature status from the conversion_questions dictionary

Expand Down Expand Up @@ -1580,6 +1704,26 @@ def swifter2swiftest(swifter_param, plname="", tpname="", cbname="", conversion_
return swiftest_param

def swift2swiftest(swift_param, plname="", tpname="", cbname="", conversion_questions={}):
"""
Converts from a Swift run to a Swiftest run
Parameters
----------
swift_param : dictionary
Swift input parameters.
plname : string
Name of massive body input file
tpname : string
Name of test particle input file
cbname : string
Name of the central body input file
conversion_questions : dictronary
Dictionary of additional parameters required to convert between formats
Returns
-------
swiftest_param : A set of input files for a new Swiftest run
"""
if plname == '':
plname = input("PL_IN: Name of new planet input file: [pl.swiftest.in]> ")
if plname == '':
Expand All @@ -1605,6 +1749,21 @@ def swift2swiftest(swift_param, plname="", tpname="", cbname="", conversion_ques
return swiftest_param

def swiftest2swifter_param(swiftest_param, J2=0.0, J4=0.0):
"""
Converts from a Swiftest run to a Swifter run
Parameters
----------
swiftest_param : dictionary
Swiftest input parameters.
J2 : float
Central body oblateness term. Default spherical.
J4 : float
Central body oblateness term. Default spherical.
Returns
-------
swifter_param : A set of input files for a new Swifter run
"""
swifter_param = swiftest_param
CBIN = swifter_param.pop("CB_IN", None)
GMTINY = swifter_param.pop("GMTINY", None)
Expand Down Expand Up @@ -1639,6 +1798,21 @@ def swiftest2swifter_param(swiftest_param, J2=0.0, J4=0.0):


def swifter2swift_param(swifter_param, J2=0.0, J4=0.0):
"""
Converts from a Swifter run to a Swift run
Parameters
----------
swifter_param : dictionary
Swifter input parameters.
J2 : float
Central body oblateness term. Default spherical.
J4 : float
Central body oblateness term. Default spherical.
Returns
-------
swift_param : A set of input files for a new Swift run
"""
swift_param = {
'! VERSION': f"Swift parameter input file converted from Swifter",
'T0': 0.0,
Expand Down
Loading

0 comments on commit 1bd4d53

Please sign in to comment.