1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode;
19
20 import junit.framework.*;
21
22 import org.apache.commons.math.ode.DerivativeException;
23 import org.apache.commons.math.ode.FirstOrderIntegrator;
24 import org.apache.commons.math.ode.IntegratorException;
25 import org.apache.commons.math.ode.MidpointIntegrator;
26 import org.apache.commons.math.ode.SwitchingFunction;
27
28 public class MidpointIntegratorTest
29 extends TestCase {
30
31 public MidpointIntegratorTest(String name) {
32 super(name);
33 }
34
35 public void testDimensionCheck() {
36 try {
37 TestProblem1 pb = new TestProblem1();
38 new MidpointIntegrator(0.01).integrate(pb,
39 0.0, new double[pb.getDimension()+10],
40 1.0, new double[pb.getDimension()+10]);
41 fail("an exception should have been thrown");
42 } catch(DerivativeException de) {
43 fail("wrong exception caught");
44 } catch(IntegratorException ie) {
45 }
46 }
47
48 public void testDecreasingSteps()
49 throws DerivativeException, IntegratorException {
50
51 TestProblemAbstract[] problems = TestProblemFactory.getProblems();
52 for (int k = 0; k < problems.length; ++k) {
53
54 double previousError = Double.NaN;
55 for (int i = 4; i < 10; ++i) {
56
57 TestProblemAbstract pb = (TestProblemAbstract) problems[k].clone();
58 double step = (pb.getFinalTime() - pb.getInitialTime())
59 * Math.pow(2.0, -i);
60 FirstOrderIntegrator integ = new MidpointIntegrator(step);
61 TestProblemHandler handler = new TestProblemHandler(pb, integ);
62 integ.setStepHandler(handler);
63 SwitchingFunction[] functions = pb.getSwitchingFunctions();
64 for (int l = 0; l < functions.length; ++l) {
65 integ.addSwitchingFunction(functions[l],
66 Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000);
67 }
68 integ.integrate(pb,
69 pb.getInitialTime(), pb.getInitialState(),
70 pb.getFinalTime(), new double[pb.getDimension()]);
71
72 double error = handler.getMaximalValueError();
73 if (i > 4) {
74 assertTrue(error < Math.abs(previousError));
75 }
76 previousError = error;
77 assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
78
79 }
80
81 }
82
83 }
84
85 public void testSmallStep()
86 throws DerivativeException, IntegratorException {
87
88 TestProblem1 pb = new TestProblem1();
89 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
90
91 FirstOrderIntegrator integ = new MidpointIntegrator(step);
92 TestProblemHandler handler = new TestProblemHandler(pb, integ);
93 integ.setStepHandler(handler);
94 integ.integrate(pb,
95 pb.getInitialTime(), pb.getInitialState(),
96 pb.getFinalTime(), new double[pb.getDimension()]);
97
98 assertTrue(handler.getLastError() < 2.0e-7);
99 assertTrue(handler.getMaximalValueError() < 1.0e-6);
100 assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
101 assertEquals("midpoint", integ.getName());
102
103 }
104
105 public void testBigStep()
106 throws DerivativeException, IntegratorException {
107
108 TestProblem1 pb = new TestProblem1();
109 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;
110
111 FirstOrderIntegrator integ = new MidpointIntegrator(step);
112 TestProblemHandler handler = new TestProblemHandler(pb, integ);
113 integ.setStepHandler(handler);
114 integ.integrate(pb,
115 pb.getInitialTime(), pb.getInitialState(),
116 pb.getFinalTime(), new double[pb.getDimension()]);
117
118 assertTrue(handler.getLastError() > 0.01);
119 assertTrue(handler.getMaximalValueError() > 0.05);
120 assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
121
122 }
123
124 public static Test suite() {
125 return new TestSuite(MidpointIntegratorTest.class);
126 }
127
128 }