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.math.ode.nonstiff;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.IOException;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.util.Random;
29  
30  import org.apache.commons.math.ode.ContinuousOutputModel;
31  import org.apache.commons.math.ode.DerivativeException;
32  import org.apache.commons.math.ode.IntegratorException;
33  import org.apache.commons.math.ode.TestProblem3;
34  import org.apache.commons.math.ode.sampling.StepHandler;
35  import org.apache.commons.math.ode.sampling.StepInterpolator;
36  import org.apache.commons.math.ode.sampling.StepInterpolatorTestUtils;
37  import org.junit.Test;
38  
39  public class DormandPrince853StepInterpolatorTest {
40  
41    @Test
42    public void derivativesConsistency()
43    throws DerivativeException, IntegratorException {
44      TestProblem3 pb = new TestProblem3(0.1);
45      double minStep = 0;
46      double maxStep = pb.getFinalTime() - pb.getInitialTime();
47      double scalAbsoluteTolerance = 1.0e-8;
48      double scalRelativeTolerance = scalAbsoluteTolerance;
49      DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep,
50                                                                        scalAbsoluteTolerance,
51                                                                        scalRelativeTolerance);
52      StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10);
53    }
54  
55    @Test
56    public void serialization()
57      throws DerivativeException, IntegratorException,
58             IOException, ClassNotFoundException {
59  
60      TestProblem3 pb = new TestProblem3(0.9);
61      double minStep = 0;
62      double maxStep = pb.getFinalTime() - pb.getInitialTime();
63      double scalAbsoluteTolerance = 1.0e-8;
64      double scalRelativeTolerance = scalAbsoluteTolerance;
65      DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep,
66                                                                        scalAbsoluteTolerance,
67                                                                        scalRelativeTolerance);
68      integ.addStepHandler(new ContinuousOutputModel());
69      integ.integrate(pb,
70                      pb.getInitialTime(), pb.getInitialState(),
71                      pb.getFinalTime(), new double[pb.getDimension()]);
72  
73      ByteArrayOutputStream bos = new ByteArrayOutputStream();
74      ObjectOutputStream    oos = new ObjectOutputStream(bos);
75      for (StepHandler handler : integ.getStepHandlers()) {
76          oos.writeObject(handler);
77      }
78  
79      assertTrue(bos.size () > 86000);
80      assertTrue(bos.size () < 87000);
81  
82      ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
83      ObjectInputStream     ois = new ObjectInputStream(bis);
84      ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();
85  
86      Random random = new Random(347588535632l);
87      double maxError = 0.0;
88      for (int i = 0; i < 1000; ++i) {
89        double r = random.nextDouble();
90        double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
91        cm.setInterpolatedTime(time);
92        double[] interpolatedY = cm.getInterpolatedState ();
93        double[] theoreticalY  = pb.computeTheoreticalState(time);
94        double dx = interpolatedY[0] - theoreticalY[0];
95        double dy = interpolatedY[1] - theoreticalY[1];
96        double error = dx * dx + dy * dy;
97        if (error > maxError) {
98          maxError = error;
99        }
100     }
101 
102     assertTrue(maxError < 2.4e-10);
103 
104   }
105 
106   @Test
107   public void checklone()
108   throws DerivativeException, IntegratorException {
109     TestProblem3 pb = new TestProblem3(0.9);
110     double minStep = 0;
111     double maxStep = pb.getFinalTime() - pb.getInitialTime();
112     double scalAbsoluteTolerance = 1.0e-8;
113     double scalRelativeTolerance = scalAbsoluteTolerance;
114     DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep,
115                                                                       scalAbsoluteTolerance,
116                                                                       scalRelativeTolerance);
117     integ.addStepHandler(new StepHandler() {
118         public void handleStep(StepInterpolator interpolator, boolean isLast)
119         throws DerivativeException {
120             StepInterpolator cloned = interpolator.copy();
121             double tA = cloned.getPreviousTime();
122             double tB = cloned.getCurrentTime();
123             double halfStep = Math.abs(tB - tA) / 2;
124             assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
125             assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
126             for (int i = 0; i < 10; ++i) {
127                 double t = (i * tB + (9 - i) * tA) / 9;
128                 interpolator.setInterpolatedTime(t);
129                 assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
130                 cloned.setInterpolatedTime(t);
131                 assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
132                 double[] referenceState = interpolator.getInterpolatedState();
133                 double[] cloneState     = cloned.getInterpolatedState();
134                 for (int j = 0; j < referenceState.length; ++j) {
135                     assertEquals(referenceState[j], cloneState[j], 1.0e-12);
136                 }
137             }
138         }
139         public boolean requiresDenseOutput() {
140             return true;
141         }
142         public void reset() {
143         }
144     });
145     integ.integrate(pb,
146             pb.getInitialTime(), pb.getInitialState(),
147             pb.getFinalTime(), new double[pb.getDimension()]);
148 
149   }
150 
151 }