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

Contents of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 962 - (show annotations) (download) (as text)
Mon Dec 11 14:43:02 2006 UTC (13 years, 9 months ago) by johnpye
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 import unittest
2 import ascpy
3 import math
4 import os, subprocess
5
6 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 class Ascend(unittest.TestCase):
18
19 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 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 def testlog10(self):
37 self.L.load('johnpye/testlog10.a4c')
38 T = self.L.findType('testlog10')
39 M = T.getSimulation('sim')
40 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
41 M.run(T.getMethod('self_test'))
42
43 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 # this routine is reused by both testIDA and testLSODE
50 def _testIntegrator(self,integratorname):
51 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 t_end = math.pi
57
58 I = ascpy.Integrator(M)
59 I.setReporter(ascpy.IntegratorReporterNull(I))
60 I.setEngine(integratorname);
61 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
62 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 if(integratorname=='IDA'):
67 I.setParameter('autodiff',False)
68 I.analyse();
69 I.solve();
70 print "At end of simulation,"
71 print "x = %f" % M.sim.x
72 print "v = %f" % M.sim.v
73 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 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 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 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 def testIDA(self):
122 self._testIntegrator('IDA')
123
124
125 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 for p in P:
132 print p.getName(),"=",p.getValue()
133 assert len(P)==7
134 assert P[0].isStr()
135 assert P[0].getName()=="linsolver"
136 assert P[0].getValue()=='SPGMR'
137 assert P[1].getName()=="autodiff"
138 assert P[1].getValue()==True
139 assert P[5].getName()=="atolvect"
140 assert P[5].getBoolValue() == True
141 P[1].setBoolValue(False)
142 assert P[1].getBoolValue()==False
143 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 def testIDAdenx(self):
156 self.L.load('johnpye/idadenx.a4c')
157 M = self.L.findType('idadenx').getSimulation('sim')
158 I = ascpy.Integrator(M)
159 I.setEngine('IDA')
160 I.setReporter(ascpy.IntegratorReporterConsole(I))
161 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
162 I.setMaxSubStep(0);
163 I.setInitialSubStep(0);
164 I.setMaxSubSteps(0);
165 I.setParameter('autodiff',True)
166 I.setParameter('linsolver','DENSE')
167 I.analyse()
168 I.solve()
169 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
173 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 M.build()
196 I = ascpy.Integrator(M)
197 I.setEngine('IDA')
198 I.setReporter(ascpy.IntegratorReporterConsole(I))
199 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
211 # move code above down here if you want to temporarily avoid testing it
212 class NotToBeTested:
213 def nothing(self):
214 pass
215
216 if __name__=='__main__':
217 unittest.main()

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