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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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