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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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