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