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

Commit

Permalink
Fixed more bugs and inconsistencites with the progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
daminton committed Nov 19, 2022
1 parent e959465 commit 217f846
Show file tree
Hide file tree
Showing 11 changed files with 1,580 additions and 125 deletions.
1,434 changes: 1,418 additions & 16 deletions examples/Basic_Simulation/initial_conditions.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/Basic_Simulation/initial_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from numpy.random import default_rng

# Initialize the simulation object as a variable
sim = swiftest.Simulation(tstart=0.0, tstop=1.0e6, dt=0.005, tstep_out=1.0e3, fragmentation=True, minimum_fragment_mass = 2.5e-11, mtiny=2.5e-8)
sim = swiftest.Simulation(tstart=0.0, tstop=1.0e3, dt=0.005, tstep_out=1.0e0, fragmentation=True, minimum_fragment_mass = 2.5e-11, mtiny=2.5e-8)

# Add the modern planets and the Sun using the JPL Horizons Database
sim.add_solar_system_body(["Sun","Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"])
Expand Down
4 changes: 2 additions & 2 deletions examples/Basic_Simulation/run_from_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import swiftest
sim = swiftest.Simulation()
sim.run(tstop=20.0)
sim = swiftest.Simulation(read_param=True)
sim.run()
37 changes: 37 additions & 0 deletions examples/Basic_Simulation/simdata/param.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
! VERSION Swiftest input file
T0 0.0
TSTART 0.0
TSTOP 1000.0
DT 0.005
ISTEP_OUT 200
ISTEP_DUMP 200
NC_IN init_cond.nc
IN_TYPE NETCDF_DOUBLE
IN_FORM EL
BIN_OUT bin.nc
OUT_FORM XVEL
OUT_TYPE NETCDF_DOUBLE
OUT_STAT REPLACE
CHK_QMIN 0.004650467260962157
CHK_RMIN 0.004650467260962157
CHK_RMAX 10000.0
CHK_EJECT 10000.0
CHK_QMIN_COORD HELIO
CHK_QMIN_RANGE 0.004650467260962157 10000.0
MU2KG 1.988409870698051e+30
TU2S 31557600.0
DU2M 149597870700.0
GMTINY 9.869231602224408e-07
FRAGMENTATION YES
MIN_GMFRAG 9.869231602224408e-10
RESTART NO
CHK_CLOSE YES
GR YES
ROTATION YES
ENERGY NO
EXTRA_FORCE NO
BIG_DISCARD NO
RHILL_PRESENT NO
INTERACTION_LOOPS TRIANGULAR
ENCOUNTER_CHECK TRIANGULAR
TIDES NO
21 changes: 13 additions & 8 deletions python/swiftest/swiftest/simulation_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def run(self,**kwargs):
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)}")
f.write(f"{str(self.driver_executable)} {self.integrator} {str(self.param_file)} {os.linesep}")

try:
cmd = f"{env['SHELL']} -l {driver_script}"
Expand All @@ -402,13 +402,18 @@ def run(self,**kwargs):
stderr=subprocess.PIPE,
env=env,
universal_newlines=True) as p:
for line in p.stdout:
print(line.replace(']\n',']\r').replace("Normal termination","\n\nNormal termination"), end='')
res = p.communicate()
if p.returncode != 0:
for line in res[1]:
print(line, end='')
raise Exception ("Failure in swiftest_driver")
for line in p.stdout:
if '[' in line:
print(line.replace('\n','\r'))
elif "Normal termination" in line:
print(line.replace("Normal termination","\n\nNormal termination"))
else:
print(line)
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
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ SET(FAST_MATH_FILES
${SRC}/helio/helio_step.f90
${SRC}/helio/helio_util.f90
${SRC}/io/io.f90
${SRC}/io/io_progress_bar.f90
${SRC}/netcdf/netcdf.f90
${SRC}/obl/obl.f90
${SRC}/operators/operator_cross.f90
Expand Down
71 changes: 0 additions & 71 deletions src/io/io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,6 @@

contains

module subroutine io_pbar_reset(self, nloops)
!! author: David A. Minton
!!
!! Resets the progress bar to the beginning
implicit none
! Arguments
class(progress_bar),intent(inout) :: self
integer(I8B), intent(in) :: nloops
! Internals
character(len=2) :: numchar
character(len=5) :: barfmt

if (.not.allocated(self%barstr)) then
allocate(character(self%PBARSIZE+4) :: self%barstr)
end if
write(numchar,'(I2)') self%PBARSIZE
self%fmt = '(A1,"[",A' // numchar // ',"]",$)'
barfmt = '(A' // numchar // ')'
write(self%barstr,barfmt) " "
self%nloops = nloops
self%spinner = 0

call self%update(0)

return
end subroutine io_pbar_reset

module subroutine io_pbar_update(self,i)
!! author: David A. Minton
!!
!! Updates the progress bar with new values and causes the "spinner" to flip.
implicit none
! Arguments
class(progress_bar), intent(inout) :: self
integer(I8B), intent(in) :: i
! Internals
integer(I4B) :: k
real(DP) :: frac
integer(I4B) :: pos !! The current integer position of the progress bar

! Compute the current position
frac = real(i,kind=DP) / real(self%nloops,kind=DP)
pos = 1 + min(int(ceiling(frac * self%PBARSIZE),kind=I4B),self%PBARSIZE)

! Fill in the bar character up to the current position
do k = 1, pos
self%barstr(k:k) = self%barchar
end do

self%spinner = self%spinner + 1
if (self%spinner > 4) self%spinner = 1
select case(self%spinner)
case(1)
self%barstr(pos:pos) = "/"
case(2)
self%barstr(pos:pos) = "-"
case(3)
self%barstr(pos:pos) = "\"
case(4)
self%barstr(pos:pos) = "|"
end select

write(*,fmt=self%fmt) char(13),self%barstr


return
end subroutine io_pbar_update




module subroutine io_conservation_report(self, param, lterminal)
!! author: The Purdue Swiftest Team - David A. Minton, Carlisle A. Wishard, Jennifer L.L. Pouplin, and Jacob R. Elliott
!!
Expand Down
105 changes: 105 additions & 0 deletions src/io/io_progress_bar.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module io_progress_bar
!! author: The Purdue Swiftest Team - David A. Minton, Carlisle A. Wishard, Jennifer L.L. Pouplin, and Jacob R. Elliott
!!
!! Definition of classes and methods used to determine close encounters
use swiftest_globals
use swiftest_classes
implicit none
public

type :: progress_bar
!! author: David A. Minton
!!
!! Implements a class for a simple progress bar that can print on the screen.
integer(I4B) :: PBARSIZE = 80 !! Number of characters acros for a whole progress bar
integer(I8B) :: nloops !! The total number of loops that the progrees bar is executing
character(len=:), allocatable :: barstr !! The string that prints out as the progress bar
integer(I4B) :: spinner !! Position of the "spinner" that indicates that progress is being made
character(len=1) :: barchar = "=" !! The progress bar character
character(len=32) :: fmt !! The format string that is used to define the progress bar itself
integer(I4B) :: pos !! The current position of the progress bar
character(len=32) :: message !! The current message displayed at the end of the progress bar
contains
procedure :: reset => io_pbar_reset !! Resets the progress bar to the beginning
procedure :: update => io_pbar_update !! Updates the progress bar with new values and causes the "spinner" to flip.
end type progress_bar

contains

subroutine io_pbar_reset(self, nloops)
!! author: David A. Minton
!!
!! Resets the progress bar to the beginning
implicit none
! Arguments
class(progress_bar),intent(inout) :: self
integer(I8B), intent(in) :: nloops
! Internals
character(len=2) :: numchar,numchar2
character(len=32) :: startfmt
character(len=self%PBARSIZE) :: empty
integer(I4B) :: k

if (.not.allocated(self%barstr)) then
allocate(character(self%PBARSIZE) :: self%barstr)
end if
do k = 1, self%PBARSIZE
self%barstr(k:k) = " "
end do
write(numchar,'(I2)') self%PBARSIZE
self%fmt = '(A1,"[",A' // numchar // ',"]",$)'
self%nloops = nloops
self%spinner = 0
self%pos = 0
self%message = ""

write(*,fmt=self%fmt) char(13),empty

return
end subroutine io_pbar_reset


subroutine io_pbar_update(self,i,message)
!! author: David A. Minton
!!
!! Updates the progress bar with new values and causes the "spinner" to flip.
implicit none
! Arguments
class(progress_bar), intent(inout) :: self !! Progres bar object
integer(I8B), intent(in) :: i !! The current loop index of the progress loop
character(len=*), intent(in), optional :: message !! An optional message to display to the right of the progress bar
! Internals
real(DP) :: frac
integer(I4B) :: pos !! The current integer position of the progress bar

! Compute the current position
frac = real(i,kind=DP) / real(self%nloops,kind=DP)
pos = min(int(ceiling(frac * self%PBARSIZE),kind=I4B),self%PBARSIZE)
if (pos /= self%pos) then
self%pos = pos

! Fill in the bar character up to the current position
self%barstr(pos:pos) = self%barchar
end if

self%spinner = self%spinner + 1
if (self%spinner > 4) self%spinner = 1
select case(self%spinner)
case(1)
self%barstr(pos+1:pos+1) = "/"
case(2)
self%barstr(pos+1:pos+1) = "-"
case(3)
self%barstr(pos+1:pos+1) = "\"
case(4)
self%barstr(pos+1:pos+1) = "|"
end select

write(*,fmt=self%fmt) char(13),self%barstr


return
end subroutine io_pbar_update


end module io_progress_bar
3 changes: 2 additions & 1 deletion src/main/swiftest_driver.f90
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ program swiftest_driver
write(*, *) " *************** Main Loop *************** "
if (param%lrestart .and. param%lenergy) call nbody_system%conservation_report(param, lterminal=.true.)
call pbar%reset(nloops)
call pbar%update(1)
do iloop = 1, nloops
!> Step the system forward in time
call integration_timer%start()
Expand All @@ -102,7 +103,6 @@ program swiftest_driver
call nbody_system%discard(param)

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

iout = istep_out
call pbar%update(iloop)
end if
end if

Expand Down
1 change: 1 addition & 0 deletions src/modules/swiftest.f90
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module swiftest
use lambda_function
use walltime_classes
use encounter_classes
use io_progress_bar
!use advisor_annotate
!$ use omp_lib
implicit none
Expand Down
26 changes: 0 additions & 26 deletions src/modules/swiftest_classes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,6 @@ module swiftest_classes
generic :: read_particle_info => read_particle_info_bin, read_particle_info_netcdf !! Genereric method call for reading in the particle information metadata
end type swiftest_nbody_system

type :: progress_bar
!! author: David A. Minton
!!
!! Implements a class for a simple progress bar that can print on the screen.
integer(I4B) :: PBARSIZE = 80 !! Number of characters acros for a whole progress bar
integer(I8B) :: nloops !! The total number of loops that the progrees bar is executing
character(len=:), allocatable :: barstr !! The string that prints out as the progress bar
integer(I4B) :: spinner !! Position of the "spinner" that indicates that progress is being made
character(len=1) :: barchar = "=" !! The progress bar character
character(len=32) :: fmt !! The format string that is used to define the progress bar itself
contains
procedure :: reset => io_pbar_reset !! Resets the progress bar to the beginning
procedure :: update => io_pbar_update !! Updates the progress bar with new values and causes the "spinner" to flip.
end type progress_bar

abstract interface

Expand Down Expand Up @@ -597,18 +583,6 @@ pure module subroutine gr_vh2pv_body(self, param)
class(swiftest_parameters), intent(in) :: param !! Current run configuration parameters
end subroutine gr_vh2pv_body

module subroutine io_pbar_reset(self, nloops)
implicit none
class(progress_bar),intent(inout) :: self
integer(I8B), intent(in) :: nloops
end subroutine io_pbar_reset

module subroutine io_pbar_update(self,i)
implicit none
class(progress_bar), intent(inout) :: self
integer(I8B), intent(in) :: i
end subroutine io_pbar_update

module subroutine io_conservation_report(self, param, lterminal)
implicit none
class(swiftest_nbody_system), intent(inout) :: self !! Swiftest nbody system object
Expand Down

0 comments on commit 217f846

Please sign in to comment.