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

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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