001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.ode.nonstiff;
019    
020    import org.apache.commons.math.util.FastMath;
021    
022    
023    /**
024     * This class implements the 5(4) Higham and Hall integrator for
025     * Ordinary Differential Equations.
026     *
027     * <p>This integrator is an embedded Runge-Kutta integrator
028     * of order 5(4) used in local extrapolation mode (i.e. the solution
029     * is computed using the high order formula) with stepsize control
030     * (and automatic step initialization) and continuous output. This
031     * method uses 7 functions evaluations per step.</p>
032     *
033     * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 ao??t 2010) $
034     * @since 1.2
035     */
036    
037    public class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator {
038    
039      /** Integrator method name. */
040      private static final String METHOD_NAME = "Higham-Hall 5(4)";
041    
042      /** Time steps Butcher array. */
043      private static final double[] STATIC_C = {
044        2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0
045      };
046    
047      /** Internal weights Butcher array. */
048      private static final double[][] STATIC_A = {
049        {2.0/9.0},
050        {1.0/12.0, 1.0/4.0},
051        {1.0/8.0, 0.0, 3.0/8.0},
052        {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0},
053        {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0},
054        {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0}
055      };
056    
057      /** Propagation weights Butcher array. */
058      private static final double[] STATIC_B = {
059        1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0
060      };
061    
062      /** Error weights Butcher array. */
063      private static final double[] STATIC_E = {
064        -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
065      };
066    
067      /** Simple constructor.
068       * Build a fifth order Higham and Hall integrator with the given step bounds
069       * @param minStep minimal step (must be positive even for backward
070       * integration), the last step can be smaller than this
071       * @param maxStep maximal step (must be positive even for backward
072       * integration)
073       * @param scalAbsoluteTolerance allowed absolute error
074       * @param scalRelativeTolerance allowed relative error
075       */
076      public HighamHall54Integrator(final double minStep, final double maxStep,
077                                    final double scalAbsoluteTolerance,
078                                    final double scalRelativeTolerance) {
079        super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
080              minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
081      }
082    
083      /** Simple constructor.
084       * Build a fifth order Higham and Hall integrator with the given step bounds
085       * @param minStep minimal step (must be positive even for backward
086       * integration), the last step can be smaller than this
087       * @param maxStep maximal step (must be positive even for backward
088       * integration)
089       * @param vecAbsoluteTolerance allowed absolute error
090       * @param vecRelativeTolerance allowed relative error
091       */
092      public HighamHall54Integrator(final double minStep, final double maxStep,
093                                    final double[] vecAbsoluteTolerance,
094                                    final double[] vecRelativeTolerance) {
095        super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
096              minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
097      }
098    
099      /** {@inheritDoc} */
100      @Override
101      public int getOrder() {
102        return 5;
103      }
104    
105      /** {@inheritDoc} */
106      @Override
107      protected double estimateError(final double[][] yDotK,
108                                     final double[] y0, final double[] y1,
109                                     final double h) {
110    
111        double error = 0;
112    
113        for (int j = 0; j < mainSetDimension; ++j) {
114          double errSum = STATIC_E[0] * yDotK[0][j];
115          for (int l = 1; l < STATIC_E.length; ++l) {
116            errSum += STATIC_E[l] * yDotK[l][j];
117          }
118    
119          final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
120          final double tol = (vecAbsoluteTolerance == null) ?
121                             (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
122                             (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
123          final double ratio  = h * errSum / tol;
124          error += ratio * ratio;
125    
126        }
127    
128        return FastMath.sqrt(error / mainSetDimension);
129    
130      }
131    
132    }