/[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 939 by gross, Thu Jan 25 04:23:38 2007 UTC revision 1020 by phornby, Mon Mar 12 10:12:36 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 71  class ESySXMLParser(object): Line 73  class ESySXMLParser(object):
73        self.__dom = minidom.parseString(xml)        self.__dom = minidom.parseString(xml)
74        self.__linkable_object_registry= {}        self.__linkable_object_registry= {}
75        self.__link_registry=  []        self.__link_registry=  []
76        self.__esys=self.__dom.firstChild        self.__esys=self.__dom.getElementsByTagName('ESys')[0]
77        self.debug=debug        self.debug=debug
78        
79      def getClassPath(self, node):      def getClassPath(self, node):
# Line 467  class ParameterSet(LinkableObject): Line 469  class ParameterSet(LinkableObject):
469              setattr(self,prm,value)              setattr(self,prm,value)
470              self.parameters.add(prm)              self.parameters.add(prm)
471    
             self.trace("parameter %s has been declared."%prm)  
   
472      def releaseParameters(self,name):      def releaseParameters(self,name):
473          """          """
474      Removes parameter name from the paramameters.      Removes parameter name from the paramameters.
# Line 476  class ParameterSet(LinkableObject): Line 476  class ParameterSet(LinkableObject):
476          if self.isParameter(name):          if self.isParameter(name):
477              self.parameters.remove(name)              self.parameters.remove(name)
478              self.trace("parameter %s has been removed."%name)              self.trace("parameter %s has been removed."%name)
479    
480        def checkLinkTargets(self, models, hash):
481            """
482            returns a set of tuples ("<self>(<name>)", <target model>) if the parameter <name> is linked to model <target model>
483            but <target model> is not in the list models. If the a parameter is linked to another parameter set which is not in the hash list
484            the parameter set is checked for its models. hash gives the call history.
485            """
486            out=set()
487            for name, value in self:
488                if isinstance(value, Link):
489                   m=value.getTarget()
490                   if isinstance(m, Model):
491                       if not m in models: out.add( (str(self)+"("+name+")",m) )
492                   elif isinstance(m, ParameterSet) and not m in hash:
493                         out|=set( [ (str(self)+"("+name+")."+f[0],f[1]) for f in m.checkLinkTargets(models, hash+[ self ] ) ] )
494            return out
495            
496      def __iter__(self):      def __iter__(self):
497          """          """
# Line 527  class ParameterSet(LinkableObject): Line 543  class ParameterSet(LinkableObject):
543                  for i in value:                  for i in value:
544                      if isinstance(i, bool):                      if isinstance(i, bool):
545                          elem_type = max(elem_type,0)                          elem_type = max(elem_type,0)
546                      if isinstance(i, int) and not isinstance(i, bool):                      elif isinstance(i, int):
547                          elem_type = max(elem_type,1)                          elem_type = max(elem_type,1)
548                      if isinstance(i, float):                      elif isinstance(i, float):
549                          elem_type = max(elem_type,2)                          elem_type = max(elem_type,2)
550                  if elem_type == 0: value = numarray.array(value,numarray.Bool)                  if elem_type == 0: value = numarray.array(value,numarray.Bool)
551                  if elem_type == 1: value = numarray.array(value,numarray.Int)                  if elem_type == 1: value = numarray.array(value,numarray.Int)
# Line 587  class ParameterSet(LinkableObject): Line 603  class ParameterSet(LinkableObject):
603                      dic.appendChild(i)                      dic.appendChild(i)
604                   param.appendChild(dic)                   param.appendChild(dic)
605              else:              else:
                 print value  
606                  raise ValueError("cannot serialize %s type to XML."%str(value.__class__))                  raise ValueError("cannot serialize %s type to XML."%str(value.__class__))
607    
608              node.appendChild(param)              node.appendChild(param)
# Line 729  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 746  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 849  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 909  class Simulation(Model): Line 935  class Simulation(Model):
935                 out.append(m)                 out.append(m)
936          return list(set(out))          return list(set(out))
937    
938      def checkModelLinks(self, models):      def checkModels(self, models, hash):
939          """          """
940          returns a list of (model,parameter, target model ) if the the parameter of model          returns a list of (model,parameter, target model ) if the the parameter of model
941          is linking to the target_model which is not in list of models.          is linking to the target_model which is not in list of models.
942          """          """
943          out=[]          out=self.checkLinkTargets(models, hash + [self])
944          for m in self.iterModels():          for m in self.iterModels():
945              if isinstance(m, Simulation):              if isinstance(m, Simulation):
946                 out+=[ (m,) + f for f in  m.checkModelLinks(models) ]                   out|=m.checkModels(models, hash)
947              else:              else:
948                for p in m:                   out|=m.checkLinkTargets(models, hash + [self])
949                   if isinstance(p[1], Link):          return set( [ (str(self)+"."+f[0],f[1]) for f in out ] )
                     l_m=p[1].getTarget()  
                     if isinstance(l_m, Model) and not l_m in models: out.append( (m,p[0],l_m) )  
         return out  
950    
951            
952      def getSafeTimeStepSize(self,dt):      def getSafeTimeStepSize(self,dt):
# Line 934  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 1004  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 1019  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 1053  class Simulation(Model): Line 1084  class Simulation(Model):
1084          # check the completness of the models:          # check the completness of the models:
1085          # first a list of all the models involved in the simulation including subsimulations:          # first a list of all the models involved in the simulation including subsimulations:
1086          #          #
1087          missing=self.checkModelLinks(self.getAllModels())          missing=self.checkModels(self.getAllModels(), [])
1088          if len(missing)>0:          if len(missing)>0:
1089              msg=""              msg=""
1090              for l in missing:              for l in missing:
1091                   msg+="\n\t"+str(l[-1])+" at "+str(self)                   msg+="\n\t"+str(l[1])+" at "+l[0]
                  for i in xrange(len(l)-1): msg+="."+str(l[i])  
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(self.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 1090  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)
1130              dt=dt_new              if not check_pointing==None:
1131              if not check_point==None:                 if check_pointing.doDump():
                 if n%check_point==0:  
1132                      self.trace("check point is created.")                      self.trace("check point is created.")
1133                      self.writeXML()                      self.writeXML()
1134          self.doFinalization()          self.doFinalization()
# Line 1124  class Simulation(Model): Line 1154  class Simulation(Model):
1154              if isinstance(n, minidom.Text):              if isinstance(n, minidom.Text):
1155                  continue                  continue
1156              sims.append(esysxml.getComponent(n))              sims.append(esysxml.getComponent(n))
1157          sims.sort(cmp=_comp)          sims.sort(_comp)
1158          sim=cls([s[1] for s in sims], debug=esysxml.debug)          sim=cls([s[1] for s in sims], debug=esysxml.debug)
1159          esysxml.registerLinkableObject(sim, node)          esysxml.registerLinkableObject(sim, node)
1160          return sim          return sim
# Line 1203  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.939  
changed lines
  Added in v.1020

  ViewVC Help
Powered by ViewVC 1.1.26