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

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 951 - (show annotations) (download) (as text)
Sun Nov 26 05:01:49 2006 UTC (13 years ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 4462 byte(s)
Added 'GSMODIFIED' parameter to IDA implementation.
Working on getting the idadenx model to converge using the SPGMR solver.
1 import unittest
2 import ascpy
3 import math
4
5 class AscendTest(unittest.TestCase):
6
7 def setUp(self):
8 import ascpy
9 self.L = ascpy.Library()
10
11 def tearDown(self):
12 self.L.clear()
13 del self.L
14
15 def testloading(self):
16 pass
17
18 def testsystema4l(self):
19 self.L.load('system.a4l')
20
21 def testatomsa4l(self):
22 self.L.load('atoms.a4l')
23
24 def testlog10(self):
25 self.L.load('johnpye/testlog10.a4c')
26 T = self.L.findType('testlog10')
27 M = T.getSimulation('sim')
28 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
29 M.run(T.getMethod('self_test'))
30
31 def testListIntegrators(self):
32 I = ascpy.Integrator.getEngines()
33 s1 = sorted([str(i) for i in I.values()])
34 s2 = sorted(['IDA','LSODE'])
35 assert s1==s2
36
37 # this routine is reused by both testIDA and testLSODE
38 def _testIntegrator(self,integratorname):
39 self.L.load('johnpye/shm.a4c')
40 M = self.L.findType('shm').getSimulation('sim')
41 print M.sim.getChildren()
42 assert float(M.sim.x) == 10.0
43 assert float(M.sim.v) == 0.0
44 t_end = math.pi
45
46 I = ascpy.Integrator(M)
47 I.setReporter(ascpy.IntegratorReporterNull(I))
48 I.setEngine(integratorname);
49 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
50 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
51 I.setMaxSubStep(0.02);
52 I.setInitialSubStep(0.001);
53 I.setMaxSubSteps(200);
54 if(integratorname=='IDA'):
55 I.setParameter('autodiff',False)
56 I.analyse();
57 I.solve();
58 print "At end of simulation,"
59 print "x = %f" % M.sim.x
60 print "v = %f" % M.sim.v
61 assert abs(float(M.sim.x) + 10) < 1e-2
62 assert abs(float(M.sim.v)) < 1e-2
63 assert I.getNumObservedVars() == 3
64
65 def testInvalidIntegrator(self):
66 self.L.load('johnpye/shm.a4c')
67 M = self.L.findType('shm').getSimulation('sim')
68 I = ascpy.Integrator(M)
69 try:
70 I.setEngine('___NONEXISTENT____')
71 except IndexError:
72 return
73 self.fail("setEngine did not raise error!")
74
75 def testLSODE(self):
76 self._testIntegrator('LSODE')
77
78 def testIDA(self):
79 self._testIntegrator('IDA')
80
81 def testIDAparameters(self):
82 self.L.load('johnpye/shm.a4c')
83 M = self.L.findType('shm').getSimulation('sim')
84 I = ascpy.Integrator(M)
85 I.setEngine('IDA')
86 P = I.getParameters()
87 for p in P:
88 print p.getName(),"=",p.getValue()
89 assert len(P)==5
90 assert P[0].isStr()
91 assert P[0].getName()=="linsolver"
92 assert P[0].getValue()=='DENSE'
93 assert P[1].getName()=="autodiff"
94 assert P[1].getValue()==True
95 assert P[4].getName()=="atolvect"
96 assert P[4].getBoolValue() == True
97 P[1].setBoolValue(False)
98 assert P[1].getBoolValue()==False
99 I.setParameters(P)
100 for p in I.getParameters():
101 print p.getName(),"=",p.getValue()
102 assert I.getParameterValue('autodiff')==False
103 I.setParameter('autodiff',True)
104 try:
105 v = I.getParameterValue('nonexist')
106 except KeyError:
107 pass
108 else:
109 self.fail('Failed to trip invalid Integrator parameter')
110
111 def testIDAdenx(self):
112 self.L.load('johnpye/idadenx.a4c')
113 M = self.L.findType('idadenx').getSimulation('sim')
114 I = ascpy.Integrator(M)
115 I.setEngine('IDA')
116 I.setReporter(ascpy.IntegratorReporterConsole(I))
117 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
118 I.setMaxSubStep(0);
119 I.setInitialSubStep(0);
120 I.setMaxSubSteps(0);
121 I.setParameter('autodiff',True)
122 I.setParameter('linsolver','DENSE')
123 I.analyse()
124 I.solve()
125 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
126 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
127 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
128
129 def testIDAdenxSPGMR(self):
130 self.L.load('johnpye/idadenx.a4c')
131 M = self.L.findType('idadenx').getSimulation('sim')
132 I = ascpy.Integrator(M)
133 I.setEngine('IDA')
134 I.setReporter(ascpy.IntegratorReporterConsole(I))
135 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
136 I.setMaxSubStep(0);
137 I.setInitialSubStep(0);
138 I.setMaxSubSteps(0);
139 I.setParameter('autodiff',True)
140 I.setParameter('linsolver','SPGMR')
141 I.setParameter('gsmodified',False)
142 I.analyse()
143 I.solve()
144 assert abs(float(M.sim.y1) - 5.1091e-08) < 1e-10;
145 assert abs(float(M.sim.y2) - 2.0437e-13) < 1e-15;
146 assert abs(float(M.sim.y3) - 1.0) < 1e-5;
147
148 def testIDAkryx(self):
149 self.L.load('johnpye/idakryx.a4c')
150 M = self.L.findType('idakryx').getSimulation('sim')
151 I = ascpy.Integrator(M)
152 I.setEngine('IDA')
153 I.setReporter(ascpy.IntegratorReporterConsole(I))
154
155 # move code above down here if you want to temporarily avoid testing it
156 class NotToBeTested:
157 def nothing(self):
158 pass
159
160 if __name__=='__main__':
161 unittest.main()

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