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

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

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