/[escript]/trunk/escript/test/python/run_xml.py
ViewVC logotype

Contents of /trunk/escript/test/python/run_xml.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 918 - (show annotations)
Wed Jan 3 06:30:00 2007 UTC (13 years, 9 months ago) by gross
File MIME type: text/x-python
File size: 14287 byte(s)
fixes on ESySXML to get runmodel going.

    * object ids are now local for read and write of XML
    * ParameterSets are now written with class name
    * ParameterSets linked by other ParameterSets (previously only Models) are written now 
    * list are now lists of str (rather than bools), lists with bool, int or float are mapped to numarray
    * numarray are writen with generic types Bool, Int, Float (portability!)


1 #!/usr/bin/python
2
3 # $Id$
4 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
5 http://www.access.edu.au
6 Primary Business: Queensland, Australia"""
7 __license__="""Licensed under the Open Software License version 3.0
8 http://www.opensource.org/licenses/osl-3.0.php"""
9
10 import unittest
11 from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,ESySXMLParser,DataSource
12 import math
13 from cStringIO import StringIO
14 from xml.dom import minidom
15
16 class XMLDocumentTestCase(unittest.TestCase):
17
18 def setUp(self):
19
20 o1=ODETEST(debug=False)
21 o1.u=10
22 o2=ODETEST(debug=False)
23 o2.u=-10.
24 o1.f=Link(o2,"u")
25 o2.f=Link(o1,"u")
26 m=Messenger()
27 o1.dt=0.01
28 m.message=Link(o1)
29 s=Simulation([o1,o2,m],debug=False)
30 s.run()
31 output = StringIO()
32 s.writeXML(output)
33 output.reset()
34 outputList = output.readlines()
35 self.xmlList = outputList
36
37 def testFirstLine(self):
38 firstLine = self.xmlList[0]
39 self.assertEqual('<?xml version="1.0" ?>\n', firstLine)
40
41 def testEsysHeader(self):
42 header = self.xmlList[1]
43 self.assertEqual('<ESys>\n', header)
44
45 def testEsysFooter(self):
46 footer = self.xmlList[-1]
47 self.assertEqual('</ESys>\n', footer)
48
49 def testSimulationHeader(self):
50 pass
51
52 def testSimulationFooter(self):
53 pass
54
55 class SimulationTestCase(unittest.TestCase):
56 def setUp(self):
57 o1=ODETEST(debug=False)
58 o1.u=10
59 o2=ODETEST(debug=False)
60 o2.u=-10.
61 o1.f=Link(o2,"u")
62 o2.f=Link(o1,"u")
63 m=Messenger()
64 o1.dt=0.01
65 m.message=Link(o1)
66 self.s=Simulation([o1,o2,m],debug=False)
67 self.s.run()
68 output = StringIO()
69 self.s.writeXML(output)
70 output.reset()
71 self.xml = output.read()
72
73 def testSimulation(self):
74 assert "<Simulation" in self.xml, "I should see a Simulation"
75
76 def testParseAndInstanceOfSimulation(self):
77
78 xml = ESySXMLParser(self.xml)
79 newSim = xml.parse()[0]
80 assert (isinstance (newSim, Simulation))
81 newout = StringIO()
82 newSim.writeXML(newout)
83 newout.reset()
84 xml = newout.read()
85 assert '<Simulation' in xml, "Missing a Simulation! It should be in this!"
86
87
88
89
90 class LinkTestCase(unittest.TestCase):
91
92
93 def setUp(self):
94
95 self.o1=ODETEST(debug=False)
96 #self.o1.u=10
97 self.o2=ODETEST(debug=False)
98 self.o2.u=-10.
99 self.o1.f=Link(self.o2,"u")
100 self.o2.f=Link(self.o1,"u")
101 self.o2.declareParameter(child=self.o1)
102
103 def testLinkCreation(self):
104 self.o1.f=Link(self.o2,"u")
105 assert self.o1.f
106
107
108 def testLinkValue(self):
109 self.assertEqual(self.o1.f, -10)
110
111 def testLinkTarget(self):
112 pass
113
114 def testLinkDefaultAttribute(self):
115 Link(self.o2)
116
117 def testLinkXML(self):
118 s = StringIO()
119 self.o2.writeXML(s)
120 s.reset()
121 xmlout = s.read()
122 assert '<Link' in xmlout
123
124 def testLinkTargetXML(self):
125 pass
126
127 class ParamaterSetTestCase(unittest.TestCase):
128
129
130 def setUp(self):
131 self.p = ParameterSet()
132 self.p.declareParameter(gamma1=1.,gamma2=2.,gamma3=3.)
133
134 def testParameterSetCreation(self):
135 self.assertEqual(self.p.gamma1, 1.)
136
137 def testParameterSetXMLCreation(self):
138 s = StringIO()
139 self.p.writeXML(s)
140 s.reset()
141 xmlout = s.read()
142 assert ("gamma1" in xmlout)
143 assert ("gamma2" in xmlout)
144 assert ("gamma3" in xmlout)
145 esysxml=ESySXMLParser(xmlout)
146 parsable = esysxml.parse()[0]
147 assert (isinstance (parsable, ParameterSet))
148 assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
149
150 def testParameterSetFromXML(self):
151 s = StringIO()
152 self.p.writeXML(s)
153 s.reset()
154 xmlout = s.read()
155 esysxml=ESySXMLParser(xmlout)
156 doc = self._class(self.p)
157 pset = ParameterSet.fromDom(esysxml, self._dom(self.p).getElementsByTagName("ParameterSet")[0])
158 assert (isinstance(pset, ParameterSet))
159 assert (isinstance(doc, ParameterSet))
160 self.assertEqual(self.p.gamma1,doc.gamma1)
161
162
163 def testParameterSetWithChildrenFromXML(self):
164 p2 = ParameterSet()
165 p2.declareParameter(s="abc", f=3.)
166 self.p.declareParameter(child=p2)
167 doc = self._class(self.p)
168 #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])
169 self.assertEqual(self.p.child.f, doc.child.f)
170
171 def testParameterSetChild(self):
172 p2 = ParameterSet()
173 p2.declareParameter(s="abc", f=3.)
174 self.p.declareParameter(child=p2)
175 self.assertEqual(self.p.child.s, "abc")
176 self.assertEqual(self.p.child.f, 3.)
177
178 def _dom(self, input):
179 s = StringIO()
180 input.writeXML(s)
181 s.reset()
182 xmlout = s.read()
183 doc = minidom.parseString(xmlout)
184 return doc
185
186 def _class(self, input):
187 s = StringIO()
188 input.writeXML(s)
189 s.reset()
190 xmlout = s.read()
191 esysxml=ESySXMLParser(xmlout)
192 doc =esysxml.parse()[0]
193 return doc
194
195 def testFromDomInt(self):
196 p3 = ParameterSet()
197 p3.declareParameter(inttest=1)
198 doc = self._class(p3)
199 assert type(doc.inttest)==int
200
201 def testFromDomNumarrayVector(self):
202 import numarray
203 p3 = ParameterSet()
204 mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)
205 p3.declareParameter(numtest=mynumarray)
206 doc = self._class(p3)
207 assert doc.numtest.type() == numarray.Float64
208 assert type(doc.numtest) == numarray.NumArray
209
210 def testFromDomNumarrayMulti(self):
211 import numarray
212 p3 = ParameterSet()
213 mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)
214 p3.declareParameter(numtest=mynumarray)
215 doc = self._class(p3)
216 assert doc.numtest.type() == numarray.Float64
217 assert type(doc.numtest) == numarray.NumArray
218
219 def testLists(self):
220 import numarray
221 p4 = ParameterSet()
222 mylist = [True, False, False, True]
223 p4.declareParameter(listest=mylist)
224 doc = self._class(p4)
225 assert doc.listest.type() == numarray.Bool
226 assert type(doc.listest) == numarray.NumArray
227 assert numarray.all(numarray.equal(mylist, doc.listest))
228
229 def testDatasource(self):
230 p5 = ParameterSet()
231 myURI = DataSource("somelocalfile.txt", "text")
232 p5.declareParameter(uritest=myURI)
233 doc = self._class(p5)
234 self.assertEquals(myURI.uri, doc.uritest.uri)
235 self.assertEquals(myURI.fileformat, doc.uritest.fileformat)
236 assert type(doc.uritest) == DataSource
237
238
239
240 class ModeltoDomTestCase(unittest.TestCase):
241
242 def _class(self):
243 # returns a modelframe class, generated from the xml
244 s = StringIO()
245 self.o1.writeXML(s)
246 s.reset()
247 self.xmlout = s.read()
248 esysxml=ESySXMLParser(xmlout)
249 doc =esysxml.parse()[0]
250 return doc
251
252 def _dom(self):
253 # returns a minidom dom element, generated from the xml
254 s = StringIO()
255 self.o1.writeXML(s)
256 s.reset()
257 self.xmlout = s.read()
258 doc = minidom.parseString(self.xmlout)
259 return doc
260
261 def setUp(self):
262 self.o1=ODETEST(debug=False)
263 self.o1.message='blah'
264
265 def testModelExists(self):
266 modeldoc = self._class()
267 assert (isinstance, (modeldoc, Model))
268 assert self._dom().getElementsByTagName("Model")
269
270 def testModelhasID(self):
271 assert int(self._dom().getElementsByTagName("Model")[0].getAttribute("id"))>99
272
273 class ModeltoDomTestCase(unittest.TestCase):
274 def _xml(self, modulename, modelname):
275 # returns a modelframe class, generated from the xml
276 return '''<?xml version="1.0" ?>
277 <ESys> <Simulation type="Simulation"> <Component rank="0">
278
279 <Model id="127" module="%s" type="%s">
280
281 <Parameter type="float"> <Name> a </Name> <Value> 0.9 </Value> </Parameter>
282 <Parameter type="Link"> <Name> f </Name> <Value> <Link> <Target> 128 </Target>
283 <Attribute> u </Attribute> </Link> </Value> </Parameter> <Parameter
284 type="float"> <Name> tend </Name> <Value>
285 1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
286 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
287 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
288 <Value>
289 0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
290 <Value> current error = 9.516258e-01 </Value> </Parameter> </Model>
291 </Component> <Component rank="1"> <Model id="128" type="ODETEST"> <Parameter
292 type="float"> <Name> a </Name> <Value>
293 0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
294 <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
295 </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
296 1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
297 -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
298 <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
299 </Name> <Value>
300 0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
301 current error = 1.904837e+01 </Value> </Parameter> </Model> </Component>
302 <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
303 type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
304 <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
305 </Component> </Simulation> <Model id="150" type="ODETEST"> <Parameter
306 type="float"> <Name> a </Name> <Value>
307 0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
308 <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
309 </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
310 1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
311 -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
312 <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
313 </Name> <Value>
314 0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
315 current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="130"
316 type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
317 0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
318 <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
319 </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
320 1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
321 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
322 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
323 <Value>
324 0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
325 <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
326 id="170" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
327 0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
328 <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
329 </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
330 1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
331 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
332 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
333 <Value>
334 0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
335 <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> </ESys>
336 ''' % (modulename, modelname)
337
338 def testModuleAttribute(self):
339 esysxml=ESySXMLParser(self._xml('run_xml', 'ODETEST'))
340 modeldoc=esysxml.parse()[0]
341
342 def testModuleAttributeFails(self):
343 try:
344 esysxml=ESySXMLParser(self._xml('a', 'b'))
345 modeldoc=esysxml.parse()[0]
346 except ImportError:
347 return # correct
348
349 assert False, "This test should have resulted in an ImportError"
350
351 class Messenger(Model):
352 def __init__(self, *args, **kwargs):
353 Model.__init__(self, *args, **kwargs)
354 self.declareParameter(message="none")
355
356 def doInitialization(self):
357 self.__t=0
358 #print "I start talking now!"
359
360 def doStepPostprocessing(self,dt):
361 self.__t+=dt
362 #print "Message (time %e) : %s "%(self.__t,self.message)
363
364 def doFinalization(self):
365 #print "I have no more to say!"
366 pass
367
368
369
370 class ODETEST(Model):
371 """ implements a solver for the ODE
372
373 du/dt=a*u+f(t)
374
375 we use a implicit euler scheme :
376
377 u_n-u_{n-1}= dt*a u_n + st*f(t_n)
378
379 to get u_n we run an iterative process
380
381 u_{n.k}=u_{n-1}+dt*(a u_{n.i-1} + f(t_n))
382
383
384 input for this model are step size dt, end time tend and a value for
385 a, f and initial value for u. we need also a tolerance tol for a
386 stopping criterion.
387
388 """
389
390 def __init__(self, *args, **kwargs):
391 Model.__init__(self, *args, **kwargs)
392 self.declareParameter(tend=1.,dt=0.1,a=0.9,u=10.,f=0.,message="",tol=1.e-8)
393
394 def doInitialization(self):
395 self.__tn=0
396 self.__iter=0
397
398 def doStepPreprocessing(self,dt):
399 self.__iter=0
400 self.__u_last=self.u
401
402 def doStep(self,dt):
403 self.__iter+=1
404 self.__u_old=self.u
405 self.u=self.__u_last+dt*(self.a*self.__u_old+self.f)
406
407 def terminate(self):
408 if self.__iter<1:
409 return False
410 else:
411 return abs(self.__u_old-self.u)<self.tol*abs(self.u)
412
413 def doStepPostprocessing(self,dt):
414 self.__tn+=dt
415 self.message="current error = %e"%abs(self.u-10.*math.exp((self.a-1.)*self.__tn))
416
417 def getSafeTimeStepSize(self,dt):
418 return min(self.dt,1./(abs(self.a)+1.))
419
420 def finalize(self):
421 return self.__tn>=self.tend
422
423
424
425 if __name__ == "__main__":
426 unittest.main()
427

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26