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 import java.io.ByteArrayOutputStream;
23 import java.io.ByteArrayInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.ObjectInputStream;
26 import java.io.IOException;
27
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;
32
33 public class DormandPrince54StepInterpolatorTest
34 extends TestCase {
35
36 public DormandPrince54StepInterpolatorTest(String name) {
37 super(name);
38 }
39
40 public void testSerialization()
41 throws DerivativeException, IntegratorException,
42 IOException, ClassNotFoundException {
43
44 TestProblem3 pb = new TestProblem3(0.9);
45 double minStep = 0;
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());
53 integ.integrate(pb,
54 pb.getInitialTime(), pb.getInitialState(),
55 pb.getFinalTime(), new double[pb.getDimension()]);
56
57 ByteArrayOutputStream bos = new ByteArrayOutputStream();
58 ObjectOutputStream oos = new ObjectOutputStream(bos);
59 oos.writeObject(integ.getStepHandler());
60
61 assertTrue(bos.size () > 119500);
62 assertTrue(bos.size () < 120500);
63
64 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
65 ObjectInputStream ois = new ObjectInputStream(bis);
66 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
67
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) {
80 maxError = error;
81 }
82 }
83
84 assertTrue(maxError < 7.0e-10);
85
86 }
87
88 public void testClone()
89 throws DerivativeException, IntegratorException {
90 TestProblem3 pb = new TestProblem3(0.9);
91 double minStep = 0;
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);
117 }
118 }
119 }
120 public boolean requiresDenseOutput() {
121 return true;
122 }
123 public void reset() {
124 }
125 });
126 integ.integrate(pb,
127 pb.getInitialTime(), pb.getInitialState(),
128 pb.getFinalTime(), new double[pb.getDimension()]);
129
130 }
131
132 public static Test suite() {
133 return new TestSuite(DormandPrince54StepInterpolatorTest.class);
134 }
135
136 }