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

Commit

Permalink
Added SyMBA variables to NetCDF
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Aug 27, 2021
1 parent d24f2bf commit a634afd
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 1,194 deletions.
1,132 changes: 13 additions & 1,119 deletions examples/symba_mars_disk/testnetcdf.ipynb

Large diffs are not rendered by default.

552 changes: 501 additions & 51 deletions examples/whm_gr_test/Untitled.ipynb

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions python/swiftest/swiftest/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,11 +702,6 @@ def swiftest2xr(param):
return None
print(f"Successfully converted {ds.sizes['time']} output frames.")

if param['PARTICLE_OUT'] != "":
infoxr = swiftest_particle_2xr(param)
print('\nAdding particle info to Dataset')
ds = xr.merge([ds, infoxr])

return ds


Expand All @@ -723,7 +718,9 @@ def clean_string_values(param, ds):
-------
ds : xarray dataset with the strings cleaned up
"""
ds['name'] = ds['name'].str.decode(encoding='utf-8')
#ds['name'] = ds['name'].str.decode(encoding='utf-8')
#ds['particle_type'] = ds['particle_type'].str.decode(encoding='utf-8')
# ds['origin_type'] = ds['origin_type'].str.decode(encoding='utf-8')
return ds


Expand Down
2 changes: 2 additions & 0 deletions src/io/io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,8 @@ module subroutine io_write_frame_system(self, param)
call nciu%open(param)
case('NEW', 'REPLACE', 'UNKNOWN')
call nciu%initialize(param)
call nciu%close(param)
call nciu%open(param)
end select
lfirst = .false.
else
Expand Down
5 changes: 3 additions & 2 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ module swiftest_classes
integer(I4B) :: str_dimid !! NetCDF ID for the character string dimension
integer(I4B) :: time_varid !! NetCDF ID for the time variable
integer(I4B) :: id_varid !! NetCDF ID for the particle name variable
integer(I4B) :: name_varid !! NetCDF ID for the semimajor axis variable
integer(I4B) :: name_varid !! NetCDF ID for the namevariable
integer(I4B) :: ptype_varid !! NetCDF ID for the particle type variable
integer(I4B) :: npl_varid !! NetCDF ID for the number of active massive bodies variable
integer(I4B) :: ntp_varid !! NetCDF ID for the number of active test particles variable
integer(I4B) :: a_varid !! NetCDF ID for the semimajor axis variable
Expand Down Expand Up @@ -147,7 +148,7 @@ module swiftest_classes
!> of fragments produced in collisional events.
type :: swiftest_particle_info
character(len=NAMELEN) :: name !! Non-unique name
character(len=NAMELEN) :: particle_type !! String containing a description of the particle type (e.g. CentralBody, MassiveBody, TestParticle)
character(len=NAMELEN) :: particle_type !! String containing a description of the particle type (e.g. Central Body, Massive Body, Test Particle)
contains
procedure :: dump => io_dump_particle_info !! Dumps contents of particle information to file
procedure :: read_in => io_read_in_particle_info !! Read in a particle information object from an open file
Expand Down
7 changes: 4 additions & 3 deletions src/modules/swiftest_globals.f90
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ module swiftest_globals
character(*), parameter :: XV = 'XV' !! Symbolic name for binary output file contents for cartesian position and velocity vectors
character(*), parameter :: XVEL = 'XVEL' !! Symbolic name for binary output file contents for both cartesian position and velocity and orbital elements

character(*), parameter :: CB_TYPE_NAME = "CentralBody"
character(*), parameter :: PL_TYPE_NAME = "MassiveBody"
character(*), parameter :: TP_TYPE_NAME = "TestParticle"
character(*), parameter :: CB_TYPE_NAME = "Central Body"
character(*), parameter :: PL_TYPE_NAME = "Massive Body"
character(*), parameter :: TP_TYPE_NAME = "Test Particle"

! OpenMP Parameters
integer(I4B) :: nthreads = 1 !! Number of OpenMP threads
Expand Down Expand Up @@ -139,6 +139,7 @@ module swiftest_globals
character(*), parameter :: TIME_DIMNAME = "time" !! NetCDF name of the time dimension
character(*), parameter :: ID_DIMNAME = "id" !! NetCDF name of the particle id dimension
character(*), parameter :: STR_DIMNAME = "str" !! NetCDF name of the particle id dimension
character(*), parameter :: PTYPE_VARNAME = "particle_type" ! NetCDF name of the particle type variable
character(*), parameter :: NAME_VARNAME = "name" !! NetCDF name of the particle name variable
character(*), parameter :: NPL_VARNAME = "npl" !! NetCDF name of the number of active massive bodies variable
character(*), parameter :: NTP_VARNAME = "ntp" !! NetCDF name of the number of active test particles variable
Expand Down
48 changes: 47 additions & 1 deletion src/modules/symba_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module symba_classes
!! Definition of classes and methods specific to the Democratic SyMBAcentric Method
!! Adapted from David E. Kaufmann's Swifter routine: helio.f90
use swiftest_globals
use swiftest_classes, only : swiftest_parameters, swiftest_base, swiftest_encounter, swiftest_particle_info
use swiftest_classes, only : swiftest_parameters, swiftest_base, swiftest_encounter, swiftest_particle_info, netcdf_parameters
use helio_classes, only : helio_cb, helio_pl, helio_tp, helio_nbody_system
use rmvs_classes, only : rmvs_chk_ind
implicit none
Expand All @@ -14,6 +14,28 @@ module symba_classes
integer(I4B), private, parameter :: NTENC = 3
real(DP), private, parameter :: RHSCALE = 6.5_DP
real(DP), private, parameter :: RSHELL = 0.48075_DP
character(*), parameter :: ORIGIN_TYPE_VARNAME = "origin_type"
character(*), parameter :: ORIGIN_TIME_VARNAME = "origin_time"
character(*), parameter :: ORIGIN_XHX_VARNAME = "origin_xhx"
character(*), parameter :: ORIGIN_XHY_VARNAME = "origin_xhy"
character(*), parameter :: ORIGIN_XHZ_VARNAME = "origin_xhz"
character(*), parameter :: ORIGIN_VHX_VARNAME = "origin_vhx"
character(*), parameter :: ORIGIN_VHY_VARNAME = "origin_vhy"
character(*), parameter :: ORIGIN_VHZ_VARNAME = "origin_vhz"

type, extends(netcdf_parameters) :: symba_netcdf_parameters
integer(I4B) :: origin_type_varid !! NetCDF ID for the origin type
integer(I4B) :: origin_time_varid !! NetCDF ID for the origin type
integer(I4B) :: origin_xhx_varid !! NetCDF ID for the origin xh x component
integer(I4B) :: origin_xhy_varid !! NetCDF ID for the origin xh y component
integer(I4B) :: origin_xhz_varid !! NetCDF ID for the origin xh z component
integer(I4B) :: origin_vhx_varid !! NetCDF ID for the origin xh x component
integer(I4B) :: origin_vhy_varid !! NetCDF ID for the origin xh y component
integer(I4B) :: origin_vhz_varid !! NetCDF ID for the origin xh z component
contains
procedure :: initialize => symba_netcdf_initialize_output !! Initialize a set of parameters used to identify a NetCDF output objec
procedure :: open => symba_netcdf_open !! Opens a NetCDF file
end type symba_netcdf_parameters

type, extends(swiftest_parameters) :: symba_parameters
real(DP) :: GMTINY = -1.0_DP !! Smallest G*mass that is fully gravitating
Expand Down Expand Up @@ -96,6 +118,8 @@ module symba_classes
procedure :: sort => symba_util_sort_pl !! Sorts body arrays by a sortable componen
procedure :: rearrange => symba_util_sort_rearrange_pl !! Rearranges the order of array elements of body based on an input index array. Used in sorting methods
procedure :: spill => symba_util_spill_pl !! "Spills" bodies from one object to another depending on the results of a mask (uses the PACK intrinsic)
procedure :: write_frame_netcdf => symba_netcdf_write_frame_pl !! I/O routine for writing out a single frame of time-series data for all bodies in the system in NetCDF format
generic :: write_frame => write_frame_netcdf
end type symba_pl

type, extends(symba_pl) :: symba_merger
Expand Down Expand Up @@ -433,6 +457,28 @@ module subroutine symba_kick_encounter(self, system, dt, irec, sgn)
integer(I4B), intent(in) :: irec !! Current recursion level
integer(I4B), intent(in) :: sgn !! sign to be applied to acceleration
end subroutine symba_kick_encounter

module subroutine symba_netcdf_initialize_output(self, param)
use swiftest_classes, only : swiftest_parameters
implicit none
class(symba_netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
end subroutine symba_netcdf_initialize_output

module subroutine symba_netcdf_open(self, param)
use swiftest_classes, only : swiftest_parameters
implicit none
class(symba_netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
end subroutine symba_netcdf_open

module subroutine symba_netcdf_write_frame_pl(self, iu, param)
use swiftest_classes, only : swiftest_parameters, netcdf_parameters
implicit none
class(symba_pl), intent(in) :: self !! Swiftest particle object
class(netcdf_parameters), intent(inout) :: iu !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
end subroutine symba_netcdf_write_frame_pl

module subroutine symba_setup_initialize_particle_info_system(self, param)
use swiftest_classes, only : swiftest_parameters
Expand Down
33 changes: 21 additions & 12 deletions src/netcdf/netcdf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module subroutine netcdf_initialize_output(self, param)
integer(I4B) :: old_mode

!! Create the new output file, deleting any previously existing output file of the same name
call check( nf90_create(param%outfile, NF90_HDF5, self%ncid) )
call check( nf90_create(param%outfile, NF90_NETCDF4, self%ncid) )
call check( nf90_set_fill(self%ncid, nf90_nofill, old_mode) )

! Define the NetCDF dimensions with particle name as the record dimension
Expand All @@ -68,6 +68,7 @@ module subroutine netcdf_initialize_output(self, param)
call check( nf90_def_var(self%ncid, NPL_VARNAME, NF90_INT, self%time_dimid, self%npl_varid) )
call check( nf90_def_var(self%ncid, NTP_VARNAME, NF90_INT, self%time_dimid, self%ntp_varid) )
call check( nf90_def_var(self%ncid, NAME_VARNAME, NF90_CHAR, [self%str_dimid, self%id_dimid], self%name_varid) )
call check( nf90_def_var(self%ncid, PTYPE_VARNAME, NF90_CHAR, [self%str_dimid, self%id_dimid], self%ptype_varid) )
if ((param%out_form == XV) .or. (param%out_form == XVEL)) then
call check( nf90_def_var(self%ncid, XHX_VARNAME, self%out_type, [self%id_dimid, self%time_dimid], self%xhx_varid) )
call check( nf90_def_var(self%ncid, XHY_VARNAME, self%out_type, [self%id_dimid, self%time_dimid], self%xhy_varid) )
Expand Down Expand Up @@ -119,8 +120,6 @@ module subroutine netcdf_initialize_output(self, param)
call check( nf90_def_var(self%ncid, GMESCAPE_VARNAME, self%out_type, self%time_dimid, self%GMescape_varid) )
end if

call check( nf90_enddef(self%ncid) )

return
end subroutine netcdf_initialize_output

Expand All @@ -144,6 +143,7 @@ module subroutine netcdf_open(self, param)
call check( nf90_inq_varid(self%ncid, NPL_VARNAME, self%npl_varid))
call check( nf90_inq_varid(self%ncid, NTP_VARNAME, self%ntp_varid))
call check( nf90_inq_varid(self%ncid, NAME_VARNAME, self%name_varid))
call check( nf90_inq_varid(self%ncid, PTYPE_VARNAME, self%ptype_varid))

if ((param%out_form == XV) .or. (param%out_form == XVEL)) then
call check( nf90_inq_varid(self%ncid, XHX_VARNAME, self%xhx_varid))
Expand Down Expand Up @@ -214,9 +214,7 @@ module subroutine netcdf_write_frame_base(self, iu, param)
! Internals
integer(I4B) :: i, j, tslot, strlen, idslot
integer(I4B), dimension(:), allocatable :: ind
character(len=:), allocatable :: name

!! Open the netCDF file
character(len=:), allocatable :: charstring

tslot = int(param%ioutput, kind=I4B) + 1

Expand All @@ -231,9 +229,14 @@ module subroutine netcdf_write_frame_base(self, iu, param)
j = ind(i)
idslot = self%id(j) + 1
call check( nf90_put_var(iu%ncid, iu%id_varid, self%id(j), start=[idslot]) )
name = trim(adjustl(self%info(j)%name))
strlen = len(name)
call check( nf90_put_var(iu%ncid, iu%name_varid, name, start=[1, idslot], count=[strlen, 1]) )
charstring = trim(adjustl(self%info(j)%name))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info(j)%particle_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

if ((param%out_form == XV) .or. (param%out_form == XVEL)) then
call check( nf90_put_var(iu%ncid, iu%xhx_varid, self%xh(1, j), start=[idslot, tslot]) )
call check( nf90_put_var(iu%ncid, iu%xhy_varid, self%xh(2, j), start=[idslot, tslot]) )
Expand Down Expand Up @@ -277,9 +280,15 @@ module subroutine netcdf_write_frame_base(self, iu, param)
class is (swiftest_cb)
idslot = self%id + 1
call check( nf90_put_var(iu%ncid, iu%id_varid, self%id, start=[idslot]) )
name = trim(adjustl(self%info%name))
strlen = len(name)
call check( nf90_put_var(iu%ncid, iu%name_varid, name, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info%name))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%name_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

charstring = trim(adjustl(self%info%particle_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%ptype_varid, charstring, start=[1, idslot], count=[strlen, 1]) )

call check( nf90_put_var(iu%ncid, iu%Gmass_varid, self%Gmass, start=[idslot, tslot]) )
call check( nf90_put_var(iu%ncid, iu%radius_varid, self%radius, start=[idslot, tslot]) )
if (param%lrotation) then
Expand Down
115 changes: 115 additions & 0 deletions src/symba/symba_netcdf.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
submodule (symba_classes) s_symba_netcdf
use swiftest
use netcdf
contains

subroutine check(status)
!! author: Carlisle A. Wishard, Dana Singh, and David A. Minton
!!
!! Checks the status of all NetCDF operations to catch errors
implicit none
! Arguments
integer, intent (in) :: status

if(status /= nf90_noerr) then
write(*,*) trim(nf90_strerror(status))
call util_exit(FAILURE)
end if

return
end subroutine check

module subroutine symba_netcdf_initialize_output(self, param)
!! author: Carlisle A. Wishard, Dana Singh, and David A. Minton
!!
!! Initialize a NetCDF file system and defines all variables.
implicit none
! Arguments
class(symba_netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters


call netcdf_initialize_output(self, param)

! Define the variables
call check( nf90_def_var(self%ncid, ORIGIN_TYPE_VARNAME, NF90_CHAR, [self%str_dimid, self%id_dimid], self%origin_type_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_TIME_VARNAME, self%out_type, self%id_dimid, self%origin_time_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_XHX_VARNAME, self%out_type, self%id_dimid, self%origin_xhx_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_XHY_VARNAME, self%out_type, self%id_dimid, self%origin_xhy_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_XHZ_VARNAME, self%out_type, self%id_dimid, self%origin_xhz_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_VHX_VARNAME, self%out_type, self%id_dimid, self%origin_vhx_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_VHY_VARNAME, self%out_type, self%id_dimid, self%origin_vhy_varid) )
call check( nf90_def_var(self%ncid, ORIGIN_VHZ_VARNAME, self%out_type, self%id_dimid, self%origin_vhz_varid) )

return

end subroutine symba_netcdf_initialize_output


module subroutine symba_netcdf_open(self, param)
!! author: Carlisle A. Wishard, Dana Singh, and David A. Minton
!!
!! Opens a NetCDF file and does the variable inquiries to activate variable ids
implicit none
! Arguments
class(symba_netcdf_parameters), intent(inout) :: self !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
integer(I4B) :: old_mode

call netcdf_open(self, param)

call check( nf90_inq_varid(self%ncid, ORIGIN_TYPE_VARNAME, self%origin_type_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_TIME_VARNAME, self%origin_type_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_XHX_VARNAME, self%origin_xhx_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_XHY_VARNAME, self%origin_xhy_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_XHZ_VARNAME, self%origin_xhz_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_VHX_VARNAME, self%origin_vhx_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_VHY_VARNAME, self%origin_vhy_varid))
call check( nf90_inq_varid(self%ncid, ORIGIN_VHZ_VARNAME, self%origin_vhz_varid))

return
end subroutine symba_netcdf_open


module subroutine symba_netcdf_write_frame_pl(self, iu, param)
!! author: Carlisle A. Wishard, Dana Singh, and David A. Minton
!!
!! Write a frame of output of a SyMBA massive body data to the binary output file
!! Note: If outputting to orbital elements, but sure that the conversion is done prior to calling this method
implicit none
! Arguments
class(symba_pl), intent(in) :: self !! SyMBA massive body object
class(netcdf_parameters), intent(inout) :: iu !! Parameters used to identify a particular NetCDF dataset
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
! Internals
integer(I4B) :: i, j, tslot, strlen, idslot
integer(I4B), dimension(:), allocatable :: ind
character(len=:), allocatable :: charstring

call netcdf_write_frame_base(self, iu, param)
tslot = int(param%ioutput, kind=I4B) + 1
select type(iu)
class is (symba_netcdf_parameters)
associate(npl => self%nbody)
if (npl == 0) return
allocate(ind(npl))
call util_sort(self%id(1:npl), ind(1:npl))
select type(info => self%info)
class is (symba_particle_info)
do i = 1, npl
j = ind(i)
idslot = self%id(j) + 1

charstring = trim(adjustl(info(j)%origin_type))
strlen = len(charstring)
call check( nf90_put_var(iu%ncid, iu%origin_type_varid, charstring, start=[1, idslot], count=[strlen, 1]) )
end do
end select
end associate

end select

return
end subroutine symba_netcdf_write_frame_pl
end submodule s_symba_netcdf

0 comments on commit a634afd

Please sign in to comment.