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.DormandPrince54Integrator;
31
import org.apache.commons.math.ode.IntegratorException;
33
public class DormandPrince54StepInterpolatorTest
36
public DormandPrince54StepInterpolatorTest(String name) {
40
public void testSerialization()
41
throws DerivativeException, IntegratorException,
42
IOException, ClassNotFoundException {
44
TestProblem3 pb = new TestProblem3(0.9);
46
double maxStep = pb.getFinalTime() - pb.getInitialTime();
47
double scalAbsoluteTolerance = 1.0e-8;
48
double scalRelativeTolerance = scalAbsoluteTolerance;
49
DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep,
50
scalAbsoluteTolerance,
51
scalRelativeTolerance);
52
integ.setStepHandler(new ContinuousOutputModel());
54
pb.getInitialTime(), pb.getInitialState(),
55
pb.getFinalTime(), new double[pb.getDimension()]);
57
ByteArrayOutputStream bos = new ByteArrayOutputStream();
58
ObjectOutputStream oos = new ObjectOutputStream(bos);
59
oos.writeObject(integ.getStepHandler());
61
assertTrue(bos.size () > 119500);
62
assertTrue(bos.size () < 120500);
64
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
65
ObjectInputStream ois = new ObjectInputStream(bis);
66
ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
68
Random random = new Random(347588535632l);
69
double maxError = 0.0;
70
for (int i = 0; i < 1000; ++i) {
71
double r = random.nextDouble();
72
double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
73
cm.setInterpolatedTime(time);
74
double[] interpolatedY = cm.getInterpolatedState ();
75
double[] theoreticalY = pb.computeTheoreticalState(time);
76
double dx = interpolatedY[0] - theoreticalY[0];
77
double dy = interpolatedY[1] - theoreticalY[1];
78
double error = dx * dx + dy * dy;
79
if (error > maxError) {
84
assertTrue(maxError < 7.0e-10);
88
public void testClone()
89
throws DerivativeException, IntegratorException {
90
TestProblem3 pb = new TestProblem3(0.9);
92
double maxStep = pb.getFinalTime() - pb.getInitialTime();
93
double scalAbsoluteTolerance = 1.0e-8;
94
double scalRelativeTolerance = scalAbsoluteTolerance;
95
DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep,
96
scalAbsoluteTolerance,
97
scalRelativeTolerance);
98
integ.setStepHandler(new StepHandler() {
99
public void handleStep(StepInterpolator interpolator, boolean isLast)
100
throws DerivativeException {
101
StepInterpolator cloned = interpolator.copy();
102
double tA = cloned.getPreviousTime();
103
double tB = cloned.getCurrentTime();
104
double halfStep = Math.abs(tB - tA) / 2;
105
assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
106
assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
107
for (int i = 0; i < 10; ++i) {
108
double t = (i * tB + (9 - i) * tA) / 9;
109
interpolator.setInterpolatedTime(t);
110
assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
111
cloned.setInterpolatedTime(t);
112
assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
113
double[] referenceState = interpolator.getInterpolatedState();
114
double[] cloneState = cloned.getInterpolatedState();
115
for (int j = 0; j < referenceState.length; ++j) {
116
assertEquals(referenceState[j], cloneState[j], 1.0e-12);
120
public boolean requiresDenseOutput() {
123
public void reset() {
127
pb.getInitialTime(), pb.getInitialState(),
128
pb.getFinalTime(), new double[pb.getDimension()]);
132
public static Test suite() {
133
return new TestSuite(DormandPrince54StepInterpolatorTest.class);