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;
19  
20  /**
21   * This class implements the 5(4) Higham and Hall integrator for
22   * Ordinary Differential Equations.
23   *
24   * <p>This integrator is an embedded Runge-Kutta integrator
25   * of order 5(4) used in local extrapolation mode (i.e. the solution
26   * is computed using the high order formula) with stepsize control
27   * (and automatic step initialization) and continuous output. This
28   * method uses 7 functions evaluations per step.</p>
29   *
30   * @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
31   * @since 1.2
32   */
33  
34  public class HighamHall54Integrator
35    extends EmbeddedRungeKuttaIntegrator {
36  
37    /** Integrator method name. */
38    private static final String methodName = "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(double minStep, double maxStep,
75                                  double scalAbsoluteTolerance,
76                                  double scalRelativeTolerance) {
77      super(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(double minStep, double maxStep,
91                                  double[] vecAbsoluteTolerance,
92                                  double[] vecRelativeTolerance) {
93      super(false, staticC, staticA, staticB, new HighamHall54StepInterpolator(),
94            minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
95    }
96  
97    /** Get the name of the method.
98     * @return name of the method
99     */
100   public String getName() {
101     return methodName;
102   }
103 
104   /** Get the order of the method.
105    * @return order of the method
106    */
107   public int getOrder() {
108     return 5;
109   }
110 
111   /** Compute the error ratio.
112    * @param yDotK derivatives computed during the first stages
113    * @param y0 estimate of the step at the start of the step
114    * @param y1 estimate of the step at the end of the step
115    * @param h  current step
116    * @return error ratio, greater than 1 if step should be rejected
117    */
118   protected double estimateError(double[][] yDotK,
119                                  double[] y0, double[] y1,
120                                  double h) {
121 
122     double error = 0;
123 
124     for (int j = 0; j < y0.length; ++j) {
125       double errSum = staticE[0] * yDotK[0][j];
126       for (int l = 1; l < staticE.length; ++l) {
127         errSum += staticE[l] * yDotK[l][j];
128       }
129 
130       double yScale = Math.max(Math.abs(y0[j]), Math.abs(y1[j]));
131       double tol = (vecAbsoluteTolerance == null) ?
132                    (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
133                    (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
134       double ratio  = h * errSum / tol;
135       error += ratio * ratio;
136 
137     }
138 
139     return Math.sqrt(error / y0.length);
140 
141   }
142 
143 }