/[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 871 - (hide annotations)
Sat Oct 14 08:25:54 2006 UTC (13 years ago) by elspeth
File MIME type: text/x-python
File size: 13143 byte(s)
Numarray support added to modelframe and tests for numarray added to run_xml.
Numarrays will look like this in ESysXML:

A 2 x 3 numarray will look like this:

<Parameter type="NumArray">
                        <Name>
                                numtest
                        </Name>
                        <Value>
                                <ArrayType>
                                        Float64
                                </ArrayType>
                                <Shape>
                                        2 3
                                </Shape>
                                <Data>
                                        1.0 2.0 3.0 3.0 4.0 5.0
                                </Data>
                        </Value>
                </Parameter>

A numarray vector will look like this:

<Parameter type="NumArray">
                        <Name>
                                numtest
                        </Name>
                        <Value>
                                <ArrayType>
                                        Float64
                                </ArrayType>
                                <Shape>
                                        3
                                </Shape>
                                <Data>
                                        3.0 4.0 5.0
                                </Data>
                        </Value>
                </Parameter>



1 jgs 147 #!/usr/bin/python
2    
3     # $Id$
4 elspeth 629 __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 jgs 147
10     import unittest
11 jgs 149 from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,parse
12 jgs 147 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     newSim = parse(self.xml)
79     assert (isinstance (newSim, Simulation))
80     newout = StringIO()
81     newSim.writeXML(newout)
82     newout.reset()
83     xml = newout.read()
84     assert '<Simulation' in xml, "Missing a Simulation! It should be in this!"
85    
86    
87    
88    
89     class LinkTestCase(unittest.TestCase):
90    
91    
92     def setUp(self):
93    
94     self.o1=ODETEST(debug=False)
95     #self.o1.u=10
96     self.o2=ODETEST(debug=False)
97     self.o2.u=-10.
98     self.o1.f=Link(self.o2,"u")
99     self.o2.f=Link(self.o1,"u")
100     self.o2.declareParameter(child=self.o1)
101    
102     def testLinkCreation(self):
103     self.o1.f=Link(self.o2,"u")
104     assert self.o1.f
105    
106    
107     def testLinkValue(self):
108     self.assertEqual(self.o1.f, -10)
109    
110     def testLinkTarget(self):
111     pass
112    
113     def testLinkDefaultAttribute(self):
114     Link(self.o2)
115    
116     def testLinkXML(self):
117     s = StringIO()
118     self.o2.writeXML(s)
119     s.reset()
120     xmlout = s.read()
121     assert '<Link' in xmlout
122    
123     def testLinkTargetXML(self):
124     pass
125    
126     class ParamaterSetTestCase(unittest.TestCase):
127    
128    
129     def setUp(self):
130     self.p = ParameterSet()
131     self.p.declareParameter(gamma1=1.,gamma2=2.,gamma3=3.)
132    
133     def testParameterSetCreation(self):
134     self.assertEqual(self.p.gamma1, 1.)
135    
136     def testParameterSetXMLCreation(self):
137     s = StringIO()
138     self.p.writeXML(s)
139     s.reset()
140     xmlout = s.read()
141     assert ("gamma1" in xmlout)
142     assert ("gamma2" in xmlout)
143     assert ("gamma3" in xmlout)
144     parsable = parse(xmlout)
145     assert (isinstance (parsable, ParameterSet))
146 elspeth 871 assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
147 jgs 147
148     def testParameterSetFromXML(self):
149 elspeth 871 doc = self._class(self.p)
150     pset = ParameterSet.fromDom(self._dom(self.p).getElementsByTagName("ParameterSet")[0])
151 jgs 147 assert (isinstance(pset, ParameterSet))
152     assert (isinstance(doc, ParameterSet))
153     self.assertEqual(self.p.gamma1,doc.gamma1)
154    
155    
156     def testParameterSetWithChildrenFromXML(self):
157     p2 = ParameterSet()
158     p2.declareParameter(s="abc", f=3.)
159     self.p.declareParameter(child=p2)
160 elspeth 871 doc = self._class(self.p)
161 jgs 147 #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])
162     self.assertEqual(self.p.child.f, doc.child.f)
163    
164     def testParameterSetChild(self):
165     p2 = ParameterSet()
166     p2.declareParameter(s="abc", f=3.)
167     self.p.declareParameter(child=p2)
168     self.assertEqual(self.p.child.s, "abc")
169     self.assertEqual(self.p.child.f, 3.)
170    
171 elspeth 871 def _dom(self, input):
172 jgs 147 s = StringIO()
173 elspeth 871 input.writeXML(s)
174 jgs 147 s.reset()
175     xmlout = s.read()
176     doc = minidom.parseString(xmlout)
177     return doc
178    
179 elspeth 871 def _class(self, input):
180 jgs 147 s = StringIO()
181 elspeth 871 input.writeXML(s)
182 jgs 147 s.reset()
183     xmlout = s.read()
184     doc = parse(xmlout)
185     return doc
186    
187 elspeth 871 def testFromDomInt(self):
188     p3 = ParameterSet()
189     p3.declareParameter(inttest=1)
190     doc = self._class(p3)
191     assert type(doc.inttest)==int
192    
193     def testFromDomNumarrayVector(self):
194     import numarray
195     p3 = ParameterSet()
196     mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)
197     p3.declareParameter(numtest=mynumarray)
198     doc = self._class(p3)
199     assert doc.numtest.type() == numarray.Float64
200     assert type(doc.numtest) == numarray.NumArray
201    
202     def testFromDomNumarrayMulti(self):
203     import numarray
204     p3 = ParameterSet()
205     mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)
206     p3.declareParameter(numtest=mynumarray)
207     doc = self._class(p3)
208     assert doc.numtest.type() == numarray.Float64
209     assert type(doc.numtest) == numarray.NumArray
210    
211    
212 jgs 147 class ModeltoDomTestCase(unittest.TestCase):
213    
214     def _class(self):
215     # returns a modelframe class, generated from the xml
216     s = StringIO()
217     self.o1.writeXML(s)
218     s.reset()
219     self.xmlout = s.read()
220     doc = parse(self.xmlout)
221     return doc
222    
223     def _dom(self):
224     # returns a minidom dom element, generated from the xml
225     s = StringIO()
226     self.o1.writeXML(s)
227     s.reset()
228     self.xmlout = s.read()
229     doc = minidom.parseString(self.xmlout)
230     return doc
231    
232     def setUp(self):
233     self.o1=ODETEST(debug=False)
234     self.o1.message='blah'
235    
236     def testModelExists(self):
237     modeldoc = self._class()
238     assert (isinstance, (modeldoc, Model))
239     assert self._dom().getElementsByTagName("Model")
240    
241     def testModelhasID(self):
242     assert int(self._dom().getElementsByTagName("Model")[0].getAttribute("id"))>99
243 jgs 150
244     class ModeltoDomTestCase(unittest.TestCase):
245     def _xml(self, modulename, modelname):
246     # returns a modelframe class, generated from the xml
247     return '''<?xml version="1.0" ?>
248     <ESys> <Simulation type="Simulation"> <Component rank="0">
249    
250     <Model id="127" module="%s" type="%s">
251    
252     <Parameter type="float"> <Name> a </Name> <Value> 0.9 </Value> </Parameter>
253     <Parameter type="Link"> <Name> f </Name> <Value> <Link> <Target> 128 </Target>
254     <Attribute> u </Attribute> </Link> </Value> </Parameter> <Parameter
255     type="float"> <Name> tend </Name> <Value>
256     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
257     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
258     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
259     <Value>
260     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
261     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model>
262     </Component> <Component rank="1"> <Model id="128" type="ODETEST"> <Parameter
263     type="float"> <Name> a </Name> <Value>
264     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
265     <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
266     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
267     1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
268     -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
269     <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
270     </Name> <Value>
271     0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
272     current error = 1.904837e+01 </Value> </Parameter> </Model> </Component>
273     <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
274     type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
275     <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
276     </Component> </Simulation> <Model id="128" type="ODETEST"> <Parameter
277     type="float"> <Name> a </Name> <Value>
278     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
279     <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
280     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
281     1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
282     -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
283     <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
284     </Name> <Value>
285     0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
286     current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="127"
287     type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
288     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
289     <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
290     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
291     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
292     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
293     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
294     <Value>
295     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
296     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
297     id="127" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
298     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
299     <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
300     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
301     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
302     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
303     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
304     <Value>
305     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
306     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> </ESys>
307     ''' % (modulename, modelname)
308    
309     def testModuleAttribute(self):
310 gross 707 modeldoc = parse(self._xml('run_xml', 'ODETEST'))
311 jgs 150
312     def testModuleAttributeFails(self):
313     try:
314     modeldoc = parse(self._xml('a', 'b'))
315     except ImportError:
316     return # correct
317    
318     assert False, "This test should have resulted in an ImportError"
319 jgs 147
320     class Messenger(Model):
321     def __init__(self, *args, **kwargs):
322     Model.__init__(self, *args, **kwargs)
323     self.declareParameter(message="none")
324    
325     def doInitialization(self):
326     self.__t=0
327     #print "I start talking now!"
328    
329     def doStepPostprocessing(self,dt):
330     self.__t+=dt
331     #print "Message (time %e) : %s "%(self.__t,self.message)
332    
333     def doFinalization(self):
334     #print "I have no more to say!"
335     pass
336    
337    
338    
339     class ODETEST(Model):
340     """ implements a solver for the ODE
341    
342     du/dt=a*u+f(t)
343    
344     we use a implicit euler scheme :
345    
346     u_n-u_{n-1}= dt*a u_n + st*f(t_n)
347    
348     to get u_n we run an iterative process
349    
350     u_{n.k}=u_{n-1}+dt*(a u_{n.i-1} + f(t_n))
351    
352    
353     input for this model are step size dt, end time tend and a value for
354     a, f and initial value for u. we need also a tolerance tol for a
355     stopping criterion.
356    
357     """
358    
359     def __init__(self, *args, **kwargs):
360     Model.__init__(self, *args, **kwargs)
361     self.declareParameter(tend=1.,dt=0.1,a=0.9,u=10.,f=0.,message="",tol=1.e-8)
362    
363     def doInitialization(self):
364     self.__tn=0
365     self.__iter=0
366    
367     def doStepPreprocessing(self,dt):
368     self.__iter=0
369     self.__u_last=self.u
370    
371     def doStep(self,dt):
372     self.__iter+=1
373     self.__u_old=self.u
374     self.u=self.__u_last+dt*(self.a*self.__u_old+self.f)
375    
376     def terminate(self):
377     if self.__iter<1:
378     return False
379     else:
380     return abs(self.__u_old-self.u)<self.tol*abs(self.u)
381    
382     def doStepPostprocessing(self,dt):
383     self.__tn+=dt
384     self.message="current error = %e"%abs(self.u-10.*math.exp((self.a-1.)*self.__tn))
385    
386     def getSafeTimeStepSize(self,dt):
387     return min(self.dt,1./(abs(self.a)+1.))
388    
389     def finalize(self):
390     return self.__tn>=self.tend
391    
392 jgs 150
393 jgs 147
394     if __name__ == "__main__":
395 jgs 150 unittest.main()
396 jgs 147

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26