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