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

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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