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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 961 - (hide annotations) (download) (as text)
Mon Dec 11 14:04:13 2006 UTC (13 years, 6 months ago) by johnpye
File MIME type: text/x-python
File size: 5600 byte(s)
Fixed a silly bug with Integrator::setEngine.
Added test case for lotka.a4c to test.py
1 johnpye 669 import unittest
2 johnpye 938 import ascpy
3 johnpye 940 import math
4 johnpye 952 import os, subprocess
5 johnpye 669
6 johnpye 952 class CUnit(unittest.TestCase):
7     def setUp(self):
8     self.cunitexe = "../base/generic/test/test"
9    
10     def testcunittests(self):
11     res = os.system(self.cunitexe)
12     if res:
13     raise RuntimeError("CUnit tests failed (returned %d -- run %s for details)" % (res,self.cunitexe))
14     else:
15     print "CUnit returned %s" % res
16    
17 johnpye 956 class Ascend(unittest.TestCase):
18 johnpye 669
19 johnpye 933 def setUp(self):
20     import ascpy
21     self.L = ascpy.Library()
22    
23     def tearDown(self):
24     self.L.clear()
25     del self.L
26    
27 johnpye 941 def testloading(self):
28     pass
29    
30     def testsystema4l(self):
31     self.L.load('system.a4l')
32    
33     def testatomsa4l(self):
34     self.L.load('atoms.a4l')
35    
36 johnpye 932 def testlog10(self):
37 johnpye 933 self.L.load('johnpye/testlog10.a4c')
38     T = self.L.findType('testlog10')
39 johnpye 932 M = T.getSimulation('sim')
40 johnpye 942 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
41 johnpye 932 M.run(T.getMethod('self_test'))
42    
43 johnpye 941 def testListIntegrators(self):
44     I = ascpy.Integrator.getEngines()
45     s1 = sorted([str(i) for i in I.values()])
46     s2 = sorted(['IDA','LSODE'])
47     assert s1==s2
48    
49 johnpye 942 # this routine is reused by both testIDA and testLSODE
50 johnpye 941 def _testIntegrator(self,integratorname):
51 johnpye 940 self.L.load('johnpye/shm.a4c')
52     M = self.L.findType('shm').getSimulation('sim')
53     print M.sim.getChildren()
54     assert float(M.sim.x) == 10.0
55     assert float(M.sim.v) == 0.0
56 johnpye 941 t_end = math.pi
57 johnpye 940
58     I = ascpy.Integrator(M)
59     I.setReporter(ascpy.IntegratorReporterNull(I))
60 johnpye 941 I.setEngine(integratorname);
61 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
62 johnpye 941 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
63     I.setMaxSubStep(0.02);
64     I.setInitialSubStep(0.001);
65     I.setMaxSubSteps(200);
66 johnpye 944 if(integratorname=='IDA'):
67     I.setParameter('autodiff',False)
68 johnpye 940 I.analyse();
69     I.solve();
70 johnpye 941 print "At end of simulation,"
71     print "x = %f" % M.sim.x
72     print "v = %f" % M.sim.v
73 johnpye 940 assert abs(float(M.sim.x) + 10) < 1e-2
74     assert abs(float(M.sim.v)) < 1e-2
75     assert I.getNumObservedVars() == 3
76    
77 johnpye 941 def testInvalidIntegrator(self):
78     self.L.load('johnpye/shm.a4c')
79     M = self.L.findType('shm').getSimulation('sim')
80     I = ascpy.Integrator(M)
81     try:
82     I.setEngine('___NONEXISTENT____')
83     except IndexError:
84     return
85     self.fail("setEngine did not raise error!")
86    
87     def testLSODE(self):
88     self._testIntegrator('LSODE')
89    
90 johnpye 961 def testlotka(self):
91     self.L.load('johnpye/lotka.a4c')
92     M = self.L.findType('lotka').getSimulation('sim')
93     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
94     I = ascpy.Integrator(M)
95     I.setEngine('LSODE')
96     I.setReporter(ascpy.IntegratorReporterConsole(I))
97     I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
98     I.analyse()
99     I.solve()
100     assert I.getNumObservedVars() == 3;
101     assert abs(float(M.sim.R) - 832) < 1.0
102     assert abs(float(M.sim.F) - 21.36) < 0.1
103    
104    
105 johnpye 941 def testIDA(self):
106     self._testIntegrator('IDA')
107    
108 johnpye 961
109 johnpye 943 def testIDAparameters(self):
110     self.L.load('johnpye/shm.a4c')
111     M = self.L.findType('shm').getSimulation('sim')
112     I = ascpy.Integrator(M)
113     I.setEngine('IDA')
114     P = I.getParameters()
115 johnpye 945 for p in P:
116     print p.getName(),"=",p.getValue()
117 johnpye 961 assert len(P)==7
118 johnpye 945 assert P[0].isStr()
119     assert P[0].getName()=="linsolver"
120 johnpye 961 assert P[0].getValue()=='SPGMR'
121 johnpye 945 assert P[1].getName()=="autodiff"
122     assert P[1].getValue()==True
123 johnpye 961 assert P[5].getName()=="atolvect"
124     assert P[5].getBoolValue() == True
125 johnpye 945 P[1].setBoolValue(False)
126     assert P[1].getBoolValue()==False
127 johnpye 943 I.setParameters(P)
128     for p in I.getParameters():
129     print p.getName(),"=",p.getValue()
130     assert I.getParameterValue('autodiff')==False
131     I.setParameter('autodiff',True)
132     try:
133     v = I.getParameterValue('nonexist')
134     except KeyError:
135     pass
136     else:
137     self.fail('Failed to trip invalid Integrator parameter')
138    
139 johnpye 951 def testIDAdenx(self):
140 johnpye 942 self.L.load('johnpye/idadenx.a4c')
141     M = self.L.findType('idadenx').getSimulation('sim')
142     I = ascpy.Integrator(M)
143     I.setEngine('IDA')
144 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
145 johnpye 945 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
146 johnpye 950 I.setMaxSubStep(0);
147     I.setInitialSubStep(0);
148     I.setMaxSubSteps(0);
149 johnpye 944 I.setParameter('autodiff',True)
150 johnpye 946 I.setParameter('linsolver','DENSE')
151 johnpye 944 I.analyse()
152     I.solve()
153 johnpye 950 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
154     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
155     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
156 johnpye 942
157 johnpye 951 def testIDAdenxSPGMR(self):
158     self.L.load('johnpye/idadenx.a4c')
159     M = self.L.findType('idadenx').getSimulation('sim')
160     I = ascpy.Integrator(M)
161     I.setEngine('IDA')
162     I.setReporter(ascpy.IntegratorReporterConsole(I))
163     I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
164     I.setMaxSubStep(0);
165     I.setInitialSubStep(0);
166     I.setMaxSubSteps(0);
167     I.setParameter('autodiff',True)
168     I.setParameter('linsolver','SPGMR')
169     I.setParameter('gsmodified',False)
170     I.analyse()
171     I.solve()
172     assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
173     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
174     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
175    
176     def testIDAkryx(self):
177     self.L.load('johnpye/idakryx.a4c')
178     M = self.L.findType('idakryx').getSimulation('sim')
179 johnpye 952 M.build()
180 johnpye 951 I = ascpy.Integrator(M)
181     I.setEngine('IDA')
182     I.setReporter(ascpy.IntegratorReporterConsole(I))
183 johnpye 952 I.setParameter('linsolver','SPGMR')
184     I.setParameter('gsmodified',False)
185     I.setParameter('autodiff',True)
186     I.setParameter('rtol',0)
187     I.setParameter('atol',1e-3);
188     I.setParameter('atolvect',False)
189     I.analyse()
190     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
191     print M.sim.udot[1][3];
192     I.solve()
193     assert 0
194 johnpye 951
195 johnpye 943 # move code above down here if you want to temporarily avoid testing it
196 johnpye 932 class NotToBeTested:
197     def nothing(self):
198     pass
199 johnpye 689
200 johnpye 669 if __name__=='__main__':
201     unittest.main()

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