Package PyFoam :: Package Applications :: Module CommonWriteAllTrigger
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.CommonWriteAllTrigger

 1  """Implements a trigger that manipulates the controlDict in 
 2  such a way that every time-step is written to disk""" 
 3   
 4  import sys 
 5   
 6  from os import path 
 7  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 8  from PyFoam.Error import warning 
 9   
10 -class CommonWriteAllTrigger(object):
11 """ The class that does the actual triggering 12 """ 13
14 - def addOptions(self):
15 self.ensureGeneralOptions() 16 self.generalOpts.add_option("--write-all-timesteps", 17 action="store_true", 18 dest="writeAll", 19 default=False, 20 help="Write all the timesteps to disk") 21 self.generalOpts.add_option("--purge-write", 22 action="store", 23 type="int", 24 dest="purgeWrite", 25 default=None, 26 help="Together with write-all-timesteps determines the number of time-steps that is kept on disc. All will be kept if unset") 27 self.generalOpts.add_option("--run-until", 28 action="store", 29 type="float", 30 dest="runUntil", 31 default=None, 32 help="Change the endTime so that the case only runs until this time")
33
34 - def addWriteAllTrigger(self,run,sol):
35 if self.opts.purgeWrite!=None and not self.opts.writeAll: 36 warning("purgeWrite of",self.opts.purgeWrite,"ignored because write-all-timesteps unused") 37 38 if self.opts.writeAll or self.opts.runUntil!=None: 39 warning("Adding Trigger and resetting to safer start-settings") 40 trig=WriteAllTrigger(sol, 41 self.opts.writeAll, 42 self.opts.purgeWrite, 43 self.opts.runUntil) 44 run.addEndTrigger(trig.resetIt)
45
46 -class WriteAllTrigger:
47 - def __init__(self,sol,writeAll,purge,until):
48 self.control=ParsedParameterFile(path.join(sol.systemDir(),"controlDict"), 49 backup=True, 50 doMacroExpansion=True) 51 52 self.fresh=True 53 54 try: 55 if writeAll: 56 self.control["writeControl"]="timeStep" 57 self.control["writeInterval"]="1" 58 if purge!=None: 59 self.control["purgeWrite"]=purge 60 61 if until!=None: 62 self.control["endTime"]=until 63 64 self.control.writeFile() 65 except Exception: 66 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 67 warning("Restoring defaults") 68 self.control.restore() 69 raise e
70
71 - def resetIt(self):
72 if self.fresh: 73 warning("Trigger called: Resetting the controlDict") 74 self.control.restore() 75 self.fresh=False
76 77 # Should work with Python3 and Python2 78