/[ascend]/trunk/test.py
ViewVC logotype

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 998 - (show annotations) (download) (as text)
Sat Dec 30 06:05:48 2006 UTC (12 years, 11 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 10953 byte(s)
Fixed some memory leaks in ospath.c and test_ospath.c.
1 import unittest
2 import ascpy
3 import math
4 import os, subprocess, sys
5 import atexit
6 import cunit
7
8 class Ascend(unittest.TestCase):
9
10 def setUp(self):
11 import ascpy
12 self.L = ascpy.Library()
13
14 def tearDown(self):
15 self.L.clear()
16 del self.L
17
18 class TestCompiler(Ascend):
19
20 def testloading(self):
21 pass
22
23 def testsystema4l(self):
24 self.L.load('system.a4l')
25
26 def testatomsa4l(self):
27 self.L.load('atoms.a4l')
28
29 class TestSolver(Ascend):
30
31 def _run(self,modelname,solvername="QRSlv",filename=None):
32 if filename==None:
33 filename = 'johnpye/%s.a4c' % modelname
34 self.L.load(filename)
35 T = self.L.findType(modelname)
36 M = T.getSimulation('sim')
37 M.build()
38 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
39 M.run(T.getMethod('self_test'))
40
41 def testlog10(self):
42 self._run('testlog10')
43
44 def testconopt(self):
45 self._run('testconopt',"CONOPT")
46
47 def testcmslv2(self):
48 self._run('testcmslv2',"CMSlv")
49
50 def testsunpos1(self):
51 self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
52
53 def testsunpos2(self):
54 self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
55
56 def testsunpos3(self):
57 self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
58
59 def testsunpos4(self):
60 self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
61
62 def testsunpos5(self):
63 self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
64
65 def testsunpos6(self):
66 self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
67
68 class TestIntegrator(Ascend):
69
70 def testListIntegrators(self):
71 I = ascpy.Integrator.getEngines()
72 s1 = sorted([str(i) for i in I.values()])
73 s2 = sorted(['IDA','LSODE','AWW'])
74 assert s1==s2
75
76 # this routine is reused by both testIDA and testLSODE
77 def _testIntegrator(self,integratorname):
78 self.L.load('johnpye/shm.a4c')
79 M = self.L.findType('shm').getSimulation('sim')
80 M.setSolver(ascpy.Solver('QRSlv'))
81 print M.getChildren()
82 assert float(M.x) == 10.0
83 assert float(M.v) == 0.0
84 t_end = math.pi
85
86 I = ascpy.Integrator(M)
87 I.setReporter(ascpy.IntegratorReporterNull(I))
88 I.setEngine(integratorname);
89 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
90 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
91 I.setMaxSubStep(0.02);
92 I.setInitialSubStep(0.001);
93 I.setMaxSubSteps(200);
94 if(integratorname=='IDA'):
95 I.setParameter('autodiff',False)
96 I.analyse();
97 I.solve();
98 print "At end of simulation,"
99 print "x = %f" % M.x
100 print "v = %f" % M.v
101 assert abs(float(M.x) + 10) < 1e-2
102 assert abs(float(M.v)) < 1e-2
103 assert I.getNumObservedVars() == 3
104
105 def testInvalidIntegrator(self):
106 self.L.load('johnpye/shm.a4c')
107 M = self.L.findType('shm').getSimulation('sim')
108 M.setSolver(ascpy.Solver('QRSlv'))
109 I = ascpy.Integrator(M)
110 try:
111 I.setEngine('___NONEXISTENT____')
112 except RuntimeError:
113 return
114 self.fail("setEngine did not raise error!")
115
116 def testLSODE(self):
117 self._testIntegrator('LSODE')
118
119 def testIDA(self):
120 self._testIntegrator('IDA')
121
122 class TestLSODE(Ascend):
123
124 def testzill(self):
125 self.L.load('johnpye/zill.a4c')
126 T = self.L.findType('zill')
127 M = T.getSimulation('sim')
128 M.setSolver(ascpy.Solver('QRSlv'))
129 I = ascpy.Integrator(M)
130 I.setEngine('LSODE')
131 I.setMinSubStep(1e-7)
132 I.setMaxSubStep(0.001)
133 I.setMaxSubSteps(10000)
134 I.setReporter(ascpy.IntegratorReporterConsole(I))
135 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
136 I.analyse()
137 I.solve()
138 M.run(T.getMethod('self_test'))
139
140 def testnewton(self):
141 sys.stderr.write("STARTING TESTNEWTON\n")
142 self.L.load('johnpye/newton.a4c')
143 T = self.L.findType('newton')
144 M = T.getSimulation('sim')
145 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
146 I = ascpy.Integrator(M)
147 I.setEngine('LSODE')
148 I.setParameter('rtolvect',False)
149 I.setParameter('rtol',1e-7)
150 I.setParameter('atolvect',False)
151 I.setParameter('atol',1e-7)
152 I.setMinSubStep(1e-7)
153 I.setMaxSubStep(0.001)
154 I.setMaxSubSteps(10000)
155
156 I.setReporter(ascpy.IntegratorReporterConsole(I))
157 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2);
158 I.analyse()
159 I.solve()
160 print "At end of simulation,"
161 print "x = %f" % M.x
162 print "v = %f" % M.v
163 M.run(T.getMethod('self_test'))
164
165 def testlotka(self):
166 self.L.load('johnpye/lotka.a4c')
167 M = self.L.findType('lotka').getSimulation('sim')
168 M.setSolver(ascpy.Solver("QRSlv"))
169 I = ascpy.Integrator(M)
170 I.setEngine('LSODE')
171 I.setReporter(ascpy.IntegratorReporterConsole(I))
172 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
173 I.analyse()
174 print "Number of vars = %d" % I.getNumVars()
175 assert I.getNumVars()==2
176 I.solve()
177 assert I.getNumObservedVars() == 3;
178 assert abs(M.R - 832) < 1.0
179 assert abs(M.F - 21.36) < 0.1
180
181 class TestIDA(Ascend):
182
183 def testparameters(self):
184 self.L.load('johnpye/shm.a4c')
185 M = self.L.findType('shm').getSimulation('sim')
186 M.build()
187 I = ascpy.Integrator(M)
188 I.setEngine('IDA')
189 P = I.getParameters()
190 for p in P:
191 print p.getName(),"=",p.getValue()
192 assert len(P)==9
193 assert P[0].isStr()
194 assert P[0].getName()=="linsolver"
195 assert P[0].getValue()=='SPGMR'
196 assert P[2].getName()=="autodiff"
197 assert P[2].getValue()==True
198 assert P[7].getName()=="atolvect"
199 assert P[7].getBoolValue() == True
200 P[2].setBoolValue(False)
201 assert P[2].getBoolValue()==False
202 I.setParameters(P)
203 for p in I.getParameters():
204 print p.getName(),"=",p.getValue()
205 assert I.getParameterValue('autodiff')==False
206 I.setParameter('autodiff',True)
207 try:
208 v = I.getParameterValue('nonexist')
209 except KeyError:
210 pass
211 else:
212 self.fail('Failed to trip invalid Integrator parameter')
213
214 def testnewton(self):
215 sys.stderr.write("STARTING TESTNEWTON\n")
216 self.L.load('johnpye/newton.a4c')
217 T = self.L.findType('newton')
218 M = T.getSimulation('sim')
219 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
220 I = ascpy.Integrator(M)
221 I.setEngine('IDA')
222 I.setParameter('safeeval',True)
223 I.setParameter('rtol',1e-8)
224 I.setMaxSubStep(0.001)
225 I.setMaxSubSteps(10000)
226
227 I.setReporter(ascpy.IntegratorReporterConsole(I))
228 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2);
229 I.analyse()
230 I.solve()
231 print "At end of simulation,"
232 print "x = %f" % M.x
233 print "v = %f" % M.v
234 M.run(T.getMethod('self_test'))
235
236 def testlotka(self):
237 self.L.load('johnpye/lotka.a4c')
238 M = self.L.findType('lotka').getSimulation('sim')
239 M.setSolver(ascpy.Solver("QRSlv"))
240 I = ascpy.Integrator(M)
241 I.setEngine('IDA')
242 I.setReporter(ascpy.IntegratorReporterConsole(I))
243 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
244 I.setParameter('rtol',1e-8);
245 I.analyse()
246 assert I.getNumVars()==2
247 assert abs(M.R - 1000) < 1e-300
248 I.solve()
249 assert I.getNumObservedVars() == 3;
250 assert abs(M.R - 832) < 1.0
251 assert abs(M.F - 21.36) < 0.1
252
253 def testlotkaDENSE(self):
254 self.L.load('johnpye/lotka.a4c')
255 M = self.L.findType('lotka').getSimulation('sim')
256 M.setSolver(ascpy.Solver("QRSlv"))
257 I = ascpy.Integrator(M)
258 I.setEngine('IDA')
259 I.setReporter(ascpy.IntegratorReporterConsole(I))
260 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
261 I.setParameter('linsolver','DENSE')
262 I.setParameter('rtol',1e-8);
263 I.analyse()
264 assert I.getNumVars()==2
265 assert abs(M.R - 1000) < 1e-300
266 I.solve()
267 assert I.getNumObservedVars() == 3;
268 assert abs(M.R - 832) < 1.0
269 assert abs(M.F - 21.36) < 0.1
270
271 def testzill(self):
272 self.L.load('johnpye/zill.a4c')
273 T = self.L.findType('zill')
274 M = T.getSimulation('sim')
275 M.setSolver(ascpy.Solver('QRSlv'))
276 I = ascpy.Integrator(M)
277 I.setEngine('IDA')
278 I.setParameter('safeeval',False)
279 I.setMinSubStep(1e-7)
280 I.setMaxSubStep(0.001)
281 I.setMaxSubSteps(10000)
282 I.setReporter(ascpy.IntegratorReporterConsole(I))
283 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
284 I.analyse()
285 I.solve()
286 M.run(T.getMethod('self_test'))
287
288 def testdenx(self):
289 self.L.load('johnpye/idadenx.a4c')
290 M = self.L.findType('idadenx').getSimulation('sim')
291 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
292 I = ascpy.Integrator(M)
293 I.setEngine('IDA')
294 I.setParameter('calcic',False)
295 I.setParameter('linsolver','DENSE')
296 I.setReporter(ascpy.IntegratorReporterConsole(I))
297 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
298 I.setMaxSubStep(0);
299 I.setInitialSubStep(0);
300 I.setMaxSubSteps(0);
301 I.setParameter('autodiff',True)
302 I.analyse()
303 I.solve()
304 assert abs(float(M.y1) - 5.1091e-08) < 1e-10;
305 assert abs(float(M.y2) - 2.0437e-13) < 1e-15;
306 assert abs(float(M.y3) - 1.0) < 1e-5;
307
308 def testkryxDENSE(self):
309 self.L.load('johnpye/idakryx.a4c')
310 M = self.L.findType('idakryx').getSimulation('sim')
311 M.setSolver(ascpy.Solver('QRSlv'))
312 M.build()
313 I = ascpy.Integrator(M)
314 I.setEngine('IDA')
315 I.setReporter(ascpy.IntegratorReporterConsole(I))
316 I.setParameter('linsolver','DENSE')
317 I.setParameter('maxl',8)
318 I.setParameter('gsmodified',False)
319 I.setParameter('autodiff',True)
320 I.setParameter('rtol',0)
321 I.setParameter('atol',1e-3);
322 I.setParameter('atolvect',False)
323 I.setParameter('calcic',True)
324 I.analyse()
325 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11);
326 I.solve()
327 assert abs(M.u[2][2].getValue()) < 1e-5
328
329 def testdenxSPGMR(self):
330 self.L.load('johnpye/idadenx.a4c')
331 M = self.L.findType('idadenx').getSimulation('sim')
332 M.setSolver(ascpy.Solver('QRSlv'))
333 I = ascpy.Integrator(M)
334 I.setEngine('IDA')
335 I.setReporter(ascpy.IntegratorReporterConsole(I))
336 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
337 I.setMaxSubStep(0);
338 I.setInitialSubStep(0);
339 I.setMaxSubSteps(0);
340 I.setParameter('autodiff',True)
341 I.setParameter('linsolver','SPGMR')
342 I.setParameter('gsmodified',False)
343 I.setParameter('maxncf',10)
344 I.analyse()
345 I.solve()
346 assert abs(float(M.y1) - 5.1091e-08) < 1e-10;
347 assert abs(float(M.y2) - 2.0437e-13) < 1e-15;
348 assert abs(float(M.y3) - 1.0) < 1e-5;
349
350 def testkryx(self):
351 self.L.load('johnpye/idakryx.a4c')
352 M = self.L.findType('idakryx').getSimulation('sim')
353 M.build()
354 I = ascpy.Integrator(M)
355 I.setEngine('IDA')
356 I.setReporter(ascpy.IntegratorReporterConsole(I))
357 I.setParameter('linsolver','SPGMR')
358 I.setParameter('prec','JACOBI')
359 I.setParameter('maxl',8)
360 I.setParameter('gsmodified',False)
361 I.setParameter('autodiff',True)
362 I.setParameter('gsmodified',True)
363 I.setParameter('rtol',0)
364 I.setParameter('atol',1e-3);
365 I.setParameter('atolvect',False)
366 I.setParameter('calcic','Y')
367 I.analyse()
368 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
369 print M.udot[1][3];
370 I.solve()
371 assert 0
372
373 # move code above down here if you want to temporarily avoid testing it
374 class NotToBeTested:
375 def nothing(self):
376 pass
377
378 if __name__=='__main__':
379 pass
380 atexit.register(ascpy.shutdown)
381 suite = unittest.TestSuite()
382 suite.addTest(unittest.makeSuite(TestCompiler))
383 suite.addTest(unittest.makeSuite(TestSolver))
384 #suite.addTest(TestLSODE)
385 csuites = cunit.load("base/generic/test/libasctestsuite.so")
386 #suite.addTests(csuites)
387 suite.addTests(csuites)
388 unittest.TextTestRunner(verbosity=2).run(suite)
389 #unittest.main()

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22