View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.TestProblem1;
34  import org.apache.commons.math4.legacy.ode.TestProblem3;
35  import org.apache.commons.math4.legacy.ode.sampling.StepHandler;
36  import org.apache.commons.math4.legacy.ode.sampling.StepInterpolatorTestUtils;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  public class MidpointStepInterpolatorTest {
41  
42    @Test
43    public void testDerivativesConsistency()
44        throws DimensionMismatchException, NumberIsTooSmallException,
45               MaxCountExceededException, NoBracketingException {
46      TestProblem3 pb = new TestProblem3();
47      double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
48      MidpointIntegrator integ = new MidpointIntegrator(step);
49      StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 0.01, 6.6e-12);
50    }
51  
52    @Test
53    public void serialization()
54      throws IOException, ClassNotFoundException,
55             DimensionMismatchException, NumberIsTooSmallException,
56             MaxCountExceededException, NoBracketingException {
57  
58      TestProblem1 pb = new TestProblem1();
59      double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
60      MidpointIntegrator integ = new MidpointIntegrator(step);
61      integ.addStepHandler(new ContinuousOutputModel());
62      integ.integrate(pb,
63                      pb.getInitialTime(), pb.getInitialState(),
64                      pb.getFinalTime(), new double[pb.getDimension()]);
65  
66      ByteArrayOutputStream bos = new ByteArrayOutputStream();
67      ObjectOutputStream    oos = new ObjectOutputStream(bos);
68      for (StepHandler handler : integ.getStepHandlers()) {
69          oos.writeObject(handler);
70      }
71  
72      Assert.assertTrue(bos.size () > 135000);
73      Assert.assertTrue(bos.size () < 145000);
74  
75      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
76      ObjectInputStream     ois = new ObjectInputStream(bis);
77      ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();
78  
79      Random random = new Random(347588535632L);
80      double maxError = 0.0;
81      for (int i = 0; i < 1000; ++i) {
82        double r = random.nextDouble();
83        double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
84        cm.setInterpolatedTime(time);
85        double[] interpolatedY = cm.getInterpolatedState ();
86        double[] theoreticalY  = pb.computeTheoreticalState(time);
87        double dx = interpolatedY[0] - theoreticalY[0];
88        double dy = interpolatedY[1] - theoreticalY[1];
89        double error = dx * dx + dy * dy;
90        if (error > maxError) {
91          maxError = error;
92        }
93      }
94  
95      Assert.assertTrue(maxError < 1.0e-6);
96    }
97  }