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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 973 - (show annotations) (download) (as text)
Tue Dec 19 10:48:38 2006 UTC (16 years, 1 month ago) by johnpye
File MIME type: text/x-python
File size: 8418 byte(s)
Fixed the 'repeated test' bug (a static pointer to a destroyed symchar was being used in type.cpp)
Improved error reporting for 'not a refined solver_var' message.
Refactors Library::listModules and Library::getModules to use shared code.
TODO: find a more efficient solution to the static pointer problem above.
1 import unittest
2 import ascpy
3 import math
4 import os, subprocess, sys
5 import atexit
6
7 class Ascend(unittest.TestCase):
8
9 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 class TestCompiler(Ascend):
18
19 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 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 M = T.getSimulation('sim')
36 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
37 M.run(T.getMethod('self_test'))
38
39 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 def testListIntegrators(self):
69 I = ascpy.Integrator.getEngines()
70 s1 = sorted([str(i) for i in I.values()])
71 s2 = sorted(['IDA','LSODE','AWW'])
72 assert s1==s2
73
74 # this routine is reused by both testIDA and testLSODE
75 def _testIntegrator(self,integratorname):
76 self.L.load('johnpye/shm.a4c')
77 M = self.L.findType('shm').getSimulation('sim')
78 M.setSolver(ascpy.Solver('QRSlv'))
79 print M.sim.getChildren()
80 assert float(M.sim.x) == 10.0
81 assert float(M.sim.v) == 0.0
82 t_end = math.pi
83
84 I = ascpy.Integrator(M)
85 I.setReporter(ascpy.IntegratorReporterNull(I))
86 I.setEngine(integratorname);
87 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
88 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 if(integratorname=='IDA'):
93 I.setParameter('autodiff',False)
94 I.analyse();
95 I.solve();
96 print "At end of simulation,"
97 print "x = %f" % M.sim.x
98 print "v = %f" % M.sim.v
99 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 def testInvalidIntegrator(self):
104 self.L.load('johnpye/shm.a4c')
105 M = self.L.findType('shm').getSimulation('sim')
106 M.setSolver(ascpy.Solver('QRSlv'))
107 I = ascpy.Integrator(M)
108 try:
109 I.setEngine('___NONEXISTENT____')
110 except RuntimeError:
111 return
112 self.fail("setEngine did not raise error!")
113
114 def testLSODE(self):
115 self._testIntegrator('LSODE')
116
117 def testIDA(self):
118 self._testIntegrator('IDA')
119
120 class TestLSODE(Ascend):
121
122 def testzill(self):
123 self.L.load('johnpye/zill.a4c')
124 T = self.L.findType('zill')
125 M = T.getSimulation('sim')
126 M.setSolver(ascpy.Solver('QRSlv'))
127 I = ascpy.Integrator(M)
128 I.setEngine('LSODE')
129 I.setMinSubStep(1e-7)
130 I.setMaxSubStep(0.001)
131 I.setMaxSubSteps(10000)
132 I.setReporter(ascpy.IntegratorReporterConsole(I))
133 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
134 I.analyse()
135 I.solve()
136 M.run(T.getMethod('self_test'))
137
138 def testnewton(self):
139 sys.stderr.write("STARTING TESTNEWTON\n")
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 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 I.setReporter(ascpy.IntegratorReporterConsole(I))
155 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.sim.v)/float(M.sim.g), 2);
156 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 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 class TestIDA(Ascend):
178
179 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 for p in P:
186 print p.getName(),"=",p.getValue()
187 assert len(P)==7
188 assert P[0].isStr()
189 assert P[0].getName()=="linsolver"
190 assert P[0].getValue()=='SPGMR'
191 assert P[1].getName()=="autodiff"
192 assert P[1].getValue()==True
193 assert P[5].getName()=="atolvect"
194 assert P[5].getBoolValue() == True
195 P[1].setBoolValue(False)
196 assert P[1].getBoolValue()==False
197 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 def testIDAzill(self):
210 self.L.load('johnpye/zill.a4c')
211 T = self.L.findType('zill')
212 M = T.getSimulation('sim')
213 M.setSolver(ascpy.Solver('QRSlv'))
214 I = ascpy.Integrator(M)
215 I.setEngine('IDA')
216 I.setParameter('safeeval',True)
217 I.setMinSubStep(1e-7)
218 I.setMaxSubStep(0.001)
219 I.setMaxSubSteps(10000)
220 I.setReporter(ascpy.IntegratorReporterConsole(I))
221 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
222 I.analyse()
223 I.solve()
224 M.run(T.getMethod('self_test'))
225
226 def testIDAdenx(self):
227 self.L.load('johnpye/idadenx.a4c')
228 M = self.L.findType('idadenx').getSimulation('sim')
229 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
230 I = ascpy.Integrator(M)
231 I.setEngine('IDA')
232 I.setParameter('calcic',False)
233 I.setParameter('linsolver','DENSE')
234 I.setReporter(ascpy.IntegratorReporterConsole(I))
235 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
236 I.setMaxSubStep(0);
237 I.setInitialSubStep(0);
238 I.setMaxSubSteps(0);
239 I.setParameter('autodiff',True)
240 I.analyse()
241 I.solve()
242 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
243 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
244 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
245
246 def testIDAdenxSPGMR(self):
247 self.L.load('johnpye/idadenx.a4c')
248 M = self.L.findType('idadenx').getSimulation('sim')
249 I = ascpy.Integrator(M)
250 I.setEngine('IDA')
251 I.setReporter(ascpy.IntegratorReporterConsole(I))
252 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
253 I.setMaxSubStep(0);
254 I.setInitialSubStep(0);
255 I.setMaxSubSteps(0);
256 I.setParameter('autodiff',True)
257 I.setParameter('linsolver','SPGMR')
258 I.setParameter('gsmodified',False)
259 I.analyse()
260 I.solve()
261 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
262 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
263 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
264
265 def testIDAkryx(self):
266 self.L.load('johnpye/idakryx.a4c')
267 M = self.L.findType('idakryx').getSimulation('sim')
268 M.build()
269 I = ascpy.Integrator(M)
270 I.setEngine('IDA')
271 I.setReporter(ascpy.IntegratorReporterConsole(I))
272 I.setParameter('linsolver','SPGMR')
273 I.setParameter('maxl',8)
274 I.setParameter('gsmodified',False)
275 I.setParameter('autodiff',True)
276 I.setParameter('rtol',0)
277 I.setParameter('atol',1e-3);
278 I.setParameter('atolvect',False)
279 I.analyse()
280 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
281 print M.sim.udot[1][3];
282 I.solve()
283 assert 0
284
285 class CUnit(unittest.TestCase):
286 def setUp(self):
287 self.cunitexe = "../base/generic/test/test"
288
289 def testcunittests(self):
290 res = os.system(self.cunitexe)
291 if res:
292 raise RuntimeError("CUnit tests failed (returned %d -- run %s for details)" % (res,self.cunitexe))
293 else:
294 print "CUnit returned %s" % res
295
296 # move code above down here if you want to temporarily avoid testing it
297 class NotToBeTested:
298 def nothing(self):
299 pass
300
301 if __name__=='__main__':
302 atexit.register(ascpy.shutdown)
303 unittest.main()

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