Home | History | Annotate | Download | only in nonstiff
      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.nonstiff;
     19 
     20 import org.apache.commons.math.util.FastMath;
     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  * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 aot 2010) $
     34  * @since 1.2
     35  */
     36 
     37 public class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator {
     38 
     39   /** Integrator method name. */
     40   private static final String METHOD_NAME = "Higham-Hall 5(4)";
     41 
     42   /** Time steps Butcher array. */
     43   private static final double[] STATIC_C = {
     44     2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0
     45   };
     46 
     47   /** Internal weights Butcher array. */
     48   private static final double[][] STATIC_A = {
     49     {2.0/9.0},
     50     {1.0/12.0, 1.0/4.0},
     51     {1.0/8.0, 0.0, 3.0/8.0},
     52     {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0},
     53     {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0},
     54     {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0}
     55   };
     56 
     57   /** Propagation weights Butcher array. */
     58   private static final double[] STATIC_B = {
     59     1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0
     60   };
     61 
     62   /** Error weights Butcher array. */
     63   private static final double[] STATIC_E = {
     64     -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
     65   };
     66 
     67   /** Simple constructor.
     68    * Build a fifth order Higham and Hall integrator with the given step bounds
     69    * @param minStep minimal step (must be positive even for backward
     70    * integration), the last step can be smaller than this
     71    * @param maxStep maximal step (must be positive even for backward
     72    * integration)
     73    * @param scalAbsoluteTolerance allowed absolute error
     74    * @param scalRelativeTolerance allowed relative error
     75    */
     76   public HighamHall54Integrator(final double minStep, final double maxStep,
     77                                 final double scalAbsoluteTolerance,
     78                                 final double scalRelativeTolerance) {
     79     super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
     80           minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
     81   }
     82 
     83   /** Simple constructor.
     84    * Build a fifth order Higham and Hall integrator with the given step bounds
     85    * @param minStep minimal step (must be positive even for backward
     86    * integration), the last step can be smaller than this
     87    * @param maxStep maximal step (must be positive even for backward
     88    * integration)
     89    * @param vecAbsoluteTolerance allowed absolute error
     90    * @param vecRelativeTolerance allowed relative error
     91    */
     92   public HighamHall54Integrator(final double minStep, final double maxStep,
     93                                 final double[] vecAbsoluteTolerance,
     94                                 final double[] vecRelativeTolerance) {
     95     super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
     96           minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
     97   }
     98 
     99   /** {@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 }
    133