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

Commit

Permalink
Merge branch 'Simulation_API_improvements' into debug
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Nov 21, 2022
2 parents 3d7a3ad + 6f90ed3 commit d962381
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
72 changes: 40 additions & 32 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import json
import os
from pathlib import Path
import sys
import datetime
import xarray as xr
import numpy as np
Expand Down Expand Up @@ -353,6 +352,45 @@ def __init__(self,read_param: bool = True, **kwargs: Any):
warnings.warn(f"BIN_OUT file {binpath} not found.",stacklevel=2)
return

def _run_swiftest_driver(self):
"""
Internal callable function that executes the swiftest_driver run
"""

# Get current environment variables

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\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)} progress\n")

cmd = f"{env['SHELL']} -l {driver_script}"

try:
with subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True) as p:
for line in p.stdout:
if '[' in line:
print(line.replace('\n', '\r'), end='')
elif "Normal termination" in line:
print(line.replace("Normal termination", "\n\nNormal termination"), end='')
else:
print(line, end='')
res = p.communicate()
if p.returncode != 0:
for line in res[1]:
print(line, end='')
warnings.warn("Failure in swiftest_driver", stacklevel=2)
except:
warnings.warn(f"Error executing main swiftest_driver program", stacklevel=2)

return

def run(self,**kwargs):
"""
Expand Down Expand Up @@ -387,37 +425,7 @@ def run(self,**kwargs):

print(f"Running a {self.codename} {self.integrator} run from tstart={self.param['TSTART']} {self.TU_name} to tstop={self.param['TSTOP']} {self.TU_name}")

# Get current environment variables
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\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
with subprocess.Popen(shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True) as p:
for line in p.stdout:
if '[' in line:
print(line.replace('\n','\r'), end='')
elif "Normal termination" in line:
print(line.replace("Normal termination","\n\nNormal termination"),end='')
else:
print(line, end='')
res = p.communicate()
if p.returncode != 0:
for line in res[1]:
print(line, end='')
raise Exception ("Failure in swiftest_driver")
#except:
# warnings.warn(f"Error executing main swiftest_driver program",stacklevel=2)
# return
self._run_swiftest_driver()

# Read in new data
self.bin2xr()
Expand Down
12 changes: 6 additions & 6 deletions src/io/io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ module subroutine io_get_args(integrator, param_file_name, display_style)
! Arguments
integer(I4B) :: integrator !! Symbolic code of the requested integrator
character(len=:), allocatable :: param_file_name !! Name of the input parameters file
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT"}). Default is "STANDARD"
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT", "PROGRESS"}). Default is "STANDARD"
! Internals
character(len=STRMAX), dimension(:), allocatable :: arg
integer(I4B), dimension(:), allocatable :: ierr
Expand Down Expand Up @@ -1788,7 +1788,7 @@ module subroutine io_read_in_param(self, param_file_name)
character(STRMAX) :: errmsg !! Error message in UDIO procedure

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

!! todo: Currently this procedure does not work in user-defined derived-type input mode
Expand All @@ -1799,7 +1799,7 @@ module subroutine io_read_in_param(self, param_file_name)
if (ierr == 0) return

667 continue
write(*,*) "Error reading parameter file: " // trim(adjustl(errmsg))
write(self%display_unit,*) "Error reading parameter file: " // trim(adjustl(errmsg))
call util_exit(FAILURE)
end subroutine io_read_in_param

Expand Down Expand Up @@ -1915,11 +1915,11 @@ module subroutine io_set_display_param(self, display_style)
select case(display_style)
case ('STANDARD')
self%display_unit = OUTPUT_UNIT !! stdout from iso_fortran_env
self%compact_display = .false.
case ('COMPACT')
self%log_output = .false.
case ('COMPACT', 'PROGRESS')
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.
self%log_output = .true.
case default
write(*,*) display_style, " is an unknown display style"
call util_exit(USAGE)
Expand Down
16 changes: 8 additions & 8 deletions src/main/swiftest_driver.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ program swiftest_driver
class(swiftest_parameters), allocatable :: param !! Run configuration parameters
integer(I4B) :: integrator !! Integrator type code (see swiftest_globals for symbolic names)
character(len=:),allocatable :: param_file_name !! Name of the file containing user-defined parameters
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT"}). Default is "STANDARD"
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT", "PROGRESS"}). Default is "STANDARD"
integer(I4B) :: ierr !! I/O error code
integer(I8B) :: iloop !! Loop counter
integer(I8B) :: idump !! Dump cadence counter
Expand All @@ -37,7 +37,7 @@ program swiftest_driver
'"; Number of active pl, tp = ", I5, ", ", I5)'
character(*), parameter :: symbastatfmt = '("Time = ", ES12.5, "; fraction done = ", F6.3, ' // &
'"; Number of active plm, pl, tp = ", I5, ", ", I5, ", ", I5)'
character(*), parameter :: pbarfmt = '("Time = ", G9.2," of ",G9.2)'
character(*), parameter :: pbarfmt = '("Time = ", ES12.5," of ",ES12.5)'
character(len=64) :: pbarmessage


Expand All @@ -59,7 +59,7 @@ program swiftest_driver
!$ write(param%display_unit,'(a)') ' OpenMP parameters:'
!$ write(param%display_unit,'(a)') ' ------------------'
!$ write(param%display_unit,'(a,i3,/)') ' Number of threads = ', nthreads
!$ if (param%compact_display) write(*,'(a,i3)') ' OpenMP: Number of threads = ',nthreads
!$ if (param%log_output) write(*,'(a,i3)') ' OpenMP: Number of threads = ',nthreads

call setup_construct_system(nbody_system, param)
call param%read_in(param_file_name)
Expand All @@ -71,8 +71,8 @@ program swiftest_driver
istep_out => param%istep_out, &
istep_dump => param%istep_dump, &
ioutput => param%ioutput, &
display_unit => param%display_unit, &
compact_display => param%compact_display)
display_style => param%display_style, &
display_unit => param%display_unit)

call nbody_system%initialize(param)
t = t0
Expand All @@ -96,7 +96,7 @@ program swiftest_driver

write(display_unit, *) " *************** Main Loop *************** "
if (param%lrestart .and. param%lenergy) call nbody_system%conservation_report(param, lterminal=.true.)
if (compact_display) then
if (display_style == "PROGRESS") then
call pbar%reset(nloops)
write(pbarmessage,fmt=pbarfmt) t0, tstop
call pbar%update(1,message=pbarmessage)
Expand All @@ -111,7 +111,7 @@ program swiftest_driver

!> Evaluate any discards or collisional outcomes
call nbody_system%discard(param)
if (compact_display) call pbar%update(iloop)
if (display_style == "PROGRESS") call pbar%update(iloop)

!> If the loop counter is at the output cadence value, append the data file with a single frame
if (istep_out > 0) then
Expand All @@ -133,7 +133,7 @@ program swiftest_driver
call integration_timer%reset()

iout = istep_out
if (compact_display) then
if (display_style == "PROGRESS") then
write(pbarmessage,fmt=pbarfmt) t, tstop
call pbar%update(1,message=pbarmessage)
end if
Expand Down
6 changes: 3 additions & 3 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ module swiftest_classes
logical :: lfirstkick = .true. !! Initiate the first kick in a symplectic step
logical :: lrestart = .false. !! Indicates whether or not this is a restarted run

character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT"}). Default is "STANDARD"
integer(I4B) :: display_unit !! File unit number for display (either to stdout or to a log file)
logical :: compact_display = .false. !! Turns on the compact display
character(len=:), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT"}). Default is "STANDARD"
integer(I4B) :: display_unit !! File unit number for display (either to stdout or to a log file)
logical :: log_output = .false. !! Logs the output to file instead of displaying it on the terminal

! Future features not implemented or in development
logical :: lgr = .false. !! Turn on GR
Expand Down
2 changes: 1 addition & 1 deletion src/util/util_exit.f90
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module subroutine util_exit(code)
character(*), parameter :: BAR = '("------------------------------------------------")'
character(*), parameter :: SUCCESS_MSG = '(/, "Normal termination of Swiftest (version ", f3.1, ")")'
character(*), parameter :: FAIL_MSG = '(/, "Terminating Swiftest (version ", f3.1, ") due to error!!")'
character(*), parameter :: USAGE_MSG = '("Usage: swiftest [bs|helio|ra15|rmvs|symba|tu4|whm] <paramfile> [standard|compact|NONE]")'
character(*), parameter :: USAGE_MSG = '("Usage: swiftest [bs|helio|ra15|rmvs|symba|tu4|whm] <paramfile> [standard|compact|progress|NONE]")'
character(*), parameter :: HELP_MSG = USAGE_MSG

select case(code)
Expand Down

0 comments on commit d962381

Please sign in to comment.