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

Contents of /branches/fprops2/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1098 - (show annotations) (download) (as text)
Wed Jan 10 06:00:06 2007 UTC (12 years, 10 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 21700 byte(s)
Fixed test.py so that env vars not required
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')
434 # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
435 # M.run(T.getMethod('fixed_states'))
436 # I = ascpy.Integrator(M)
437 # I.setEngine('LSODE')
438 # I.setReporter(ascpy.IntegratorReporterConsole(I))
439 # I.setReporter(ascpy.IntegratorReporterConsole(I))
440 # I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 100)
441 # I.setMinSubStep(0.01)
442 # I.setMaxSubStep(0.02)
443 # I.setInitialSubStep(0.1)
444 # I.analyse()
445 # I.solve()
446 # #M.checkStructuralSingularity() causes crash!
447
448 #-------------------------------------------------------------------------------
449 # Testing of freesteam external steam properties functions
450
451 with_freesteam = True
452 try:
453 # we assume that if the freesteam python module is installed, the ASCEND
454 # external library will also be.
455 import freesteam
456 have_freesteam = True
457 except ImportError,e:
458 have_freesteam = False
459
460 if with_freesteam and have_freesteam:
461 class TestFreesteam(AscendSelfTester):
462 def testfreesteamtest(self):
463 """run the self-test cases bundled with freesteam"""
464 self._run('testfreesteam',filename='testfreesteam.a4c')
465
466 def testload(self):
467 """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""
468 self.L.load('johnpye/thermalequilibrium2.a4c')
469
470 def testinstantiate(self):
471 """load an instantiate 'thermalequilibrium2'"""
472 self.testload()
473 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
474 return M
475
476 def testintegrate(self):
477 """integrate transfer of heat from one mass of water/steam to another
478 according to Newton's law of cooling"""
479 M = self.testinstantiate()
480 M.setSolver(ascpy.Solver("QRSlv"))
481 I = ascpy.Integrator(M)
482 I.setEngine('LSODE')
483 I.setReporter(ascpy.IntegratorReporterConsole(I))
484 I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
485 I.setMinSubStep(0.01)
486 I.setInitialSubStep(1)
487 I.analyse()
488 print "Number of vars = %d" % I.getNumVars()
489 assert I.getNumVars()==2
490 I.solve()
491 assert I.getNumObservedVars() == 3;
492 print "S[1].T = %f K" % M.S[1].T
493 print "S[2].T = %f K" % M.S[2].T
494 print "Q = %f W" % M.Q
495 self.assertAlmostEqual(float(M.S[1].T),506.77225109,5);
496 self.assertAlmostEqual(float(M.S[2].T),511.605173967,5);
497 self.assertAlmostEqual(float(M.Q),-48.32922877329,3);
498 self.assertAlmostEqual(float(M.t),3000);
499 print "Note that the above values have not been verified analytically"
500
501 #-------------------------------------------------------------------------------
502 # Testing of IDA models using DENSE linear solver
503
504 class TestIDADENSE(Ascend):
505 """IDA DAE integrator, DENSE linear solver"""
506
507 def testnewton(self):
508 sys.stderr.write("STARTING TESTNEWTON\n")
509 self.L.load('johnpye/newton.a4c')
510 T = self.L.findType('newton')
511 M = T.getSimulation('sim')
512 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
513 I = ascpy.Integrator(M)
514 I.setEngine('IDA')
515 I.setParameter('linsolver','DENSE')
516 I.setParameter('safeeval',True)
517 I.setParameter('rtol',1e-8)
518 I.setMaxSubStep(0.001)
519 I.setMaxSubSteps(10000)
520
521 I.setReporter(ascpy.IntegratorReporterConsole(I))
522 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
523 I.analyse()
524 I.solve()
525 print "At end of simulation,"
526 print "x = %f" % M.x
527 print "v = %f" % M.v
528 M.run(T.getMethod('self_test'))
529
530 def testlotka(self):
531 self.L.load('johnpye/lotka.a4c')
532 M = self.L.findType('lotka').getSimulation('sim')
533 M.setSolver(ascpy.Solver("QRSlv"))
534 I = ascpy.Integrator(M)
535 I.setEngine('IDA')
536 I.setReporter(ascpy.IntegratorReporterConsole(I))
537 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
538 I.setParameter('linsolver','DENSE')
539 I.setParameter('rtol',1e-8);
540 I.analyse()
541 assert I.getNumVars()==2
542 assert abs(M.R - 1000) < 1e-300
543 I.solve()
544 assert I.getNumObservedVars() == 3
545 assert abs(M.R - 832) < 1.0
546 assert abs(M.F - 21.36) < 0.1
547
548 def testdenx(self):
549 print "-----------------------------====="
550 self.L.load('johnpye/idadenx.a4c')
551 M = self.L.findType('idadenx').getSimulation('sim')
552 M.setSolver(ascpy.Solver("QRSlv"))
553 I = ascpy.Integrator(M)
554 I.setEngine('IDA')
555 I.setParameter('calcic','YA_YPD')
556 I.setParameter('linsolver','DENSE')
557 I.setReporter(ascpy.IntegratorReporterConsole(I))
558 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
559 I.setMaxSubStep(0);
560 I.setInitialSubStep(0)
561 I.setMaxSubSteps(0);
562 I.setParameter('autodiff',True)
563 I.analyse()
564 I.solve()
565 assert abs(float(M.y1) - 5.1091e-08) < 2e-9
566 assert abs(float(M.y2) - 2.0437e-13) < 2e-14
567 assert abs(float(M.y3) - 1.0) < 1e-5
568
569 ## @TODO fails during IDACalcIC (model too big?)
570 # def testkryx(self):
571 # self.L.load('johnpye/idakryx.a4c')
572 # ascpy.getCompiler().setUseRelationSharing(False)
573 # M = self.L.findType('idakryx').getSimulation('sim')
574 # M.setSolver(ascpy.Solver('QRSlv'))
575 # M.build()
576 # I = ascpy.Integrator(M)
577 # I.setEngine('IDA')
578 # I.setReporter(ascpy.IntegratorReporterConsole(I))
579 # I.setParameter('linsolver','DENSE')
580 # I.setParameter('maxl',8)
581 # I.setParameter('gsmodified',False)
582 # I.setParameter('autodiff',True)
583 # I.setParameter('rtol',0)
584 # I.setParameter('atol',1e-3);
585 # I.setParameter('atolvect',False)
586 # I.setParameter('calcic','YA_YDP')
587 # I.analyse()
588 # I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11)
589 # I.solve()
590 # assert abs(M.u[2][2].getValue()) < 1e-5
591
592 #-------------------------------------------------------------------------------
593 # Testing of IDA models using SPGMR linear solver (Krylov)
594
595 # these tests are disabled until SPGMR preconditioning has been implemented
596 class TestIDASPGMR:#(Ascend):
597 def testlotka(self):
598 self.L.load('johnpye/lotka.a4c')
599 M = self.L.findType('lotka').getSimulation('sim')
600 M.setSolver(ascpy.Solver("QRSlv"))
601 I = ascpy.Integrator(M)
602 I.setEngine('IDA')
603 I.setReporter(ascpy.IntegratorReporterConsole(I))
604 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
605 I.setParameter('rtol',1e-8)
606 I.analyse()
607 assert I.getNumVars()==2
608 assert abs(M.R - 1000) < 1e-300
609 I.solve()
610 assert I.getNumObservedVars() == 3
611 assert abs(M.R - 832) < 1.0
612 assert abs(M.F - 21.36) < 0.1
613
614
615 def testkryx(self):
616 self.L.load('johnpye/idakryx.a4c')
617 M = self.L.findType('idakryx').getSimulation('sim')
618 M.build()
619 I = ascpy.Integrator(M)
620 I.setEngine('IDA')
621 I.setReporter(ascpy.IntegratorReporterConsole(I))
622 I.setParameter('linsolver','SPGMR')
623 I.setParameter('prec','JACOBI')
624 I.setParameter('maxl',8)
625 I.setParameter('gsmodified',False)
626 I.setParameter('autodiff',True)
627 I.setParameter('gsmodified',True)
628 I.setParameter('rtol',0)
629 I.setParameter('atol',1e-3);
630 I.setParameter('atolvect',False)
631 I.setParameter('calcic','Y')
632 I.analyse()
633 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
634 print M.udot[1][3]
635 I.solve()
636 assert 0
637
638 def testzill(self):
639 self.L.load('johnpye/zill.a4c')
640 T = self.L.findType('zill')
641 M = T.getSimulation('sim')
642 M.setSolver(ascpy.Solver('QRSlv'))
643 I = ascpy.Integrator(M)
644 I.setEngine('IDA')
645 I.setParameter('safeeval',False)
646 I.setMinSubStep(1e-7)
647 I.setMaxSubStep(0.001)
648 I.setMaxSubSteps(10000)
649 I.setReporter(ascpy.IntegratorReporterConsole(I))
650 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
651 I.analyse()
652 I.solve()
653 M.run(T.getMethod('self_test'))
654
655 def testdenxSPGMR(self):
656 self.L.load('johnpye/idadenx.a4c')
657 M = self.L.findType('idadenx').getSimulation('sim')
658 M.setSolver(ascpy.Solver('QRSlv'))
659 I = ascpy.Integrator(M)
660 I.setEngine('IDA')
661 I.setReporter(ascpy.IntegratorReporterConsole(I))
662 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
663 I.setMaxSubStep(0);
664 I.setInitialSubStep(0);
665 I.setMaxSubSteps(0);
666 I.setParameter('autodiff',True)
667 I.setParameter('linsolver','SPGMR')
668 I.setParameter('gsmodified',False)
669 I.setParameter('maxncf',10)
670 I.analyse()
671 I.solve()
672 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
673 assert abs(float(M.y2) - 2.0437e-13) < 1e-15
674 assert abs(float(M.y3) - 1.0) < 1e-5
675
676 # move code above down here if you want to temporarily avoid testing it
677 class NotToBeTested:
678 def nothing(self):
679 pass
680
681 if __name__=='__main__':
682 restart = 0
683 modelsdir = None
684
685 if not os.environ.get('ASCENDLIBRARY'):
686 modelsdir = os.path.normpath(os.path.join(sys.path[0],"../models"))
687 os.environ['ASCENDLIBRARY'] = modelsdir
688 restart = 1
689
690 if platform.system()=="Windows":
691 LD_LIBRARY_PATTH="PATH"
692 SEP = ";"
693 else:
694 LD_LIBRARY_PATH="LD_LIBRARY_PATH"
695 SEP = ":"
696
697 libdirs = [".",".."]
698 libdirs = [os.path.normpath(os.path.join(sys.path[0],l)) for l in libdirs]
699 if not os.environ.get(LD_LIBRARY_PATH):
700 os.environ[LD_LIBRARY_PATH]=libdirs
701 else:
702 envlibdirs = [os.path.normpath(i) for i in os.environ[LD_LIBRARY_PATH].split(SEP)]
703 for l in libdirs:
704 if l not in envlibdirs:
705 envlibdirs.insert(0,l)
706 restart = 1
707 os.environ[LD_LIBRARY_PATH] = SEP.join(envlibdirs)
708
709 if restart:
710 script = os.path.join(sys.path[0],"test.py")
711 os.execvp("python",[script] + sys.argv)
712
713 import ascpy
714
715 try:
716 import cunit
717 except:
718 pass
719
720 atexit.register(ascpy.shutdown)
721 #suite = unittest.TestSuite()
722 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
723 #unittest.TextTestRunner(verbosity=2).run(suite)
724 unittest.main()

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