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.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 GillStepInterpolatorTest {
40  
41    @Test
42    public void testDerivativesConsistency()
43        throws DimensionMismatchException, NumberIsTooSmallException,
44               MaxCountExceededException, NoBracketingException {
45      TestProblem3 pb = new TestProblem3();
46      double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
47      GillIntegrator integ = new GillIntegrator(step);
48      StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 0.01, 6.6e-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      GillIntegrator integ = new GillIntegrator(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 () > 880000);
72      Assert.assertTrue(bos.size () < 900000);
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 < 0.003);
95    }
96  }