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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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