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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1018 - (show annotations) (download) (as text)
Wed Jan 3 06:44:39 2007 UTC (13 years, 5 months ago) by johnpye
File MIME type: text/x-python
File size: 12141 byte(s)
Fixing ImportError typo
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 #-------------------------------------------------------------------------------
212 # Testing of freesteam external steam properties functions
213
214 try:
215 import freesteam
216 have_freesteam = True
217 except ImportError,e:
218 have_freesteam = False
219
220 if have_freesteam:
221 class TestFreesteam(Ascend):
222 def testimport(self):
223 self.L.load('johnpye/thermalequilibrium2.a4c')
224
225 def testevaluate(self):
226 self.L.load('johnpye/thermalequilibrium2.a4c')
227 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
228 M.setSolver(ascpy.Solver("QRSlv"))
229 #I = ascpy.Integrator(M)
230 #I.setEngine('LSODE')
231 #I.setReporter(ascpy.IntegratorReporterConsole(I))
232 #I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
233 #I.setMinSubStep(0.01)
234 #I.setInitialSubStep(0.1)
235 #I.analyse()
236 #print "Number of vars = %d" % I.getNumVars()
237 #assert I.getNumVars()==2
238 #I.solve()
239 #assert I.getNumObservedVars() == 3;
240 #assert abs(M.R - 832) < 1.0
241 #assert abs(M.F - 21.36) < 0.1
242
243
244 #-------------------------------------------------------------------------------
245 # Testing of IDA models using DENSE linear solver
246
247 class TestIDADENSE(Ascend):
248 """IDA DAE integrator, DENSE linear solver"""
249
250 def testnewton(self):
251 sys.stderr.write("STARTING TESTNEWTON\n")
252 self.L.load('johnpye/newton.a4c')
253 T = self.L.findType('newton')
254 M = T.getSimulation('sim')
255 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
256 I = ascpy.Integrator(M)
257 I.setEngine('IDA')
258 I.setParameter('linsolver','DENSE')
259 I.setParameter('safeeval',True)
260 I.setParameter('rtol',1e-8)
261 I.setMaxSubStep(0.001)
262 I.setMaxSubSteps(10000)
263
264 I.setReporter(ascpy.IntegratorReporterConsole(I))
265 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
266 I.analyse()
267 I.solve()
268 print "At end of simulation,"
269 print "x = %f" % M.x
270 print "v = %f" % M.v
271 M.run(T.getMethod('self_test'))
272
273 def testlotkaDENSE(self):
274 self.L.load('johnpye/lotka.a4c')
275 M = self.L.findType('lotka').getSimulation('sim')
276 M.setSolver(ascpy.Solver("QRSlv"))
277 I = ascpy.Integrator(M)
278 I.setEngine('IDA')
279 I.setReporter(ascpy.IntegratorReporterConsole(I))
280 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
281 I.setParameter('linsolver','DENSE')
282 I.setParameter('rtol',1e-8);
283 I.analyse()
284 assert I.getNumVars()==2
285 assert abs(M.R - 1000) < 1e-300
286 I.solve()
287 assert I.getNumObservedVars() == 3
288 assert abs(M.R - 832) < 1.0
289 assert abs(M.F - 21.36) < 0.1
290
291 def testdenx(self):
292 self.L.load('johnpye/idadenx.a4c')
293 M = self.L.findType('idadenx').getSimulation('sim')
294 M.setSolver(ascpy.Solver("QRSlv"))
295 I = ascpy.Integrator(M)
296 I.setEngine('IDA')
297 I.setParameter('calcic','NONE')
298 I.setParameter('linsolver','DENSE')
299 I.setReporter(ascpy.IntegratorReporterConsole(I))
300 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
301 I.setMaxSubStep(0);
302 I.setInitialSubStep(0)
303 I.setMaxSubSteps(0);
304 I.setParameter('autodiff',True)
305 I.analyse()
306 I.solve()
307 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
308 assert abs(float(M.y2) - 2.0437e-13) < 1e-15
309 assert abs(float(M.y3) - 1.0) < 1e-5
310
311 def testkryxDENSE(self):
312 self.L.load('johnpye/idakryx.a4c')
313 M = self.L.findType('idakryx').getSimulation('sim')
314 M.setSolver(ascpy.Solver('QRSlv'))
315 M.build()
316 I = ascpy.Integrator(M)
317 I.setEngine('IDA')
318 I.setReporter(ascpy.IntegratorReporterConsole(I))
319 I.setParameter('linsolver','DENSE')
320 I.setParameter('maxl',8)
321 I.setParameter('gsmodified',False)
322 I.setParameter('autodiff',True)
323 I.setParameter('rtol',0)
324 I.setParameter('atol',1e-3);
325 I.setParameter('atolvect',False)
326 I.setParameter('calcic','YA_YDP')
327 I.analyse()
328 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11)
329 I.solve()
330 assert abs(M.u[2][2].getValue()) < 1e-5
331
332 #-------------------------------------------------------------------------------
333 # Testing of IDA models using SPGMR linear solver (Krylov)
334
335 # these tests are disabled until SPGMR preconditioning has been implemented
336 class TestIDASPGMR:#(Ascend):
337 def testlotka(self):
338 self.L.load('johnpye/lotka.a4c')
339 M = self.L.findType('lotka').getSimulation('sim')
340 M.setSolver(ascpy.Solver("QRSlv"))
341 I = ascpy.Integrator(M)
342 I.setEngine('IDA')
343 I.setReporter(ascpy.IntegratorReporterConsole(I))
344 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
345 I.setParameter('rtol',1e-8)
346 I.analyse()
347 assert I.getNumVars()==2
348 assert abs(M.R - 1000) < 1e-300
349 I.solve()
350 assert I.getNumObservedVars() == 3
351 assert abs(M.R - 832) < 1.0
352 assert abs(M.F - 21.36) < 0.1
353
354
355 def testkryx(self):
356 self.L.load('johnpye/idakryx.a4c')
357 M = self.L.findType('idakryx').getSimulation('sim')
358 M.build()
359 I = ascpy.Integrator(M)
360 I.setEngine('IDA')
361 I.setReporter(ascpy.IntegratorReporterConsole(I))
362 I.setParameter('linsolver','SPGMR')
363 I.setParameter('prec','JACOBI')
364 I.setParameter('maxl',8)
365 I.setParameter('gsmodified',False)
366 I.setParameter('autodiff',True)
367 I.setParameter('gsmodified',True)
368 I.setParameter('rtol',0)
369 I.setParameter('atol',1e-3);
370 I.setParameter('atolvect',False)
371 I.setParameter('calcic','Y')
372 I.analyse()
373 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
374 print M.udot[1][3]
375 I.solve()
376 assert 0
377
378 def testzill(self):
379 self.L.load('johnpye/zill.a4c')
380 T = self.L.findType('zill')
381 M = T.getSimulation('sim')
382 M.setSolver(ascpy.Solver('QRSlv'))
383 I = ascpy.Integrator(M)
384 I.setEngine('IDA')
385 I.setParameter('safeeval',False)
386 I.setMinSubStep(1e-7)
387 I.setMaxSubStep(0.001)
388 I.setMaxSubSteps(10000)
389 I.setReporter(ascpy.IntegratorReporterConsole(I))
390 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
391 I.analyse()
392 I.solve()
393 M.run(T.getMethod('self_test'))
394
395 def testdenxSPGMR(self):
396 self.L.load('johnpye/idadenx.a4c')
397 M = self.L.findType('idadenx').getSimulation('sim')
398 M.setSolver(ascpy.Solver('QRSlv'))
399 I = ascpy.Integrator(M)
400 I.setEngine('IDA')
401 I.setReporter(ascpy.IntegratorReporterConsole(I))
402 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
403 I.setMaxSubStep(0);
404 I.setInitialSubStep(0);
405 I.setMaxSubSteps(0);
406 I.setParameter('autodiff',True)
407 I.setParameter('linsolver','SPGMR')
408 I.setParameter('gsmodified',False)
409 I.setParameter('maxncf',10)
410 I.analyse()
411 I.solve()
412 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
413 assert abs(float(M.y2) - 2.0437e-13) < 1e-15
414 assert abs(float(M.y3) - 1.0) < 1e-5
415
416 # move code above down here if you want to temporarily avoid testing it
417 class NotToBeTested:
418 def nothing(self):
419 pass
420
421 if __name__=='__main__':
422 atexit.register(ascpy.shutdown)
423 #suite = unittest.TestSuite()
424 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
425 #unittest.TextTestRunner(verbosity=2).run(suite)
426 unittest.main()

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