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