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

Contents of /branches/fprops2/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 975 - (show annotations) (download) (as text)
Tue Dec 19 11:49:59 2006 UTC (12 years, 10 months ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 8414 byte(s)
Fixed bug with Compute_J (using NumberRels instead of slv_get_number_solvers_rels).
Added GPL header to sensitivity.c.
Renamed IDA test cases.
1 import unittest
2 import ascpy
3 import math
4 import os, subprocess, sys
5 import atexit
6
7 class Ascend(unittest.TestCase):
8
9 def setUp(self):
10 import ascpy
11 self.L = ascpy.Library()
12
13 def tearDown(self):
14 self.L.clear()
15 del self.L
16
17 class TestCompiler(Ascend):
18
19 def testloading(self):
20 pass
21
22 def testsystema4l(self):
23 self.L.load('system.a4l')
24
25 def testatomsa4l(self):
26 self.L.load('atoms.a4l')
27
28 class TestSolver(Ascend):
29
30 def _run(self,modelname,solvername="QRSlv",filename=None):
31 if filename==None:
32 filename = 'johnpye/%s.a4c' % modelname
33 self.L.load(filename)
34 T = self.L.findType(modelname)
35 M = T.getSimulation('sim')
36 M.build()
37 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
38 M.run(T.getMethod('self_test'))
39
40 def testlog10(self):
41 self._run('testlog10')
42
43 def testconopt(self):
44 self._run('testconopt',"CONOPT")
45
46 def testcmslv2(self):
47 self._run('testcmslv2',"CMSlv")
48
49 def testsunpos1(self):
50 self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
51
52 def testsunpos2(self):
53 self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
54
55 def testsunpos3(self):
56 self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
57
58 def testsunpos4(self):
59 self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
60
61 def testsunpos5(self):
62 self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
63
64 def testsunpos6(self):
65 self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
66
67 class TestIntegrator(Ascend):
68
69 def testListIntegrators(self):
70 I = ascpy.Integrator.getEngines()
71 s1 = sorted([str(i) for i in I.values()])
72 s2 = sorted(['IDA','LSODE','AWW'])
73 assert s1==s2
74
75 # this routine is reused by both testIDA and testLSODE
76 def _testIntegrator(self,integratorname):
77 self.L.load('johnpye/shm.a4c')
78 M = self.L.findType('shm').getSimulation('sim')
79 M.setSolver(ascpy.Solver('QRSlv'))
80 print M.sim.getChildren()
81 assert float(M.sim.x) == 10.0
82 assert float(M.sim.v) == 0.0
83 t_end = math.pi
84
85 I = ascpy.Integrator(M)
86 I.setReporter(ascpy.IntegratorReporterNull(I))
87 I.setEngine(integratorname);
88 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
89 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
90 I.setMaxSubStep(0.02);
91 I.setInitialSubStep(0.001);
92 I.setMaxSubSteps(200);
93 if(integratorname=='IDA'):
94 I.setParameter('autodiff',False)
95 I.analyse();
96 I.solve();
97 print "At end of simulation,"
98 print "x = %f" % M.sim.x
99 print "v = %f" % M.sim.v
100 assert abs(float(M.sim.x) + 10) < 1e-2
101 assert abs(float(M.sim.v)) < 1e-2
102 assert I.getNumObservedVars() == 3
103
104 def testInvalidIntegrator(self):
105 self.L.load('johnpye/shm.a4c')
106 M = self.L.findType('shm').getSimulation('sim')
107 M.setSolver(ascpy.Solver('QRSlv'))
108 I = ascpy.Integrator(M)
109 try:
110 I.setEngine('___NONEXISTENT____')
111 except RuntimeError:
112 return
113 self.fail("setEngine did not raise error!")
114
115 def testLSODE(self):
116 self._testIntegrator('LSODE')
117
118 def testIDA(self):
119 self._testIntegrator('IDA')
120
121 class TestLSODE(Ascend):
122
123 def testzill(self):
124 self.L.load('johnpye/zill.a4c')
125 T = self.L.findType('zill')
126 M = T.getSimulation('sim')
127 M.setSolver(ascpy.Solver('QRSlv'))
128 I = ascpy.Integrator(M)
129 I.setEngine('LSODE')
130 I.setMinSubStep(1e-7)
131 I.setMaxSubStep(0.001)
132 I.setMaxSubSteps(10000)
133 I.setReporter(ascpy.IntegratorReporterConsole(I))
134 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
135 I.analyse()
136 I.solve()
137 M.run(T.getMethod('self_test'))
138
139 def testnewton(self):
140 sys.stderr.write("STARTING TESTNEWTON\n")
141 self.L.load('johnpye/newton.a4c')
142 T = self.L.findType('newton')
143 M = T.getSimulation('sim')
144 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
145 I = ascpy.Integrator(M)
146 I.setEngine('LSODE')
147 I.setParameter('rtolvect',False)
148 I.setParameter('rtol',1e-7)
149 I.setParameter('atolvect',False)
150 I.setParameter('atol',1e-7)
151 I.setMinSubStep(1e-7)
152 I.setMaxSubStep(0.001)
153 I.setMaxSubSteps(10000)
154
155 I.setReporter(ascpy.IntegratorReporterConsole(I))
156 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.sim.v)/float(M.sim.g), 2);
157 I.analyse()
158 I.solve()
159 print "At end of simulation,"
160 print "x = %f" % M.sim.x
161 print "v = %f" % M.sim.v
162 M.run(T.getMethod('self_test'))
163
164 def testlotka(self):
165 self.L.load('johnpye/lotka.a4c')
166 M = self.L.findType('lotka').getSimulation('sim')
167 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
168 I = ascpy.Integrator(M)
169 I.setEngine('LSODE')
170 I.setReporter(ascpy.IntegratorReporterConsole(I))
171 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
172 I.analyse()
173 I.solve()
174 assert I.getNumObservedVars() == 3;
175 assert abs(float(M.sim.R) - 832) < 1.0
176 assert abs(float(M.sim.F) - 21.36) < 0.1
177
178 class TestIDA(Ascend):
179
180 def testparameters(self):
181 self.L.load('johnpye/shm.a4c')
182 M = self.L.findType('shm').getSimulation('sim')
183 I = ascpy.Integrator(M)
184 I.setEngine('IDA')
185 P = I.getParameters()
186 for p in P:
187 print p.getName(),"=",p.getValue()
188 assert len(P)==7
189 assert P[0].isStr()
190 assert P[0].getName()=="linsolver"
191 assert P[0].getValue()=='SPGMR'
192 assert P[1].getName()=="autodiff"
193 assert P[1].getValue()==True
194 assert P[5].getName()=="atolvect"
195 assert P[5].getBoolValue() == True
196 P[1].setBoolValue(False)
197 assert P[1].getBoolValue()==False
198 I.setParameters(P)
199 for p in I.getParameters():
200 print p.getName(),"=",p.getValue()
201 assert I.getParameterValue('autodiff')==False
202 I.setParameter('autodiff',True)
203 try:
204 v = I.getParameterValue('nonexist')
205 except KeyError:
206 pass
207 else:
208 self.fail('Failed to trip invalid Integrator parameter')
209
210 def testzill(self):
211 self.L.load('johnpye/zill.a4c')
212 T = self.L.findType('zill')
213 M = T.getSimulation('sim')
214 M.setSolver(ascpy.Solver('QRSlv'))
215 I = ascpy.Integrator(M)
216 I.setEngine('IDA')
217 I.setParameter('safeeval',True)
218 I.setMinSubStep(1e-7)
219 I.setMaxSubStep(0.001)
220 I.setMaxSubSteps(10000)
221 I.setReporter(ascpy.IntegratorReporterConsole(I))
222 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5);
223 I.analyse()
224 I.solve()
225 M.run(T.getMethod('self_test'))
226
227 def testdenx(self):
228 self.L.load('johnpye/idadenx.a4c')
229 M = self.L.findType('idadenx').getSimulation('sim')
230 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
231 I = ascpy.Integrator(M)
232 I.setEngine('IDA')
233 I.setParameter('calcic',False)
234 I.setParameter('linsolver','DENSE')
235 I.setReporter(ascpy.IntegratorReporterConsole(I))
236 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
237 I.setMaxSubStep(0);
238 I.setInitialSubStep(0);
239 I.setMaxSubSteps(0);
240 I.setParameter('autodiff',True)
241 I.analyse()
242 I.solve()
243 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
244 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
245 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
246
247 def testdenxSPGMR(self):
248 self.L.load('johnpye/idadenx.a4c')
249 M = self.L.findType('idadenx').getSimulation('sim')
250 I = ascpy.Integrator(M)
251 I.setEngine('IDA')
252 I.setReporter(ascpy.IntegratorReporterConsole(I))
253 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
254 I.setMaxSubStep(0);
255 I.setInitialSubStep(0);
256 I.setMaxSubSteps(0);
257 I.setParameter('autodiff',True)
258 I.setParameter('linsolver','SPGMR')
259 I.setParameter('gsmodified',False)
260 I.analyse()
261 I.solve()
262 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
263 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
264 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
265
266 def testkryx(self):
267 self.L.load('johnpye/idakryx.a4c')
268 M = self.L.findType('idakryx').getSimulation('sim')
269 M.build()
270 I = ascpy.Integrator(M)
271 I.setEngine('IDA')
272 I.setReporter(ascpy.IntegratorReporterConsole(I))
273 I.setParameter('linsolver','SPGMR')
274 I.setParameter('maxl',8)
275 I.setParameter('gsmodified',False)
276 I.setParameter('autodiff',True)
277 I.setParameter('rtol',0)
278 I.setParameter('atol',1e-3);
279 I.setParameter('atolvect',False)
280 I.analyse()
281 I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
282 print M.sim.udot[1][3];
283 I.solve()
284 assert 0
285
286 class CUnit(unittest.TestCase):
287 def setUp(self):
288 self.cunitexe = "../base/generic/test/test"
289
290 def testcunittests(self):
291 res = os.system(self.cunitexe)
292 if res:
293 raise RuntimeError("CUnit tests failed (returned %d -- run %s for details)" % (res,self.cunitexe))
294 else:
295 print "CUnit returned %s" % res
296
297 # move code above down here if you want to temporarily avoid testing it
298 class NotToBeTested:
299 def nothing(self):
300 pass
301
302 if __name__=='__main__':
303 atexit.register(ascpy.shutdown)
304 unittest.main()

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