2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.ode;
20
import junit.framework.*;
21
import java.util.Random;
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;
32
public class ContinuousOutputModelTest
35
public ContinuousOutputModelTest(String name) {
41
public void testBoundaries()
42
throws DerivativeException, IntegratorException {
43
integ.addStepHandler(new ContinuousOutputModel());
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()));
53
public void testRandomAccess()
54
throws DerivativeException, IntegratorException {
56
ContinuousOutputModel cm = new ContinuousOutputModel();
57
integ.addStepHandler(cm);
59
pb.getInitialTime(), pb.getInitialState(),
60
pb.getFinalTime(), new double[pb.getDimension()]);
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) {
78
assertTrue(maxError < 1.0e-9);
82
public void testModelsMerging()
83
throws DerivativeException, IntegratorException {
85
// theoretical solution: y[0] = cos(t), y[1] = sin(t)
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 {
94
public int getDimension() {
99
// integrate backward from π to 0;
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 },
107
// integrate backward from 2π to π
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]);
115
// merge the two half circles
116
ContinuousOutputModel cm = new ContinuousOutputModel();
118
cm.append(new ContinuousOutputModel());
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);
134
public void testErrorConditions()
135
throws DerivativeException {
137
ContinuousOutputModel cm = new ContinuousOutputModel();
138
cm.handleStep(buildInterpolator(0, new double[] { 0.0, 1.0, -2.0 }, 1), true);
140
// dimension mismatch
141
assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0 }, 2.0));
143
// hole between time ranges
144
assertTrue(checkAppendError(cm, 10.0, new double[] { 0.0, 1.0, -2.0 }, 20.0));
146
// propagation direction mismatch
147
assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 0.0));
150
assertFalse(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 2.0));
154
private boolean checkAppendError(ContinuousOutputModel cm,
155
double t0, double[] y0, double t1)
156
throws DerivativeException {
158
ContinuousOutputModel otherCm = new ContinuousOutputModel();
159
otherCm.handleStep(buildInterpolator(t0, y0, t1), true);
161
} catch(IllegalArgumentException iae) {
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);
176
public void checkValue(double value, double reference) {
177
assertTrue(Math.abs(value - reference) < 1.0e-10);
180
public static Test suite() {
181
return new TestSuite(ContinuousOutputModelTest.class);
185
public void setUp() {
186
pb = new TestProblem3(0.9);
188
double maxStep = pb.getFinalTime() - pb.getInitialTime();
189
integ = new DormandPrince54Integrator(minStep, maxStep, 1.0e-8, 1.0e-8);
193
public void tearDown() {
199
FirstOrderIntegrator integ;