/[ascend]/branches/fprops2/test.py
ViewVC logotype

Contents of /branches/fprops2/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1016 - (show annotations) (download) (as text)
Wed Jan 3 05:56:12 2007 UTC (12 years, 10 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 10890 byte(s)
Fixed a silly bug with IDACalcIC.
Disabled all SPGMR tests from the python test suite.
All active tests in the Python tests should now be passing (24 tests currently)
1 #!/usr/bin/env python
2 import unittest
3 import ascpy
4 import math
5 import os, subprocess, sys
6 import atexit
7 import cunit
8
9 class Ascend(unittest.TestCase):
10
11 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 class TestCompiler(Ascend):
20
21 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 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 M = T.getSimulation('sim')
38 M.build()
39 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
40 M.run(T.getMethod('self_test'))
41
42 def testlog10(self):
43 self._run('testlog10')
44
45 def testconopt(self):
46 self._run('testconopt',"CONOPT")
47
48 def testcmslv2(self):
49 self._run('testcmslv2',"CMSlv")
50
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 def testListIntegrators(self):
72 I = ascpy.Integrator.getEngines()
73 s1 = sorted([str(i) for i in I.values()])
74 s2 = sorted(['IDA','LSODE','AWW'])
75 assert s1==s2
76
77 # this routine is reused by both testIDA and testLSODE
78 def _testIntegrator(self,integratorname):
79 self.L.load('johnpye/shm.a4c')
80 M = self.L.findType('shm').getSimulation('sim')
81 M.setSolver(ascpy.Solver('QRSlv'))
82 print M.getChildren()
83 assert float(M.x) == 10.0
84 assert float(M.v) == 0.0
85 t_end = math.pi
86
87 I = ascpy.Integrator(M)
88 I.setReporter(ascpy.IntegratorReporterNull(I))
89 I.setEngine(integratorname);
90 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
91 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 if(integratorname=='IDA'):
96 I.setParameter('autodiff',False)
97 I.analyse();
98 I.solve();
99 print "At end of simulation,"
100 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 assert I.getNumObservedVars() == 3
105
106 def testInvalidIntegrator(self):
107 self.L.load('johnpye/shm.a4c')
108 M = self.L.findType('shm').getSimulation('sim')
109 M.setSolver(ascpy.Solver('QRSlv'))
110 I = ascpy.Integrator(M)
111 try:
112 I.setEngine('___NONEXISTENT____')
113 except RuntimeError:
114 return
115 self.fail("setEngine did not raise error!")
116
117 def testLSODE(self):
118 self._testIntegrator('LSODE')
119
120 def testIDA(self):
121 self._testIntegrator('IDA')
122
123 def testparameters(self):
124 self.L.load('johnpye/shm.a4c')
125 M = self.L.findType('shm').getSimulation('sim')
126 M.build()
127 I = ascpy.Integrator(M)
128 I.setEngine('IDA')
129 P = I.getParameters()
130 for p in P:
131 print p.getName(),"=",p.getValue()
132 assert len(P)==11
133 assert P[0].isStr()
134 assert P[0].getName()=="linsolver"
135 assert P[0].getValue()=='SPGMR'
136 assert P[2].getName()=="autodiff"
137 assert P[2].getValue()==True
138 assert P[7].getName()=="atolvect"
139 assert P[7].getBoolValue() == True
140 P[2].setBoolValue(False)
141 assert P[2].getBoolValue()==False
142 I.setParameters(P)
143 assert I.getParameterValue('autodiff')==False
144 I.setParameter('autodiff',True)
145 try:
146 v = I.getParameterValue('nonexist')
147 except KeyError:
148 pass
149 else:
150 self.fail('Failed to trip invalid Integrator parameter')
151
152 class TestLSODE(Ascend):
153
154 def testzill(self):
155 self.L.load('johnpye/zill.a4c')
156 T = self.L.findType('zill')
157 M = T.getSimulation('sim')
158 M.setSolver(ascpy.Solver('QRSlv'))
159 I = ascpy.Integrator(M)
160 I.setEngine('LSODE')
161 I.setMinSubStep(1e-7)
162 I.setMaxSubStep(0.001)
163 I.setMaxSubSteps(10000)
164 I.setReporter(ascpy.IntegratorReporterConsole(I))
165 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
166 I.analyse()
167 I.solve()
168 M.run(T.getMethod('self_test'))
169
170 def testnewton(self):
171 sys.stderr.write("STARTING TESTNEWTON\n")
172 self.L.load('johnpye/newton.a4c')
173 T = self.L.findType('newton')
174 M = T.getSimulation('sim')
175 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
176 I = ascpy.Integrator(M)
177 I.setEngine('LSODE')
178 I.setParameter('rtolvect',False)
179 I.setParameter('rtol',1e-7)
180 I.setParameter('atolvect',False)
181 I.setParameter('atol',1e-7)
182 I.setMinSubStep(1e-7)
183 I.setMaxSubStep(0.001)
184 I.setMaxSubSteps(10000)
185
186 I.setReporter(ascpy.IntegratorReporterConsole(I))
187 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2);
188 I.analyse()
189 I.solve()
190 print "At end of simulation,"
191 print "x = %f" % M.x
192 print "v = %f" % M.v
193 M.run(T.getMethod('self_test'))
194
195 def testlotka(self):
196 self.L.load('johnpye/lotka.a4c')
197 M = self.L.findType('lotka').getSimulation('sim')
198 M.setSolver(ascpy.Solver("QRSlv"))
199 I = ascpy.Integrator(M)
200 I.setEngine('LSODE')
201 I.setReporter(ascpy.IntegratorReporterConsole(I))
202 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
203 I.analyse()
204 print "Number of vars = %d" % I.getNumVars()
205 assert I.getNumVars()==2
206 I.solve()
207 assert I.getNumObservedVars() == 3;
208 assert abs(M.R - 832) < 1.0
209 assert abs(M.F - 21.36) < 0.1
210
211 class TestIDADENSE(Ascend):
212
213 def testnewton(self):
214 sys.stderr.write("STARTING TESTNEWTON\n")
215 self.L.load('johnpye/newton.a4c')
216 T = self.L.findType('newton')
217 M = T.getSimulation('sim')
218 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
219 I = ascpy.Integrator(M)
220 I.setEngine('IDA')
221 I.setParameter('linsolver','DENSE')
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 testlotkaDENSE(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('linsolver','DENSE')
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 def testdenx(self):
255 self.L.load('johnpye/idadenx.a4c')
256 M = self.L.findType('idadenx').getSimulation('sim')
257 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
258 I = ascpy.Integrator(M)
259 I.setEngine('IDA')
260 I.setParameter('calcic','NONE')
261 I.setParameter('linsolver','DENSE')
262 I.setReporter(ascpy.IntegratorReporterConsole(I))
263 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
264 I.setMaxSubStep(0);
265 I.setInitialSubStep(0);
266 I.setMaxSubSteps(0);
267 I.setParameter('autodiff',True)
268 I.analyse()
269 I.solve()
270 assert abs(float(M.y1) - 5.1091e-08) < 1e-10;
271 assert abs(float(M.y2) - 2.0437e-13) < 1e-15;
272 assert abs(float(M.y3) - 1.0) < 1e-5;
273
274 def testkryxDENSE(self):
275 self.L.load('johnpye/idakryx.a4c')
276 M = self.L.findType('idakryx').getSimulation('sim')
277 M.setSolver(ascpy.Solver('QRSlv'))
278 M.build()
279 I = ascpy.Integrator(M)
280 I.setEngine('IDA')
281 I.setReporter(ascpy.IntegratorReporterConsole(I))
282 I.setParameter('linsolver','DENSE')
283 I.setParameter('maxl',8)
284 I.setParameter('gsmodified',False)
285 I.setParameter('autodiff',True)
286 I.setParameter('rtol',0)
287 I.setParameter('atol',1e-3);
288 I.setParameter('atolvect',False)
289 I.setParameter('calcic','YA_YDP')
290 I.analyse()
291 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11);
292 I.solve()
293 assert abs(M.u[2][2].getValue()) < 1e-5
294
295 # these tests are disabled until SPGMR preconditioning has been implemented
296 class TestIDASPGMR:#(Ascend):
297 def testlotka(self):
298 self.L.load('johnpye/lotka.a4c')
299 M = self.L.findType('lotka').getSimulation('sim')
300 M.setSolver(ascpy.Solver("QRSlv"))
301 I = ascpy.Integrator(M)
302 I.setEngine('IDA')
303 I.setReporter(ascpy.IntegratorReporterConsole(I))
304 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
305 I.setParameter('rtol',1e-8);
306 I.analyse()
307 assert I.getNumVars()==2
308 assert abs(M.R - 1000) < 1e-300
309 I.solve()
310 assert I.getNumObservedVars() == 3;
311 assert abs(M.R - 832) < 1.0
312 assert abs(M.F - 21.36) < 0.1
313
314
315 def testkryx(self):
316 self.L.load('johnpye/idakryx.a4c')
317 M = self.L.findType('idakryx').getSimulation('sim')
318 M.build()
319 I = ascpy.Integrator(M)
320 I.setEngine('IDA')
321 I.setReporter(ascpy.IntegratorReporterConsole(I))
322 I.setParameter('linsolver','SPGMR')
323 I.setParameter('prec','JACOBI')
324 I.setParameter('maxl',8)
325 I.setParameter('gsmodified',False)
326 I.setParameter('autodiff',True)
327 I.setParameter('gsmodified',True)
328 I.setParameter('rtol',0)
329 I.setParameter('atol',1e-3);
330 I.setParameter('atolvect',False)
331 I.setParameter('calcic','Y')
332 I.analyse()
333 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
334 print M.udot[1][3];
335 I.solve()
336 assert 0
337
338 def testzill(self):
339 self.L.load('johnpye/zill.a4c')
340 T = self.L.findType('zill')
341 M = T.getSimulation('sim')
342 M.setSolver(ascpy.Solver('QRSlv'))
343 I = ascpy.Integrator(M)
344 I.setEngine('IDA')
345 I.setParameter('safeeval',False)
346 I.setMinSubStep(1e-7)
347 I.setMaxSubStep(0.001)
348 I.setMaxSubSteps(10000)
349 I.setReporter(ascpy.IntegratorReporterConsole(I))
350 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
351 I.analyse()
352 I.solve()
353 M.run(T.getMethod('self_test'))
354
355 def testdenxSPGMR(self):
356 self.L.load('johnpye/idadenx.a4c')
357 M = self.L.findType('idadenx').getSimulation('sim')
358 M.setSolver(ascpy.Solver('QRSlv'))
359 I = ascpy.Integrator(M)
360 I.setEngine('IDA')
361 I.setReporter(ascpy.IntegratorReporterConsole(I))
362 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
363 I.setMaxSubStep(0);
364 I.setInitialSubStep(0);
365 I.setMaxSubSteps(0);
366 I.setParameter('autodiff',True)
367 I.setParameter('linsolver','SPGMR')
368 I.setParameter('gsmodified',False)
369 I.setParameter('maxncf',10)
370 I.analyse()
371 I.solve()
372 assert abs(float(M.y1) - 5.1091e-08) < 1e-10;
373 assert abs(float(M.y2) - 2.0437e-13) < 1e-15;
374 assert abs(float(M.y3) - 1.0) < 1e-5;
375
376 # move code above down here if you want to temporarily avoid testing it
377 class NotToBeTested:
378 def nothing(self):
379 pass
380
381 if __name__=='__main__':
382 atexit.register(ascpy.shutdown)
383 #suite = unittest.TestSuite()
384 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
385 #unittest.TextTestRunner(verbosity=2).run(suite)
386 unittest.main()

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