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

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1100 - (hide annotations) (download) (as text)
Wed Jan 10 06:32:31 2007 UTC (12 years, 11 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 21758 byte(s)
Fixed TestSteam.testdsgsat (on_load exception ignored)
1 johnpye 1008 #!/usr/bin/env python
2 johnpye 669 import unittest
3 johnpye 1091 import os, subprocess,sys
4 johnpye 1098 import math
5     import atexit
6 johnpye 1091
7     import platform
8 johnpye 1028 if platform.system() != "Windows":
9     import dl
10     sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW)
11    
12 johnpye 956 class Ascend(unittest.TestCase):
13 johnpye 669
14 johnpye 933 def setUp(self):
15     import ascpy
16 johnpye 1094 self.L = ascpy.Library(modelsdir)
17 johnpye 933
18     def tearDown(self):
19     self.L.clear()
20     del self.L
21    
22 johnpye 1024 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 johnpye 966 class TestCompiler(Ascend):
36    
37 johnpye 941 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 johnpye 1024 class TestSolver(AscendSelfTester):
47 johnpye 966
48     def testlog10(self):
49     self._run('testlog10')
50    
51     def testconopt(self):
52     self._run('testconopt',"CONOPT")
53    
54     def testcmslv2(self):
55 johnpye 974 self._run('testcmslv2',"CMSlv")
56 johnpye 966
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 johnpye 1073 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 johnpye 966 class TestIntegrator(Ascend):
81    
82 johnpye 941 def testListIntegrators(self):
83     I = ascpy.Integrator.getEngines()
84     s1 = sorted([str(i) for i in I.values()])
85 johnpye 972 s2 = sorted(['IDA','LSODE','AWW'])
86 johnpye 941 assert s1==s2
87    
88 johnpye 942 # this routine is reused by both testIDA and testLSODE
89 johnpye 941 def _testIntegrator(self,integratorname):
90 johnpye 940 self.L.load('johnpye/shm.a4c')
91     M = self.L.findType('shm').getSimulation('sim')
92 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
93 johnpye 979 print M.getChildren()
94     assert float(M.x) == 10.0
95     assert float(M.v) == 0.0
96 johnpye 941 t_end = math.pi
97 johnpye 940
98     I = ascpy.Integrator(M)
99     I.setReporter(ascpy.IntegratorReporterNull(I))
100 johnpye 941 I.setEngine(integratorname);
101 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
102 johnpye 941 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 johnpye 944 if(integratorname=='IDA'):
107     I.setParameter('autodiff',False)
108 johnpye 940 I.analyse();
109     I.solve();
110 johnpye 941 print "At end of simulation,"
111 johnpye 979 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 johnpye 940 assert I.getNumObservedVars() == 3
116    
117 johnpye 941 def testInvalidIntegrator(self):
118 johnpye 966 self.L.load('johnpye/shm.a4c')
119 johnpye 941 M = self.L.findType('shm').getSimulation('sim')
120 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
121 johnpye 941 I = ascpy.Integrator(M)
122     try:
123     I.setEngine('___NONEXISTENT____')
124 johnpye 972 except RuntimeError:
125 johnpye 941 return
126     self.fail("setEngine did not raise error!")
127    
128     def testLSODE(self):
129     self._testIntegrator('LSODE')
130    
131 johnpye 972 def testIDA(self):
132     self._testIntegrator('IDA')
133    
134 johnpye 1016 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 johnpye 972 class TestLSODE(Ascend):
164    
165 johnpye 964 def testzill(self):
166     self.L.load('johnpye/zill.a4c')
167     T = self.L.findType('zill')
168     M = T.getSimulation('sim')
169 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
170 johnpye 964 I = ascpy.Integrator(M)
171 johnpye 966 I.setEngine('LSODE')
172     I.setMinSubStep(1e-7)
173     I.setMaxSubStep(0.001)
174     I.setMaxSubSteps(10000)
175 johnpye 964 I.setReporter(ascpy.IntegratorReporterConsole(I))
176 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
177 johnpye 964 I.analyse()
178     I.solve()
179     M.run(T.getMethod('self_test'))
180    
181 johnpye 962 def testnewton(self):
182 johnpye 973 sys.stderr.write("STARTING TESTNEWTON\n")
183 johnpye 962 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 johnpye 963 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 johnpye 962 I.setReporter(ascpy.IntegratorReporterConsole(I))
198 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
199 johnpye 962 I.analyse()
200     I.solve()
201     print "At end of simulation,"
202 johnpye 979 print "x = %f" % M.x
203     print "v = %f" % M.v
204 johnpye 962 M.run(T.getMethod('self_test'))
205    
206 johnpye 961 def testlotka(self):
207     self.L.load('johnpye/lotka.a4c')
208     M = self.L.findType('lotka').getSimulation('sim')
209 johnpye 980 M.setSolver(ascpy.Solver("QRSlv"))
210 johnpye 961 I = ascpy.Integrator(M)
211     I.setEngine('LSODE')
212     I.setReporter(ascpy.IntegratorReporterConsole(I))
213 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
214 johnpye 961 I.analyse()
215 johnpye 979 print "Number of vars = %d" % I.getNumVars()
216     assert I.getNumVars()==2
217 johnpye 961 I.solve()
218     assert I.getNumObservedVars() == 3;
219 johnpye 979 assert abs(M.R - 832) < 1.0
220     assert abs(M.F - 21.36) < 0.1
221 johnpye 1017
222     #-------------------------------------------------------------------------------
223 johnpye 1032 # Testing of a external blackbox functions
224 johnpye 1021
225 johnpye 1032 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 johnpye 1034 try:
265 johnpye 1035 M = self.L.findType('fail1').getSimulation('sim')
266 johnpye 1034 self.fail("expected exception was not raised")
267     except RuntimeError,e:
268     print "Caught exception '%s', assumed ok" % e
269 johnpye 1032
270     def testfail2(self):
271     """Incorrect data arg check -- tests bbox, not ascend"""
272     self.L.load('test/blackbox/fail2.a4c')
273 johnpye 1035 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 johnpye 1032
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 johnpye 1037 # 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 johnpye 1032
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 johnpye 1024 class TestExtFn(AscendSelfTester):
364 johnpye 1021 def testextfntest(self):
365 johnpye 1024 M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c')
366     self.assertAlmostEqual(M.y, 2);
367     self.assertAlmostEqual(M.x, 1);
368 johnpye 1021 self.assertAlmostEqual(M.y, M.x + 1);
369    
370 johnpye 1039 def testextrelfor(self):
371     M = self._run('extrelfor',filename='johnpye/extfn/extrelfor.a4c')
372 johnpye 1024
373 johnpye 1056 ## @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 johnpye 1039
386 johnpye 1024 def testextrelrepeat(self):
387     M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
388    
389 johnpye 1021 #-------------------------------------------------------------------------------
390 johnpye 1024 # Testing of a ExtPy - external python methods
391    
392     class TestExtPy(AscendSelfTester):
393 johnpye 1055 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 johnpye 1024
409     #-------------------------------------------------------------------------------
410 johnpye 1042 # 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 johnpye 1043 def testsatsteamstream(self):
420     M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
421 johnpye 1042
422 johnpye 1043 def testsatsteamstream(self):
423     M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
424 johnpye 1042
425 johnpye 1056 ## @TODO fix error capture from bounds checking during initialisation
426     # def testiapwssat1(self):
427     # M = self._run('testiapwssat1',filename='steam/iapwssat.a4c')
428 johnpye 1042
429 johnpye 1100 # @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 johnpye 1046
453 johnpye 1042 #-------------------------------------------------------------------------------
454 johnpye 1017 # Testing of freesteam external steam properties functions
455    
456 johnpye 1032 with_freesteam = True
457 johnpye 1017 try:
458 johnpye 1039 # we assume that if the freesteam python module is installed, the ASCEND
459     # external library will also be.
460 johnpye 1017 import freesteam
461     have_freesteam = True
462 johnpye 1018 except ImportError,e:
463 johnpye 1017 have_freesteam = False
464    
465 johnpye 1024 if with_freesteam and have_freesteam:
466 johnpye 1039 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 johnpye 1021 def testload(self):
472 johnpye 1039 """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""
473 johnpye 1017 self.L.load('johnpye/thermalequilibrium2.a4c')
474 johnpye 1018
475 johnpye 1021 def testinstantiate(self):
476 johnpye 1039 """load an instantiate 'thermalequilibrium2'"""
477 johnpye 1021 self.testload()
478 johnpye 1018 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
479 johnpye 1039 return M
480 johnpye 1021
481 johnpye 1039 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 johnpye 1018 M.setSolver(ascpy.Solver("QRSlv"))
486 johnpye 1039 I = ascpy.Integrator(M)
487     I.setEngine('LSODE')
488     I.setReporter(ascpy.IntegratorReporterConsole(I))
489     I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
490 johnpye 1055 I.setMinSubStep(0.01)
491     I.setInitialSubStep(1)
492 johnpye 1039 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 johnpye 1056 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 johnpye 1039 self.assertAlmostEqual(float(M.t),3000);
504     print "Note that the above values have not been verified analytically"
505 johnpye 1017
506     #-------------------------------------------------------------------------------
507     # Testing of IDA models using DENSE linear solver
508    
509 johnpye 1016 class TestIDADENSE(Ascend):
510 johnpye 1017 """IDA DAE integrator, DENSE linear solver"""
511 johnpye 961
512 johnpye 979 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 johnpye 1016 I.setParameter('linsolver','DENSE')
521 johnpye 979 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 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
528 johnpye 979 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 johnpye 1042 def testlotka(self):
536 johnpye 991 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 johnpye 1017 assert I.getNumObservedVars() == 3
550 johnpye 991 assert abs(M.R - 832) < 1.0
551     assert abs(M.F - 21.36) < 0.1
552 johnpye 972
553 johnpye 975 def testdenx(self):
554 johnpye 1026 print "-----------------------------====="
555 johnpye 942 self.L.load('johnpye/idadenx.a4c')
556     M = self.L.findType('idadenx').getSimulation('sim')
557 johnpye 1017 M.setSolver(ascpy.Solver("QRSlv"))
558 johnpye 942 I = ascpy.Integrator(M)
559     I.setEngine('IDA')
560 johnpye 1024 I.setParameter('calcic','YA_YPD')
561 johnpye 972 I.setParameter('linsolver','DENSE')
562 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
563 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
564 johnpye 950 I.setMaxSubStep(0);
565 johnpye 1017 I.setInitialSubStep(0)
566 johnpye 950 I.setMaxSubSteps(0);
567 johnpye 944 I.setParameter('autodiff',True)
568     I.analyse()
569     I.solve()
570 johnpye 1022 assert abs(float(M.y1) - 5.1091e-08) < 2e-9
571     assert abs(float(M.y2) - 2.0437e-13) < 2e-14
572 johnpye 1017 assert abs(float(M.y3) - 1.0) < 1e-5
573 johnpye 942
574 johnpye 1058 ## @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 johnpye 1017
597     #-------------------------------------------------------------------------------
598     # Testing of IDA models using SPGMR linear solver (Krylov)
599    
600 johnpye 1016 # 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 johnpye 951 I = ascpy.Integrator(M)
607     I.setEngine('IDA')
608     I.setReporter(ascpy.IntegratorReporterConsole(I))
609 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
610     I.setParameter('rtol',1e-8)
611 johnpye 951 I.analyse()
612 johnpye 1016 assert I.getNumVars()==2
613     assert abs(M.R - 1000) < 1e-300
614 johnpye 951 I.solve()
615 johnpye 1017 assert I.getNumObservedVars() == 3
616 johnpye 1016 assert abs(M.R - 832) < 1.0
617     assert abs(M.F - 21.36) < 0.1
618 johnpye 951
619 johnpye 1016
620 johnpye 991 def testkryx(self):
621 johnpye 951 self.L.load('johnpye/idakryx.a4c')
622     M = self.L.findType('idakryx').getSimulation('sim')
623 johnpye 952 M.build()
624 johnpye 951 I = ascpy.Integrator(M)
625     I.setEngine('IDA')
626     I.setReporter(ascpy.IntegratorReporterConsole(I))
627 johnpye 992 I.setParameter('linsolver','SPGMR')
628 johnpye 993 I.setParameter('prec','JACOBI')
629 johnpye 970 I.setParameter('maxl',8)
630 johnpye 952 I.setParameter('gsmodified',False)
631     I.setParameter('autodiff',True)
632 johnpye 993 I.setParameter('gsmodified',True)
633 johnpye 952 I.setParameter('rtol',0)
634     I.setParameter('atol',1e-3);
635     I.setParameter('atolvect',False)
636 johnpye 993 I.setParameter('calcic','Y')
637 johnpye 952 I.analyse()
638     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
639 johnpye 1017 print M.udot[1][3]
640 johnpye 952 I.solve()
641     assert 0
642 johnpye 967
643 johnpye 1016 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 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
656 johnpye 1016 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 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
668 johnpye 1016 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 johnpye 1017 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 johnpye 1016
681 johnpye 943 # move code above down here if you want to temporarily avoid testing it
682 johnpye 932 class NotToBeTested:
683     def nothing(self):
684     pass
685 johnpye 1016
686 johnpye 669 if __name__=='__main__':
687 johnpye 1098 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 johnpye 966 atexit.register(ascpy.shutdown)
726 johnpye 1008 #suite = unittest.TestSuite()
727 johnpye 1003 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
728 johnpye 1008 #unittest.TextTestRunner(verbosity=2).run(suite)
729     unittest.main()

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