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