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) Higham and Hall integrator for
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Ordinary 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.</p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator {
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Integrator method name. */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final String METHOD_NAME = "Higham-Hall 5(4)";
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Time steps Butcher array. */
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[] STATIC_C = {
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Internal weights Butcher array. */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[][] STATIC_A = {
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {2.0/9.0},
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {1.0/12.0, 1.0/4.0},
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {1.0/8.0, 0.0, 3.0/8.0},
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0},
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0},
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0}
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Propagation weights Butcher array. */
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[] STATIC_B = {
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Error weights Butcher array. */
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  private static final double[] STATIC_E = {
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    -1.0/20.0, 0.0, 81.0/160.0, -6.0/5.0, 25.0/32.0, 1.0/16.0, -1.0/10.0
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  };
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a fifth order Higham and Hall integrator with the given step bounds
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param minStep minimal step (must be positive even for backward
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration), the last step can be smaller than this
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param maxStep maximal step (must be positive even for backward
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration)
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param scalAbsoluteTolerance allowed absolute error
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param scalRelativeTolerance allowed relative error
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public HighamHall54Integrator(final double minStep, final double maxStep,
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                final double scalAbsoluteTolerance,
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                final double scalRelativeTolerance) {
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a fifth order Higham and Hall integrator with the given step bounds
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param minStep minimal step (must be positive even for backward
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration), the last step can be smaller than this
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param maxStep maximal step (must be positive even for backward
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * integration)
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param vecAbsoluteTolerance allowed absolute error
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param vecRelativeTolerance allowed relative error
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public HighamHall54Integrator(final double minStep, final double maxStep,
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                final double[] vecAbsoluteTolerance,
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                final double[] vecRelativeTolerance) {
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(),
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond          minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** {@inheritDoc} */
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public int getOrder() {
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return 5;
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** {@inheritDoc} */
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  @Override
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  protected double estimateError(final double[][] yDotK,
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                 final double[] y0, final double[] y1,
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                 final double h) {
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    double error = 0;
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    for (int j = 0; j < mainSetDimension; ++j) {
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      double errSum = STATIC_E[0] * yDotK[0][j];
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      for (int l = 1; l < STATIC_E.length; ++l) {
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        errSum += STATIC_E[l] * yDotK[l][j];
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      }
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j]));
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      final double tol = (vecAbsoluteTolerance == null) ?
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                         (scalAbsoluteTolerance + scalRelativeTolerance * yScale) :
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                         (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale);
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      final double ratio  = h * errSum / tol;
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      error += ratio * ratio;
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return FastMath.sqrt(error / mainSetDimension);
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
133