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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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