/[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 1312 - (hide annotations)
Mon Sep 24 06:18:44 2007 UTC (12 years ago) by ksteube
File MIME type: text/x-python
File size: 16366 byte(s)
The MPI branch is hereby closed. All future work should be in trunk.

Previously in revision 1295 I merged the latest changes to trunk into trunk-mpi-branch.
In this revision I copied all files from trunk-mpi-branch over the corresponding
trunk files. I did not use 'svn merge', it was a copy.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26