1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.ode.nonstiff;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class implements the 5(4) Dormand-Prince integrator for Ordinary
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Differential Equations.
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This integrator is an embedded Runge-Kutta integrator
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * of order 5(4) used in local extrapolation mode (i.e. the solution
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * is computed using the high order formula) with stepsize control
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (and automatic step initialization) and continuous output. This
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * method uses 7 functions evaluations per step. However, since this
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * is an <i>fsal</i>, the last evaluation of one step is the same as
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the first evaluation of the next step and hence can be avoided. So
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the cost is really 6 functions evaluations per step.</p>
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This method has been published (whithout the continuous output
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * that was added by Shampine in 1986) in the following article :
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <pre>
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  A family of embedded Runge-Kutta formulae
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  J. R. Dormand and P. J. Prince
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  Journal of Computational and Applied Mathematics
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  volume 6, no 1, 1980, pp. 19-26
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </pre></p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class DormandPrince54Integrator extends EmbeddedRungeKuttaIntegrator {
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Integrator method name. */
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final String METHOD_NAME = "Dormand-Prince 5(4)";
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Time steps Butcher array. */
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[] STATIC_C = {
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    1.0/5.0, 3.0/10.0, 4.0/5.0, 8.0/9.0, 1.0, 1.0
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Internal weights Butcher array. */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[][] STATIC_A = {
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {1.0/5.0},
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {3.0/40.0, 9.0/40.0},
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {44.0/45.0, -56.0/15.0, 32.0/9.0},
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {19372.0/6561.0, -25360.0/2187.0, 64448.0/6561.0,  -212.0/729.0},
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {9017.0/3168.0, -355.0/33.0, 46732.0/5247.0, 49.0/176.0, -5103.0/18656.0},
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0}
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Propagation weights Butcher array. */
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[] STATIC_B = {
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0, 0.0
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 1. */
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E1 =     71.0 / 57600.0;
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  // element 2 is zero, so it is neither stored nor used
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 3. */
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E3 =    -71.0 / 16695.0;
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 4. */
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E4 =     71.0 / 1920.0;
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 5. */
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E5 = -17253.0 / 339200.0;
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 6. */
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E6 =     22.0 / 525.0;
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error array, element 7. */
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double E7 =     -1.0 / 40.0;
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a fifth order Dormand-Prince integrator with the given step bounds
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param minStep minimal step (must be positive even for backward
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration), the last step can be smaller than this
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param maxStep maximal step (must be positive even for backward
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration)
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param scalAbsoluteTolerance allowed absolute error
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param scalRelativeTolerance allowed relative error
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public DormandPrince54Integrator(final double minStep, final double maxStep,
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                   final double scalAbsoluteTolerance,
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                   final double scalRelativeTolerance) {
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(),
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a fifth order Dormand-Prince integrator with the given step bounds
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param minStep minimal step (must be positive even for backward
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration), the last step can be smaller than this
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param maxStep maximal step (must be positive even for backward
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration)
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param vecAbsoluteTolerance allowed absolute error
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param vecRelativeTolerance allowed relative error
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public DormandPrince54Integrator(final double minStep, final double maxStep,
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                   final double[] vecAbsoluteTolerance,
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                   final double[] vecRelativeTolerance) {
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(),
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** {@inheritDoc} */
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public int getOrder() {
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return 5;
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** {@inheritDoc} */
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  protected double estimateError(final double[][] yDotK,
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                 final double[] y0, final double[] y1,
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                 final double h) {
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double error = 0;
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    for (int j = 0; j < mainSetDimension; ++j) {
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double errSum = E1 * yDotK[0][j] +  E3 * yDotK[2][j] +
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                              E4 * yDotK[3][j] +  E5 * yDotK[4][j] +
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                              E6 * yDotK[5][j] +  E7 * yDotK[6][j];
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double tol = (vecAbsoluteTolerance == null) ?
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                           (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                               (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double ratio  = h * errSum / tol;
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        error += ratio * ratio;
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return FastMath.sqrt(error / mainSetDimension);
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
159