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