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

Annotation of /trunk/pygtk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 950 - (hide annotations) (download) (as text)
Sun Nov 26 03:14:02 2006 UTC (13 years, 3 months ago) by johnpye
File MIME type: text/x-python
File size: 3621 byte(s)
Completed adding idadenx to test suite.
Restored behaviour of mxstep to IDA.
1 johnpye 669 import unittest
2 johnpye 938 import ascpy
3 johnpye 940 import math
4 johnpye 669
5     class AscendTest(unittest.TestCase):
6    
7 johnpye 933 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 johnpye 941 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 johnpye 932 def testlog10(self):
25 johnpye 933 self.L.load('johnpye/testlog10.a4c')
26     T = self.L.findType('testlog10')
27 johnpye 932 M = T.getSimulation('sim')
28 johnpye 942 M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
29 johnpye 932 M.run(T.getMethod('self_test'))
30    
31 johnpye 941 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 johnpye 942 # this routine is reused by both testIDA and testLSODE
38 johnpye 941 def _testIntegrator(self,integratorname):
39 johnpye 940 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 johnpye 941 t_end = math.pi
45 johnpye 940
46     I = ascpy.Integrator(M)
47     I.setReporter(ascpy.IntegratorReporterNull(I))
48 johnpye 941 I.setEngine(integratorname);
49 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
50 johnpye 941 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 johnpye 944 if(integratorname=='IDA'):
55     I.setParameter('autodiff',False)
56 johnpye 940 I.analyse();
57     I.solve();
58 johnpye 941 print "At end of simulation,"
59     print "x = %f" % M.sim.x
60     print "v = %f" % M.sim.v
61 johnpye 940 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 johnpye 941 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 johnpye 943 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 johnpye 945 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 johnpye 943 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 johnpye 942 def testIDAwithDAE(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 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
117 johnpye 945 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
118 johnpye 950 I.setMaxSubStep(0);
119     I.setInitialSubStep(0);
120     I.setMaxSubSteps(0);
121 johnpye 944 I.setParameter('autodiff',True)
122 johnpye 946 I.setParameter('linsolver','DENSE')
123 johnpye 944 I.analyse()
124     I.solve()
125 johnpye 950 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 johnpye 942
129 johnpye 943 # move code above down here if you want to temporarily avoid testing it
130 johnpye 932 class NotToBeTested:
131     def nothing(self):
132     pass
133 johnpye 689
134 johnpye 669 if __name__=='__main__':
135     unittest.main()

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