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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1008 - (hide annotations) (download) (as text)
Tue Jan 2 11:45:24 2007 UTC (13 years, 9 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 johnpye 1008 #!/usr/bin/env python
2 johnpye 669 import unittest
3 johnpye 938 import ascpy
4 johnpye 940 import math
5 johnpye 973 import os, subprocess, sys
6 johnpye 966 import atexit
7 johnpye 998 import cunit
8 johnpye 669
9 johnpye 956 class Ascend(unittest.TestCase):
10 johnpye 669
11 johnpye 933 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 johnpye 966 class TestCompiler(Ascend):
20    
21 johnpye 941 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 johnpye 966 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 johnpye 932 M = T.getSimulation('sim')
38 johnpye 974 M.build()
39 johnpye 966 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
40     M.run(T.getMethod('self_test'))
41 johnpye 932
42 johnpye 966 def testlog10(self):
43     self._run('testlog10')
44    
45     def testconopt(self):
46     self._run('testconopt',"CONOPT")
47    
48     def testcmslv2(self):
49 johnpye 974 self._run('testcmslv2',"CMSlv")
50 johnpye 966
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 johnpye 941 def testListIntegrators(self):
72     I = ascpy.Integrator.getEngines()
73     s1 = sorted([str(i) for i in I.values()])
74 johnpye 972 s2 = sorted(['IDA','LSODE','AWW'])
75 johnpye 941 assert s1==s2
76    
77 johnpye 942 # this routine is reused by both testIDA and testLSODE
78 johnpye 941 def _testIntegrator(self,integratorname):
79 johnpye 940 self.L.load('johnpye/shm.a4c')
80     M = self.L.findType('shm').getSimulation('sim')
81 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
82 johnpye 979 print M.getChildren()
83     assert float(M.x) == 10.0
84     assert float(M.v) == 0.0
85 johnpye 941 t_end = math.pi
86 johnpye 940
87     I = ascpy.Integrator(M)
88     I.setReporter(ascpy.IntegratorReporterNull(I))
89 johnpye 941 I.setEngine(integratorname);
90 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
91 johnpye 941 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 johnpye 944 if(integratorname=='IDA'):
96     I.setParameter('autodiff',False)
97 johnpye 940 I.analyse();
98     I.solve();
99 johnpye 941 print "At end of simulation,"
100 johnpye 979 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 johnpye 940 assert I.getNumObservedVars() == 3
105    
106 johnpye 941 def testInvalidIntegrator(self):
107 johnpye 966 self.L.load('johnpye/shm.a4c')
108 johnpye 941 M = self.L.findType('shm').getSimulation('sim')
109 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
110 johnpye 941 I = ascpy.Integrator(M)
111     try:
112     I.setEngine('___NONEXISTENT____')
113 johnpye 972 except RuntimeError:
114 johnpye 941 return
115     self.fail("setEngine did not raise error!")
116    
117     def testLSODE(self):
118     self._testIntegrator('LSODE')
119    
120 johnpye 972 def testIDA(self):
121     self._testIntegrator('IDA')
122    
123     class TestLSODE(Ascend):
124    
125 johnpye 964 def testzill(self):
126     self.L.load('johnpye/zill.a4c')
127     T = self.L.findType('zill')
128     M = T.getSimulation('sim')
129 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
130 johnpye 964 I = ascpy.Integrator(M)
131 johnpye 966 I.setEngine('LSODE')
132     I.setMinSubStep(1e-7)
133     I.setMaxSubStep(0.001)
134     I.setMaxSubSteps(10000)
135 johnpye 964 I.setReporter(ascpy.IntegratorReporterConsole(I))
136 johnpye 972 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
137 johnpye 964 I.analyse()
138     I.solve()
139     M.run(T.getMethod('self_test'))
140    
141 johnpye 962 def testnewton(self):
142 johnpye 973 sys.stderr.write("STARTING TESTNEWTON\n")
143 johnpye 962 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 johnpye 963 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 johnpye 962 I.setReporter(ascpy.IntegratorReporterConsole(I))
158 johnpye 979 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2);
159 johnpye 962 I.analyse()
160     I.solve()
161     print "At end of simulation,"
162 johnpye 979 print "x = %f" % M.x
163     print "v = %f" % M.v
164 johnpye 962 M.run(T.getMethod('self_test'))
165    
166 johnpye 961 def testlotka(self):
167     self.L.load('johnpye/lotka.a4c')
168     M = self.L.findType('lotka').getSimulation('sim')
169 johnpye 980 M.setSolver(ascpy.Solver("QRSlv"))
170 johnpye 961 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 johnpye 979 print "Number of vars = %d" % I.getNumVars()
176     assert I.getNumVars()==2
177 johnpye 961 I.solve()
178     assert I.getNumObservedVars() == 3;
179 johnpye 979 assert abs(M.R - 832) < 1.0
180     assert abs(M.F - 21.36) < 0.1
181 johnpye 961
182 johnpye 972 class TestIDA(Ascend):
183 johnpye 961
184 johnpye 975 def testparameters(self):
185 johnpye 943 self.L.load('johnpye/shm.a4c')
186     M = self.L.findType('shm').getSimulation('sim')
187 johnpye 976 M.build()
188 johnpye 943 I = ascpy.Integrator(M)
189     I.setEngine('IDA')
190     P = I.getParameters()
191 johnpye 945 for p in P:
192     print p.getName(),"=",p.getValue()
193 johnpye 976 assert len(P)==9
194 johnpye 945 assert P[0].isStr()
195     assert P[0].getName()=="linsolver"
196 johnpye 961 assert P[0].getValue()=='SPGMR'
197 johnpye 976 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 johnpye 943 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 johnpye 979 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 johnpye 991 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 johnpye 975 def testzill(self):
273 johnpye 972 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 johnpye 979 I.setParameter('safeeval',False)
280 johnpye 972 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 johnpye 975 def testdenx(self):
290 johnpye 942 self.L.load('johnpye/idadenx.a4c')
291     M = self.L.findType('idadenx').getSimulation('sim')
292 johnpye 972 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
293 johnpye 942 I = ascpy.Integrator(M)
294     I.setEngine('IDA')
295 johnpye 972 I.setParameter('calcic',False)
296     I.setParameter('linsolver','DENSE')
297 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
298 johnpye 945 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
299 johnpye 950 I.setMaxSubStep(0);
300     I.setInitialSubStep(0);
301     I.setMaxSubSteps(0);
302 johnpye 944 I.setParameter('autodiff',True)
303     I.analyse()
304     I.solve()
305 johnpye 979 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 johnpye 942
309 johnpye 990 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 johnpye 991 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11);
327 johnpye 990 I.solve()
328     assert abs(M.u[2][2].getValue()) < 1e-5
329    
330 johnpye 975 def testdenxSPGMR(self):
331 johnpye 951 self.L.load('johnpye/idadenx.a4c')
332     M = self.L.findType('idadenx').getSimulation('sim')
333 johnpye 991 M.setSolver(ascpy.Solver('QRSlv'))
334 johnpye 951 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 johnpye 991 I.setParameter('maxncf',10)
345 johnpye 951 I.analyse()
346     I.solve()
347 johnpye 979 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 johnpye 951
351 johnpye 991 def testkryx(self):
352 johnpye 951 self.L.load('johnpye/idakryx.a4c')
353     M = self.L.findType('idakryx').getSimulation('sim')
354 johnpye 952 M.build()
355 johnpye 951 I = ascpy.Integrator(M)
356     I.setEngine('IDA')
357     I.setReporter(ascpy.IntegratorReporterConsole(I))
358 johnpye 992 I.setParameter('linsolver','SPGMR')
359 johnpye 993 I.setParameter('prec','JACOBI')
360 johnpye 970 I.setParameter('maxl',8)
361 johnpye 952 I.setParameter('gsmodified',False)
362     I.setParameter('autodiff',True)
363 johnpye 993 I.setParameter('gsmodified',True)
364 johnpye 952 I.setParameter('rtol',0)
365     I.setParameter('atol',1e-3);
366     I.setParameter('atolvect',False)
367 johnpye 993 I.setParameter('calcic','Y')
368 johnpye 952 I.analyse()
369     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
370 johnpye 979 print M.udot[1][3];
371 johnpye 952 I.solve()
372     assert 0
373 johnpye 967
374 johnpye 943 # move code above down here if you want to temporarily avoid testing it
375 johnpye 932 class NotToBeTested:
376     def nothing(self):
377     pass
378 johnpye 689
379 johnpye 669 if __name__=='__main__':
380 johnpye 966 atexit.register(ascpy.shutdown)
381 johnpye 1008 #suite = unittest.TestSuite()
382 johnpye 1003 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
383 johnpye 1008 #unittest.TextTestRunner(verbosity=2).run(suite)
384     unittest.main()

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