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.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  }