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

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 972 - (hide annotations) (download) (as text)
Tue Dec 19 07:55:02 2006 UTC (12 years, 11 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 8393 byte(s)
Fixed a bug with initial time step in IDA (and added a checking asc_assert)
Added switch to turn on and off 'IDACalcIC'.
Changed 'already loaded' ASC_PROG_ERR into ASC_PROG_WARN (so that repeated 'IMPORT' doesn't cause problems)
Removed unnecessary solver tricks from shm.a4c (for easier debugging of Integrator API)
Widened self_test limits in zill.a4c.
Some more debugging in simulation.cpp.
Breaking down test.py into smaller test classes (thinking about moving it to its own directory)
1 johnpye 669 import unittest
2 johnpye 938 import ascpy
3 johnpye 940 import math
4 johnpye 952 import os, subprocess
5 johnpye 966 import atexit
6 johnpye 669
7 johnpye 956 class Ascend(unittest.TestCase):
8 johnpye 669
9 johnpye 933 def setUp(self):
10     import ascpy
11     self.L = ascpy.Library()
12    
13     def tearDown(self):
14     self.L.clear()
15     del self.L
16    
17 johnpye 966 class TestCompiler(Ascend):
18    
19 johnpye 941 def testloading(self):
20     pass
21    
22     def testsystema4l(self):
23     self.L.load('system.a4l')
24    
25     def testatomsa4l(self):
26     self.L.load('atoms.a4l')
27    
28 johnpye 966 class TestSolver(Ascend):
29    
30     def _run(self,modelname,solvername="QRSlv",filename=None):
31     if filename==None:
32     filename = 'johnpye/%s.a4c' % modelname
33     self.L.load(filename)
34     T = self.L.findType(modelname)
35 johnpye 932 M = T.getSimulation('sim')
36 johnpye 966 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
37     M.run(T.getMethod('self_test'))
38 johnpye 932
39 johnpye 966 def testlog10(self):
40     self._run('testlog10')
41    
42     def testconopt(self):
43     self._run('testconopt',"CONOPT")
44    
45     def testcmslv2(self):
46     self._run('testcmslv2',"CONOPT")
47    
48     def testsunpos1(self):
49     self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
50    
51     def testsunpos2(self):
52     self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
53    
54     def testsunpos3(self):
55     self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
56    
57     def testsunpos4(self):
58     self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
59    
60     def testsunpos5(self):
61     self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
62    
63     def testsunpos6(self):
64     self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
65    
66     class TestIntegrator(Ascend):
67    
68 johnpye 941 def testListIntegrators(self):
69     I = ascpy.Integrator.getEngines()
70     s1 = sorted([str(i) for i in I.values()])
71 johnpye 972 s2 = sorted(['IDA','LSODE','AWW'])
72 johnpye 941 assert s1==s2
73    
74 johnpye 942 # this routine is reused by both testIDA and testLSODE
75 johnpye 941 def _testIntegrator(self,integratorname):
76 johnpye 940 self.L.load('johnpye/shm.a4c')
77     M = self.L.findType('shm').getSimulation('sim')
78 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
79 johnpye 940 print M.sim.getChildren()
80     assert float(M.sim.x) == 10.0
81     assert float(M.sim.v) == 0.0
82 johnpye 941 t_end = math.pi
83 johnpye 940
84     I = ascpy.Integrator(M)
85     I.setReporter(ascpy.IntegratorReporterNull(I))
86 johnpye 941 I.setEngine(integratorname);
87 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
88 johnpye 941 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
89     I.setMaxSubStep(0.02);
90     I.setInitialSubStep(0.001);
91     I.setMaxSubSteps(200);
92 johnpye 944 if(integratorname=='IDA'):
93     I.setParameter('autodiff',False)
94 johnpye 940 I.analyse();
95     I.solve();
96 johnpye 941 print "At end of simulation,"
97     print "x = %f" % M.sim.x
98     print "v = %f" % M.sim.v
99 johnpye 940 assert abs(float(M.sim.x) + 10) < 1e-2
100     assert abs(float(M.sim.v)) < 1e-2
101     assert I.getNumObservedVars() == 3
102    
103 johnpye 941 def testInvalidIntegrator(self):
104 johnpye 966 self.L.load('johnpye/shm.a4c')
105 johnpye 941 M = self.L.findType('shm').getSimulation('sim')
106 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
107 johnpye 941 I = ascpy.Integrator(M)
108     try:
109     I.setEngine('___NONEXISTENT____')
110 johnpye 972 except RuntimeError:
111 johnpye 941 return
112     self.fail("setEngine did not raise error!")
113    
114     def testLSODE(self):
115     self._testIntegrator('LSODE')
116    
117 johnpye 972 def testIDA(self):
118     self._testIntegrator('IDA')
119    
120     class TestLSODE(Ascend):
121    
122 johnpye 964 def testzill(self):
123     self.L.load('johnpye/zill.a4c')
124     T = self.L.findType('zill')
125     M = T.getSimulation('sim')
126 johnpye 972 M.build()
127     M.setSolver(ascpy.Solver('QRSlv'))
128 johnpye 964 I = ascpy.Integrator(M)
129 johnpye 966 I.setEngine('LSODE')
130     I.setMinSubStep(1e-7)
131     I.setMaxSubStep(0.001)
132     I.setMaxSubSteps(10000)
133 johnpye 964 I.setReporter(ascpy.IntegratorReporterConsole(I))
134 johnpye 972 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
135 johnpye 964 I.analyse()
136     I.solve()
137     M.run(T.getMethod('self_test'))
138    
139 johnpye 962 def testnewton(self):
140     self.L.load('johnpye/newton.a4c')
141     T = self.L.findType('newton')
142     M = T.getSimulation('sim')
143     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
144     I = ascpy.Integrator(M)
145     I.setEngine('LSODE')
146 johnpye 963 I.setParameter('rtolvect',False)
147     I.setParameter('rtol',1e-7)
148     I.setParameter('atolvect',False)
149     I.setParameter('atol',1e-7)
150     I.setMinSubStep(1e-7)
151     I.setMaxSubStep(0.001)
152     I.setMaxSubSteps(10000)
153    
154 johnpye 962 I.setReporter(ascpy.IntegratorReporterConsole(I))
155 johnpye 964 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.sim.v)/float(M.sim.g), 2);
156 johnpye 962 I.analyse()
157     I.solve()
158     print "At end of simulation,"
159     print "x = %f" % M.sim.x
160     print "v = %f" % M.sim.v
161     M.run(T.getMethod('self_test'))
162    
163 johnpye 961 def testlotka(self):
164     self.L.load('johnpye/lotka.a4c')
165     M = self.L.findType('lotka').getSimulation('sim')
166     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
167     I = ascpy.Integrator(M)
168     I.setEngine('LSODE')
169     I.setReporter(ascpy.IntegratorReporterConsole(I))
170     I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
171     I.analyse()
172     I.solve()
173     assert I.getNumObservedVars() == 3;
174     assert abs(float(M.sim.R) - 832) < 1.0
175     assert abs(float(M.sim.F) - 21.36) < 0.1
176    
177 johnpye 972 class TestIDA(Ascend):
178 johnpye 961
179 johnpye 943 def testIDAparameters(self):
180     self.L.load('johnpye/shm.a4c')
181     M = self.L.findType('shm').getSimulation('sim')
182     I = ascpy.Integrator(M)
183     I.setEngine('IDA')
184     P = I.getParameters()
185 johnpye 945 for p in P:
186     print p.getName(),"=",p.getValue()
187 johnpye 961 assert len(P)==7
188 johnpye 945 assert P[0].isStr()
189     assert P[0].getName()=="linsolver"
190 johnpye 961 assert P[0].getValue()=='SPGMR'
191 johnpye 945 assert P[1].getName()=="autodiff"
192     assert P[1].getValue()==True
193 johnpye 961 assert P[5].getName()=="atolvect"
194     assert P[5].getBoolValue() == True
195 johnpye 945 P[1].setBoolValue(False)
196     assert P[1].getBoolValue()==False
197 johnpye 943 I.setParameters(P)
198     for p in I.getParameters():
199     print p.getName(),"=",p.getValue()
200     assert I.getParameterValue('autodiff')==False
201     I.setParameter('autodiff',True)
202     try:
203     v = I.getParameterValue('nonexist')
204     except KeyError:
205     pass
206     else:
207     self.fail('Failed to trip invalid Integrator parameter')
208    
209 johnpye 972 def testIDAzill(self):
210     self.L.load('johnpye/zill.a4c')
211     T = self.L.findType('zill')
212     M = T.getSimulation('sim')
213     M.build()
214     M.setSolver(ascpy.Solver('QRSlv'))
215     I = ascpy.Integrator(M)
216     I.setEngine('IDA')
217     I.setParameter('safeeval',True)
218     I.setMinSubStep(1e-7)
219     I.setMaxSubStep(0.001)
220     I.setMaxSubSteps(10000)
221     I.setReporter(ascpy.IntegratorReporterConsole(I))
222     I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
223     I.analyse()
224     I.solve()
225     M.run(T.getMethod('self_test'))
226    
227 johnpye 951 def testIDAdenx(self):
228 johnpye 942 self.L.load('johnpye/idadenx.a4c')
229     M = self.L.findType('idadenx').getSimulation('sim')
230 johnpye 972 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
231 johnpye 942 I = ascpy.Integrator(M)
232     I.setEngine('IDA')
233 johnpye 972 I.setParameter('calcic',False)
234     I.setParameter('linsolver','DENSE')
235 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
236 johnpye 945 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
237 johnpye 950 I.setMaxSubStep(0);
238     I.setInitialSubStep(0);
239     I.setMaxSubSteps(0);
240 johnpye 944 I.setParameter('autodiff',True)
241     I.analyse()
242     I.solve()
243 johnpye 950 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
244     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
245     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
246 johnpye 942
247 johnpye 951 def testIDAdenxSPGMR(self):
248     self.L.load('johnpye/idadenx.a4c')
249     M = self.L.findType('idadenx').getSimulation('sim')
250     I = ascpy.Integrator(M)
251     I.setEngine('IDA')
252     I.setReporter(ascpy.IntegratorReporterConsole(I))
253     I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
254     I.setMaxSubStep(0);
255     I.setInitialSubStep(0);
256     I.setMaxSubSteps(0);
257     I.setParameter('autodiff',True)
258     I.setParameter('linsolver','SPGMR')
259     I.setParameter('gsmodified',False)
260     I.analyse()
261     I.solve()
262     assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
263     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
264     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
265    
266     def testIDAkryx(self):
267     self.L.load('johnpye/idakryx.a4c')
268     M = self.L.findType('idakryx').getSimulation('sim')
269 johnpye 952 M.build()
270 johnpye 951 I = ascpy.Integrator(M)
271     I.setEngine('IDA')
272     I.setReporter(ascpy.IntegratorReporterConsole(I))
273 johnpye 952 I.setParameter('linsolver','SPGMR')
274 johnpye 970 I.setParameter('maxl',8)
275 johnpye 952 I.setParameter('gsmodified',False)
276     I.setParameter('autodiff',True)
277     I.setParameter('rtol',0)
278     I.setParameter('atol',1e-3);
279     I.setParameter('atolvect',False)
280     I.analyse()
281     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
282     print M.sim.udot[1][3];
283     I.solve()
284     assert 0
285 johnpye 951
286 johnpye 967 class CUnit(unittest.TestCase):
287     def setUp(self):
288     self.cunitexe = "../base/generic/test/test"
289    
290     def testcunittests(self):
291     res = os.system(self.cunitexe)
292     if res:
293     raise RuntimeError("CUnit tests failed (returned %d -- run %s for details)" % (res,self.cunitexe))
294     else:
295     print "CUnit returned %s" % res
296    
297 johnpye 943 # move code above down here if you want to temporarily avoid testing it
298 johnpye 932 class NotToBeTested:
299     def nothing(self):
300     pass
301 johnpye 689
302 johnpye 669 if __name__=='__main__':
303 johnpye 966 atexit.register(ascpy.shutdown)
304 johnpye 669 unittest.main()

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