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

Annotation of /branches/fprops2/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1028 - (hide annotations) (download) (as text)
Wed Jan 3 13:26:54 2007 UTC (12 years, 9 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 13519 byte(s)
Added missing dl import in test library (hoping to fix SunOS problem)
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 998 import cunit
14 johnpye 669
15 johnpye 956 class Ascend(unittest.TestCase):
16 johnpye 669
17 johnpye 933 def setUp(self):
18     import ascpy
19     self.L = ascpy.Library()
20    
21     def tearDown(self):
22     self.L.clear()
23     del self.L
24    
25 johnpye 1024 class AscendSelfTester(Ascend):
26    
27     def _run(self,modelname,solvername="QRSlv",filename=None):
28     if filename==None:
29     filename = 'johnpye/%s.a4c' % modelname
30     self.L.load(filename)
31     T = self.L.findType(modelname)
32     M = T.getSimulation('sim')
33     M.build()
34     M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
35     M.run(T.getMethod('self_test'))
36     return M
37    
38 johnpye 966 class TestCompiler(Ascend):
39    
40 johnpye 941 def testloading(self):
41     pass
42    
43     def testsystema4l(self):
44     self.L.load('system.a4l')
45    
46     def testatomsa4l(self):
47     self.L.load('atoms.a4l')
48    
49 johnpye 1024 class TestSolver(AscendSelfTester):
50 johnpye 966
51     def testlog10(self):
52     self._run('testlog10')
53    
54     def testconopt(self):
55     self._run('testconopt',"CONOPT")
56    
57     def testcmslv2(self):
58 johnpye 974 self._run('testcmslv2',"CMSlv")
59 johnpye 966
60     def testsunpos1(self):
61     self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
62    
63     def testsunpos2(self):
64     self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
65    
66     def testsunpos3(self):
67     self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
68    
69     def testsunpos4(self):
70     self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
71    
72     def testsunpos5(self):
73     self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
74    
75     def testsunpos6(self):
76     self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
77    
78     class TestIntegrator(Ascend):
79    
80 johnpye 941 def testListIntegrators(self):
81     I = ascpy.Integrator.getEngines()
82     s1 = sorted([str(i) for i in I.values()])
83 johnpye 972 s2 = sorted(['IDA','LSODE','AWW'])
84 johnpye 941 assert s1==s2
85    
86 johnpye 942 # this routine is reused by both testIDA and testLSODE
87 johnpye 941 def _testIntegrator(self,integratorname):
88 johnpye 940 self.L.load('johnpye/shm.a4c')
89     M = self.L.findType('shm').getSimulation('sim')
90 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
91 johnpye 979 print M.getChildren()
92     assert float(M.x) == 10.0
93     assert float(M.v) == 0.0
94 johnpye 941 t_end = math.pi
95 johnpye 940
96     I = ascpy.Integrator(M)
97     I.setReporter(ascpy.IntegratorReporterNull(I))
98 johnpye 941 I.setEngine(integratorname);
99 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
100 johnpye 941 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
101     I.setMaxSubStep(0.02);
102     I.setInitialSubStep(0.001);
103     I.setMaxSubSteps(200);
104 johnpye 944 if(integratorname=='IDA'):
105     I.setParameter('autodiff',False)
106 johnpye 940 I.analyse();
107     I.solve();
108 johnpye 941 print "At end of simulation,"
109 johnpye 979 print "x = %f" % M.x
110     print "v = %f" % M.v
111     assert abs(float(M.x) + 10) < 1e-2
112     assert abs(float(M.v)) < 1e-2
113 johnpye 940 assert I.getNumObservedVars() == 3
114    
115 johnpye 941 def testInvalidIntegrator(self):
116 johnpye 966 self.L.load('johnpye/shm.a4c')
117 johnpye 941 M = self.L.findType('shm').getSimulation('sim')
118 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
119 johnpye 941 I = ascpy.Integrator(M)
120     try:
121     I.setEngine('___NONEXISTENT____')
122 johnpye 972 except RuntimeError:
123 johnpye 941 return
124     self.fail("setEngine did not raise error!")
125    
126     def testLSODE(self):
127     self._testIntegrator('LSODE')
128    
129 johnpye 972 def testIDA(self):
130     self._testIntegrator('IDA')
131    
132 johnpye 1016 def testparameters(self):
133     self.L.load('johnpye/shm.a4c')
134     M = self.L.findType('shm').getSimulation('sim')
135     M.build()
136     I = ascpy.Integrator(M)
137     I.setEngine('IDA')
138     P = I.getParameters()
139     for p in P:
140     print p.getName(),"=",p.getValue()
141     assert len(P)==11
142     assert P[0].isStr()
143     assert P[0].getName()=="linsolver"
144     assert P[0].getValue()=='SPGMR'
145     assert P[2].getName()=="autodiff"
146     assert P[2].getValue()==True
147     assert P[7].getName()=="atolvect"
148     assert P[7].getBoolValue() == True
149     P[2].setBoolValue(False)
150     assert P[2].getBoolValue()==False
151     I.setParameters(P)
152     assert I.getParameterValue('autodiff')==False
153     I.setParameter('autodiff',True)
154     try:
155     v = I.getParameterValue('nonexist')
156     except KeyError:
157     pass
158     else:
159     self.fail('Failed to trip invalid Integrator parameter')
160    
161 johnpye 972 class TestLSODE(Ascend):
162    
163 johnpye 964 def testzill(self):
164     self.L.load('johnpye/zill.a4c')
165     T = self.L.findType('zill')
166     M = T.getSimulation('sim')
167 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
168 johnpye 964 I = ascpy.Integrator(M)
169 johnpye 966 I.setEngine('LSODE')
170     I.setMinSubStep(1e-7)
171     I.setMaxSubStep(0.001)
172     I.setMaxSubSteps(10000)
173 johnpye 964 I.setReporter(ascpy.IntegratorReporterConsole(I))
174 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
175 johnpye 964 I.analyse()
176     I.solve()
177     M.run(T.getMethod('self_test'))
178    
179 johnpye 962 def testnewton(self):
180 johnpye 973 sys.stderr.write("STARTING TESTNEWTON\n")
181 johnpye 962 self.L.load('johnpye/newton.a4c')
182     T = self.L.findType('newton')
183     M = T.getSimulation('sim')
184     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
185     I = ascpy.Integrator(M)
186     I.setEngine('LSODE')
187 johnpye 963 I.setParameter('rtolvect',False)
188     I.setParameter('rtol',1e-7)
189     I.setParameter('atolvect',False)
190     I.setParameter('atol',1e-7)
191     I.setMinSubStep(1e-7)
192     I.setMaxSubStep(0.001)
193     I.setMaxSubSteps(10000)
194    
195 johnpye 962 I.setReporter(ascpy.IntegratorReporterConsole(I))
196 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
197 johnpye 962 I.analyse()
198     I.solve()
199     print "At end of simulation,"
200 johnpye 979 print "x = %f" % M.x
201     print "v = %f" % M.v
202 johnpye 962 M.run(T.getMethod('self_test'))
203    
204 johnpye 961 def testlotka(self):
205     self.L.load('johnpye/lotka.a4c')
206     M = self.L.findType('lotka').getSimulation('sim')
207 johnpye 980 M.setSolver(ascpy.Solver("QRSlv"))
208 johnpye 961 I = ascpy.Integrator(M)
209     I.setEngine('LSODE')
210     I.setReporter(ascpy.IntegratorReporterConsole(I))
211 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
212 johnpye 961 I.analyse()
213 johnpye 979 print "Number of vars = %d" % I.getNumVars()
214     assert I.getNumVars()==2
215 johnpye 961 I.solve()
216     assert I.getNumObservedVars() == 3;
217 johnpye 979 assert abs(M.R - 832) < 1.0
218     assert abs(M.F - 21.36) < 0.1
219 johnpye 1017
220     #-------------------------------------------------------------------------------
221 johnpye 1021 # Testing of a simple external function
222    
223 johnpye 1024 class TestExtFn(AscendSelfTester):
224 johnpye 1021 def testextfntest(self):
225 johnpye 1024 M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c')
226     self.assertAlmostEqual(M.y, 2);
227     self.assertAlmostEqual(M.x, 1);
228 johnpye 1021 self.assertAlmostEqual(M.y, M.x + 1);
229    
230 johnpye 1024 # THIS TEST FAILS
231     # def testextrelfor(self):
232     # self.L.load('johnpye/extfn/extrelfor.a4c')
233     # T = self.L.findType('extrelfor')
234     # M = T.getSimulation('sim')
235     # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
236     # print "x[1] = %f" % M.x[1]
237     # print "x[2] = %f" % M.x[2]
238     # print "x[3] = %f" % M.x[3]
239     # M.run(T.getMethod('self_test'))
240    
241     def testextrelrepeat(self):
242     M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
243    
244 johnpye 1021 #-------------------------------------------------------------------------------
245 johnpye 1024 # Testing of a ExtPy - external python methods
246    
247     class TestExtPy(AscendSelfTester):
248     def testextpytest(self):
249 johnpye 1026 print "-------------------=--=-=-=-"
250 johnpye 1024 M = self._run('extpytest',filename='johnpye/extpy/extpytest.a4c')
251    
252     #-------------------------------------------------------------------------------
253 johnpye 1017 # Testing of freesteam external steam properties functions
254    
255 johnpye 1024 with_freesteam = False
256 johnpye 1017 try:
257     import freesteam
258     have_freesteam = True
259 johnpye 1018 except ImportError,e:
260 johnpye 1017 have_freesteam = False
261    
262 johnpye 1024 if with_freesteam and have_freesteam:
263 johnpye 1017 class TestFreesteam(Ascend):
264 johnpye 1021 def testload(self):
265 johnpye 1017 self.L.load('johnpye/thermalequilibrium2.a4c')
266 johnpye 1018
267 johnpye 1021 def testinstantiate(self):
268     self.testload()
269 johnpye 1018 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
270 johnpye 1021
271     def testsolve(self):
272     self.testinstantiate()
273 johnpye 1018 M.setSolver(ascpy.Solver("QRSlv"))
274 johnpye 1017 #I = ascpy.Integrator(M)
275     #I.setEngine('LSODE')
276     #I.setReporter(ascpy.IntegratorReporterConsole(I))
277     #I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
278     #I.setMinSubStep(0.01)
279     #I.setInitialSubStep(0.1)
280     #I.analyse()
281     #print "Number of vars = %d" % I.getNumVars()
282     #assert I.getNumVars()==2
283     #I.solve()
284     #assert I.getNumObservedVars() == 3;
285     #assert abs(M.R - 832) < 1.0
286     #assert abs(M.F - 21.36) < 0.1
287    
288    
289     #-------------------------------------------------------------------------------
290     # Testing of IDA models using DENSE linear solver
291    
292 johnpye 1016 class TestIDADENSE(Ascend):
293 johnpye 1017 """IDA DAE integrator, DENSE linear solver"""
294 johnpye 961
295 johnpye 979 def testnewton(self):
296     sys.stderr.write("STARTING TESTNEWTON\n")
297     self.L.load('johnpye/newton.a4c')
298     T = self.L.findType('newton')
299     M = T.getSimulation('sim')
300     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
301     I = ascpy.Integrator(M)
302     I.setEngine('IDA')
303 johnpye 1016 I.setParameter('linsolver','DENSE')
304 johnpye 979 I.setParameter('safeeval',True)
305     I.setParameter('rtol',1e-8)
306     I.setMaxSubStep(0.001)
307     I.setMaxSubSteps(10000)
308    
309     I.setReporter(ascpy.IntegratorReporterConsole(I))
310 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
311 johnpye 979 I.analyse()
312     I.solve()
313     print "At end of simulation,"
314     print "x = %f" % M.x
315     print "v = %f" % M.v
316     M.run(T.getMethod('self_test'))
317    
318 johnpye 991 def testlotkaDENSE(self):
319     self.L.load('johnpye/lotka.a4c')
320     M = self.L.findType('lotka').getSimulation('sim')
321     M.setSolver(ascpy.Solver("QRSlv"))
322     I = ascpy.Integrator(M)
323     I.setEngine('IDA')
324     I.setReporter(ascpy.IntegratorReporterConsole(I))
325     I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
326     I.setParameter('linsolver','DENSE')
327     I.setParameter('rtol',1e-8);
328     I.analyse()
329     assert I.getNumVars()==2
330     assert abs(M.R - 1000) < 1e-300
331     I.solve()
332 johnpye 1017 assert I.getNumObservedVars() == 3
333 johnpye 991 assert abs(M.R - 832) < 1.0
334     assert abs(M.F - 21.36) < 0.1
335 johnpye 972
336 johnpye 975 def testdenx(self):
337 johnpye 1026 print "-----------------------------====="
338 johnpye 942 self.L.load('johnpye/idadenx.a4c')
339     M = self.L.findType('idadenx').getSimulation('sim')
340 johnpye 1017 M.setSolver(ascpy.Solver("QRSlv"))
341 johnpye 942 I = ascpy.Integrator(M)
342     I.setEngine('IDA')
343 johnpye 1024 I.setParameter('calcic','YA_YPD')
344 johnpye 972 I.setParameter('linsolver','DENSE')
345 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
346 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
347 johnpye 950 I.setMaxSubStep(0);
348 johnpye 1017 I.setInitialSubStep(0)
349 johnpye 950 I.setMaxSubSteps(0);
350 johnpye 944 I.setParameter('autodiff',True)
351     I.analyse()
352     I.solve()
353 johnpye 1022 assert abs(float(M.y1) - 5.1091e-08) < 2e-9
354     assert abs(float(M.y2) - 2.0437e-13) < 2e-14
355 johnpye 1017 assert abs(float(M.y3) - 1.0) < 1e-5
356 johnpye 942
357 johnpye 990 def testkryxDENSE(self):
358     self.L.load('johnpye/idakryx.a4c')
359     M = self.L.findType('idakryx').getSimulation('sim')
360     M.setSolver(ascpy.Solver('QRSlv'))
361     M.build()
362     I = ascpy.Integrator(M)
363     I.setEngine('IDA')
364     I.setReporter(ascpy.IntegratorReporterConsole(I))
365     I.setParameter('linsolver','DENSE')
366     I.setParameter('maxl',8)
367     I.setParameter('gsmodified',False)
368     I.setParameter('autodiff',True)
369     I.setParameter('rtol',0)
370     I.setParameter('atol',1e-3);
371     I.setParameter('atolvect',False)
372 johnpye 1016 I.setParameter('calcic','YA_YDP')
373 johnpye 990 I.analyse()
374 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11)
375 johnpye 990 I.solve()
376     assert abs(M.u[2][2].getValue()) < 1e-5
377 johnpye 1017
378     #-------------------------------------------------------------------------------
379     # Testing of IDA models using SPGMR linear solver (Krylov)
380    
381 johnpye 1016 # these tests are disabled until SPGMR preconditioning has been implemented
382     class TestIDASPGMR:#(Ascend):
383     def testlotka(self):
384     self.L.load('johnpye/lotka.a4c')
385     M = self.L.findType('lotka').getSimulation('sim')
386     M.setSolver(ascpy.Solver("QRSlv"))
387 johnpye 951 I = ascpy.Integrator(M)
388     I.setEngine('IDA')
389     I.setReporter(ascpy.IntegratorReporterConsole(I))
390 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
391     I.setParameter('rtol',1e-8)
392 johnpye 951 I.analyse()
393 johnpye 1016 assert I.getNumVars()==2
394     assert abs(M.R - 1000) < 1e-300
395 johnpye 951 I.solve()
396 johnpye 1017 assert I.getNumObservedVars() == 3
397 johnpye 1016 assert abs(M.R - 832) < 1.0
398     assert abs(M.F - 21.36) < 0.1
399 johnpye 951
400 johnpye 1016
401 johnpye 991 def testkryx(self):
402 johnpye 951 self.L.load('johnpye/idakryx.a4c')
403     M = self.L.findType('idakryx').getSimulation('sim')
404 johnpye 952 M.build()
405 johnpye 951 I = ascpy.Integrator(M)
406     I.setEngine('IDA')
407     I.setReporter(ascpy.IntegratorReporterConsole(I))
408 johnpye 992 I.setParameter('linsolver','SPGMR')
409 johnpye 993 I.setParameter('prec','JACOBI')
410 johnpye 970 I.setParameter('maxl',8)
411 johnpye 952 I.setParameter('gsmodified',False)
412     I.setParameter('autodiff',True)
413 johnpye 993 I.setParameter('gsmodified',True)
414 johnpye 952 I.setParameter('rtol',0)
415     I.setParameter('atol',1e-3);
416     I.setParameter('atolvect',False)
417 johnpye 993 I.setParameter('calcic','Y')
418 johnpye 952 I.analyse()
419     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
420 johnpye 1017 print M.udot[1][3]
421 johnpye 952 I.solve()
422     assert 0
423 johnpye 967
424 johnpye 1016 def testzill(self):
425     self.L.load('johnpye/zill.a4c')
426     T = self.L.findType('zill')
427     M = T.getSimulation('sim')
428     M.setSolver(ascpy.Solver('QRSlv'))
429     I = ascpy.Integrator(M)
430     I.setEngine('IDA')
431     I.setParameter('safeeval',False)
432     I.setMinSubStep(1e-7)
433     I.setMaxSubStep(0.001)
434     I.setMaxSubSteps(10000)
435     I.setReporter(ascpy.IntegratorReporterConsole(I))
436 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
437 johnpye 1016 I.analyse()
438     I.solve()
439     M.run(T.getMethod('self_test'))
440    
441     def testdenxSPGMR(self):
442     self.L.load('johnpye/idadenx.a4c')
443     M = self.L.findType('idadenx').getSimulation('sim')
444     M.setSolver(ascpy.Solver('QRSlv'))
445     I = ascpy.Integrator(M)
446     I.setEngine('IDA')
447     I.setReporter(ascpy.IntegratorReporterConsole(I))
448 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
449 johnpye 1016 I.setMaxSubStep(0);
450     I.setInitialSubStep(0);
451     I.setMaxSubSteps(0);
452     I.setParameter('autodiff',True)
453     I.setParameter('linsolver','SPGMR')
454     I.setParameter('gsmodified',False)
455     I.setParameter('maxncf',10)
456     I.analyse()
457     I.solve()
458 johnpye 1017 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
459     assert abs(float(M.y2) - 2.0437e-13) < 1e-15
460     assert abs(float(M.y3) - 1.0) < 1e-5
461 johnpye 1016
462 johnpye 943 # move code above down here if you want to temporarily avoid testing it
463 johnpye 932 class NotToBeTested:
464     def nothing(self):
465     pass
466 johnpye 1016
467 johnpye 669 if __name__=='__main__':
468 johnpye 966 atexit.register(ascpy.shutdown)
469 johnpye 1008 #suite = unittest.TestSuite()
470 johnpye 1003 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
471 johnpye 1008 #unittest.TextTestRunner(verbosity=2).run(suite)
472     unittest.main()

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