/[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 149 - (hide annotations)
Thu Sep 1 03:31:39 2005 UTC (14 years, 2 months ago) by jgs
Original Path: trunk/esys2/escript/test/python/test_xml.py
File MIME type: text/x-python
File size: 7590 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-09-01

1 jgs 147 #!/usr/bin/python
2    
3     # $Id$
4    
5     import unittest
6 jgs 149 from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,parse
7 jgs 147 import math
8     from cStringIO import StringIO
9     from xml.dom import minidom
10    
11     class XMLDocumentTestCase(unittest.TestCase):
12    
13     def setUp(self):
14    
15     o1=ODETEST(debug=False)
16     o1.u=10
17     o2=ODETEST(debug=False)
18     o2.u=-10.
19     o1.f=Link(o2,"u")
20     o2.f=Link(o1,"u")
21     m=Messenger()
22     o1.dt=0.01
23     m.message=Link(o1)
24     s=Simulation([o1,o2,m],debug=False)
25     s.run()
26     output = StringIO()
27     s.writeXML(output)
28     output.reset()
29     outputList = output.readlines()
30     self.xmlList = outputList
31    
32     def testFirstLine(self):
33     firstLine = self.xmlList[0]
34     self.assertEqual('<?xml version="1.0" ?>\n', firstLine)
35    
36     def testEsysHeader(self):
37     header = self.xmlList[1]
38     self.assertEqual('<ESys>\n', header)
39    
40     def testEsysFooter(self):
41     footer = self.xmlList[-1]
42     self.assertEqual('</ESys>\n', footer)
43    
44     def testSimulationHeader(self):
45     pass
46    
47     def testSimulationFooter(self):
48     pass
49    
50     class SimulationTestCase(unittest.TestCase):
51     def setUp(self):
52     o1=ODETEST(debug=False)
53     o1.u=10
54     o2=ODETEST(debug=False)
55     o2.u=-10.
56     o1.f=Link(o2,"u")
57     o2.f=Link(o1,"u")
58     m=Messenger()
59     o1.dt=0.01
60     m.message=Link(o1)
61     self.s=Simulation([o1,o2,m],debug=False)
62     self.s.run()
63     output = StringIO()
64     self.s.writeXML(output)
65     output.reset()
66     self.xml = output.read()
67    
68     def testSimulation(self):
69     assert "<Simulation" in self.xml, "I should see a Simulation"
70    
71     def testParseAndInstanceOfSimulation(self):
72    
73     newSim = parse(self.xml)
74     assert (isinstance (newSim, Simulation))
75     newout = StringIO()
76     newSim.writeXML(newout)
77     newout.reset()
78     xml = newout.read()
79     assert '<Simulation' in xml, "Missing a Simulation! It should be in this!"
80    
81    
82    
83    
84     class LinkTestCase(unittest.TestCase):
85    
86    
87     def setUp(self):
88    
89     self.o1=ODETEST(debug=False)
90     #self.o1.u=10
91     self.o2=ODETEST(debug=False)
92     self.o2.u=-10.
93     self.o1.f=Link(self.o2,"u")
94     self.o2.f=Link(self.o1,"u")
95     self.o2.declareParameter(child=self.o1)
96    
97     def testLinkCreation(self):
98     self.o1.f=Link(self.o2,"u")
99     assert self.o1.f
100    
101    
102     def testLinkValue(self):
103     self.assertEqual(self.o1.f, -10)
104    
105     def testLinkTarget(self):
106     pass
107    
108     def testLinkDefaultAttribute(self):
109     Link(self.o2)
110    
111     def testLinkXML(self):
112     s = StringIO()
113     self.o2.writeXML(s)
114     s.reset()
115     xmlout = s.read()
116     assert '<Link' in xmlout
117    
118     def testLinkTargetXML(self):
119     pass
120    
121     class ParamaterSetTestCase(unittest.TestCase):
122    
123    
124     def setUp(self):
125     self.p = ParameterSet()
126     self.p.declareParameter(gamma1=1.,gamma2=2.,gamma3=3.)
127    
128     def testParameterSetCreation(self):
129     self.assertEqual(self.p.gamma1, 1.)
130    
131     def testParameterSetXMLCreation(self):
132     s = StringIO()
133     self.p.writeXML(s)
134     s.reset()
135     xmlout = s.read()
136     assert ("gamma1" in xmlout)
137     assert ("gamma2" in xmlout)
138     assert ("gamma3" in xmlout)
139     parsable = parse(xmlout)
140     assert (isinstance (parsable, ParameterSet))
141     assert (self._dom().getElementsByTagName("ParameterSet"))
142    
143     def testParameterSetFromXML(self):
144     doc = self._class()
145     pset = ParameterSet.fromDom(self._dom().getElementsByTagName("ParameterSet")[0])
146     assert (isinstance(pset, ParameterSet))
147     assert (isinstance(doc, ParameterSet))
148     self.assertEqual(self.p.gamma1,doc.gamma1)
149    
150    
151     def testParameterSetWithChildrenFromXML(self):
152     p2 = ParameterSet()
153     p2.declareParameter(s="abc", f=3.)
154     self.p.declareParameter(child=p2)
155     doc = self._class()
156     #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])
157     self.assertEqual(self.p.child.f, doc.child.f)
158    
159     def testParameterSetChild(self):
160     p2 = ParameterSet()
161     p2.declareParameter(s="abc", f=3.)
162     self.p.declareParameter(child=p2)
163     self.assertEqual(self.p.child.s, "abc")
164     self.assertEqual(self.p.child.f, 3.)
165    
166     def _dom(self):
167     s = StringIO()
168     self.p.writeXML(s)
169     s.reset()
170     xmlout = s.read()
171     doc = minidom.parseString(xmlout)
172     return doc
173    
174     def _class(self):
175     s = StringIO()
176     self.p.writeXML(s)
177     s.reset()
178     xmlout = s.read()
179     doc = parse(xmlout)
180     return doc
181    
182     class ModeltoDomTestCase(unittest.TestCase):
183    
184     def _class(self):
185     # returns a modelframe class, generated from the xml
186     s = StringIO()
187     self.o1.writeXML(s)
188     s.reset()
189     self.xmlout = s.read()
190     doc = parse(self.xmlout)
191     return doc
192    
193     def _dom(self):
194     # returns a minidom dom element, generated from the xml
195     s = StringIO()
196     self.o1.writeXML(s)
197     s.reset()
198     self.xmlout = s.read()
199     doc = minidom.parseString(self.xmlout)
200     return doc
201    
202     def setUp(self):
203     self.o1=ODETEST(debug=False)
204     self.o1.message='blah'
205    
206     def testModelExists(self):
207     modeldoc = self._class()
208     assert (isinstance, (modeldoc, Model))
209     assert self._dom().getElementsByTagName("Model")
210    
211     def testModelhasID(self):
212     assert int(self._dom().getElementsByTagName("Model")[0].getAttribute("id"))>99
213    
214     class Messenger(Model):
215     def __init__(self, *args, **kwargs):
216     Model.__init__(self, *args, **kwargs)
217     self.declareParameter(message="none")
218    
219     def doInitialization(self):
220     self.__t=0
221     #print "I start talking now!"
222    
223     def doStepPostprocessing(self,dt):
224     self.__t+=dt
225     #print "Message (time %e) : %s "%(self.__t,self.message)
226    
227     def doFinalization(self):
228     #print "I have no more to say!"
229     pass
230    
231    
232    
233     class ODETEST(Model):
234     """ implements a solver for the ODE
235    
236     du/dt=a*u+f(t)
237    
238     we use a implicit euler scheme :
239    
240     u_n-u_{n-1}= dt*a u_n + st*f(t_n)
241    
242     to get u_n we run an iterative process
243    
244     u_{n.k}=u_{n-1}+dt*(a u_{n.i-1} + f(t_n))
245    
246    
247     input for this model are step size dt, end time tend and a value for
248     a, f and initial value for u. we need also a tolerance tol for a
249     stopping criterion.
250    
251     """
252    
253     def __init__(self, *args, **kwargs):
254     Model.__init__(self, *args, **kwargs)
255     self.declareParameter(tend=1.,dt=0.1,a=0.9,u=10.,f=0.,message="",tol=1.e-8)
256    
257     def doInitialization(self):
258     self.__tn=0
259     self.__iter=0
260    
261     def doStepPreprocessing(self,dt):
262     self.__iter=0
263     self.__u_last=self.u
264    
265     def doStep(self,dt):
266     self.__iter+=1
267     self.__u_old=self.u
268     self.u=self.__u_last+dt*(self.a*self.__u_old+self.f)
269    
270     def terminate(self):
271     if self.__iter<1:
272     return False
273     else:
274     return abs(self.__u_old-self.u)<self.tol*abs(self.u)
275    
276     def doStepPostprocessing(self,dt):
277     self.__tn+=dt
278     self.message="current error = %e"%abs(self.u-10.*math.exp((self.a-1.)*self.__tn))
279    
280     def getSafeTimeStepSize(self,dt):
281     return min(self.dt,1./(abs(self.a)+1.))
282    
283     def finalize(self):
284     return self.__tn>=self.tend
285    
286    
287     if __name__ == "__main__":
288     unittest.main()
289    

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26