/[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 123 by jgs, Fri Jul 8 04:08:13 2005 UTC revision 142 by jgs, Mon Jul 25 05:28:20 2005 UTC
# Line 3  Line 3 
3  from types import StringType,IntType,FloatType,BooleanType,ListType,DictType  from types import StringType,IntType,FloatType,BooleanType,ListType,DictType
4  from sys import stdout  from sys import stdout
5  import itertools  import itertools
6    # import modellib  temporarily removed!!!
 import modellib  
7    
8  # 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)
9  try:  try:
# Line 15  except NameError: Line 14  except NameError:
14  from xml.dom import minidom  from xml.dom import minidom
15    
16  def dataNode(document, tagName, data):  def dataNode(document, tagName, data):
17        """
18        dataNodes are the building blocks of the xml documents constructed in
19        this module. document is the current xml document, tagName is the
20        associated xml tag, and data is the values in the tag.
21        """
22      t = document.createTextNode(str(data))      t = document.createTextNode(str(data))
23      n = document.createElement(tagName)      n = document.createElement(tagName)
24      n.appendChild(t)      n.appendChild(t)
25      return n      return n
26    
27  def esysDoc():  def esysDoc():
28        """
29        Global method for creating an instance of an EsysXML document.
30        """
31      doc = minidom.Document()      doc = minidom.Document()
32      esys = doc.createElement('ESys')      esys = doc.createElement('ESys')
33      doc.appendChild(esys)      doc.appendChild(esys)
# Line 85  class Link: Line 92  class Link:
92       """       """
93            
94      def __init__(self,target,attribute=None):      def __init__(self,target,attribute=None):
95          """creates a link to the object target. If attribute is given, the link is          """
96            creates a link to the object target. If attribute is given, the link is
97          establised to this attribute of the target.  Otherwise the attribute is          establised to this attribute of the target.  Otherwise the attribute is
98          undefined."""          undefined.
99            """
100          self.target = target          self.target = target
101          self.attribute = None          self.attribute = None
102          self.setAttributeName(attribute)          self.setAttributeName(attribute)
103            
104      def setAttributeName(self,attribute):      def setAttributeName(self,attribute):
105          """set a new attribute name to be collected from the target object. The          """
106          target object must have the attribute with name attribute."""          set a new attribute name to be collected from the target object. The
107            target object must have the attribute with name attribute.
108            """
109          if attribute and self.target and not hasattr(self.target, attribute):          if attribute and self.target and not hasattr(self.target, attribute):
110              raise AttributeError("%s: target %s has no attribute %s."%(self, self.target, attribute))              raise AttributeError("%s: target %s has no attribute %s."%(self, self.target, attribute))
111          self.attribute = attribute          self.attribute = attribute
112            
113      def hasDefinedAttributeName(self):      def hasDefinedAttributeName(self):
114          """returns true if an attribute name is set"""          """
115            returns true if an attribute name is set
116            """
117          return self.attribute != None          return self.attribute != None
118            
119      def __repr__(self):      def __repr__(self):
120          """returns a string representation of the link"""          """
121            returns a string representation of the link
122            """
123          if self.hasDefinedAttributeName():          if self.hasDefinedAttributeName():
124              return "<Link to attribute %s of %s>" % (self.attribute, self.target)              return "<Link to attribute %s of %s>" % (self.attribute, self.target)
125          else:          else:
126              return "<Link to target %s>" % self.target              return "<Link to target %s>" % self.target
127            
128      def __call__(self,name=None):      def __call__(self,name=None):
129          """returns the value of the attribute of the target object. If the          """
130          atrribute is callable then the return value of the call is returned."""          returns the value of the attribute of the target object. If the
131            atrribute is callable then the return value of the call is returned.
132            """
133          if name:          if name:
134              out=getattr(self.target, name)              out=getattr(self.target, name)
135          else:          else:
# Line 124  class Link: Line 141  class Link:
141              return out              return out
142    
143      def toDom(self, document, node):      def toDom(self, document, node):
144          """ toDom method of Link. Creates a Link node and appends it to the current XML          """
145          document """          toDom method of Link. Creates a Link node and appends it to the current XML
146            document
147            """
148          link = document.createElement('Link')          link = document.createElement('Link')
149          link.appendChild(dataNode(document, 'Target', self.target.id))          link.appendChild(dataNode(document, 'Target', self.target.id))
150          # this use of id will not work for purposes of being able to retrieve the intended          # this use of id will not work for purposes of being able to retrieve the intended
# Line 144  class Link: Line 163  class Link:
163      fromDom = classmethod(fromDom)      fromDom = classmethod(fromDom)
164            
165      def writeXML(self,ostream=stdout):      def writeXML(self,ostream=stdout):
166          """writes an XML representation of self to the output stream ostream.          """
167            writes an XML representation of self to the output stream ostream.
168          If ostream is nor present the standart output stream is used.  If          If ostream is nor present the standart output stream is used.  If
169          esysheader==True the esys XML header is written"""          esysheader==True the esys XML header is written
170            """
171    
172          document, rootnode = esysDoc()          document, rootnode = esysDoc()
173          self.toDom(document, rootnode)          self.toDom(document, rootnode)
# Line 240  class SimulationFrame(LinkableObject): Line 261  class SimulationFrame(LinkableObject):
261      MAX_TIME_STEP_REDUCTION=20      MAX_TIME_STEP_REDUCTION=20
262      MAX_ITER_STEPS=50      MAX_ITER_STEPS=50
263            
264      def __init__(self,*args, **kwargs):      def __init__(self,**kwargs):
265          """          """
266          Initialises a simulation          Initialises a simulation
267                    
268          Just calls the parent constructor.          Just calls the parent constructor.
269          """          """
270          LinkableObject.__init__(self,*args, **kwargs)          LinkableObject.__init__(self,**kwargs)
271            
272      def doInitialization(self,t):      def doInitialization(self,t):
273          """initializes the time stepping scheme. This function may be          """initializes the time stepping scheme. This function may be
# Line 363  class SimulationFrame(LinkableObject): Line 384  class SimulationFrame(LinkableObject):
384  class Simulation(SimulationFrame):  class Simulation(SimulationFrame):
385      """A Simulation object is comprised by SimulationFrame(s) called subsimulations."""      """A Simulation object is comprised by SimulationFrame(s) called subsimulations."""
386            
387      def __init__(self, subsimulations=[], *args, **kwargs):      def __init__(self, subsimulations=[], **kwargs):
388          """initiates a simulation from a list of subsimulations. """          """initiates a simulation from a list of subsimulations. """
389          SimulationFrame.__init__(self, *args, **kwargs)          SimulationFrame.__init__(self, **kwargs)
390          self.__subsimulations=[]          self.__subsimulations=[]
391    
392          for i in range(len(subsimulations)):          for i in range(len(subsimulations)):
# Line 634  class ParameterSet(LinkableObject): Line 655  class ParameterSet(LinkableObject):
655    
656          def _stringfromValue(doc):          def _stringfromValue(doc):
657              return str(doc.nodeValue.strip())              return str(doc.nodeValue.strip())
658                  
659            def _intfromValue(doc):
660                return int(doc.nodeValue.strip())
661    
662            def _boolfromValue(doc):
663                return bool(doc.nodeValue.strip())
664          
665          # Mapping from text types in the xml to methods used to process trees of that type          # Mapping from text types in the xml to methods used to process trees of that type
666          ptypemap = {"Simulation": Simulation.fromDom,          ptypemap = {"Simulation": Simulation.fromDom,
667                      "Model":Model.fromDom,                      "Model":Model.fromDom,
668                      "ParameterSet":ParameterSet.fromDom,                      "ParameterSet":ParameterSet.fromDom,
669                      "Link":Link.fromDom,                      "Link":Link.fromDom,
670                      "float":_floatfromValue,                      "float":_floatfromValue,
671                      #"int":_intfromValue,                      "int":_intfromValue,
672                      "str":_stringfromValue,                      "str":_stringfromValue,
673                      #"bool":_boolfromValue                      "bool":_boolfromValue
674                      }                      }
675    
676          parameters = {}          parameters = {}
# Line 680  class ParameterSet(LinkableObject): Line 707  class ParameterSet(LinkableObject):
707  class Model(ParameterSet,SimulationFrame):  class Model(ParameterSet,SimulationFrame):
708      """a Model is a SimulationFrame which is also a ParameterSet."""      """a Model is a SimulationFrame which is also a ParameterSet."""
709    
710      def __init__(self,parameters=[],*args,**kwargs):      def __init__(self,parameters=[],**kwargs):
711          """creates a model"""          """creates a model"""
712          ParameterSet.__init__(self, parameters=parameters)          ParameterSet.__init__(self, parameters=parameters)
713          SimulationFrame.__init__(self,*args,**kwargs)          SimulationFrame.__init__(self,**kwargs)
714    
715      def toDom(self, document, node):      def toDom(self, document, node):
716          """ toDom method of Model class """          """ toDom method of Model class """
# Line 713  class NonPositiveStepSizeError(Exception Line 740  class NonPositiveStepSizeError(Exception
740  #  #
741  #   ignore this text:  #   ignore this text:
742  #  #
743  """ the Model class provides a framework to run a time-dependent simulation. A  """
744    the Model class provides a framework to run a time-dependent simulation. A
745  Model has a set of parameter which may be fixed or altered by the Model itself  Model has a set of parameter which may be fixed or altered by the Model itself
746  or other Models over time.    or other Models over time.  
747    

Legend:
Removed from v.123  
changed lines
  Added in v.142

  ViewVC Help
Powered by ViewVC 1.1.26