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 import java.util.Random;
22
23 import org.apache.commons.math.ode.ContinuousOutputModel;
24 import org.apache.commons.math.ode.DerivativeException;
25 import org.apache.commons.math.ode.FirstOrderIntegrator;
26 import org.apache.commons.math.ode.IntegratorException;
27 import org.apache.commons.math.ode.nonstiff.DormandPrince54Integrator;
28 import org.apache.commons.math.ode.nonstiff.DormandPrince853Integrator;
29 import org.apache.commons.math.ode.sampling.DummyStepInterpolator;
30 import org.apache.commons.math.ode.sampling.StepInterpolator;
31
32 public class ContinuousOutputModelTest
33 extends TestCase {
34
35 public ContinuousOutputModelTest(String name) {
36 super(name);
37 pb = null;
38 integ = null;
39 }
40
41 public void testBoundaries()
42 throws DerivativeException, IntegratorException {
43 integ.addStepHandler(new ContinuousOutputModel());
44 integ.integrate(pb,
45 pb.getInitialTime(), pb.getInitialState(),
46 pb.getFinalTime(), new double[pb.getDimension()]);
47 ContinuousOutputModel cm = (ContinuousOutputModel) integ.getStepHandlers().iterator().next();
48 cm.setInterpolatedTime(2.0 * pb.getInitialTime() - pb.getFinalTime());
49 cm.setInterpolatedTime(2.0 * pb.getFinalTime() - pb.getInitialTime());
50 cm.setInterpolatedTime(0.5 * (pb.getFinalTime() + pb.getInitialTime()));
51 }
52
53 public void testRandomAccess()
54 throws DerivativeException, IntegratorException {
55
56 ContinuousOutputModel cm = new ContinuousOutputModel();
57 integ.addStepHandler(cm);
58 integ.integrate(pb,
59 pb.getInitialTime(), pb.getInitialState(),
60 pb.getFinalTime(), new double[pb.getDimension()]);
61
62 Random random = new Random(347588535632l);
63 double maxError = 0.0;
64 for (int i = 0; i < 1000; ++i) {
65 double r = random.nextDouble();
66 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
67 cm.setInterpolatedTime(time);
68 double[] interpolatedY = cm.getInterpolatedState ();
69 double[] theoreticalY = pb.computeTheoreticalState(time);
70 double dx = interpolatedY[0] - theoreticalY[0];
71 double dy = interpolatedY[1] - theoreticalY[1];
72 double error = dx * dx + dy * dy;
73 if (error > maxError) {
74 maxError = error;
75 }
76 }
77
78 assertTrue(maxError < 1.0e-9);
79
80 }
81
82 public void testModelsMerging()
83 throws DerivativeException, IntegratorException {
84
85
86 FirstOrderDifferentialEquations problem =
87 new FirstOrderDifferentialEquations() {
88 private static final long serialVersionUID = 2472449657345878299L;
89 public void computeDerivatives(double t, double[] y, double[] dot)
90 throws DerivativeException {
91 dot[0] = -y[1];
92 dot[1] = y[0];
93 }
94 public int getDimension() {
95 return 2;
96 }
97 };
98
99
100 ContinuousOutputModel cm1 = new ContinuousOutputModel();
101 FirstOrderIntegrator integ1 =
102 new DormandPrince853Integrator(0, 1.0, 1.0e-8, 1.0e-8);
103 integ1.addStepHandler(cm1);
104 integ1.integrate(problem, Math.PI, new double[] { -1.0, 0.0 },
105 0, new double[2]);
106
107
108 ContinuousOutputModel cm2 = new ContinuousOutputModel();
109 FirstOrderIntegrator integ2 =
110 new DormandPrince853Integrator(0, 0.1, 1.0e-12, 1.0e-12);
111 integ2.addStepHandler(cm2);
112 integ2.integrate(problem, 2.0 * Math.PI, new double[] { 1.0, 0.0 },
113 Math.PI, new double[2]);
114
115
116 ContinuousOutputModel cm = new ContinuousOutputModel();
117 cm.append(cm2);
118 cm.append(new ContinuousOutputModel());
119 cm.append(cm1);
120
121
122 assertEquals(2.0 * Math.PI, cm.getInitialTime(), 1.0e-12);
123 assertEquals(0, cm.getFinalTime(), 1.0e-12);
124 assertEquals(cm.getFinalTime(), cm.getInterpolatedTime(), 1.0e-12);
125 for (double t = 0; t < 2.0 * Math.PI; t += 0.1) {
126 cm.setInterpolatedTime(t);
127 double[] y = cm.getInterpolatedState();
128 assertEquals(Math.cos(t), y[0], 1.0e-7);
129 assertEquals(Math.sin(t), y[1], 1.0e-7);
130 }
131
132 }
133
134 public void testErrorConditions()
135 throws DerivativeException {
136
137 ContinuousOutputModel cm = new ContinuousOutputModel();
138 cm.handleStep(buildInterpolator(0, new double[] { 0.0, 1.0, -2.0 }, 1), true);
139
140
141 assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0 }, 2.0));
142
143
144 assertTrue(checkAppendError(cm, 10.0, new double[] { 0.0, 1.0, -2.0 }, 20.0));
145
146
147 assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 0.0));
148
149
150 assertFalse(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 2.0));
151
152 }
153
154 private boolean checkAppendError(ContinuousOutputModel cm,
155 double t0, double[] y0, double t1)
156 throws DerivativeException {
157 try {
158 ContinuousOutputModel otherCm = new ContinuousOutputModel();
159 otherCm.handleStep(buildInterpolator(t0, y0, t1), true);
160 cm.append(otherCm);
161 } catch(IllegalArgumentException iae) {
162
163 return true;
164 }
165 return false;
166 }
167
168 private StepInterpolator buildInterpolator(double t0, double[] y0, double t1) {
169 DummyStepInterpolator interpolator = new DummyStepInterpolator(y0, t1 >= t0);
170 interpolator.storeTime(t0);
171 interpolator.shift();
172 interpolator.storeTime(t1);
173 return interpolator;
174 }
175
176 public void checkValue(double value, double reference) {
177 assertTrue(Math.abs(value - reference) < 1.0e-10);
178 }
179
180 public static Test suite() {
181 return new TestSuite(ContinuousOutputModelTest.class);
182 }
183
184 @Override
185 public void setUp() {
186 pb = new TestProblem3(0.9);
187 double minStep = 0;
188 double maxStep = pb.getFinalTime() - pb.getInitialTime();
189 integ = new DormandPrince54Integrator(minStep, maxStep, 1.0e-8, 1.0e-8);
190 }
191
192 @Override
193 public void tearDown() {
194 pb = null;
195 integ = null;
196 }
197
198 TestProblem3 pb;
199 FirstOrderIntegrator integ;
200
201 }