/[escript]/trunk/escript/py_src/modelframe.py
ViewVC logotype

Diff of /trunk/escript/py_src/modelframe.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 964 by gross, Tue Feb 13 05:10:26 2007 UTC revision 975 by gross, Thu Feb 15 10:18:18 2007 UTC
# Line 27  from sys import stdout Line 27  from sys import stdout
27  import numarray  import numarray
28  import operator  import operator
29  import itertools  import itertools
30    import time
31    import os
32    
33  # import the 'set' module if it's not defined (python2.3/2.4 difference)  # import the 'set' module if it's not defined (python2.3/2.4 difference)
34  try:  try:
# Line 742  class Model(ParameterSet): Line 744  class Model(ParameterSet):
744         return "<%s %d>"%(self.__class__.__name__,id(self))         return "<%s %d>"%(self.__class__.__name__,id(self))
745    
746    
747        def setUp(self):
748            """
749            Sets up the model.
750    
751            This function may be overwritten.
752            """
753            pass
754    
755      def doInitialization(self):      def doInitialization(self):
756          """          """
757      Initializes the time stepping scheme.        Initializes the time stepping scheme. This method is not called in case of a restart.
758            
759      This function may be overwritten.      This function may be overwritten.
760      """      """
761          pass          pass
762      def doInitialStep(self):      def doInitialStep(self):
763          """          """
764      performs an iteration step in the initialization phase      performs an iteration step in the initialization phase. This method is not called in case of a restart.
765    
766      This function may be overwritten.      This function may be overwritten.
767      """      """
# Line 759  class Model(ParameterSet): Line 769  class Model(ParameterSet):
769    
770      def terminateInitialIteration(self):      def terminateInitialIteration(self):
771          """          """
772      Returns True if iteration at the inital phase is terminated.      Returns True if iteration at the inital phase is terminated.
773      """      """
774          return True          return True
775    
776      def doInitialPostprocessing(self):      def doInitialPostprocessing(self):
777          """          """
778      finalises the initialization iteration process      finalises the initialization iteration process. This method is not called in case of a restart.
779    
780      This function may be overwritten.      This function may be overwritten.
781      """      """
# Line 862  class Simulation(Model): Line 872  class Simulation(Model):
872      Initiates a simulation from a list of models.      Initiates a simulation from a list of models.
873      """      """
874          super(Simulation, self).__init__(**kwargs)          super(Simulation, self).__init__(**kwargs)
875            self.declareParameter(time=0.,
876                                  time_step=0,
877                                  dt = self.UNDEF_DT)
878          for m in models:          for m in models:
879              if not isinstance(m, Model):              if not isinstance(m, Model):
880                   raise TypeError("%s is not a subclass of Model."%m)                   raise TypeError("%s is not a subclass of Model."%m)
# Line 944  class Simulation(Model): Line 957  class Simulation(Model):
957      """      """
958          out=min([o.getSafeTimeStepSize(dt) for o in self.iterModels()])          out=min([o.getSafeTimeStepSize(dt) for o in self.iterModels()])
959          return out          return out
960    
961        def setUp(self):
962            """
963            performs the setup for all models
964            """
965            for o in self.iterModels():
966                 o.setUp()
967            
968      def doInitialization(self):      def doInitialization(self):
969          """          """
970      Initializes all models.      Initializes all models.
971      """      """
         self.n=0  
         self.tn=0.  
972          for o in self.iterModels():          for o in self.iterModels():
973               o.doInitialization()               o.doInitialization()
974      def doInitialStep(self):      def doInitialStep(self):
# Line 1014  class Simulation(Model): Line 1032  class Simulation(Model):
1032      """      """
1033          for o in self.iterModels():          for o in self.iterModels():
1034              o.doStepPostprocessing(dt)              o.doStepPostprocessing(dt)
1035          self.n+=1          self.time_step+=1
1036          self.tn+=dt          self.time+=dt
1037            self.dt=dt
1038            
1039      def doStep(self,dt):      def doStep(self,dt):
1040          """          """
# Line 1029  class Simulation(Model): Line 1048  class Simulation(Model):
1048          """          """
1049          self.iter=0          self.iter=0
1050          while not self.terminateIteration():          while not self.terminateIteration():
1051              if self.iter==0: self.trace("iteration at %d-th time step %e starts"%(self.n+1,self.tn+dt))              if self.iter==0: self.trace("iteration at %d-th time step %e starts"%(self.time_step+1,self.time+dt))
1052              self.iter+=1              self.iter+=1
1053              self.trace("iteration step %d"%(self.iter))              self.trace("iteration step %d"%(self.iter))
1054              for o in self.iterModels():              for o in self.iterModels():
1055                    o.doStep(dt)                    o.doStep(dt)
1056          if self.iter>0: self.trace("iteration at %d-th time step %e finalized."%(self.n+1,self.tn+dt))          if self.iter>0: self.trace("iteration at %d-th time step %e finalized."%(self.time_step+1,self.time+dt))
1057    
1058      def run(self,check_point=None):      def run(self,check_pointing=None):
1059          """          """
1060      Run the simulation by performing essentially::      Run the simulation by performing essentially::
1061            
1062          self.doInitialization()              self.setUp()
1063              while not self.terminateInitialIteration(): self.doInitialStep()              if not restart:
1064              self.doInitialPostprocessing()              self.doInitialization()
1065                    while not self.terminateInitialIteration(): self.doInitialStep()
1066                    self.doInitialPostprocessing()
1067          while not self.finalize():          while not self.finalize():
1068              dt=self.getSafeTimeStepSize()              dt=self.getSafeTimeStepSize()
1069              self.doStep(dt)                  self.doStepPreprocessing(dt_new)
1070              if n%check_point==0:                  self.doStep(dt_new)
1071              self.writeXML()                  self.doStepPostprocessing(dt_new)
1072          self.doFinalization()          self.doFinalization()
1073    
1074          If one of the models in throws a C{FailedTimeStepError} exception a          If one of the models in throws a C{FailedTimeStepError} exception a
# Line 1070  class Simulation(Model): Line 1091  class Simulation(Model):
1091                   msg+="\n\t"+str(l[1])+" at "+l[0]                   msg+="\n\t"+str(l[1])+" at "+l[0]
1092              raise MissingLink("link targets missing in the Simulation: %s"%msg)              raise MissingLink("link targets missing in the Simulation: %s"%msg)
1093          #==============================          #==============================
1094          self.doInitialization()          self.setUp()
1095          self.doInitialStep()          if self.time_step < 1:
1096          self.doInitialPostprocessing()             self.doInitialization()
1097          dt=self.UNDEF_DT             self.doInitialStep()
1098               self.doInitialPostprocessing()
1099          while not self.finalize():          while not self.finalize():
1100              step_fail_counter=0              step_fail_counter=0
1101              iteration_fail_counter=0              iteration_fail_counter=0
1102              if self.n==0:              if self.time_step==0:
1103                  dt_new=self.getSafeTimeStepSize(dt)                  dt_new=self.getSafeTimeStepSize(dt)
1104              else:              else:
1105                  dt_new=min(max(self.getSafeTimeStepSize(dt),dt/self.MAX_CHANGE_OF_DT),dt*self.MAX_CHANGE_OF_DT)                  dt_new=min(max(self.getSafeTimeStepSize(self.dt),self.dt/self.MAX_CHANGE_OF_DT),self.dt*self.MAX_CHANGE_OF_DT)
1106              self.trace("%d. time step %e (step size %e.)" % (self.n+1,self.tn+dt_new,dt_new))              self.trace("%d. time step %e (step size %e.)" % (self.time_step+1,self.time+dt_new,dt_new))
1107              end_of_step=False              end_of_step=False
1108              while not end_of_step:              while not end_of_step:
1109                 end_of_step=True                 end_of_step=True
1110                 if not dt_new>0:                 if not dt_new>0:
1111                    raise NonPositiveStepSizeError("non-positive step size in step %d"%(self.n+1))                    raise NonPositiveStepSizeError("non-positive step size in step %d"%(self.time_step+1))
1112                 try:                 try:
1113                    self.doStepPreprocessing(dt_new)                    self.doStepPreprocessing(dt_new)
1114                    self.doStep(dt_new)                    self.doStep(dt_new)
# Line 1099  class Simulation(Model): Line 1121  class Simulation(Model):
1121                             raise SimulationBreakDownError("reduction of time step to achieve convergence failed after %s steps."%self.FAILED_TIME_STEPS_MAX)                             raise SimulationBreakDownError("reduction of time step to achieve convergence failed after %s steps."%self.FAILED_TIME_STEPS_MAX)
1122                    self.trace("Iteration failed. Time step is repeated with new step size %s."%dt_new)                    self.trace("Iteration failed. Time step is repeated with new step size %s."%dt_new)
1123                 except FailedTimeStepError:                 except FailedTimeStepError:
1124                    dt_new=self.getSafeTimeStepSize(dt)                    dt_new=self.getSafeTimeStepSize(self.dt)
1125                    end_of_step=False                    end_of_step=False
1126                    step_fail_counter+=1                    step_fail_counter+=1
1127                    self.trace("Time step is repeated with new time step size %s."%dt_new)                    self.trace("Time step is repeated with new time step size %s."%dt_new)
1128                    if step_fail_counter>self.FAILED_TIME_STEPS_MAX:                    if step_fail_counter>self.FAILED_TIME_STEPS_MAX:
1129                          raise SimulationBreakDownError("Time integration is given up after %d attempts."%step_fail_counter)                          raise SimulationBreakDownError("Time integration is given up after %d attempts."%step_fail_counter)
             dt=dt_new  
1130              if not check_point==None:              if not check_point==None:
1131                  if n%check_point==0:                 if check_point.doDump():
1132                      self.trace("check point is created.")                      self.trace("check point is created.")
1133                      self.writeXML()                      self.writeXML()
1134          self.doFinalization()          self.doFinalization()
# Line 1212  class DataSource(object): Line 1233  class DataSource(object):
1233          return self.uri          return self.uri
1234    
1235      fromDom = classmethod(fromDom)      fromDom = classmethod(fromDom)
1236        
1237    class RestartManager(object):
1238         """
1239         A restart manager which does two things: it decides when restart files have created (when doDump returns true) and
1240         manages directories for restart files. The method getNewDumper creates a new directory and returns its name.
1241        
1242         This restart manager will decide to dump restart files if every dump_step calls of doDump or
1243         if more than dump_time since the last dump has elapsed. The restart manager controls two directories for dumping restart data, namely
1244         for the current and previous dump. This way the previous dump can be used for restart in the case the current dump failed.
1245    
1246         @cvar SEC: unit of seconds, for instance for 5*RestartManager.SEC to define 5 seconds.
1247         @cvar MIN: unit of minutes, for instance for 5*RestartManager.MIN to define 5 minutes.
1248         @cvar H: unit of hours, for instance for 5*RestartManager.H to define 5 hours.
1249         @cvar D: unit of days, for instance for 5*RestartManager.D to define 5 days.
1250         """
1251         SEC=1.
1252         MIN=60.
1253         H=360.
1254         D=8640.
1255         def __init__(self,dump_time=1080., dump_step=None, dumper=None):
1256             """
1257             initializes the RestartManager.
1258    
1259             @param dump_time: defines the minimum time interval in SEC between to dumps. If None, time is not used as criterion.
1260             @param dump_step: defines the number of calls of doDump between to dump events. If None, the call counter is not used as criterion.
1261             @param dumper: defines the directory for dumping restart files. Additionally the directories dumper+"_bkp" and dumper+"_bkp2" are used.
1262                            if the directory does not exist it is created. If dumper is not present a unique directory within the current
1263                            working directory is used.
1264             """
1265             self.__dump_step=dump_time
1266             self.__dump_time=dump_step
1267             self.__counter=0
1268             self.__saveMarker()
1269             if dumper == None:
1270                self.__dumper="restart"+str(os.getpid())
1271             else:
1272                self.__dumper=dumper
1273             self.__dumper_bkp=self.__dumper+"_bkp"
1274             self.__dumper_bkp2=self.__dumper+"_bkp2"
1275             self.__current_dumper=None
1276         def __saveMarker(self):
1277             self.__last_restart_time=time.time()
1278             self.__last_restart_counter=self.__counter
1279         def getCurrentDumper(self):
1280             """
1281             returns the name of the currently used dumper
1282             """
1283             return self.__current_dumper
1284         def doDump(self):
1285            """
1286            returns true the restart should be dumped. use C{getNewDumper} to get the directory name to be used.
1287            """
1288            if self.__dump_step == None:
1289               if self.__dump_step == None:
1290                  out = False
1291               else:
1292                  out = (self.__dump_step + self.__last_restart_counter) <= self.__counter
1293            else:
1294               if dump_step == None:
1295                  out = (self.__last_restart_time + self.__dump_time) <= time.time()
1296               else:
1297                  out =    ( (self.__dump_step + self.__last_restart_counter) <= self.__counter)  \
1298                        or ( (self.__last_restart_time + self.__dump_time) <= time.time() )
1299            if out: self.__saveMarker()
1300            self__counter+=1
1301         def getNewDumper(self):
1302           """
1303           creates a new directory to be used for dumping and returns its name.
1304           """
1305           if os.access(self.__dumper_bkp,os.F_OK):
1306              if os.access(self.__dumper_bkp2, os.F_OK):
1307                 raise RunTimeError("please remove %s."%self.__dumper_bkp2)
1308              try:
1309                 os.rename(self.__dumper_bkp, self.__dumper_bkp2)
1310              except:
1311                 self.__current_dumper=self.__dumper
1312                 raise RunTimeError("renaming back-up directory %s failed. Use %s for restart."%(self.__dumper_bkp,self.__dumper))
1313           if os.access(self.__dumper,os.F_OK):
1314              if os.access(self.__dumper_bkp, os.F_OK):
1315                 raise RunTimeError("please remove %s."%self.__dumper_bkp)
1316              try:
1317                 os.rename(self.__dumper, self.__dumper_bkp)
1318              except:
1319                 self.__current_dumper=self.__dumper_bkp2
1320                 raise RunTimeError("moving directory %s to back-up failed. Use %s for restart."%(self.__dumper,self.__dumper_bkp2))
1321           try:
1322              os.mkdir(self.__dumper)
1323           except:
1324              self.__current_dumper=self.__dumper_bkp
1325              raise RunTimeError("creating a new restart directory %s failed. Use %s for restart."%(self.__dumper,self.__dumper_bkp))
1326           if os.access(self.__dumper_bkp2, os.F_OK): os.rmdir(self.__dumper_bkp2)
1327           return self.getCurrentDumper()
1328            
1329        
1330  # vim: expandtab shiftwidth=4:  # vim: expandtab shiftwidth=4:

Legend:
Removed from v.964  
changed lines
  Added in v.975

  ViewVC Help
Powered by ViewVC 1.1.26