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.nonstiff;
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.TestProblem3;
34  import org.apache.commons.math4.legacy.ode.sampling.StepHandler;
35  import org.apache.commons.math4.legacy.ode.sampling.StepInterpolatorTestUtils;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  public class LutherStepInterpolatorTest {
40  
41      @Test
42      public void derivativesConsistency()
43              throws DimensionMismatchException, NumberIsTooSmallException,
44              MaxCountExceededException, NoBracketingException {
45          TestProblem3 pb = new TestProblem3();
46          double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
47          LutherIntegrator integ = new LutherIntegrator(step);
48          StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 0.01, 6.5e-12);
49      }
50  
51      @Test
52      public void serialization()
53              throws IOException, ClassNotFoundException,
54              DimensionMismatchException, NumberIsTooSmallException,
55              MaxCountExceededException, NoBracketingException  {
56  
57          TestProblem3 pb = new TestProblem3(0.9);
58          double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
59          LutherIntegrator integ = new LutherIntegrator(step);
60          integ.addStepHandler(new ContinuousOutputModel());
61          integ.integrate(pb,
62                          pb.getInitialTime(), pb.getInitialState(),
63                          pb.getFinalTime(), new double[pb.getDimension()]);
64  
65          ByteArrayOutputStream bos = new ByteArrayOutputStream();
66          ObjectOutputStream    oos = new ObjectOutputStream(bos);
67          for (StepHandler handler : integ.getStepHandlers()) {
68              oos.writeObject(handler);
69          }
70  
71          Assert.assertTrue(bos.size() > 1200000);
72          Assert.assertTrue(bos.size() < 1250000);
73  
74          ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
75          ObjectInputStream     ois = new ObjectInputStream(bis);
76          ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();
77  
78          Random random = new Random(347588535632L);
79          double maxError = 0.0;
80          for (int i = 0; i < 1000; ++i) {
81              double r = random.nextDouble();
82              double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
83              cm.setInterpolatedTime(time);
84              double[] interpolatedY = cm.getInterpolatedState ();
85              double[] theoreticalY  = pb.computeTheoreticalState(time);
86              double dx = interpolatedY[0] - theoreticalY[0];
87              double dy = interpolatedY[1] - theoreticalY[1];
88              double error = dx * dx + dy * dy;
89              if (error > maxError) {
90                  maxError = error;
91              }
92          }
93  
94          Assert.assertTrue(maxError < 2.2e-7);
95      }
96  }