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

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1035 - (show annotations) (download) (as text)
Thu Jan 4 07:35:22 2007 UTC (12 years, 11 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 17518 byte(s)
Softened error from bintoken.c
Improved error reporting in instantiate.c
Rearranged #includes in relation_util.[ch]
Changed to asc_assert in relman.c
Lots of fixed in slv3.c to get reporting of 'calc_ok' right.
Also some better error reporting from slv3.c.
Fixed usage info for error_reporter_tree_start().
Improved error/status reporting from bboxtest.c.
Set PyGTK GUI to correctly catch ::solve errors.
Improved reporting about when solver fails (diverged, inconsistent, exceeded time/iters)
Fixed TestBlackBox.testfail1 and TestBlackBox.testfail2.
Fixed type.cpp to throw errors when instantiation fails.
1 #!/usr/bin/env python
2 import unittest
3
4 import platform, sys
5 if platform.system() != "Windows":
6 import dl
7 sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW)
8
9 import ascpy
10 import math
11 import os, subprocess
12 import atexit
13 import cunit
14
15 class Ascend(unittest.TestCase):
16
17 def setUp(self):
18 import ascpy
19 self.L = ascpy.Library()
20
21 def tearDown(self):
22 self.L.clear()
23 del self.L
24
25 class AscendSelfTester(Ascend):
26
27 def _run(self,modelname,solvername="QRSlv",filename=None):
28 if filename==None:
29 filename = 'johnpye/%s.a4c' % modelname
30 self.L.load(filename)
31 T = self.L.findType(modelname)
32 M = T.getSimulation('sim')
33 M.build()
34 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
35 M.run(T.getMethod('self_test'))
36 return M
37
38 class TestCompiler(Ascend):
39
40 def testloading(self):
41 pass
42
43 def testsystema4l(self):
44 self.L.load('system.a4l')
45
46 def testatomsa4l(self):
47 self.L.load('atoms.a4l')
48
49 class TestSolver(AscendSelfTester):
50
51 def testlog10(self):
52 self._run('testlog10')
53
54 def testconopt(self):
55 self._run('testconopt',"CONOPT")
56
57 def testcmslv2(self):
58 self._run('testcmslv2',"CMSlv")
59
60 def testsunpos1(self):
61 self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
62
63 def testsunpos2(self):
64 self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
65
66 def testsunpos3(self):
67 self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
68
69 def testsunpos4(self):
70 self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
71
72 def testsunpos5(self):
73 self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
74
75 def testsunpos6(self):
76 self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
77
78 class TestIntegrator(Ascend):
79
80 def testListIntegrators(self):
81 I = ascpy.Integrator.getEngines()
82 s1 = sorted([str(i) for i in I.values()])
83 s2 = sorted(['IDA','LSODE','AWW'])
84 assert s1==s2
85
86 # this routine is reused by both testIDA and testLSODE
87 def _testIntegrator(self,integratorname):
88 self.L.load('johnpye/shm.a4c')
89 M = self.L.findType('shm').getSimulation('sim')
90 M.setSolver(ascpy.Solver('QRSlv'))
91 print M.getChildren()
92 assert float(M.x) == 10.0
93 assert float(M.v) == 0.0
94 t_end = math.pi
95
96 I = ascpy.Integrator(M)
97 I.setReporter(ascpy.IntegratorReporterNull(I))
98 I.setEngine(integratorname);
99 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
100 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
101 I.setMaxSubStep(0.02);
102 I.setInitialSubStep(0.001);
103 I.setMaxSubSteps(200);
104 if(integratorname=='IDA'):
105 I.setParameter('autodiff',False)
106 I.analyse();
107 I.solve();
108 print "At end of simulation,"
109 print "x = %f" % M.x
110 print "v = %f" % M.v
111 assert abs(float(M.x) + 10) < 1e-2
112 assert abs(float(M.v)) < 1e-2
113 assert I.getNumObservedVars() == 3
114
115 def testInvalidIntegrator(self):
116 self.L.load('johnpye/shm.a4c')
117 M = self.L.findType('shm').getSimulation('sim')
118 M.setSolver(ascpy.Solver('QRSlv'))
119 I = ascpy.Integrator(M)
120 try:
121 I.setEngine('___NONEXISTENT____')
122 except RuntimeError:
123 return
124 self.fail("setEngine did not raise error!")
125
126 def testLSODE(self):
127 self._testIntegrator('LSODE')
128
129 def testIDA(self):
130 self._testIntegrator('IDA')
131
132 def testparameters(self):
133 self.L.load('johnpye/shm.a4c')
134 M = self.L.findType('shm').getSimulation('sim')
135 M.build()
136 I = ascpy.Integrator(M)
137 I.setEngine('IDA')
138 P = I.getParameters()
139 for p in P:
140 print p.getName(),"=",p.getValue()
141 assert len(P)==11
142 assert P[0].isStr()
143 assert P[0].getName()=="linsolver"
144 assert P[0].getValue()=='SPGMR'
145 assert P[2].getName()=="autodiff"
146 assert P[2].getValue()==True
147 assert P[7].getName()=="atolvect"
148 assert P[7].getBoolValue() == True
149 P[2].setBoolValue(False)
150 assert P[2].getBoolValue()==False
151 I.setParameters(P)
152 assert I.getParameterValue('autodiff')==False
153 I.setParameter('autodiff',True)
154 try:
155 v = I.getParameterValue('nonexist')
156 except KeyError:
157 pass
158 else:
159 self.fail('Failed to trip invalid Integrator parameter')
160
161 class TestLSODE(Ascend):
162
163 def testzill(self):
164 self.L.load('johnpye/zill.a4c')
165 T = self.L.findType('zill')
166 M = T.getSimulation('sim')
167 M.setSolver(ascpy.Solver('QRSlv'))
168 I = ascpy.Integrator(M)
169 I.setEngine('LSODE')
170 I.setMinSubStep(1e-7)
171 I.setMaxSubStep(0.001)
172 I.setMaxSubSteps(10000)
173 I.setReporter(ascpy.IntegratorReporterConsole(I))
174 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
175 I.analyse()
176 I.solve()
177 M.run(T.getMethod('self_test'))
178
179 def testnewton(self):
180 sys.stderr.write("STARTING TESTNEWTON\n")
181 self.L.load('johnpye/newton.a4c')
182 T = self.L.findType('newton')
183 M = T.getSimulation('sim')
184 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
185 I = ascpy.Integrator(M)
186 I.setEngine('LSODE')
187 I.setParameter('rtolvect',False)
188 I.setParameter('rtol',1e-7)
189 I.setParameter('atolvect',False)
190 I.setParameter('atol',1e-7)
191 I.setMinSubStep(1e-7)
192 I.setMaxSubStep(0.001)
193 I.setMaxSubSteps(10000)
194
195 I.setReporter(ascpy.IntegratorReporterConsole(I))
196 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
197 I.analyse()
198 I.solve()
199 print "At end of simulation,"
200 print "x = %f" % M.x
201 print "v = %f" % M.v
202 M.run(T.getMethod('self_test'))
203
204 def testlotka(self):
205 self.L.load('johnpye/lotka.a4c')
206 M = self.L.findType('lotka').getSimulation('sim')
207 M.setSolver(ascpy.Solver("QRSlv"))
208 I = ascpy.Integrator(M)
209 I.setEngine('LSODE')
210 I.setReporter(ascpy.IntegratorReporterConsole(I))
211 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
212 I.analyse()
213 print "Number of vars = %d" % I.getNumVars()
214 assert I.getNumVars()==2
215 I.solve()
216 assert I.getNumObservedVars() == 3;
217 assert abs(M.R - 832) < 1.0
218 assert abs(M.F - 21.36) < 0.1
219
220 #-------------------------------------------------------------------------------
221 # Testing of a external blackbox functions
222
223 class TestBlackBox(AscendSelfTester):
224 def testparsefail0(self):
225 try:
226 self.L.load('test/blackbox/parsefail0.a4c')
227 self.fail("parsefail0 should not have loaded without errors")
228 except:
229 pass
230
231 def testparsefail1(self):
232 try:
233 self.L.load('test/blackbox/parsefail1.a4c')
234 self.fail("parsefail1 should not have loaded without errors")
235 except:
236 pass
237
238 def testparsefail2(self):
239 try:
240 self.L.load('test/blackbox/parsefail2.a4c')
241 self.fail("parsefail2 should not have loaded without errors")
242 except:
243 pass
244
245 def testparsefail3(self):
246 try:
247 self.L.load('test/blackbox/parsefail3.a4c')
248 self.fail("parsefail3 should not have loaded without errors")
249 except:
250 pass
251
252 def testparsefail4(self):
253 try:
254 self.L.load('test/blackbox/parsefail4.a4c')
255 self.fail("parsefail4 should not have loaded")
256 except:
257 pass
258
259 def testfail1(self):
260 """Mismatched arg counts check-- tests bbox, not ascend."""
261 self.L.load('test/blackbox/fail1.a4c')
262 try:
263 M = self.L.findType('fail1').getSimulation('sim')
264 self.fail("expected exception was not raised")
265 except RuntimeError,e:
266 print "Caught exception '%s', assumed ok" % e
267
268 def testfail2(self):
269 """Incorrect data arg check -- tests bbox, not ascend"""
270 self.L.load('test/blackbox/fail2.a4c')
271 try:
272 M = self.L.findType('fail2').getSimulation('sim')
273 self.fail("expected exception was not raised")
274 except RuntimeError,e:
275 print "Caught exception '%s', assumed ok (should mention errors during instantiation)" % e
276
277 def testpass1(self):
278 """simple single bbox forward solve"""
279 M = self._run('pass1',filename='test/blackbox/pass.a4c')
280
281 def testpass2(self):
282 """simple single bbox reverse solve"""
283 M = self._run('pass2',filename='test/blackbox/pass.a4c')
284
285 def testpass3(self):
286 """simple double bbox solve"""
287 M = self._run('pass3',filename='test/blackbox/pass3.a4c')
288
289 def testpass4(self):
290 """simple double bbox reverse solve"""
291 M = self._run('pass4',filename='test/blackbox/pass3.a4c')
292
293 def testpass5(self):
294 M = self._run('pass5',filename='test/blackbox/pass5.a4c')
295
296 def testpass6(self):
297 M = self._run('pass6',filename='test/blackbox/pass5.a4c')
298
299 def testpass7(self):
300 M = self._run('pass7',filename='test/blackbox/passmerge.a4c')
301
302 def testpass8(self):
303 M = self._run('pass8',filename='test/blackbox/passmerge.a4c')
304
305 def testpass9(self):
306 M = self._run('pass9',filename='test/blackbox/passmerge.a4c')
307
308 def testpass10(self):
309 M = self._run('pass10',filename='test/blackbox/passmerge.a4c')
310
311 def testpass11(self):
312 M = self._run('pass11',filename='test/blackbox/passmerge.a4c')
313
314 def testpass12(self):
315 M = self._run('pass12',filename='test/blackbox/passmerge.a4c')
316
317 def testpass13(self):
318 """cross-merged input/output solve"""
319 M = self._run('pass13',filename='test/blackbox/passmerge.a4c')
320
321 def testpass14(self):
322 """cross-merged input/output reverse solve"""
323 M = self._run('pass14',filename='test/blackbox/passmerge.a4c')
324
325 def testpass20(self):
326 M = self._run('pass20',filename='test/blackbox/passarray.a4c')
327
328 def testparsefail21(self):
329 """dense array of black boxes wrong syntax"""
330 try:
331 self.L.load('test/blackbox/parsefail21.a4c')
332 self.fail("parsefail21 should not have loaded without errors")
333 except:
334 pass
335
336 def testpass22(self):
337 M = self._run('pass22',filename='test/blackbox/passarray.a4c')
338
339 def testpass23(self):
340 M = self._run('pass23',filename='test/blackbox/passarray.a4c')
341
342 def testpass61(self):
343 M = self._run('pass61',filename='test/blackbox/reinstantiate.a4c')
344
345 def testpass62(self):
346 M = self._run('pass62',filename='test/blackbox/reinstantiate.a4c')
347
348 def testpass64(self):
349 M = self._run('pass64',filename='test/blackbox/reinstantiate.a4c')
350
351 def testpass65(self):
352 M = self._run('pass65',filename='test/blackbox/reinstantiate.a4c')
353
354 def testpass66(self):
355 M = self._run('pass66',filename='test/blackbox/reinstantiate.a4c')
356
357 def testpass67(self):
358 M = self._run('pass67',filename='test/blackbox/reinstantiate.a4c')
359
360 class TestExtFn(AscendSelfTester):
361 def testextfntest(self):
362 M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c')
363 self.assertAlmostEqual(M.y, 2);
364 self.assertAlmostEqual(M.x, 1);
365 self.assertAlmostEqual(M.y, M.x + 1);
366
367 # THIS TEST FAILS
368 # def testextrelfor(self):
369 # self.L.load('johnpye/extfn/extrelfor.a4c')
370 # T = self.L.findType('extrelfor')
371 # M = T.getSimulation('sim')
372 # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
373 # print "x[1] = %f" % M.x[1]
374 # print "x[2] = %f" % M.x[2]
375 # print "x[3] = %f" % M.x[3]
376 # M.run(T.getMethod('self_test'))
377
378 def testextrelrepeat(self):
379 M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
380
381 #-------------------------------------------------------------------------------
382 # Testing of a ExtPy - external python methods
383
384 class TestExtPy(AscendSelfTester):
385 def testextpytest(self):
386 print "-------------------=--=-=-=-"
387 M = self._run('extpytest',filename='johnpye/extpy/extpytest.a4c')
388
389 #-------------------------------------------------------------------------------
390 # Testing of freesteam external steam properties functions
391
392 with_freesteam = True
393 try:
394 import freesteam
395 have_freesteam = True
396 except ImportError,e:
397 have_freesteam = False
398
399 if with_freesteam and have_freesteam:
400 class TestFreesteam(Ascend):
401 def testload(self):
402 self.L.load('johnpye/thermalequilibrium2.a4c')
403
404 def testinstantiate(self):
405 self.testload()
406 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
407
408 def testsolve(self):
409 self.testinstantiate()
410 M.setSolver(ascpy.Solver("QRSlv"))
411 #I = ascpy.Integrator(M)
412 #I.setEngine('LSODE')
413 #I.setReporter(ascpy.IntegratorReporterConsole(I))
414 #I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
415 #I.setMinSubStep(0.01)
416 #I.setInitialSubStep(0.1)
417 #I.analyse()
418 #print "Number of vars = %d" % I.getNumVars()
419 #assert I.getNumVars()==2
420 #I.solve()
421 #assert I.getNumObservedVars() == 3;
422 #assert abs(M.R - 832) < 1.0
423 #assert abs(M.F - 21.36) < 0.1
424
425
426 #-------------------------------------------------------------------------------
427 # Testing of IDA models using DENSE linear solver
428
429 class TestIDADENSE(Ascend):
430 """IDA DAE integrator, DENSE linear solver"""
431
432 def testnewton(self):
433 sys.stderr.write("STARTING TESTNEWTON\n")
434 self.L.load('johnpye/newton.a4c')
435 T = self.L.findType('newton')
436 M = T.getSimulation('sim')
437 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
438 I = ascpy.Integrator(M)
439 I.setEngine('IDA')
440 I.setParameter('linsolver','DENSE')
441 I.setParameter('safeeval',True)
442 I.setParameter('rtol',1e-8)
443 I.setMaxSubStep(0.001)
444 I.setMaxSubSteps(10000)
445
446 I.setReporter(ascpy.IntegratorReporterConsole(I))
447 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
448 I.analyse()
449 I.solve()
450 print "At end of simulation,"
451 print "x = %f" % M.x
452 print "v = %f" % M.v
453 M.run(T.getMethod('self_test'))
454
455 def testlotkaDENSE(self):
456 self.L.load('johnpye/lotka.a4c')
457 M = self.L.findType('lotka').getSimulation('sim')
458 M.setSolver(ascpy.Solver("QRSlv"))
459 I = ascpy.Integrator(M)
460 I.setEngine('IDA')
461 I.setReporter(ascpy.IntegratorReporterConsole(I))
462 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
463 I.setParameter('linsolver','DENSE')
464 I.setParameter('rtol',1e-8);
465 I.analyse()
466 assert I.getNumVars()==2
467 assert abs(M.R - 1000) < 1e-300
468 I.solve()
469 assert I.getNumObservedVars() == 3
470 assert abs(M.R - 832) < 1.0
471 assert abs(M.F - 21.36) < 0.1
472
473 def testdenx(self):
474 print "-----------------------------====="
475 self.L.load('johnpye/idadenx.a4c')
476 M = self.L.findType('idadenx').getSimulation('sim')
477 M.setSolver(ascpy.Solver("QRSlv"))
478 I = ascpy.Integrator(M)
479 I.setEngine('IDA')
480 I.setParameter('calcic','YA_YPD')
481 I.setParameter('linsolver','DENSE')
482 I.setReporter(ascpy.IntegratorReporterConsole(I))
483 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
484 I.setMaxSubStep(0);
485 I.setInitialSubStep(0)
486 I.setMaxSubSteps(0);
487 I.setParameter('autodiff',True)
488 I.analyse()
489 I.solve()
490 assert abs(float(M.y1) - 5.1091e-08) < 2e-9
491 assert abs(float(M.y2) - 2.0437e-13) < 2e-14
492 assert abs(float(M.y3) - 1.0) < 1e-5
493
494 def testkryxDENSE(self):
495 self.L.load('johnpye/idakryx.a4c')
496 M = self.L.findType('idakryx').getSimulation('sim')
497 M.setSolver(ascpy.Solver('QRSlv'))
498 M.build()
499 I = ascpy.Integrator(M)
500 I.setEngine('IDA')
501 I.setReporter(ascpy.IntegratorReporterConsole(I))
502 I.setParameter('linsolver','DENSE')
503 I.setParameter('maxl',8)
504 I.setParameter('gsmodified',False)
505 I.setParameter('autodiff',True)
506 I.setParameter('rtol',0)
507 I.setParameter('atol',1e-3);
508 I.setParameter('atolvect',False)
509 I.setParameter('calcic','YA_YDP')
510 I.analyse()
511 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11)
512 I.solve()
513 assert abs(M.u[2][2].getValue()) < 1e-5
514
515 #-------------------------------------------------------------------------------
516 # Testing of IDA models using SPGMR linear solver (Krylov)
517
518 # these tests are disabled until SPGMR preconditioning has been implemented
519 class TestIDASPGMR:#(Ascend):
520 def testlotka(self):
521 self.L.load('johnpye/lotka.a4c')
522 M = self.L.findType('lotka').getSimulation('sim')
523 M.setSolver(ascpy.Solver("QRSlv"))
524 I = ascpy.Integrator(M)
525 I.setEngine('IDA')
526 I.setReporter(ascpy.IntegratorReporterConsole(I))
527 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
528 I.setParameter('rtol',1e-8)
529 I.analyse()
530 assert I.getNumVars()==2
531 assert abs(M.R - 1000) < 1e-300
532 I.solve()
533 assert I.getNumObservedVars() == 3
534 assert abs(M.R - 832) < 1.0
535 assert abs(M.F - 21.36) < 0.1
536
537
538 def testkryx(self):
539 self.L.load('johnpye/idakryx.a4c')
540 M = self.L.findType('idakryx').getSimulation('sim')
541 M.build()
542 I = ascpy.Integrator(M)
543 I.setEngine('IDA')
544 I.setReporter(ascpy.IntegratorReporterConsole(I))
545 I.setParameter('linsolver','SPGMR')
546 I.setParameter('prec','JACOBI')
547 I.setParameter('maxl',8)
548 I.setParameter('gsmodified',False)
549 I.setParameter('autodiff',True)
550 I.setParameter('gsmodified',True)
551 I.setParameter('rtol',0)
552 I.setParameter('atol',1e-3);
553 I.setParameter('atolvect',False)
554 I.setParameter('calcic','Y')
555 I.analyse()
556 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
557 print M.udot[1][3]
558 I.solve()
559 assert 0
560
561 def testzill(self):
562 self.L.load('johnpye/zill.a4c')
563 T = self.L.findType('zill')
564 M = T.getSimulation('sim')
565 M.setSolver(ascpy.Solver('QRSlv'))
566 I = ascpy.Integrator(M)
567 I.setEngine('IDA')
568 I.setParameter('safeeval',False)
569 I.setMinSubStep(1e-7)
570 I.setMaxSubStep(0.001)
571 I.setMaxSubSteps(10000)
572 I.setReporter(ascpy.IntegratorReporterConsole(I))
573 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
574 I.analyse()
575 I.solve()
576 M.run(T.getMethod('self_test'))
577
578 def testdenxSPGMR(self):
579 self.L.load('johnpye/idadenx.a4c')
580 M = self.L.findType('idadenx').getSimulation('sim')
581 M.setSolver(ascpy.Solver('QRSlv'))
582 I = ascpy.Integrator(M)
583 I.setEngine('IDA')
584 I.setReporter(ascpy.IntegratorReporterConsole(I))
585 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
586 I.setMaxSubStep(0);
587 I.setInitialSubStep(0);
588 I.setMaxSubSteps(0);
589 I.setParameter('autodiff',True)
590 I.setParameter('linsolver','SPGMR')
591 I.setParameter('gsmodified',False)
592 I.setParameter('maxncf',10)
593 I.analyse()
594 I.solve()
595 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
596 assert abs(float(M.y2) - 2.0437e-13) < 1e-15
597 assert abs(float(M.y3) - 1.0) < 1e-5
598
599 # move code above down here if you want to temporarily avoid testing it
600 class NotToBeTested:
601 def nothing(self):
602 pass
603
604 if __name__=='__main__':
605 atexit.register(ascpy.shutdown)
606 #suite = unittest.TestSuite()
607 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
608 #unittest.TextTestRunner(verbosity=2).run(suite)
609 unittest.main()

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