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

Commit

Permalink
IO changes to get dump files working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Aug 19, 2021
1 parent f75623f commit fc6c8a6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 50 deletions.
70 changes: 39 additions & 31 deletions src/io/io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ module subroutine io_dump_swiftest(self, param)
dump_file_name = trim(adjustl(param%intpfile))
end select
open(unit = iu, file = dump_file_name, form = "UNFORMATTED", status = 'replace', err = 667, iomsg = errmsg)
select type(self)
class is (swiftest_body)
write(iu, err = 667, iomsg = errmsg) self%nbody
end select
call self%write_frame(iu, param)
close(LUN, err = 667, iomsg = errmsg)
close(iu, err = 667, iomsg = errmsg)

return

Expand Down Expand Up @@ -205,6 +209,7 @@ module subroutine io_dump_system(self, param)
dump_param%intpfile = trim(adjustl(DUMP_TP_FILE(idx)))
dump_param%out_form = XV
dump_param%out_stat = 'APPEND'
dump_param%in_type = REAL8_TYPE
dump_param%T0 = param%t
call dump_param%dump(param_file_name)

Expand Down Expand Up @@ -1006,9 +1011,9 @@ module subroutine io_read_frame_body(self, iu, param, form)
if (.not.allocated(self%capom)) allocate(self%capom(n))
if (.not.allocated(self%omega)) allocate(self%omega(n))
if (.not.allocated(self%capm)) allocate(self%capm(n))
read(iu, err = 667, iomsg = errmsg) self%a(:)
read(iu, err = 667, iomsg = errmsg) self%e(:)
read(iu, err = 667, iomsg = errmsg) self%inc(:)
read(iu, err = 667, iomsg = errmsg) self%a(:)
read(iu, err = 667, iomsg = errmsg) self%e(:)
read(iu, err = 667, iomsg = errmsg) self%inc(:)
read(iu, err = 667, iomsg = errmsg) self%capom(:)
read(iu, err = 667, iomsg = errmsg) self%omega(:)
read(iu, err = 667, iomsg = errmsg) self%capm(:)
Expand Down Expand Up @@ -1044,7 +1049,14 @@ module subroutine io_read_frame_body(self, iu, param, form)
return

667 continue
write(*,*) "Error reading central body file: " // trim(adjustl(errmsg))
select type (self)
class is (swiftest_pl)
write(*,*) "Error reading massive body file: " // trim(adjustl(errmsg))
class is (swiftest_tp)
write(*,*) "Error reading test particle file: " // trim(adjustl(errmsg))
class default
write(*,*) "Error reading body file: " // trim(adjustl(errmsg))
end select
call util_exit(FAILURE)
end subroutine io_read_frame_body

Expand All @@ -1065,16 +1077,20 @@ module subroutine io_read_frame_cb(self, iu, param, form)
! Internals
character(len=STRMAX) :: errmsg

read(iu, err = 667, iomsg = errmsg) self%id
!read(iu, err = 667, iomsg = errmsg) self%name
read(iu, err = 667, iomsg = errmsg) self%id
read(iu, err = 667, iomsg = errmsg) self%Gmass
self%mass = self%Gmass / param%GU
read(iu, err = 667, iomsg = errmsg) self%radius
read(iu, err = 667, iomsg = errmsg) self%j2rp2
read(iu, err = 667, iomsg = errmsg) self%j4rp4
if (param%lrotation) then
read(iu, err = 667, iomsg = errmsg) self%Ip(:)
read(iu, err = 667, iomsg = errmsg) self%rot(:)
read(iu, err = 667, iomsg = errmsg) self%Ip(1)
read(iu, err = 667, iomsg = errmsg) self%Ip(2)
read(iu, err = 667, iomsg = errmsg) self%Ip(3)
read(iu, err = 667, iomsg = errmsg) self%rot(1)
read(iu, err = 667, iomsg = errmsg) self%rot(2)
read(iu, err = 667, iomsg = errmsg) self%rot(3)
end if
if (param%ltides) then
read(iu, err = 667, iomsg = errmsg) self%k2
Expand All @@ -1094,29 +1110,22 @@ module subroutine io_read_frame_system(self, iu, param, form)
!! Read a frame (header plus records for each massive body and active test particle) from a output binary file
implicit none
! Arguments
class(swiftest_nbody_system), intent(inout) :: self !! Swiftest system object
integer(I4B), intent(inout) :: iu !! Unit number for the output file to write frame to
class(swiftest_nbody_system), intent(inout) :: self !! Swiftest system object
integer(I4B), intent(inout) :: iu !! Unit number for the output file to write frame to
class(swiftest_parameters), intent(inout) :: param !! Current run configuration parameters
character(*), intent(in) :: form !! Input format code ("XV" or "EL")
character(*), intent(in) :: form !! Input format code ("XV" or "EL")
! Internals
logical, save :: lfirst = .true.
character(len=STRMAX) :: errmsg
integer(I4B) :: ierr

iu = BINUNIT
if (lfirst) then
open(unit = iu, file = param%outfile, status = 'OLD', form = 'UNFORMATTED', err = 667, iomsg = errmsg)
lfirst = .false.
end if
ierr = io_read_hdr(iu, param%t, self%pl%nbody, self%tp%nbody, param%out_form, param%out_type)
if (ierr /= 0) then
write(*, *) "Swiftest error:"
write(*, *) " Binary output file already exists or cannot be accessed"
return
write(errmsg, *) "Cannot read header."
goto 667
end if
call self%cb%read_frame(iu, param, form)
call self%pl%read_frame(iu, param, form)
call self%tp%read_frame(iu, param, form)
call self%cb%read_frame(iu, param, param%out_form)
call self%pl%read_frame(iu, param, param%out_form)
call self%tp%read_frame(iu, param, param%out_form)

return

Expand All @@ -1137,7 +1146,7 @@ function io_read_hdr(iu, t, npl, ntp, out_form, out_type) result(ierr)
! Arguments
integer(I4B), intent(in) :: iu
integer(I4B), intent(out) :: npl, ntp
character(*), intent(out) :: out_form
character(*), intent(out) :: out_form
real(DP), intent(out) :: t
character(*), intent(in) :: out_type
! Result
Expand All @@ -1147,19 +1156,18 @@ function io_read_hdr(iu, t, npl, ntp, out_form, out_type) result(ierr)
character(len=STRMAX) :: errmsg

select case (out_type)
case (REAL4_TYPE, SWIFTER_REAL4_TYPE)
read(iu, iostat = ierr, err = 667, iomsg = errmsg) ttmp, npl, ntp, out_form
if (ierr /= 0) return
case (REAL4_TYPE)
read(iu, iostat = ierr, err = 667, iomsg = errmsg) ttmp
t = ttmp
case (REAL8_TYPE, SWIFTER_REAL8_TYPE)
case (REAL8_TYPE)
read(iu, iostat = ierr, err = 667, iomsg = errmsg) t
read(iu, iostat = ierr, err = 667, iomsg = errmsg) npl
read(iu, iostat = ierr, err = 667, iomsg = errmsg) ntp
read(iu, iostat = ierr, err = 667, iomsg = errmsg) out_form
case default
write(errmsg,*) trim(adjustl(out_type)) // ' is an unrecognized file type'
ierr = -1
end select
read(iu, iostat = ierr, err = 667, iomsg = errmsg) npl
read(iu, iostat = ierr, err = 667, iomsg = errmsg) ntp
read(iu, iostat = ierr, err = 667, iomsg = errmsg) out_form

return

Expand Down
2 changes: 1 addition & 1 deletion src/main/swiftest_driver.f90
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ program swiftest_driver
param%integrator = integrator

call setup_construct_system(nbody_system, param)
call param%read_from_file(param_file_name)
call param%read_in(param_file_name)

associate(t => param%t, &
t0 => param%t0, &
Expand Down
19 changes: 6 additions & 13 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ module swiftest_classes
logical :: lyarkovsky = .false. !! Turn on Yarkovsky effect
logical :: lyorp = .false. !! Turn on YORP effect
contains
procedure :: reader => io_param_reader
procedure :: writer => io_param_writer
procedure :: dump => io_dump_param
procedure :: read_from_file => io_read_in_param
procedure :: reader => io_param_reader
procedure :: writer => io_param_writer
procedure :: dump => io_dump_param
procedure :: read_in => io_read_in_param
end type swiftest_parameters

!********************************************************************************************************************************
Expand All @@ -93,7 +93,6 @@ module swiftest_classes
contains
!! The minimal methods that all systems must have
procedure :: dump => io_dump_swiftest
procedure(abstract_initialize), deferred :: initialize
procedure(abstract_read_frame), deferred :: read_frame
procedure(abstract_write_frame), deferred :: write_frame
end type swiftest_base
Expand Down Expand Up @@ -128,7 +127,7 @@ module swiftest_classes
real(DP), dimension(NDIM) :: L0 = 0.0_DP !! Initial angular momentum of the central body
real(DP), dimension(NDIM) :: dL = 0.0_DP !! Change in angular momentum of the central body
contains
procedure :: initialize => io_read_in_cb !! I/O routine for reading in central body data
procedure :: read_in => io_read_in_cb !! I/O routine for reading in central body data
procedure :: read_frame => io_read_frame_cb !! I/O routine for reading out a single frame of time-series data for the central body
procedure :: write_frame => io_write_frame_cb !! I/O routine for writing out a single frame of time-series data for the central body
end type swiftest_cb
Expand Down Expand Up @@ -174,7 +173,7 @@ module swiftest_classes
procedure :: drift => drift_body !! Loop through bodies and call Danby drift routine on heliocentric variables
procedure :: v2pv => gr_vh2pv_body !! Converts from velocity to psudeovelocity for GR calculations using symplectic integrators
procedure :: pv2v => gr_pv2vh_body !! Converts from psudeovelocity to velocity for GR calculations using symplectic integrators
procedure :: initialize => io_read_in_body !! Read in body initial conditions from a file
procedure :: read_in => io_read_in_body !! Read in body initial conditions from a file
procedure :: read_frame => io_read_frame_body !! I/O routine for writing out a single frame of time-series data for the central body
procedure :: write_frame => io_write_frame_body !! I/O routine for writing out a single frame of time-series data for the central body
procedure :: accel_obl => obl_acc_body !! Compute the barycentric accelerations of bodies due to the oblateness of the central body
Expand Down Expand Up @@ -356,12 +355,6 @@ subroutine abstract_accel(self, system, param, t, lbeg)
logical, intent(in) :: lbeg !! Optional argument that determines whether or not this is the beginning or end of the step
end subroutine abstract_accel

subroutine abstract_initialize(self, param)
import swiftest_base, swiftest_parameters
class(swiftest_base), intent(inout) :: self !! Swiftest base object
class(swiftest_parameters), intent(inout) :: param !! Current run configuration parameters
end subroutine abstract_initialize

subroutine abstract_kick_body(self, system, param, t, dt, lbeg)
import swiftest_body, swiftest_nbody_system, swiftest_parameters, DP
implicit none
Expand Down
2 changes: 1 addition & 1 deletion src/modules/swiftest_globals.f90
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module swiftest_globals
character(*), dimension(2), parameter :: DUMP_CB_FILE = ['dump_cb1.bin', 'dump_cb2.bin' ]
character(*), dimension(2), parameter :: DUMP_PL_FILE = ['dump_pl1.bin', 'dump_pl2.bin' ]
character(*), dimension(2), parameter :: DUMP_TP_FILE = ['dump_tp1.bin', 'dump_tp2.bin' ]
character(*), dimension(2), parameter :: DUMP_PARAM_FILE = ['dump_param1.dat', 'dump_param2.dat']
character(*), dimension(2), parameter :: DUMP_PARAM_FILE = ['dump_param1.in', 'dump_param2.in']

!> Default file names that can be changed by the user in the parameters file
character(*), parameter :: CB_INFILE = 'cb.in'
Expand Down
8 changes: 4 additions & 4 deletions src/setup/setup.f90
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ module subroutine setup_initialize_system(self, param)
! Arguments
class(swiftest_nbody_system), intent(inout) :: self !! Swiftest system object
class(swiftest_parameters), intent(inout) :: param !! Current run configuration parameters
call self%cb%initialize(param)
call self%pl%initialize(param)
call self%tp%initialize(param)

call self%cb%read_in(param)
call self%pl%read_in(param)
call self%tp%read_in(param)
call self%validate_ids(param)
call self%set_msys()
call self%pl%set_mu(self%cb)
Expand Down

0 comments on commit fc6c8a6

Please sign in to comment.