Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
addition of the "delete_old" option for restart files (with additiona…
Browse files Browse the repository at this point in the history
…l option to retain certain user specified output times)
  • Loading branch information
cmkaul committed Apr 19, 2016
1 parent fd9cfb6 commit 370cf42
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Restart.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ cdef class Restart:
str uuid
public double last_restart_time
public double frequency
bint delete_old
list times_retained

cpdef initialize(self)
cpdef write(self, ParallelMPI.ParallelMPI Pa)
cpdef read(self, ParallelMPI.ParallelMPI Pa)
cpdef free_memory(self)
cpdef free_memory(self)
cpdef cleanup(self)
78 changes: 76 additions & 2 deletions Restart.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
import os
import shutil
import glob
try:
import cPickle as pickle
except:
Expand Down Expand Up @@ -43,6 +45,24 @@ cdef class Restart:
except:
self.frequency = 30.0



try:
self.delete_old = namelist['restart']['delete_old']
except:
self.delete_old = False
try:
times_retained_raw = namelist['restart']['times_retained']
if not type(times_retained_raw) == list:
times_retained_raw = [times_retained_raw]

# Make sure the times are strings, ie '3600' not 3600
self.times_retained = []
for time in times_retained_raw:
self.times_retained.append(str(time))
except:
self.times_retained = []

try:
if namelist['restart']['init_from']:
self.input_path = str(namelist['restart']['input_path'])
Expand All @@ -54,6 +74,8 @@ cdef class Restart:
Pa.root_print('Not a restarted simulation.')




return


Expand All @@ -68,10 +90,23 @@ cdef class Restart:
cpdef write(self, ParallelMPI.ParallelMPI Pa):

self.restart_data['last_restart_time'] = self.last_restart_time

#Set up path for writing restar files
#Set up path for writing restart files
path = self.restart_path + '/' + str(np.int(self.last_restart_time))


# Some preliminary renaming of directories if we are using the 'delete_old' option
if self.delete_old and Pa.rank == 0:
recent_dirs = glob.glob(self.restart_path +'/*_recent')
for recent_dir in recent_dirs:
prefix = recent_dir[:-7]
os.rename(recent_dir, prefix+'_old')
new_dirs = glob.glob(self.restart_path +'/*_new')
for new_dir in new_dirs:
prefix = new_dir[:-4]
os.rename(new_dir, prefix+'_recent')
if self.delete_old:
path = path + '_new'

if Pa.rank == 0:
if os.path.exists(path):
Pa.root_print("Restart path exits for safety not overwriting.")
Expand All @@ -88,12 +123,30 @@ cdef class Restart:
# No point keeping data in dictionary so empty it now
self.free_memory()

if self.delete_old and Pa.rank == 0:
old_dirs = glob.glob(self.restart_path +'/*_old')
for old_dir in old_dirs:
trim_prefix = old_dir[len(self.restart_path)+1:-4]
if trim_prefix in self.times_retained:
os.rename(old_dir, self.restart_path+'/'+trim_prefix)
else:
shutil.rmtree(old_dir)


return

cpdef read(self, ParallelMPI.ParallelMPI Pa):

with open(self.input_path + '/' + str(Pa.rank) + '.pkl', 'rb') as f:
self.restart_data = pickle.load(f)
Pa.barrier()

# We rename the input directory in case it ends in one of the suffixes
# that is used to find files for deletion
if Pa.rank == 0:
if self.delete_old:
os.rename(self.input_path, self.input_path +'_original')


return

Expand All @@ -107,3 +160,24 @@ cdef class Restart:
self.restart_data = {}

return

cpdef cleanup(self):

path = self.restart_path
originals = glob.glob(path+'/*_original')

for original in originals:
prefix = original[:-9]
os.rename(original, prefix)
recents = glob.glob(path +'/*_recent')

for recent in recents:
prefix = recent[:-7]
os.rename(recent, prefix)
new_dirs = glob.glob(path +'/*_new')

for new_dir in new_dirs:
prefix = new_dir[:-4]
os.rename(new_dir, prefix)
return

2 changes: 2 additions & 0 deletions Simulation3d.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class Simulation3d:
self.Pa.root_print('T = ' + str(self.TS.t) + ' dt = ' + str(self.TS.dt) +
' cfl_max = ' + str(self.TS.cfl_max) + ' walltime = ' + str(time2 - time1))

self.Restart.cleanup()


return

Expand Down

0 comments on commit 370cf42

Please sign in to comment.