    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.math3.ode.nonstiff;
    019
    020    import org.apache.commons.math3.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 $Id: HighamHall54Integrator.java 1416643 2012-12-03 19:37:14Z tn $
    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 (sign is irrelevant, regardless of
    070       * integration direction, forward or backward), the last step can
    071       * be smaller than this
    072       * @param maxStep maximal step (sign is irrelevant, regardless of
    073       * integration direction, forward or backward), the last step can
    074       * be smaller than this
    075       * @param scalAbsoluteTolerance allowed absolute error
    076       * @param scalRelativeTolerance allowed relative error
    077       */
    078      public HighamHall54Integrator(final double minStep, final double maxStep,
    079                                    final double scalAbsoluteTolerance,
    080                                    final double scalRelativeTolerance) {
    081        super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
    082              minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
    083      }
    084
    085      /** Simple constructor.
    086       * Build a fifth order Higham and Hall integrator with the given step bounds
    087       * @param minStep minimal step (sign is irrelevant, regardless of
    088       * integration direction, forward or backward), the last step can
    089       * be smaller than this
    090       * @param maxStep maximal step (sign is irrelevant, regardless of
    091       * integration direction, forward or backward), the last step can
    092       * be smaller than this
    093       * @param vecAbsoluteTolerance allowed absolute error
    094       * @param vecRelativeTolerance allowed relative error
    095       */
    096      public HighamHall54Integrator(final double minStep, final double maxStep,
    097                                    final double[] vecAbsoluteTolerance,
    098                                    final double[] vecRelativeTolerance) {
    099        super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
    100              minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
    101      }
    102
    103      /** {@inheritDoc} */
    104      @Override
    105      public int getOrder() {
    106        return 5;
    107      }
    108
    109      /** {@inheritDoc} */
    110      @Override
    111      protected double estimateError(final double[][] yDotK,
    112                                     final double[] y0, final double[] y1,
    113                                     final double h) {
    114
    115        double error = 0;
    116
    117        for (int j = 0; j < mainSetDimension; ++j) {
    118          double errSum = STATIC_E[0] * yDotK[0][j];
    119          for (int l = 1; l < STATIC_E.length; ++l) {
    120            errSum += STATIC_E[l] * yDotK[l][j];
    121          }
    122
    123          final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
    124          final double tol = (vecAbsoluteTolerance == null) ?
    125                             (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
    126                             (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
    127          final double ratio  = h * errSum / tol;
    128          error += ratio * ratio;
    129
    130        }
    131
    132        return FastMath.sqrt(error / mainSetDimension);
    133
    134      }
    135
    136    }