1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode.nonstiff;
19
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.Random;
23 import java.io.ByteArrayOutputStream;
24 import java.io.ByteArrayInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.ObjectInputStream;
27 import java.io.IOException;
28
29 import org.apache.commons.math.ode.ContinuousOutputModel;
30 import org.apache.commons.math.ode.DerivativeException;
31 import org.apache.commons.math.ode.IntegratorException;
32 import org.apache.commons.math.ode.TestProblem3;
33 import org.apache.commons.math.ode.nonstiff.GillIntegrator;
34 import org.apache.commons.math.ode.sampling.StepHandler;
35 import org.apache.commons.math.ode.sampling.StepInterpolatorTestUtils;
36 import org.junit.Test;
37
38 public class GillStepInterpolatorTest {
39
40 @Test
41 public void testDerivativesConsistency()
42 throws DerivativeException, IntegratorException {
43 TestProblem3 pb = new TestProblem3();
44 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
45 GillIntegrator integ = new GillIntegrator(step);
46 StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10);
47 }
48
49 @Test
50 public void serialization()
51 throws DerivativeException, IntegratorException,
52 IOException, ClassNotFoundException {
53
54 TestProblem3 pb = new TestProblem3(0.9);
55 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
56 GillIntegrator integ = new GillIntegrator(step);
57 integ.addStepHandler(new ContinuousOutputModel());
58 integ.integrate(pb,
59 pb.getInitialTime(), pb.getInitialState(),
60 pb.getFinalTime(), new double[pb.getDimension()]);
61
62 ByteArrayOutputStream bos = new ByteArrayOutputStream();
63 ObjectOutputStream oos = new ObjectOutputStream(bos);
64 for (StepHandler handler : integ.getStepHandlers()) {
65 oos.writeObject(handler);
66 }
67
68 assertTrue(bos.size () > 700000);
69 assertTrue(bos.size () < 701000);
70
71 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
72 ObjectInputStream ois = new ObjectInputStream(bis);
73 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
74
75 Random random = new Random(347588535632l);
76 double maxError = 0.0;
77 for (int i = 0; i < 1000; ++i) {
78 double r = random.nextDouble();
79 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
80 cm.setInterpolatedTime(time);
81 double[] interpolatedY = cm.getInterpolatedState ();
82 double[] theoreticalY = pb.computeTheoreticalState(time);
83 double dx = interpolatedY[0] - theoreticalY[0];
84 double dy = interpolatedY[1] - theoreticalY[1];
85 double error = dx * dx + dy * dy;
86 if (error > maxError) {
87 maxError = error;
88 }
89 }
90
91 assertTrue(maxError < 0.003);
92
93 }
94
95 }