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.math.ode.nonstiff;
19  
20  
21  /**
22   * This class implements the 5(4) Higham and Hall integrator for
23   * Ordinary Differential Equations.
24   *
25   * <p>This integrator is an embedded Runge-Kutta integrator
26   * of order 5(4) used in local extrapolation mode (i.e. the solution
27   * is computed using the high order formula) with stepsize control
28   * (and automatic step initialization) and continuous output. This
29   * method uses 7 functions evaluations per step.</p>
30   *
31   * @version $Revision: 786881 $ $Date: 2009-06-20 14:53:08 -0400 (Sat, 20 Jun 2009) $
32   * @since 1.2
33   */
34  
35  public class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator {
36  
37    /** Integrator method name. */
38    private static final String METHOD_NAME = "Higham-Hall 5(4)";
39  
40    /** Time steps Butcher array. */
41    private static final double[] staticC = {
42      2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0
43    };
44  
45    /** Internal weights Butcher array. */
46    private static final double[][] staticA = {
47      {2.0/9.0},
48      {1.0/12.0, 1.0/4.0},
49      {1.0/8.0, 0.0, 3.0/8.0},
50      {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0},
51      {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0},
52      {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0}
53    };
54  
55    /** Propagation weights Butcher array. */
56    private static final double[] staticB = {
57      1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0
58    };
59  
60    /** Error weights Butcher array. */
61    private static final double[] staticE = {
62      -1.0/20.0, 0.0, 81.0/160.0, -6.0/5.0, 25.0/32.0, 1.0/16.0, -1.0/10.0
63    };
64  
65    /** Simple constructor.
66     * Build a fifth order Higham and Hall integrator with the given step bounds
67     * @param minStep minimal step (must be positive even for backward
68     * integration), the last step can be smaller than this
69     * @param maxStep maximal step (must be positive even for backward
70     * integration)
71     * @param scalAbsoluteTolerance allowed absolute error
72     * @param scalRelativeTolerance allowed relative error
73     */
74    public HighamHall54Integrator(final double minStep, final double maxStep,
75                                  final double scalAbsoluteTolerance,
76                                  final double scalRelativeTolerance) {
77      super(METHOD_NAME, false, staticC, staticA, staticB, new HighamHall54StepInterpolator(),
78            minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
79    }
80  
81    /** Simple constructor.
82     * Build a fifth order Higham and Hall integrator with the given step bounds
83     * @param minStep minimal step (must be positive even for backward
84     * integration), the last step can be smaller than this
85     * @param maxStep maximal step (must be positive even for backward
86     * integration)
87     * @param vecAbsoluteTolerance allowed absolute error
88     * @param vecRelativeTolerance allowed relative error
89     */
90    public HighamHall54Integrator(final double minStep, final double maxStep,
91                                  final double[] vecAbsoluteTolerance,
92                                  final double[] vecRelativeTolerance) {
93      super(METHOD_NAME, false, staticC, staticA, staticB, new HighamHall54StepInterpolator(),
94            minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
95    }
96  
97    /** {@inheritDoc} */
98    @Override
99    public int getOrder() {
100     return 5;
101   }
102 
103   /** {@inheritDoc} */
104   @Override
105   protected double estimateError(final double[][] yDotK,
106                                  final double[] y0, final double[] y1,
107                                  final double h) {
108 
109     double error = 0;
110 
111     for (int j = 0; j < y0.length; ++j) {
112       double errSum = staticE[0] * yDotK[0][j];
113       for (int l = 1; l < staticE.length; ++l) {
114         errSum += staticE[l] * yDotK[l][j];
115       }
116 
117       final double yScale = Math.max(Math.abs(y0[j]), Math.abs(y1[j]));
118       final double tol = (vecAbsoluteTolerance == null) ?
119                          (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
120                          (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
121       final double ratio  = h * errSum / tol;
122       error += ratio * ratio;
123 
124     }
125 
126     return Math.sqrt(error / y0.length);
127 
128   }
129 
130 }