1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.ode.nonstiff;
19
20
21 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
22 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
23 import org.apache.commons.math4.legacy.exception.NoBracketingException;
24 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
25 import org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations;
26 import org.apache.commons.math4.legacy.ode.FirstOrderIntegrator;
27 import org.apache.commons.math4.legacy.ode.TestProblem1;
28 import org.apache.commons.math4.legacy.ode.TestProblem2;
29 import org.apache.commons.math4.legacy.ode.TestProblem3;
30 import org.apache.commons.math4.legacy.ode.TestProblem4;
31 import org.apache.commons.math4.legacy.ode.TestProblem5;
32 import org.apache.commons.math4.legacy.ode.TestProblem6;
33 import org.apache.commons.math4.legacy.ode.TestProblemAbstract;
34 import org.apache.commons.math4.legacy.ode.TestProblemHandler;
35 import org.apache.commons.math4.legacy.ode.events.EventHandler;
36 import org.apache.commons.math4.legacy.ode.sampling.StepHandler;
37 import org.apache.commons.math4.legacy.ode.sampling.StepInterpolator;
38 import org.apache.commons.math4.core.jdkmath.JdkMath;
39 import org.junit.Assert;
40 import org.junit.Test;
41
42 public class ThreeEighthesIntegratorTest {
43
44 @Test(expected=DimensionMismatchException.class)
45 public void testDimensionCheck()
46 throws DimensionMismatchException, NumberIsTooSmallException,
47 MaxCountExceededException, NoBracketingException {
48 TestProblem1 pb = new TestProblem1();
49 new ThreeEighthesIntegrator(0.01).integrate(pb,
50 0.0, new double[pb.getDimension()+10],
51 1.0, new double[pb.getDimension()+10]);
52 Assert.fail("an exception should have been thrown");
53 }
54
55 @Test
56 public void testDecreasingSteps()
57 throws DimensionMismatchException, NumberIsTooSmallException,
58 MaxCountExceededException, NoBracketingException {
59
60 for (TestProblemAbstract pb : new TestProblemAbstract[] {
61 new TestProblem1(), new TestProblem2(), new TestProblem3(),
62 new TestProblem4(), new TestProblem5(), new TestProblem6()
63 }) {
64
65 double previousValueError = Double.NaN;
66 double previousTimeError = Double.NaN;
67 for (int i = 4; i < 10; ++i) {
68
69 double step = (pb.getFinalTime() - pb.getInitialTime()) * JdkMath.pow(2.0, -i);
70
71 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
72 TestProblemHandler handler = new TestProblemHandler(pb, integ);
73 integ.addStepHandler(handler);
74 EventHandler[] functions = pb.getEventsHandlers();
75 for (int l = 0; l < functions.length; ++l) {
76 integ.addEventHandler(functions[l],
77 Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000);
78 }
79 double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
80 pb.getFinalTime(), new double[pb.getDimension()]);
81 if (functions.length == 0) {
82 Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10);
83 }
84
85 double error = handler.getMaximalValueError();
86 if (i > 4) {
87 Assert.assertTrue(error < 1.01 * JdkMath.abs(previousValueError));
88 }
89 previousValueError = error;
90
91 double timeError = handler.getMaximalTimeError();
92 if (i > 4) {
93 Assert.assertTrue(timeError <= JdkMath.abs(previousTimeError));
94 }
95 previousTimeError = timeError;
96 }
97 }
98 }
99
100 @Test
101 public void testSmallStep()
102 throws DimensionMismatchException, NumberIsTooSmallException,
103 MaxCountExceededException, NoBracketingException {
104
105 TestProblem1 pb = new TestProblem1();
106 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
107
108 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
109 TestProblemHandler handler = new TestProblemHandler(pb, integ);
110 integ.addStepHandler(handler);
111 integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
112 pb.getFinalTime(), new double[pb.getDimension()]);
113
114 Assert.assertTrue(handler.getLastError() < 2.0e-13);
115 Assert.assertTrue(handler.getMaximalValueError() < 4.0e-12);
116 Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
117 Assert.assertEquals("3/8", integ.getName());
118 }
119
120 @Test
121 public void testBigStep()
122 throws DimensionMismatchException, NumberIsTooSmallException,
123 MaxCountExceededException, NoBracketingException {
124
125 TestProblem1 pb = new TestProblem1();
126 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2;
127
128 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
129 TestProblemHandler handler = new TestProblemHandler(pb, integ);
130 integ.addStepHandler(handler);
131 integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
132 pb.getFinalTime(), new double[pb.getDimension()]);
133
134 Assert.assertTrue(handler.getLastError() > 0.0004);
135 Assert.assertTrue(handler.getMaximalValueError() > 0.005);
136 Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
137 }
138
139 @Test
140 public void testBackward()
141 throws DimensionMismatchException, NumberIsTooSmallException,
142 MaxCountExceededException, NoBracketingException {
143
144 TestProblem5 pb = new TestProblem5();
145 double step = JdkMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;
146
147 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
148 TestProblemHandler handler = new TestProblemHandler(pb, integ);
149 integ.addStepHandler(handler);
150 integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
151 pb.getFinalTime(), new double[pb.getDimension()]);
152
153 Assert.assertTrue(handler.getLastError() < 5.0e-10);
154 Assert.assertTrue(handler.getMaximalValueError() < 7.0e-10);
155 Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
156 Assert.assertEquals("3/8", integ.getName());
157 }
158
159 @Test
160 public void testKepler()
161 throws DimensionMismatchException, NumberIsTooSmallException,
162 MaxCountExceededException, NoBracketingException {
163
164 final TestProblem3 pb = new TestProblem3(0.9);
165 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
166
167 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
168 integ.addStepHandler(new KeplerHandler(pb));
169 integ.integrate(pb,
170 pb.getInitialTime(), pb.getInitialState(),
171 pb.getFinalTime(), new double[pb.getDimension()]);
172 }
173
174 private static final class KeplerHandler implements StepHandler {
175
176 KeplerHandler(TestProblem3 pb) {
177 this.pb = pb;
178 maxError = 0;
179 }
180
181 @Override
182 public void init(double t0, double[] y0, double t) {
183 maxError = 0;
184 }
185
186 @Override
187 public void handleStep(StepInterpolator interpolator, boolean isLast)
188 throws MaxCountExceededException {
189
190 double[] interpolatedY = interpolator.getInterpolatedState();
191 double[] theoreticalY = pb.computeTheoreticalState(interpolator.getCurrentTime());
192 double dx = interpolatedY[0] - theoreticalY[0];
193 double dy = interpolatedY[1] - theoreticalY[1];
194 double error = dx * dx + dy * dy;
195 if (error > maxError) {
196 maxError = error;
197 }
198 if (isLast) {
199
200
201
202 Assert.assertTrue(maxError > 0.005);
203 }
204 }
205
206 private TestProblem3 pb;
207 private double maxError = 0;
208 }
209
210 @Test
211 public void testStepSize()
212 throws DimensionMismatchException, NumberIsTooSmallException,
213 MaxCountExceededException, NoBracketingException {
214 final double step = 1.23456;
215 FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
216 integ.addStepHandler(new StepHandler() {
217 @Override
218 public void handleStep(StepInterpolator interpolator, boolean isLast) {
219 if (! isLast) {
220 Assert.assertEquals(step,
221 interpolator.getCurrentTime() - interpolator.getPreviousTime(),
222 1.0e-12);
223 }
224 }
225 @Override
226 public void init(double t0, double[] y0, double t) {
227 }
228 });
229 integ.integrate(new FirstOrderDifferentialEquations() {
230 @Override
231 public void computeDerivatives(double t, double[] y, double[] dot) {
232 dot[0] = 1.0;
233 }
234 @Override
235 public int getDimension() {
236 return 1;
237 }
238 }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
239 }
240 }