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

Commit

Permalink
Did some more to try to get dimensions into the order I want
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Dec 3, 2022
1 parent 1d4d18a commit 968d395
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/Basic_Simulation/initial_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from numpy.random import default_rng

# Initialize the simulation object as a variable
sim = swiftest.Simulation(tstart=0.0, tstop=1.0e3, dt=0.01, tstep_out=1.0e0, dump_cadence=0, fragmentation=True, minimum_fragment_mass = 2.5e-11, mtiny=2.5e-8)
sim = swiftest.Simulation(fragmentation=True, minimum_fragment_mass = 2.5e-11, mtiny=2.5e-8)

# Add the modern planets and the Sun using the JPL Horizons Database
sim.add_solar_system_body(["Sun","Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"])
Expand Down Expand Up @@ -67,4 +67,4 @@
sim.get_parameter()

# Run the simulation
sim.run()
sim.run(tstart=0.0, tstop=1.0e2, dt=0.01, tstep_out=1.0e0, dump_cadence=0, )
6 changes: 6 additions & 0 deletions python/swiftest/swiftest/init_cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,10 @@ def vec2xr(param: Dict,
ds = xr.merge([ds_info,ds])
ds["space"] = xr.DataArray(space_coords,dims=["space"],coords={"space":space_coords})

# Re-order dimension coordinates so that they are in the same order as the Fortran side
idx = ds.indexes
dim_order = ["time", "space", "id"]
idx = {index_name: idx[index_name] for index_name in dim_order}
ds = ds.reindex(idx)

return ds
15 changes: 14 additions & 1 deletion python/swiftest/swiftest/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,19 @@ def read_swiftest_param(param_file_name, param, verbose=True):
print(f"{param_file_name} not found.")
return param


def reorder_dims(ds):

# Re-order dimension coordinates so that they are in the same order as the Fortran side
idx = ds.indexes
if "id" in idx:
dim_order = ["time", "space", "id"]
elif "name" in idx:
dim_order = ["time", "space", "name"]
else:
dim_order = idx
idx = {index_name: idx[index_name] for index_name in dim_order}
ds = ds.reindex(idx)
return ds
def read_swifter_param(param_file_name, verbose=True):
"""
Reads in a Swifter param.in file and saves it as a dictionary
Expand Down Expand Up @@ -1134,6 +1146,7 @@ def swiftest_xr2infile(ds, param, in_type="NETCDF_DOUBLE", infile_name=None,fram
frame = unclean_string_values(frame)
if verbose:
print(f"Writing initial conditions to file {infile_name}")
frame = reorder_dims(frame)
frame.to_netcdf(path=infile_name)
return frame

Expand Down
1 change: 1 addition & 0 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2549,6 +2549,7 @@ def get_nvals(ds):
self.data = get_nvals(self.data)

self.data = self.data.sortby("id")
self.data = io.reorder_dims(self.data)

return dsnew

Expand Down

0 comments on commit 968d395

Please sign in to comment.