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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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