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

Commit

Permalink
Merge branch 'encounter_storage' into debug
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Dec 14, 2022
2 parents 0d649db + c9aee22 commit 73d6f2a
Show file tree
Hide file tree
Showing 62 changed files with 2,360 additions and 1,456 deletions.
2 changes: 1 addition & 1 deletion examples/Basic_Simulation/initial_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Output
------
bin.nc : A NetCDF file containing the simulation output.
data.nc : A NetCDF file containing the simulation output.
dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand Down
4 changes: 2 additions & 2 deletions examples/Basic_Simulation/output_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Input
------
bin.nc : A NetCDF file containing the simulation output.
data.nc : A NetCDF file containing the simulation output.
Output
------
Expand All @@ -28,7 +28,7 @@
import matplotlib.pyplot as plt

# Read in the simulation output and store it as an Xarray dataset.
sim = swiftest.Simulation(read_old_output_file=True)
sim = swiftest.Simulation(read_old_output=True)

# Plot of the data and save the output plot.
colors = ['white' if x == 'Massive Body' else 'black' for x in sim.data['particle_type']]
Expand Down
10 changes: 5 additions & 5 deletions examples/Fragmentation/Fragmentation_Movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def encounter_combiner(sim):
# Only keep a minimal subset of necessary data from the simulation and encounter datasets
keep_vars = ['rh','Gmass','radius']
data = sim.data[keep_vars]
enc = sim.enc[keep_vars].load()
enc = sim.encounters[keep_vars].load()

# Remove any encounter data at the same time steps that appear in the data to prevent duplicates
t_not_duplicate = ~enc['time'].isin(data['time'])
Expand Down Expand Up @@ -148,7 +148,7 @@ def setup_plot(self):
ax.set_title(self.title)
fig.add_axes(ax)

self.scatter_artist = ax.scatter([], [], animated=True)
self.scatter_artist = ax.scatter([], [], animated=True, c='k', edgecolors='face')
return fig, ax

def update_plot(self, frame):
Expand All @@ -174,7 +174,7 @@ def data_stream(self, frame=0):
radius = ds['radius'].values
Gmass = ds['Gmass'].values
rh = ds['rh'].values
point_rad = 2 * radius * self.ax_pt_size
point_rad = radius * self.ax_pt_size
yield Gmass, rh, point_rad

if __name__ == "__main__":
Expand Down Expand Up @@ -202,8 +202,8 @@ def data_stream(self, frame=0):
# Set fragmentation parameters
minimum_fragment_gmass = 0.2 * body_Gmass[style][1] # Make the minimum fragment mass a fraction of the smallest body
gmtiny = 0.99 * body_Gmass[style][1] # Make GMTINY just smaller than the smallest original body. This will prevent runaway collisional cascades
sim.set_parameter(fragmentation=True, fragmentation_save="TRAJECTORY", gmtiny=gmtiny, minimum_fragment_gmass=minimum_fragment_gmass, verbose=False)
sim.run(dt=1e-4, tstop=1.0e-3, istep_out=1, dump_cadence=0)
sim.set_parameter(fragmentation=True, encounter_save="trajectory", gmtiny=gmtiny, minimum_fragment_gmass=minimum_fragment_gmass, verbose=False)
sim.run(dt=1e-4, tstop=1.0e-3, istep_out=1, dump_cadence=1)

print("Generating animation")
anim = AnimatedScatter(sim,movie_filename,movie_titles[style],style,nskip=1)
6 changes: 3 additions & 3 deletions examples/Fragmentation/swiftest_fragmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Output
------
disruption/bin.nc : A NetCDF file containing the simulation output.
disruption/data.nc : A NetCDF file containing the simulation output.
disruption/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
disruption/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
disruption/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand All @@ -30,7 +30,7 @@
disruption/init_cond.nc : A NetCDF file containing the initial conditions for the simulation.
disruption/param.in : An ASCII file containing the parameters for the simulation.
disruption/swiftest.log : An ASCII file containing the information on the status of the simulation as it runs.
hitandrun/bin.nc : A NetCDF file containing the simulation output.
hitandrun/data.nc : A NetCDF file containing the simulation output.
hitandrun/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
hitandrun/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
hitandrun/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand All @@ -39,7 +39,7 @@
hitandrun/init_cond.nc : A NetCDF file containing the initial conditions for the simulation.
hitandrun/param.in : An ASCII file containing the parameters for the simulation.
hitandrun/swiftest.log : An ASCII file containing the information on the status of the simulation as it runs.
supercat/bin.nc : A NetCDF file containing the simulation output.
supercat/data.nc : A NetCDF file containing the simulation output.
supercat/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
supercat/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
supercat/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand Down
4 changes: 2 additions & 2 deletions examples/helio_gr_test/helio_gr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
helio_gr_mercury_precession.png : Portable Network Graphic file depicting the precession of Mercury's perihelion over time
with data sourced from the JPL Horizons database, Swiftest run with general relativity,
and Swiftest run without general relativity.
gr/bin.nc : A NetCDF file containing the simulation output.
gr/data.nc : A NetCDF file containing the simulation output.
gr/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
gr/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
gr/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
gr/dump_param2.in : An ASCII file containing the necessary parameters to restart a simulation.
gr/init_cond.nc : A NetCDF file containing the initial conditions for the simulation.
gr/param.in : An ASCII file containing the parameters for the simulation.
gr/swiftest.log : An ASCII file containing the information on the status of the simulation as it runs.
nogr/bin.nc : A NetCDF file containing the simulation output.
nogr/data.nc : A NetCDF file containing the simulation output.
nogr/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
nogr/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
nogr/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand Down
4 changes: 2 additions & 2 deletions examples/whm_gr_test/whm_gr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
whm_gr_mercury_precession.png : Portable Network Graphic file depicting the precession of Mercury's perihelion over time
with data sourced from the JPL Horizons database, Swiftest run with general relativity,
and Swiftest run without general relativity.
gr/bin.nc : A NetCDF file containing the simulation output.
gr/data.nc : A NetCDF file containing the simulation output.
gr/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
gr/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
gr/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
gr/dump_param2.in : An ASCII file containing the necessary parameters to restart a simulation.
gr/init_cond.nc : A NetCDF file containing the initial conditions for the simulation.
gr/param.in : An ASCII file containing the parameters for the simulation.
gr/swiftest.log : An ASCII file containing the information on the status of the simulation as it runs.
nogr/bin.nc : A NetCDF file containing the simulation output.
nogr/data.nc : A NetCDF file containing the simulation output.
nogr/dump_bin1.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
nogr/dump_bin2.nc : A NetCDF file containing the necessary inputs to restart a simulation from t!=0.
nogr/dump_param1.in : An ASCII file containing the necessary parameters to restart a simulation.
Expand Down
42 changes: 19 additions & 23 deletions python/swiftest/swiftest/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"ENCOUNTER_CHECK",
"TSTART",
"DUMP_CADENCE",
"ENCOUNTER_SAVE",
"FRAGMENTATION_SAVE")
"ENCOUNTER_SAVE")



Expand All @@ -55,16 +54,16 @@
float_param = ["T0", "TSTART", "TSTOP", "DT", "CHK_RMIN", "CHK_RMAX", "CHK_EJECT", "CHK_QMIN", "DU2M", "MU2KG",
"TU2S", "MIN_GMFRAG", "GMTINY"]

upper_str_param = ["OUT_TYPE","OUT_FORM","OUT_STAT","IN_TYPE","IN_FORM","ENCOUNTER_SAVE","FRAGMENTATION_SAVE", "CHK_QMIN_COORD"]
upper_str_param = ["OUT_TYPE","OUT_FORM","OUT_STAT","IN_TYPE","IN_FORM","ENCOUNTER_SAVE", "CHK_QMIN_COORD"]
lower_str_param = ["NC_IN", "PL_IN", "TP_IN", "CB_IN", "CHK_QMIN_RANGE"]

param_keys = ['! VERSION'] + int_param + float_param + upper_str_param + lower_str_param+ bool_param

# This defines Xarray Dataset variables that are strings, which must be processed due to quirks in how NetCDF-Fortran
# handles strings differently than Python's Xarray.
string_varnames = ["name", "particle_type", "status", "origin_type"]
string_varnames = ["name", "particle_type", "status", "origin_type", "stage", "regime"]
char_varnames = ["space"]
int_varnames = ["id", "ntp", "npl", "nplm", "discard_body_id", "collision_id"]
int_varnames = ["id", "ntp", "npl", "nplm", "discard_body_id", "collision_id", "loopnum"]

def bool2yesno(boolval):
"""
Expand Down Expand Up @@ -816,18 +815,19 @@ def process_netcdf_input(ds, param):
-------
ds : xarray dataset
"""

ds = ds.where(~np.isnan(ds.id) ,drop=True)
#
#ds = ds.where(ds.id >=0,drop=True)
if param['OUT_TYPE'] == "NETCDF_DOUBLE":
ds = fix_types(ds,ftype=np.float64)
elif param['OUT_TYPE'] == "NETCDF_FLOAT":
ds = fix_types(ds,ftype=np.float32)

# Check if the name variable contains unique values. If so, make name the dimension instead of id
if "id" in ds.dims:
if len(np.unique(ds['name'])) == len(ds['name']):
ds = ds.swap_dims({"id" : "name"})
ds = ds.reset_coords("id")
# # Check if the name variable contains unique values. If so, make name the dimension instead of id
# if "id" in ds.dims:
# if len(np.unique(ds['name'])) == len(ds['name']):
# ds = ds.swap_dims({"id" : "name"})
# if "id" in ds:
# ds = ds.reset_coords("id")

return ds

Expand Down Expand Up @@ -900,15 +900,14 @@ def string_converter(da):
-------
da : xarray dataset with the strings cleaned up
"""
if da.dtype == np.dtype(object):
da = da.astype('<U32')
da = xstrip_str(da)
elif type(da.values[0]) != np.str_:
da = xstrip_nonstr(da)

da = da.astype('<U32')
da = xstrip_str(da)

return da

def char_converter(da):
"""
"""`
Converts a string to a unicode string
Parameters
Expand Down Expand Up @@ -1126,9 +1125,6 @@ def swiftest_xr2infile(ds, param, in_type="NETCDF_DOUBLE", infile_name=None,fram
param_tmp['OUT_FORM'] = param['IN_FORM']
frame = select_active_from_frame(ds, param_tmp, framenum)

if "name" in frame.dims:
frame = frame.swap_dims({"name" : "id"})
frame = frame.reset_coords("name")

if in_type == "NETCDF_DOUBLE" or in_type == "NETCDF_FLOAT":
# Convert strings back to byte form and save the NetCDF file
Expand All @@ -1145,8 +1141,8 @@ def swiftest_xr2infile(ds, param, in_type="NETCDF_DOUBLE", infile_name=None,fram
return frame

# 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)
cb = frame.isel(name=0)
pl = frame.where(name != cb.name)
pl = pl.where(np.invert(np.isnan(pl['Gmass'])), drop=True).drop_vars(['j2rp2', 'j2rp2'],errors="ignore")
tp = frame.where(np.isnan(frame['Gmass']), drop=True).drop_vars(['Gmass', 'radius', 'j2rp2', 'j4rp4'],errors="ignore")

Expand Down
Loading

0 comments on commit 73d6f2a

Please sign in to comment.