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) Dormand-Prince integrator for Ordinary
025     * 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. However, since this
032     * is an <i>fsal</i>, the last evaluation of one step is the same as
033     * the first evaluation of the next step and hence can be avoided. So
034     * the cost is really 6 functions evaluations per step.</p>
035     *
036     * <p>This method has been published (whithout the continuous output
037     * that was added by Shampine in 1986) in the following article :
038     * <pre>
039     *  A family of embedded Runge-Kutta formulae
040     *  J. R. Dormand and P. J. Prince
041     *  Journal of Computational and Applied Mathematics
042     *  volume 6, no 1, 1980, pp. 19-26
043     * </pre></p>
044     *
045     * @version $Id: DormandPrince54Integrator.java 1416643 2012-12-03 19:37:14Z tn $
046     * @since 1.2
047     */
048    
049    public class DormandPrince54Integrator extends EmbeddedRungeKuttaIntegrator {
050    
051      /** Integrator method name. */
052      private static final String METHOD_NAME = "Dormand-Prince 5(4)";
053    
054      /** Time steps Butcher array. */
055      private static final double[] STATIC_C = {
056        1.0/5.0, 3.0/10.0, 4.0/5.0, 8.0/9.0, 1.0, 1.0
057      };
058    
059      /** Internal weights Butcher array. */
060      private static final double[][] STATIC_A = {
061        {1.0/5.0},
062        {3.0/40.0, 9.0/40.0},
063        {44.0/45.0, -56.0/15.0, 32.0/9.0},
064        {19372.0/6561.0, -25360.0/2187.0, 64448.0/6561.0,  -212.0/729.0},
065        {9017.0/3168.0, -355.0/33.0, 46732.0/5247.0, 49.0/176.0, -5103.0/18656.0},
066        {35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0}
067      };
068    
069      /** Propagation weights Butcher array. */
070      private static final double[] STATIC_B = {
071        35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0, 0.0
072      };
073    
074      /** Error array, element 1. */
075      private static final double E1 =     71.0 / 57600.0;
076    
077      // element 2 is zero, so it is neither stored nor used
078    
079      /** Error array, element 3. */
080      private static final double E3 =    -71.0 / 16695.0;
081    
082      /** Error array, element 4. */
083      private static final double E4 =     71.0 / 1920.0;
084    
085      /** Error array, element 5. */
086      private static final double E5 = -17253.0 / 339200.0;
087    
088      /** Error array, element 6. */
089      private static final double E6 =     22.0 / 525.0;
090    
091      /** Error array, element 7. */
092      private static final double E7 =     -1.0 / 40.0;
093    
094      /** Simple constructor.
095       * Build a fifth order Dormand-Prince integrator with the given step bounds
096       * @param minStep minimal step (sign is irrelevant, regardless of
097       * integration direction, forward or backward), the last step can
098       * be smaller than this
099       * @param maxStep maximal step (sign is irrelevant, regardless of
100       * integration direction, forward or backward), the last step can
101       * be smaller than this
102       * @param scalAbsoluteTolerance allowed absolute error
103       * @param scalRelativeTolerance allowed relative error
104       */
105      public DormandPrince54Integrator(final double minStep, final double maxStep,
106                                       final double scalAbsoluteTolerance,
107                                       final double scalRelativeTolerance) {
108        super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(),
109              minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
110      }
111    
112      /** Simple constructor.
113       * Build a fifth order Dormand-Prince integrator with the given step bounds
114       * @param minStep minimal step (sign is irrelevant, regardless of
115       * integration direction, forward or backward), the last step can
116       * be smaller than this
117       * @param maxStep maximal step (sign is irrelevant, regardless of
118       * integration direction, forward or backward), the last step can
119       * be smaller than this
120       * @param vecAbsoluteTolerance allowed absolute error
121       * @param vecRelativeTolerance allowed relative error
122       */
123      public DormandPrince54Integrator(final double minStep, final double maxStep,
124                                       final double[] vecAbsoluteTolerance,
125                                       final double[] vecRelativeTolerance) {
126        super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(),
127              minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
128      }
129    
130      /** {@inheritDoc} */
131      @Override
132      public int getOrder() {
133        return 5;
134      }
135    
136      /** {@inheritDoc} */
137      @Override
138      protected double estimateError(final double[][] yDotK,
139                                     final double[] y0, final double[] y1,
140                                     final double h) {
141    
142        double error = 0;
143    
144        for (int j = 0; j < mainSetDimension; ++j) {
145            final double errSum = E1 * yDotK[0][j] +  E3 * yDotK[2][j] +
146                                  E4 * yDotK[3][j] +  E5 * yDotK[4][j] +
147                                  E6 * yDotK[5][j] +  E7 * yDotK[6][j];
148    
149            final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
150            final double tol = (vecAbsoluteTolerance == null) ?
151                               (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
152                                   (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
153            final double ratio  = h * errSum / tol;
154            error += ratio * ratio;
155    
156        }
157    
158        return FastMath.sqrt(error / mainSetDimension);
159    
160      }
161    
162    }