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;
22
import java.io.ByteArrayOutputStream;
23
import java.io.ByteArrayInputStream;
24
import java.io.ObjectOutputStream;
25
import java.io.ObjectInputStream;
26
import java.io.IOException;
28
import org.apache.commons.math.ode.ContinuousOutputModel;
29
import org.apache.commons.math.ode.DerivativeException;
30
import org.apache.commons.math.ode.EulerIntegrator;
31
import org.apache.commons.math.ode.EulerStepInterpolator;
32
import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
33
import org.apache.commons.math.ode.IntegratorException;
35
public class EulerStepInterpolatorTest
38
public EulerStepInterpolatorTest(String name) {
42
public void testNoReset() {
44
double[] y = { 0.0, 1.0, -2.0 };
45
double[][] yDot = { { 1.0, 2.0, -2.0 } };
46
EulerStepInterpolator interpolator = new EulerStepInterpolator();
47
interpolator.reinitialize(new DummyEquations(), y, yDot, true);
48
interpolator.storeTime(0);
50
interpolator.storeTime(1);
52
double[] result = interpolator.getInterpolatedState();
53
for (int i = 0; i < result.length; ++i) {
54
assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
59
public void testInterpolationAtBounds()
60
throws DerivativeException {
63
double[] y0 = {0.0, 1.0, -2.0};
65
double[] y = (double[]) y0.clone();
66
double[][] yDot = { new double[y0.length] };
67
EulerStepInterpolator interpolator = new EulerStepInterpolator();
68
interpolator.reinitialize(new DummyEquations(), y, yDot, true);
69
interpolator.storeTime(t0);
75
yDot[0][0] = (y[0] - y0[0]) / dt;
76
yDot[0][1] = (y[1] - y0[1]) / dt;
77
yDot[0][2] = (y[2] - y0[2]) / dt;
79
interpolator.storeTime(t0 + dt);
81
interpolator.setInterpolatedTime(interpolator.getPreviousTime());
82
double[] result = interpolator.getInterpolatedState();
83
for (int i = 0; i < result.length; ++i) {
84
assertTrue(Math.abs(result[i] - y0[i]) < 1.0e-10);
87
interpolator.setInterpolatedTime(interpolator.getCurrentTime());
88
result = interpolator.getInterpolatedState();
89
for (int i = 0; i < result.length; ++i) {
90
assertTrue(Math.abs(result[i] - y[i]) < 1.0e-10);
95
public void testInterpolationInside()
96
throws DerivativeException {
98
double[] y = { 1.0, 3.0, -4.0 };
99
double[][] yDot = { { 1.0, 2.0, -2.0 } };
100
EulerStepInterpolator interpolator = new EulerStepInterpolator();
101
interpolator.reinitialize(new DummyEquations(), y, yDot, true);
102
interpolator.storeTime(0);
103
interpolator.shift();
104
interpolator.storeTime(1);
106
interpolator.setInterpolatedTime(0.1);
107
double[] result = interpolator.getInterpolatedState();
108
assertTrue(Math.abs(result[0] - 0.1) < 1.0e-10);
109
assertTrue(Math.abs(result[1] - 1.2) < 1.0e-10);
110
assertTrue(Math.abs(result[2] + 2.2) < 1.0e-10);
112
interpolator.setInterpolatedTime(0.5);
113
result = interpolator.getInterpolatedState();
114
assertTrue(Math.abs(result[0] - 0.5) < 1.0e-10);
115
assertTrue(Math.abs(result[1] - 2.0) < 1.0e-10);
116
assertTrue(Math.abs(result[2] + 3.0) < 1.0e-10);
120
public void testSerialization()
121
throws DerivativeException, IntegratorException,
122
IOException, ClassNotFoundException {
124
TestProblem1 pb = new TestProblem1();
125
double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
126
EulerIntegrator integ = new EulerIntegrator(step);
127
integ.setStepHandler(new ContinuousOutputModel());
129
pb.getInitialTime(), pb.getInitialState(),
130
pb.getFinalTime(), new double[pb.getDimension()]);
132
ByteArrayOutputStream bos = new ByteArrayOutputStream();
133
ObjectOutputStream oos = new ObjectOutputStream(bos);
134
oos.writeObject(integ.getStepHandler());
136
assertTrue(bos.size () > 82000);
137
assertTrue(bos.size () < 83000);
139
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
140
ObjectInputStream ois = new ObjectInputStream(bis);
141
ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
143
Random random = new Random(347588535632l);
144
double maxError = 0.0;
145
for (int i = 0; i < 1000; ++i) {
146
double r = random.nextDouble();
147
double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
148
cm.setInterpolatedTime(time);
149
double[] interpolatedY = cm.getInterpolatedState ();
150
double[] theoreticalY = pb.computeTheoreticalState(time);
151
double dx = interpolatedY[0] - theoreticalY[0];
152
double dy = interpolatedY[1] - theoreticalY[1];
153
double error = dx * dx + dy * dy;
154
if (error > maxError) {
159
assertTrue(maxError < 0.001);
163
private static class DummyEquations
164
implements FirstOrderDifferentialEquations {
165
public int getDimension() {
168
public void computeDerivatives(double t, double[] y, double[] yDot) {
172
public static Test suite() {
173
return new TestSuite(EulerStepInterpolatorTest.class);