/[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 956 - (show annotations)
Tue Feb 13 00:03:40 2007 UTC (12 years, 9 months ago) by gross
File MIME type: text/x-python
File size: 14231 byte(s)
Older versions of numarray dont have the "all" function. The test has been modified to run for these order versions. too.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26