DormandPrince54FieldIntegrator.java

  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.  *      https://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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.ode.nonstiff;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.ode.FieldEquationsMapper;
  25. import org.hipparchus.ode.FieldODEStateAndDerivative;
  26. import org.hipparchus.ode.nonstiff.interpolators.DormandPrince54FieldStateInterpolator;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathArrays;


  29. /**
  30.  * This class implements the 5(4) Dormand-Prince integrator for Ordinary
  31.  * Differential Equations.

  32.  * <p>This integrator is an embedded Runge-Kutta integrator
  33.  * of order 5(4) used in local extrapolation mode (i.e. the solution
  34.  * is computed using the high order formula) with stepsize control
  35.  * (and automatic step initialization) and continuous output. This
  36.  * method uses 7 functions evaluations per step. However, since this
  37.  * is an <i>fsal</i>, the last evaluation of one step is the same as
  38.  * the first evaluation of the next step and hence can be avoided. So
  39.  * the cost is really 6 functions evaluations per step.</p>
  40.  *
  41.  * <p>This method has been published (whithout the continuous output
  42.  * that was added by Shampine in 1986) in the following article :</p>
  43.  * <pre>
  44.  *  A family of embedded Runge-Kutta formulae
  45.  *  J. R. Dormand and P. J. Prince
  46.  *  Journal of Computational and Applied Mathematics
  47.  *  volume 6, no 1, 1980, pp. 19-26
  48.  * </pre>
  49.  *
  50.  * @param <T> the type of the field elements
  51.  */

  52. public class DormandPrince54FieldIntegrator<T extends CalculusFieldElement<T>>
  53.     extends EmbeddedRungeKuttaFieldIntegrator<T> {

  54.     /** Name of integration scheme. */
  55.     public static final String METHOD_NAME = DormandPrince54Integrator.METHOD_NAME;

  56.     /** Simple constructor.
  57.      * Build a fifth order Dormand-Prince integrator with the given step bounds
  58.      * @param field field to which the time and state vector elements belong
  59.      * @param minStep minimal step (sign is irrelevant, regardless of
  60.      * integration direction, forward or backward), the last step can
  61.      * be smaller than this
  62.      * @param maxStep maximal step (sign is irrelevant, regardless of
  63.      * integration direction, forward or backward), the last step can
  64.      * be smaller than this
  65.      * @param scalAbsoluteTolerance allowed absolute error
  66.      * @param scalRelativeTolerance allowed relative error
  67.      */
  68.     public DormandPrince54FieldIntegrator(final Field<T> field,
  69.                                           final double minStep, final double maxStep,
  70.                                           final double scalAbsoluteTolerance,
  71.                                           final double scalRelativeTolerance) {
  72.         super(field, METHOD_NAME, 6,
  73.               minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
  74.     }

  75.     /** Simple constructor.
  76.      * Build a fifth order Dormand-Prince integrator with the given step bounds
  77.      * @param field field to which the time and state vector elements belong
  78.      * @param minStep minimal step (sign is irrelevant, regardless of
  79.      * integration direction, forward or backward), the last step can
  80.      * be smaller than this
  81.      * @param maxStep maximal step (sign is irrelevant, regardless of
  82.      * integration direction, forward or backward), the last step can
  83.      * be smaller than this
  84.      * @param vecAbsoluteTolerance allowed absolute error
  85.      * @param vecRelativeTolerance allowed relative error
  86.      */
  87.     public DormandPrince54FieldIntegrator(final Field<T> field,
  88.                                           final double minStep, final double maxStep,
  89.                                           final double[] vecAbsoluteTolerance,
  90.                                           final double[] vecRelativeTolerance) {
  91.         super(field, DormandPrince54Integrator.METHOD_NAME, 6,
  92.               minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
  93.     }

  94.     /** {@inheritDoc} */
  95.     @Override
  96.     public T[] getC() {
  97.         final T[] c = MathArrays.buildArray(getField(), 6);
  98.         c[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1,  5);
  99.         c[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 3, 10);
  100.         c[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),4,  5);
  101.         c[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),8,  9);
  102.         c[4] = getField().getOne();
  103.         c[5] = getField().getOne();
  104.         return c;
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     public T[][] getA() {
  109.         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
  110.         for (int i = 0; i < a.length; ++i) {
  111.             a[i] = MathArrays.buildArray(getField(), i + 1);
  112.         }
  113.         a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     5);
  114.         a[1][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      3,    40);
  115.         a[1][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      9,    40);
  116.         a[2][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     44,    45);
  117.         a[2][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -56,    15);
  118.         a[2][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     32,     9);
  119.         a[3][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  19372,  6561);
  120.         a[3][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), -25360,  2187);
  121.         a[3][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  64448,  6561);
  122.         a[3][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   -212,   729);
  123.         a[4][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   9017,  3168);
  124.         a[4][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   -355,    33);
  125.         a[4][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  46732,  5247);
  126.         a[4][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     49,   176);
  127.         a[4][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -5103, 18656);
  128.         a[5][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     35,   384);
  129.         a[5][1] = getField().getZero();
  130.         a[5][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    500,  1113);
  131.         a[5][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    125,   192);
  132.         a[5][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -2187,  6784);
  133.         a[5][5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     11,    84);
  134.         return a;
  135.     }

  136.     /** {@inheritDoc} */
  137.     @Override
  138.     public T[] getB() {
  139.         final T[] b = MathArrays.buildArray(getField(), 7);
  140.         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    35,   384);
  141.         b[1] = getField().getZero();
  142.         b[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   500, 1113);
  143.         b[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   125,  192);
  144.         b[4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), -2187, 6784);
  145.         b[5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    11,   84);
  146.         b[6] = getField().getZero();
  147.         return b;
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     protected DormandPrince54FieldStateInterpolator<T>
  152.         createInterpolator(final boolean forward, T[][] yDotK,
  153.                            final FieldODEStateAndDerivative<T> globalPreviousState,
  154.                            final FieldODEStateAndDerivative<T> globalCurrentState, final FieldEquationsMapper<T> mapper) {
  155.         return new DormandPrince54FieldStateInterpolator<>(getField(), forward, yDotK,
  156.                                                            globalPreviousState, globalCurrentState,
  157.                                                            globalPreviousState, globalCurrentState,
  158.                                                            mapper);
  159.     }

  160.     /** {@inheritDoc} */
  161.     @Override
  162.     public int getOrder() {
  163.         return 5;
  164.     }

  165.     /** {@inheritDoc} */
  166.     @Override
  167.     protected double estimateError(final T[][] yDotK, final T[] y0, final T[] y1, final T h) {

  168.         final StepsizeHelper helper = getStepSizeHelper();
  169.         double error = 0;

  170.         for (int j = 0; j < helper.getMainSetDimension(); ++j) {
  171.             final double errSum = DormandPrince54Integrator.E1 * yDotK[0][j].getReal() +  DormandPrince54Integrator.E3 * yDotK[2][j].getReal() +
  172.                                   DormandPrince54Integrator.E4 * yDotK[3][j].getReal() +  DormandPrince54Integrator.E5 * yDotK[4][j].getReal() +
  173.                                   DormandPrince54Integrator.E6 * yDotK[5][j].getReal() +  DormandPrince54Integrator.E7 * yDotK[6][j].getReal();
  174.             final double tol = helper.getTolerance(j, FastMath.max(FastMath.abs(y0[j].getReal()), FastMath.abs(y1[j].getReal())));
  175.             final double ratio  = h.getReal() * errSum / tol;
  176.             error += ratio * ratio;
  177.         }

  178.         return FastMath.sqrt(error / helper.getMainSetDimension());

  179.     }

  180. }