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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (5 years, 2 months ago) by sshaw
File MIME type: text/x-python
File size: 16065 byte(s)
revamping testrunners, now uses automated discovery and allows running specific tests without modifying files (see escriptcore/py_src/testing.py for more info/examples)

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26