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.IntegratorException;
31 import org.apache.commons.math.ode.MidpointIntegrator;
32
33 public class MidpointStepInterpolatorTest
34 extends TestCase {
35
36 public MidpointStepInterpolatorTest(String name) {
37 super(name);
38 }
39
40 public void testSerialization()
41 throws DerivativeException, IntegratorException,
42 IOException, ClassNotFoundException {
43
44 TestProblem1 pb = new TestProblem1();
45 double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
46 MidpointIntegrator integ = new MidpointIntegrator(step);
47 integ.setStepHandler(new ContinuousOutputModel());
48 integ.integrate(pb,
49 pb.getInitialTime(), pb.getInitialState(),
50 pb.getFinalTime(), new double[pb.getDimension()]);
51
52 ByteArrayOutputStream bos = new ByteArrayOutputStream();
53 ObjectOutputStream oos = new ObjectOutputStream(bos);
54 oos.writeObject(integ.getStepHandler());
55
56 assertTrue(bos.size () > 98000);
57 assertTrue(bos.size () < 99000);
58
59 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
60 ObjectInputStream ois = new ObjectInputStream(bis);
61 ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject();
62
63 Random random = new Random(347588535632l);
64 double maxError = 0.0;
65 for (int i = 0; i < 1000; ++i) {
66 double r = random.nextDouble();
67 double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
68 cm.setInterpolatedTime(time);
69 double[] interpolatedY = cm.getInterpolatedState ();
70 double[] theoreticalY = pb.computeTheoreticalState(time);
71 double dx = interpolatedY[0] - theoreticalY[0];
72 double dy = interpolatedY[1] - theoreticalY[1];
73 double error = dx * dx + dy * dy;
74 if (error > maxError) {
75 maxError = error;
76 }
77 }
78
79 assertTrue(maxError < 1.0e-6);
80
81 }
82
83 public static Test suite() {
84 return new TestSuite(MidpointStepInterpolatorTest.class);
85 }
86
87 }