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 |
def _testIntegrator(self,integratorname): |
38 |
self.L.load('johnpye/shm.a4c') |
39 |
M = self.L.findType('shm').getSimulation('sim') |
40 |
print M.sim.getChildren() |
41 |
assert float(M.sim.x) == 10.0 |
42 |
assert float(M.sim.v) == 0.0 |
43 |
t_end = math.pi |
44 |
|
45 |
I = ascpy.Integrator(M) |
46 |
I.setReporter(ascpy.IntegratorReporterNull(I)) |
47 |
I.setEngine(integratorname); |
48 |
I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100); |
49 |
I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff) |
50 |
I.setMaxSubStep(0.02); |
51 |
I.setInitialSubStep(0.001); |
52 |
I.setMaxSubSteps(200); |
53 |
I.analyse(); |
54 |
I.solve(); |
55 |
print "At end of simulation," |
56 |
print "x = %f" % M.sim.x |
57 |
print "v = %f" % M.sim.v |
58 |
assert abs(float(M.sim.x) + 10) < 1e-2 |
59 |
assert abs(float(M.sim.v)) < 1e-2 |
60 |
assert I.getNumObservedVars() == 3 |
61 |
|
62 |
def testInvalidIntegrator(self): |
63 |
self.L.load('johnpye/shm.a4c') |
64 |
M = self.L.findType('shm').getSimulation('sim') |
65 |
I = ascpy.Integrator(M) |
66 |
try: |
67 |
I.setEngine('___NONEXISTENT____') |
68 |
except IndexError: |
69 |
return |
70 |
self.fail("setEngine did not raise error!") |
71 |
|
72 |
def testLSODE(self): |
73 |
self._testIntegrator('LSODE') |
74 |
|
75 |
def testIDA(self): |
76 |
self._testIntegrator('IDA') |
77 |
|
78 |
class NotToBeTested: |
79 |
def nothing(self): |
80 |
pass |
81 |
|
82 |
if __name__=='__main__': |
83 |
unittest.main() |