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

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 962 - (hide annotations) (download) (as text)
Mon Dec 11 14:43:02 2006 UTC (12 years, 11 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 6083 byte(s)
Adding more LSODE test cases.
Removing some 'error tree' debug output.
Added RTOLDEF and ATOLDEF parameters to LSODE (and fixed a bug relating to integrator_lsode_params_default).
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 962 def testnewton(self):
91     self.L.load('johnpye/newton.a4c')
92     T = self.L.findType('newton')
93     M = T.getSimulation('sim')
94     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
95     I = ascpy.Integrator(M)
96     I.setEngine('LSODE')
97     I.setReporter(ascpy.IntegratorReporterConsole(I))
98     I.setLinearTimesteps(ascpy.Units("s"), 0, 20.0/9.8, 2);
99     I.analyse()
100     I.solve()
101     print "At end of simulation,"
102     print "x = %f" % M.sim.x
103     print "v = %f" % M.sim.v
104     M.run(T.getMethod('self_test'))
105    
106 johnpye 961 def testlotka(self):
107     self.L.load('johnpye/lotka.a4c')
108     M = self.L.findType('lotka').getSimulation('sim')
109     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
110     I = ascpy.Integrator(M)
111     I.setEngine('LSODE')
112     I.setReporter(ascpy.IntegratorReporterConsole(I))
113     I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
114     I.analyse()
115     I.solve()
116     assert I.getNumObservedVars() == 3;
117     assert abs(float(M.sim.R) - 832) < 1.0
118     assert abs(float(M.sim.F) - 21.36) < 0.1
119    
120    
121 johnpye 941 def testIDA(self):
122     self._testIntegrator('IDA')
123    
124 johnpye 961
125 johnpye 943 def testIDAparameters(self):
126     self.L.load('johnpye/shm.a4c')
127     M = self.L.findType('shm').getSimulation('sim')
128     I = ascpy.Integrator(M)
129     I.setEngine('IDA')
130     P = I.getParameters()
131 johnpye 945 for p in P:
132     print p.getName(),"=",p.getValue()
133 johnpye 961 assert len(P)==7
134 johnpye 945 assert P[0].isStr()
135     assert P[0].getName()=="linsolver"
136 johnpye 961 assert P[0].getValue()=='SPGMR'
137 johnpye 945 assert P[1].getName()=="autodiff"
138     assert P[1].getValue()==True
139 johnpye 961 assert P[5].getName()=="atolvect"
140     assert P[5].getBoolValue() == True
141 johnpye 945 P[1].setBoolValue(False)
142     assert P[1].getBoolValue()==False
143 johnpye 943 I.setParameters(P)
144     for p in I.getParameters():
145     print p.getName(),"=",p.getValue()
146     assert I.getParameterValue('autodiff')==False
147     I.setParameter('autodiff',True)
148     try:
149     v = I.getParameterValue('nonexist')
150     except KeyError:
151     pass
152     else:
153     self.fail('Failed to trip invalid Integrator parameter')
154    
155 johnpye 951 def testIDAdenx(self):
156 johnpye 942 self.L.load('johnpye/idadenx.a4c')
157     M = self.L.findType('idadenx').getSimulation('sim')
158     I = ascpy.Integrator(M)
159     I.setEngine('IDA')
160 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
161 johnpye 945 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
162 johnpye 950 I.setMaxSubStep(0);
163     I.setInitialSubStep(0);
164     I.setMaxSubSteps(0);
165 johnpye 944 I.setParameter('autodiff',True)
166 johnpye 946 I.setParameter('linsolver','DENSE')
167 johnpye 944 I.analyse()
168     I.solve()
169 johnpye 950 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
170     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
171     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
172 johnpye 942
173 johnpye 951 def testIDAdenxSPGMR(self):
174     self.L.load('johnpye/idadenx.a4c')
175     M = self.L.findType('idadenx').getSimulation('sim')
176     I = ascpy.Integrator(M)
177     I.setEngine('IDA')
178     I.setReporter(ascpy.IntegratorReporterConsole(I))
179     I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
180     I.setMaxSubStep(0);
181     I.setInitialSubStep(0);
182     I.setMaxSubSteps(0);
183     I.setParameter('autodiff',True)
184     I.setParameter('linsolver','SPGMR')
185     I.setParameter('gsmodified',False)
186     I.analyse()
187     I.solve()
188     assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
189     assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
190     assert abs(float(M.sim.y3) - 1.0) < 1e-5;
191    
192     def testIDAkryx(self):
193     self.L.load('johnpye/idakryx.a4c')
194     M = self.L.findType('idakryx').getSimulation('sim')
195 johnpye 952 M.build()
196 johnpye 951 I = ascpy.Integrator(M)
197     I.setEngine('IDA')
198     I.setReporter(ascpy.IntegratorReporterConsole(I))
199 johnpye 952 I.setParameter('linsolver','SPGMR')
200     I.setParameter('gsmodified',False)
201     I.setParameter('autodiff',True)
202     I.setParameter('rtol',0)
203     I.setParameter('atol',1e-3);
204     I.setParameter('atolvect',False)
205     I.analyse()
206     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
207     print M.sim.udot[1][3];
208     I.solve()
209     assert 0
210 johnpye 951
211 johnpye 943 # move code above down here if you want to temporarily avoid testing it
212 johnpye 932 class NotToBeTested:
213     def nothing(self):
214     pass
215 johnpye 689
216 johnpye 669 if __name__=='__main__':
217     unittest.main()

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