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;
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: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
43 * @since 1.2
44 */
45
46 public class DormandPrince54Integrator
47 extends EmbeddedRungeKuttaIntegrator {
48
49 /** Integrator method name. */
50 private static final String methodName = "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(double minStep, double maxStep,
102 double scalAbsoluteTolerance,
103 double scalRelativeTolerance) {
104 super(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(double minStep, double maxStep,
118 double[] vecAbsoluteTolerance,
119 double[] vecRelativeTolerance) {
120 super(true, staticC, staticA, staticB, new DormandPrince54StepInterpolator(),
121 minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
122 }
123
124 /** Get the name of the method.
125 * @return name of the method
126 */
127 public String getName() {
128 return methodName;
129 }
130
131 /** Get the order of the method.
132 * @return order of the method
133 */
134 public int getOrder() {
135 return 5;
136 }
137
138 /** Compute the error ratio.
139 * @param yDotK derivatives computed during the first stages
140 * @param y0 estimate of the step at the start of the step
141 * @param y1 estimate of the step at the end of the step
142 * @param h current step
143 * @return error ratio, greater than 1 if step should be rejected
144 */
145 protected double estimateError(double[][] yDotK,
146 double[] y0, double[] y1,
147 double h) {
148
149 double error = 0;
150
151 for (int j = 0; j < y0.length; ++j) {
152 double errSum = e1 * yDotK[0][j] + e3 * yDotK[2][j] +
153 e4 * yDotK[3][j] + e5 * yDotK[4][j] +
154 e6 * yDotK[5][j] + e7 * yDotK[6][j];
155
156 double yScale = Math.max(Math.abs(y0[j]), Math.abs(y1[j]));
157 double tol = (vecAbsoluteTolerance == null) ?
158 (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
159 (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
160 double ratio = h * errSum / tol;
161 error += ratio * ratio;
162
163 }
164
165 return Math.sqrt(error / y0.length);
166
167 }
168
169 }