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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2344 - (hide annotations)
Mon Mar 30 02:13:58 2009 UTC (10 years, 7 months ago) by jfenwick
File MIME type: text/x-python
File size: 16431 byte(s)
Change __url__ to launchpad site

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26