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
018package org.apache.commons.math3.ode.nonstiff;
019
020import 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 * @since 1.2
034 */
035
036public class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator {
037
038  /** Integrator method name. */
039  private static final String METHOD_NAME = "Higham-Hall 5(4)";
040
041  /** Time steps Butcher array. */
042  private static final double[] STATIC_C = {
043    2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0
044  };
045
046  /** Internal weights Butcher array. */
047  private static final double[][] STATIC_A = {
048    {2.0/9.0},
049    {1.0/12.0, 1.0/4.0},
050    {1.0/8.0, 0.0, 3.0/8.0},
051    {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0},
052    {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0},
053    {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0}
054  };
055
056  /** Propagation weights Butcher array. */
057  private static final double[] STATIC_B = {
058    1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0
059  };
060
061  /** Error weights Butcher array. */
062  private static final double[] STATIC_E = {
063    -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
064  };
065
066  /** Simple constructor.
067   * Build a fifth order Higham and Hall integrator with the given step bounds
068   * @param minStep minimal step (sign is irrelevant, regardless of
069   * integration direction, forward or backward), the last step can
070   * be smaller than this
071   * @param maxStep maximal step (sign is irrelevant, regardless of
072   * integration direction, forward or backward), the last step can
073   * be smaller than this
074   * @param scalAbsoluteTolerance allowed absolute error
075   * @param scalRelativeTolerance allowed relative error
076   */
077  public HighamHall54Integrator(final double minStep, final double maxStep,
078                                final double scalAbsoluteTolerance,
079                                final double scalRelativeTolerance) {
080    super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
081          minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
082  }
083
084  /** Simple constructor.
085   * Build a fifth order Higham and Hall integrator with the given step bounds
086   * @param minStep minimal step (sign is irrelevant, regardless of
087   * integration direction, forward or backward), the last step can
088   * be smaller than this
089   * @param maxStep maximal step (sign is irrelevant, regardless of
090   * integration direction, forward or backward), the last step can
091   * be smaller than this
092   * @param vecAbsoluteTolerance allowed absolute error
093   * @param vecRelativeTolerance allowed relative error
094   */
095  public HighamHall54Integrator(final double minStep, final double maxStep,
096                                final double[] vecAbsoluteTolerance,
097                                final double[] vecRelativeTolerance) {
098    super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
099          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 = FastMath.max(FastMath.abs(y0[j]), FastMath.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
131    return FastMath.sqrt(error / mainSetDimension);
132
133  }
134
135}