View Javadoc

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  
21  /**
22   * This class implements the 5(4) Dormand-Prince integrator for Ordinary
23   * Differential Equations.
24  
25   * <p>This integrator is an embedded Runge-Kutta integrator
26   * of order 5(4) used in local extrapolation mode (i.e. the solution
27   * is computed using the high order formula) with stepsize control
28   * (and automatic step initialization) and continuous output. This
29   * method uses 7 functions evaluations per step. However, since this
30   * is an <i>fsal</i>, the last evaluation of one step is the same as
31   * the first evaluation of the next step and hence can be avoided. So
32   * the cost is really 6 functions evaluations per step.</p>
33   *
34   * <p>This method has been published (whithout the continuous output
35   * that was added by Shampine in 1986) in the following article :
36   * <pre>
37   *  A family of embedded Runge-Kutta formulae
38   *  J. R. Dormand and P. J. Prince
39   *  Journal of Computational and Applied Mathematics
40   *  volume 6, no 1, 1980, pp. 19-26
41   * </pre></p>
42   *
43   * @version $Revision: 786881 $ $Date: 2009-06-20 14:53:08 -0400 (Sat, 20 Jun 2009) $
44   * @since 1.2
45   */
46  
47  public class DormandPrince54Integrator extends EmbeddedRungeKuttaIntegrator {
48  
49    /** Integrator method name. */
50    private static final String METHOD_NAME = "Dormand-Prince 5(4)";
51  
52    /** Time steps Butcher array. */
53    private static final double[] staticC = {
54      1.0/5.0, 3.0/10.0, 4.0/5.0, 8.0/9.0, 1.0, 1.0
55    };
56  
57    /** Internal weights Butcher array. */
58    private static final double[][] staticA = {
59      {1.0/5.0},
60      {3.0/40.0, 9.0/40.0},
61      {44.0/45.0, -56.0/15.0, 32.0/9.0},
62      {19372.0/6561.0, -25360.0/2187.0, 64448.0/6561.0,  -212.0/729.0},
63      {9017.0/3168.0, -355.0/33.0, 46732.0/5247.0, 49.0/176.0, -5103.0/18656.0},
64      {35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0}
65    };
66  
67    /** Propagation weights Butcher array. */
68    private static final double[] staticB = {
69      35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0, 0.0
70    };
71  
72    /** Error array, element 1. */
73    private static final double e1 =     71.0 / 57600.0;
74  
75    // element 2 is zero, so it is neither stored nor used
76  
77    /** Error array, element 3. */
78    private static final double e3 =    -71.0 / 16695.0;
79  
80    /** Error array, element 4. */
81    private static final double e4 =     71.0 / 1920.0;
82  
83    /** Error array, element 5. */
84    private static final double e5 = -17253.0 / 339200.0;
85  
86    /** Error array, element 6. */
87    private static final double e6 =     22.0 / 525.0;
88  
89    /** Error array, element 7. */
90    private static final double e7 =     -1.0 / 40.0;
91  
92    /** Simple constructor.
93     * Build a fifth order Dormand-Prince integrator with the given step bounds
94     * @param minStep minimal step (must be positive even for backward
95     * integration), the last step can be smaller than this
96     * @param maxStep maximal step (must be positive even for backward
97     * integration)
98     * @param scalAbsoluteTolerance allowed absolute error
99     * @param scalRelativeTolerance allowed relative error
100    */
101   public DormandPrince54Integrator(final double minStep, final double maxStep,
102                                    final double scalAbsoluteTolerance,
103                                    final double scalRelativeTolerance) {
104     super(METHOD_NAME, true, staticC, staticA, staticB, new DormandPrince54StepInterpolator(),
105           minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
106   }
107 
108   /** Simple constructor.
109    * Build a fifth order Dormand-Prince integrator with the given step bounds
110    * @param minStep minimal step (must be positive even for backward
111    * integration), the last step can be smaller than this
112    * @param maxStep maximal step (must be positive even for backward
113    * integration)
114    * @param vecAbsoluteTolerance allowed absolute error
115    * @param vecRelativeTolerance allowed relative error
116    */
117   public DormandPrince54Integrator(final double minStep, final double maxStep,
118                                    final double[] vecAbsoluteTolerance,
119                                    final double[] vecRelativeTolerance) {
120     super(METHOD_NAME, true, staticC, staticA, staticB, new DormandPrince54StepInterpolator(),
121           minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
122   }
123 
124   /** {@inheritDoc} */
125   @Override
126   public int getOrder() {
127     return 5;
128   }
129 
130   /** {@inheritDoc} */
131   @Override
132   protected double estimateError(final double[][] yDotK,
133                                  final double[] y0, final double[] y1,
134                                  final double h) {
135 
136     double error = 0;
137 
138     for (int j = 0; j < y0.length; ++j) {
139         final double errSum = e1 * yDotK[0][j] +  e3 * yDotK[2][j] +
140                               e4 * yDotK[3][j] +  e5 * yDotK[4][j] +
141                               e6 * yDotK[5][j] +  e7 * yDotK[6][j];
142 
143         final double yScale = Math.max(Math.abs(y0[j]), Math.abs(y1[j]));
144         final double tol = (vecAbsoluteTolerance == null) ?
145                            (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
146                                (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
147         final double ratio  = h * errSum / tol;
148         error += ratio * ratio;
149 
150     }
151 
152     return Math.sqrt(error / y0.length);
153 
154   }
155 
156 }