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