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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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