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

Contents of /branches/fprops2/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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