diff --git a/python/swiftest/swiftest/simulation_class.py b/python/swiftest/swiftest/simulation_class.py index 1996b1625..113bab9a9 100644 --- a/python/swiftest/swiftest/simulation_class.py +++ b/python/swiftest/swiftest/simulation_class.py @@ -18,7 +18,6 @@ import json import os from pathlib import Path -import sys import datetime import xarray as xr import numpy as np @@ -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): """ @@ -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() diff --git a/src/io/io.f90 b/src/io/io.f90 index 7296d00dd..e93cc265e 100644 --- a/src/io/io.f90 +++ b/src/io/io.f90 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/src/main/swiftest_driver.f90 b/src/main/swiftest_driver.f90 index ee8fda1f1..4cd537632 100644 --- a/src/main/swiftest_driver.f90 +++ b/src/main/swiftest_driver.f90 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/src/modules/swiftest_classes.f90 b/src/modules/swiftest_classes.f90 index e499f8332..78b82d43b 100644 --- a/src/modules/swiftest_classes.f90 +++ b/src/modules/swiftest_classes.f90 @@ -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 diff --git a/src/util/util_exit.f90 b/src/util/util_exit.f90 index 65d92206d..a7b77c197 100644 --- a/src/util/util_exit.f90 +++ b/src/util/util_exit.f90 @@ -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] [standard|compact|NONE]")' + character(*), parameter :: USAGE_MSG = '("Usage: swiftest [bs|helio|ra15|rmvs|symba|tu4|whm] [standard|compact|progress|NONE]")' character(*), parameter :: HELP_MSG = USAGE_MSG select case(code)