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

Commit

Permalink
Restructured code so that you can switch between the old-style output…
Browse files Browse the repository at this point in the history
… or the new compact output with an argument.
  • Loading branch information
daminton committed Nov 21, 2022
1 parent c7b7ecb commit d44dc07
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 1,646 deletions.
1,451 changes: 17 additions & 1,434 deletions examples/Basic_Simulation/initial_conditions.ipynb

Large diffs are not rendered by default.

66 changes: 7 additions & 59 deletions examples/Basic_Simulation/run_simulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "86c845ce-1801-46ca-8a8a-1cabb266e6a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: OMP_NUM_THREADS=8\n"
]
}
],
"outputs": [],
"source": [
"import swiftest\n",
"import xarray as xr\n",
Expand All @@ -24,64 +16,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "d716c371-8eb4-4fc1-82af-8b5c444c831e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading Swiftest file /home/daminton/git_debug/swiftest/examples/Basic_Simulation/simdata/param.in\n"
]
}
],
"outputs": [],
"source": [
"sim = swiftest.Simulation()"
"sim = swiftest.Simulation(read_param=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "ec7452d6-4c9b-4df3-acc0-b11c32264b91",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing parameter inputs to file /home/daminton/git_debug/swiftest/examples/Basic_Simulation/simdata/param.in\n",
"Running a Swiftest symba run from tstart=0.0 y to tstop=1000.0 y\n",
"\u001b]2;cd /home/daminton/git_debug/swiftest/examples/Basic_Simulation/simdata\u0007\u001b]1;\u0007\u001b]2;/home/daminton/git_debug/swiftest/bin/swiftest_driver symba \u0007\u001b]1;\u0007 Parameter input file is /home/daminton/git_debug/swiftest/examples/Basic_Simulation/simdata/param.in\n",
"\n",
" \n",
"\n",
" OpenMP parameters:\n",
"\n",
" ------------------\n",
"\n",
" Number of threads = 8\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"[======/ ] Time = 6.50000E+01 of 1.00000E+03\r"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_62265/187458869.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/git_debug/swiftest/python/swiftest/swiftest/simulation_class.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 404\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 405\u001b[0m universal_newlines=True) as p:\n\u001b[0;32m--> 406\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 407\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m'['\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mline\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 408\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\n'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'\\r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"outputs": [],
"source": [
"sim.run()"
]
Expand Down
8 changes: 4 additions & 4 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ def run(self,**kwargs):
env = os.environ.copy()
driver_script = os.path.join(self.binary_path,"swiftest_driver.sh")
with open(driver_script,'w') as f:
f.write(f"#{self._shell_full} -l {os.linesep}")
f.write(f"source ~/.{self._shell}rc {os.linesep}")
f.write(f"cd {self.sim_dir} {os.linesep}")
f.write(f"{str(self.driver_executable)} {self.integrator} {str(self.param_file)} {os.linesep}")
f.write(f"#{self._shell_full} -l\n")
f.write(f"source ~/.{self._shell}rc\n")
f.write(f"cd {self.sim_dir}\n")
f.write(f"{str(self.driver_executable)} {self.integrator} {str(self.param_file)} compact\n")

cmd = f"{env['SHELL']} -l {driver_script}"
oldline = None
Expand Down
188 changes: 105 additions & 83 deletions src/io/io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,7 @@ module subroutine io_conservation_report(self, param, lterminal)
"; D(Eorbit+Ecollisions)/|E0| = ", ES12.5, &
"; DM/M0 = ", ES12.5)'

associate(system => self, pl => self%pl, cb => self%cb, npl => self%pl%nbody)
if (((param%out_type == REAL4_TYPE) .or. (param%out_type == REAL8_TYPE)) .and. (param%energy_out /= "")) then
if (param%lfirstenergy .and. (param%out_stat /= "OLD")) then
open(unit=EGYIU, file=param%energy_out, form="formatted", status="replace", action="write", err=667, iomsg=errmsg)
write(EGYIU,EGYHEADER, err=667, iomsg=errmsg)
else
open(unit=EGYIU, file=param%energy_out, form="formatted", status="old", action="write", &
position="append", err=667, iomsg=errmsg)
end if
end if

associate(system => self, pl => self%pl, cb => self%cb, npl => self%pl%nbody, display_unit => param%display_unit)
call pl%vb2vh(cb)
call pl%xh2xb(cb)

Expand All @@ -69,34 +59,22 @@ module subroutine io_conservation_report(self, param, lterminal)
param%lfirstenergy = .false.
end if

if (((param%out_type == REAL4_TYPE) .or. (param%out_type == REAL8_TYPE)) .and. (param%energy_out /= "")) then
write(EGYIU,EGYFMT, err = 667, iomsg = errmsg) param%t, Eorbit_now, system%Ecollisions, Ltot_now, GMtot_now
close(EGYIU, err = 667, iomsg = errmsg)
end if

if (.not.param%lfirstenergy) then
Lerror = norm2(Ltot_now(:) - system%Ltot_orig(:)) / norm2(system%Ltot_orig(:))
Eorbit_error = (Eorbit_now - system%Eorbit_orig) / abs(system%Eorbit_orig)
Ecoll_error = system%Ecollisions / abs(system%Eorbit_orig)
Etotal_error = (Eorbit_now - system%Ecollisions - system%Eorbit_orig - system%Euntracked) / abs(system%Eorbit_orig)
Merror = (GMtot_now - system%GMtot_orig) / system%GMtot_orig
if (lterminal) write(*, EGYTERMFMT) Lerror, Ecoll_error, Etotal_error, Merror
if (lterminal) write(display_unit, EGYTERMFMT) Lerror, Ecoll_error, Etotal_error, Merror
if (abs(Merror) > 100 * epsilon(Merror)) then
write(*,*) "Severe error! Mass not conserved! Halting!"
if ((param%out_type == REAL4_TYPE) .or. (param%out_type == REAL8_TYPE)) then
write(*,*) "Merror = ", Merror
write(*,*) "GMtot_now : ",GMtot_now
write(*,*) "GMtot_orig: ",system%GMtot_orig
write(*,*) "Difference: ",GMtot_now - system%GMtot_orig
else if ((param%out_type == NETCDF_FLOAT_TYPE) .or. (param%out_type == NETCDF_DOUBLE_TYPE)) then
! Save the frame of data to the bin file in the slot just after the present one for diagnostics
param%ioutput = param%ioutput + 1_I8B
call pl%xv2el(cb)
call self%write_hdr(param%nciu, param)
call cb%write_frame(param%nciu, param)
call pl%write_frame(param%nciu, param)
call param%nciu%close()
end if
! Save the frame of data to the bin file in the slot just after the present one for diagnostics
param%ioutput = param%ioutput + 1_I8B
call pl%xv2el(cb)
call self%write_hdr(param%nciu, param)
call cb%write_frame(param%nciu, param)
call pl%write_frame(param%nciu, param)
call param%nciu%close()
call util_exit(FAILURE)
end if
end if
Expand Down Expand Up @@ -346,67 +324,78 @@ module subroutine io_dump_system(self, param)
end subroutine io_dump_system


module function io_get_args(integrator, param_file_name) result(ierr)
module subroutine io_get_args(integrator, param_file_name, display_style)
!! author: David A. Minton
!!
!! Reads in the name of the parameter file from command line arguments.
implicit none
! Arguments
integer(I4B) :: integrator !! Symbolic code of the requested integrator
character(len=:), allocatable :: param_file_name !! Name of the input parameters file
! Result
integer(I4B) :: ierr !! I/O error code
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT"}). Default is "STANDARD"
! Internals
character(len=STRMAX) :: arg1, arg2
integer :: narg,ierr_arg1, ierr_arg2
character(len=STRMAX), dimension(:), allocatable :: arg
integer(I4B), dimension(:), allocatable :: ierr
integer :: i,narg
character(len=*),parameter :: linefmt = '(A)'

ierr = -1 ! Default is to fail
narg = command_argument_count() !
if (narg == 2) then
call get_command_argument(1, arg1, status = ierr_arg1)
call get_command_argument(2, arg2, status = ierr_arg2)
if ((ierr_arg1 == 0) .and. (ierr_arg2 == 0)) then
ierr = 0
call io_toupper(arg1)
select case(arg1)
case('BS')
integrator = BS
case('HELIO')
integrator = HELIO
case('RA15')
integrator = RA15
case('TU4')
integrator = TU4
case('WHM')
integrator = WHM
case('RMVS')
integrator = RMVS
case('SYMBA')
integrator = SYMBA
case('RINGMOONS')
integrator = RINGMOONS
case default
integrator = UNKNOWN_INTEGRATOR
write(*,*) trim(adjustl(arg1)) // ' is not a valid integrator.'
ierr = -1
end select
param_file_name = trim(adjustl(arg2))
end if
else
call get_command_argument(1, arg1, status = ierr_arg1)
if (ierr_arg1 == 0) then
if (arg1 == '-v' .or. arg1 == '--version') then
call util_version()
else if (arg1 == '-h' .or. arg1 == '--help') then
call util_exit(HELP)
end if
narg = command_argument_count()
if (narg > 0) then
allocate(arg(narg),ierr(narg))
do i = 1,narg
call get_command_argument(i, arg(i), status = ierr(i))
end do
if (any(ierr /= 0)) call util_exit(USAGE)
else
call util_exit(USAGE)
end if

if (narg == 1) then
if (arg(1) == '-v' .or. arg(1) == '--version') then
call util_version()
else if (arg(1) == '-h' .or. arg(1) == '--help') then
call util_exit(HELP)
else
call util_exit(USAGE)
end if
else if (narg >= 2) then
call io_toupper(arg(1))
select case(arg(1))
case('BS')
integrator = BS
case('HELIO')
integrator = HELIO
case('RA15')
integrator = RA15
case('TU4')
integrator = TU4
case('WHM')
integrator = WHM
case('RMVS')
integrator = RMVS
case('SYMBA')
integrator = SYMBA
case('RINGMOONS')
integrator = RINGMOONS
case default
integrator = UNKNOWN_INTEGRATOR
write(*,*) trim(adjustl(arg(1))) // ' is not a valid integrator.'
call util_exit(USAGE)
end select
param_file_name = trim(adjustl(arg(2)))
end if

if (narg == 2) then
display_style = "STANDARD"
else if (narg == 3) then
call io_toupper(arg(3))
display_style = trim(adjustl(arg(3)))
else
call util_exit(USAGE)
end if
if (ierr /= 0) call util_exit(USAGE)

return
end function io_get_args
end subroutine io_get_args


module function io_get_old_t_final_system(self, param) result(old_t_final)
Expand Down Expand Up @@ -555,7 +544,6 @@ module subroutine io_param_reader(self, unit, iotype, v_list, iostat, iomsg)
!!
!! Adapted from David E. Kaufmann's Swifter routine io_init_param.f90
!! Adapted from Martin Duncan's Swift routine io_init_param.f
use, intrinsic :: iso_fortran_env
implicit none
! Arguments
class(swiftest_parameters), intent(inout) :: self !! Collection of parameters
Expand Down Expand Up @@ -940,7 +928,7 @@ module subroutine io_param_reader(self, unit, iotype, v_list, iostat, iomsg)
iostat = 0

! Print the contents of the parameter file to standard output
! call param%writer(unit = OUTPUT_UNIT, iotype = "none", v_list = [0], iostat = iostat, iomsg = iomsg)
call param%writer(unit = param%display_unit, iotype = "none", v_list = [0], iostat = iostat, iomsg = iomsg)

end associate

Expand Down Expand Up @@ -1801,7 +1789,6 @@ module subroutine io_read_in_param(self, param_file_name)

! Read in name of parameter file
write(*, *) 'Parameter input file is ', trim(adjustl(param_file_name))
write(*, *) ' '
self%param_file_name = param_file_name

!! todo: Currently this procedure does not work in user-defined derived-type input mode
Expand Down Expand Up @@ -1913,6 +1900,41 @@ module subroutine io_read_particle_info_system(self, param)
end subroutine io_read_particle_info_system


module subroutine io_set_display_param(self, display_style)
!! author: David A. Minton
!!
!! Sets the display style parameters. If display is "STANDARD" then output goes to stdout. If display is "COMPACT"
!! then it is redirected to a log file and a progress-bar is used for stdout
implicit none
! Arguments
class(swiftest_parameters), intent(inout) :: self !! Current run configuration parameters
character(*), intent(in) :: display_style !! Style of the output display
! Internals
character(STRMAX) :: errmsg

select case(display_style)
case ('STANDARD')
self%display_unit = OUTPUT_UNIT !! stdout from iso_fortran_env
self%compact_display = .false.
case ('COMPACT')
open(unit=SWIFTEST_LOG_OUT, file=SWIFTEST_LOG_FILE, status='replace', err = 667, iomsg = errmsg)
self%display_unit = SWIFTEST_LOG_OUT
self%compact_display = .true.
case default
write(*,*) display_style, " is an unknown display style"
call util_exit(USAGE)
end select

self%display_style = display_style

return

667 continue
write(*,*) "Error opening swiftest log file: " // trim(adjustl(errmsg))
call util_exit(FAILURE)
end subroutine io_set_display_param


module subroutine io_toupper(string)
!! author: David A. Minton
!!
Expand Down Expand Up @@ -1980,9 +2002,9 @@ module subroutine io_write_discard(self, param)
case('APPEND')
open(unit=LUN, file=param%discard_out, status='OLD', position='APPEND', form='FORMATTED', err=667, iomsg=errmsg)
case('NEW', 'REPLACE', 'UNKNOWN')
open(unit=LUN, file=param%discard_out, status=param%out_stat, form='FORMATTED', err=667, iomsg=errmsg)
open(unit=LUN, file=param%discard_out, status=out_stat, form='FORMATTED', err=667, iomsg=errmsg)
case default
write(*,*) 'Invalid status code for OUT_STAT: ',trim(adjustl(param%out_stat))
write(*,*) 'Invalid status code for OUT_STAT: ',trim(adjustl(out_stat))
call util_exit(FAILURE)
end select
lfirst = .false.
Expand Down
Loading

0 comments on commit d44dc07

Please sign in to comment.