/[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 707 - (hide annotations)
Wed Apr 12 23:58:02 2006 UTC (13 years, 6 months ago) by gross
File MIME type: text/x-python
File size: 12211 byte(s)
latex build documentation is working now
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     assert (self._dom().getElementsByTagName("ParameterSet"))
147    
148     def testParameterSetFromXML(self):
149     doc = self._class()
150     pset = ParameterSet.fromDom(self._dom().getElementsByTagName("ParameterSet")[0])
151     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     doc = self._class()
161     #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     def _dom(self):
172     s = StringIO()
173     self.p.writeXML(s)
174     s.reset()
175     xmlout = s.read()
176     doc = minidom.parseString(xmlout)
177     return doc
178    
179     def _class(self):
180     s = StringIO()
181     self.p.writeXML(s)
182     s.reset()
183     xmlout = s.read()
184     doc = parse(xmlout)
185     return doc
186    
187     class ModeltoDomTestCase(unittest.TestCase):
188    
189     def _class(self):
190     # returns a modelframe class, generated from the xml
191     s = StringIO()
192     self.o1.writeXML(s)
193     s.reset()
194     self.xmlout = s.read()
195     doc = parse(self.xmlout)
196     return doc
197    
198     def _dom(self):
199     # returns a minidom dom element, generated from the xml
200     s = StringIO()
201     self.o1.writeXML(s)
202     s.reset()
203     self.xmlout = s.read()
204     doc = minidom.parseString(self.xmlout)
205     return doc
206    
207     def setUp(self):
208     self.o1=ODETEST(debug=False)
209     self.o1.message='blah'
210    
211     def testModelExists(self):
212     modeldoc = self._class()
213     assert (isinstance, (modeldoc, Model))
214     assert self._dom().getElementsByTagName("Model")
215    
216     def testModelhasID(self):
217     assert int(self._dom().getElementsByTagName("Model")[0].getAttribute("id"))>99
218 jgs 150
219     class ModeltoDomTestCase(unittest.TestCase):
220     def _xml(self, modulename, modelname):
221     # returns a modelframe class, generated from the xml
222     return '''<?xml version="1.0" ?>
223     <ESys> <Simulation type="Simulation"> <Component rank="0">
224    
225     <Model id="127" module="%s" type="%s">
226    
227     <Parameter type="float"> <Name> a </Name> <Value> 0.9 </Value> </Parameter>
228     <Parameter type="Link"> <Name> f </Name> <Value> <Link> <Target> 128 </Target>
229     <Attribute> u </Attribute> </Link> </Value> </Parameter> <Parameter
230     type="float"> <Name> tend </Name> <Value>
231     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
232     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
233     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
234     <Value>
235     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
236     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model>
237     </Component> <Component rank="1"> <Model id="128" type="ODETEST"> <Parameter
238     type="float"> <Name> a </Name> <Value>
239     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
240     <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
241     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
242     1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
243     -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
244     <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
245     </Name> <Value>
246     0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
247     current error = 1.904837e+01 </Value> </Parameter> </Model> </Component>
248     <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
249     type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
250     <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
251     </Component> </Simulation> <Model id="128" type="ODETEST"> <Parameter
252     type="float"> <Name> a </Name> <Value>
253     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
254     <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
255     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
256     1.0 </Value> </Parameter> <Parameter type="float"> <Name> u </Name> <Value>
257     -10.0 </Value> </Parameter> <Parameter type="float"> <Name> tol </Name>
258     <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
259     </Name> <Value>
260     0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
261     current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="127"
262     type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
263     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
264     <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
265     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
266     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
267     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
268     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
269     <Value>
270     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
271     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
272     id="127" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
273     0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
274     <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
275     </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
276     1.0 </Value> </Parameter> <Parameter type="int"> <Name> u </Name> <Value> 10
277     </Value> </Parameter> <Parameter type="float"> <Name> tol </Name> <Value>
278     1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt </Name>
279     <Value>
280     0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
281     <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> </ESys>
282     ''' % (modulename, modelname)
283    
284     def testModuleAttribute(self):
285 gross 707 modeldoc = parse(self._xml('run_xml', 'ODETEST'))
286 jgs 150
287     def testModuleAttributeFails(self):
288     try:
289     modeldoc = parse(self._xml('a', 'b'))
290     except ImportError:
291     return # correct
292    
293     assert False, "This test should have resulted in an ImportError"
294 jgs 147
295     class Messenger(Model):
296     def __init__(self, *args, **kwargs):
297     Model.__init__(self, *args, **kwargs)
298     self.declareParameter(message="none")
299    
300     def doInitialization(self):
301     self.__t=0
302     #print "I start talking now!"
303    
304     def doStepPostprocessing(self,dt):
305     self.__t+=dt
306     #print "Message (time %e) : %s "%(self.__t,self.message)
307    
308     def doFinalization(self):
309     #print "I have no more to say!"
310     pass
311    
312    
313    
314     class ODETEST(Model):
315     """ implements a solver for the ODE
316    
317     du/dt=a*u+f(t)
318    
319     we use a implicit euler scheme :
320    
321     u_n-u_{n-1}= dt*a u_n + st*f(t_n)
322    
323     to get u_n we run an iterative process
324    
325     u_{n.k}=u_{n-1}+dt*(a u_{n.i-1} + f(t_n))
326    
327    
328     input for this model are step size dt, end time tend and a value for
329     a, f and initial value for u. we need also a tolerance tol for a
330     stopping criterion.
331    
332     """
333    
334     def __init__(self, *args, **kwargs):
335     Model.__init__(self, *args, **kwargs)
336     self.declareParameter(tend=1.,dt=0.1,a=0.9,u=10.,f=0.,message="",tol=1.e-8)
337    
338     def doInitialization(self):
339     self.__tn=0
340     self.__iter=0
341    
342     def doStepPreprocessing(self,dt):
343     self.__iter=0
344     self.__u_last=self.u
345    
346     def doStep(self,dt):
347     self.__iter+=1
348     self.__u_old=self.u
349     self.u=self.__u_last+dt*(self.a*self.__u_old+self.f)
350    
351     def terminate(self):
352     if self.__iter<1:
353     return False
354     else:
355     return abs(self.__u_old-self.u)<self.tol*abs(self.u)
356    
357     def doStepPostprocessing(self,dt):
358     self.__tn+=dt
359     self.message="current error = %e"%abs(self.u-10.*math.exp((self.a-1.)*self.__tn))
360    
361     def getSafeTimeStepSize(self,dt):
362     return min(self.dt,1./(abs(self.a)+1.))
363    
364     def finalize(self):
365     return self.__tn>=self.tend
366    
367 jgs 150
368 jgs 147
369     if __name__ == "__main__":
370 jgs 150 unittest.main()
371 jgs 147

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26