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