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

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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