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

Contents of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 943 - (show annotations) (download) (as text)
Sat Nov 25 06:32:51 2006 UTC (13 years ago) by johnpye
Original Path: trunk/pygtk/test.py
File MIME type: text/x-python
File size: 3314 byte(s)
Some bugs fixed with SolverParameterIter
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 I.analyse();
55 I.solve();
56 print "At end of simulation,"
57 print "x = %f" % M.sim.x
58 print "v = %f" % M.sim.v
59 assert abs(float(M.sim.x) + 10) < 1e-2
60 assert abs(float(M.sim.v)) < 1e-2
61 assert I.getNumObservedVars() == 3
62
63 def testInvalidIntegrator(self):
64 self.L.load('johnpye/shm.a4c')
65 M = self.L.findType('shm').getSimulation('sim')
66 I = ascpy.Integrator(M)
67 try:
68 I.setEngine('___NONEXISTENT____')
69 except IndexError:
70 return
71 self.fail("setEngine did not raise error!")
72
73 def testLSODE(self):
74 self._testIntegrator('LSODE')
75
76 def testIDA(self):
77 self._testIntegrator('IDA')
78
79 def testIDAparameters(self):
80 self.L.load('johnpye/shm.a4c')
81 M = self.L.findType('shm').getSimulation('sim')
82 I = ascpy.Integrator(M)
83 I.setEngine('IDA')
84 P = I.getParameters()
85 assert len(P)==1
86 assert P[0].isBool()
87 assert P[0].getName()=="autodiff"
88 assert P[0].getBoolValue() == True
89 P[0].setBoolValue(False)
90 assert P[0].getBoolValue()==False
91 I.setParameters(P)
92 assert len(I.getParameters())==1
93 for p in I.getParameters():
94 print p.getName(),"=",p.getValue()
95 assert I.getParameterValue('autodiff')==False
96 I.setParameter('autodiff',True)
97 try:
98 v = I.getParameterValue('nonexist')
99 except KeyError:
100 pass
101 else:
102 self.fail('Failed to trip invalid Integrator parameter')
103
104 def testIDAwithDAE(self):
105 self.L.load('johnpye/idadenx.a4c')
106 M = self.L.findType('idadenx').getSimulation('sim')
107 I = ascpy.Integrator(M)
108 I.setEngine('IDA')
109 I.setReporter(ascpy.IntegratorReporterNull(I))
110 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11);
111 I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)
112 I.setMaxSubStep(0.02);
113 I.setInitialSubStep(0.001);
114 I.setMaxSubSteps(500);
115 I.setParameter('autodiff',False)
116 I.analyse();
117 I.solve();
118
119 # move code above down here if you want to temporarily avoid testing it
120 class NotToBeTested:
121 def nothing(self):
122 pass
123
124 if __name__=='__main__':
125 unittest.main()

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