HighamHall54FieldIntegrator.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.HighamHall54FieldStateInterpolator;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathArrays;


  29. /**
  30.  * This class implements the 5(4) Higham and Hall integrator for
  31.  * Ordinary Differential Equations.
  32.  *
  33.  * <p>This integrator is an embedded Runge-Kutta integrator
  34.  * of order 5(4) used in local extrapolation mode (i.e. the solution
  35.  * is computed using the high order formula) with stepsize control
  36.  * (and automatic step initialization) and continuous output. This
  37.  * method uses 7 functions evaluations per step.</p>
  38.  *
  39.  * @param <T> the type of the field elements
  40.  */

  41. public class HighamHall54FieldIntegrator<T extends CalculusFieldElement<T>>
  42.     extends EmbeddedRungeKuttaFieldIntegrator<T> {

  43.     /** Name of integration scheme. */
  44.     public static final String METHOD_NAME = HighamHall54Integrator.METHOD_NAME;

  45.     /** Simple constructor.
  46.      * Build a fifth order Higham and Hall integrator with the given step bounds
  47.      * @param field field to which the time and state vector elements belong
  48.      * @param minStep minimal step (sign is irrelevant, regardless of
  49.      * integration direction, forward or backward), the last step can
  50.      * be smaller than this
  51.      * @param maxStep maximal step (sign is irrelevant, regardless of
  52.      * integration direction, forward or backward), the last step can
  53.      * be smaller than this
  54.      * @param scalAbsoluteTolerance allowed absolute error
  55.      * @param scalRelativeTolerance allowed relative error
  56.      */
  57.     public HighamHall54FieldIntegrator(final Field<T> field,
  58.                                        final double minStep, final double maxStep,
  59.                                        final double scalAbsoluteTolerance,
  60.                                        final double scalRelativeTolerance) {
  61.         super(field, METHOD_NAME, -1,
  62.               minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
  63.     }

  64.     /** Simple constructor.
  65.      * Build a fifth order Higham and Hall integrator with the given step bounds
  66.      * @param field field to which the time and state vector elements belong
  67.      * @param minStep minimal step (sign is irrelevant, regardless of
  68.      * integration direction, forward or backward), the last step can
  69.      * be smaller than this
  70.      * @param maxStep maximal step (sign is irrelevant, regardless of
  71.      * integration direction, forward or backward), the last step can
  72.      * be smaller than this
  73.      * @param vecAbsoluteTolerance allowed absolute error
  74.      * @param vecRelativeTolerance allowed relative error
  75.      */
  76.     public HighamHall54FieldIntegrator(final Field<T> field,
  77.                                        final double minStep, final double maxStep,
  78.                                        final double[] vecAbsoluteTolerance,
  79.                                        final double[] vecRelativeTolerance) {
  80.         super(field, HighamHall54Integrator.METHOD_NAME, -1,
  81.               minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
  82.     }

  83.     /** {@inheritDoc} */
  84.     @Override
  85.     public T[] getC() {
  86.         final T[] c = MathArrays.buildArray(getField(), 6);
  87.         c[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 2, 9);
  88.         c[1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 3);
  89.         c[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 1, 2);
  90.         c[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 3, 5);
  91.         c[4] = getField().getOne();
  92.         c[5] = getField().getOne();
  93.         return c;
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public T[][] getA() {
  98.         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
  99.         for (int i = 0; i < a.length; ++i) {
  100.             a[i] = MathArrays.buildArray(getField(), i + 1);
  101.         }
  102.         a[0][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      2,     9);
  103.         a[1][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,    12);
  104.         a[1][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     4);
  105.         a[2][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,     8);
  106.         a[2][1] = getField().getZero();
  107.         a[2][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     3,     8);
  108.         a[3][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     91,   500);
  109.         a[3][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -27,   100);
  110.         a[3][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     78,   125);
  111.         a[3][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      8,   125);
  112.         a[4][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -11,    20);
  113.         a[4][1] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     27,    20);
  114.         a[4][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     12,     5);
  115.         a[4][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    -36,     5);
  116.         a[4][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      5,     1);
  117.         a[5][0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      1,    12);
  118.         a[5][1] = getField().getZero();
  119.         a[5][2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     27,    32);
  120.         a[5][3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),     -4,     3);
  121.         a[5][4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),    125,    96);
  122.         a[5][5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),      5,    48);
  123.         return a;
  124.     }

  125.     /** {@inheritDoc} */
  126.     @Override
  127.     public T[] getB() {
  128.         final T[] b = MathArrays.buildArray(getField(), 7);
  129.         b[0] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   1, 12);
  130.         b[1] = getField().getZero();
  131.         b[2] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  27, 32);
  132.         b[3] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),  -4,  3);
  133.         b[4] = FieldExplicitRungeKuttaIntegrator.fraction(getField(), 125, 96);
  134.         b[5] = FieldExplicitRungeKuttaIntegrator.fraction(getField(),   5, 48);
  135.         b[6] = getField().getZero();
  136.         return b;
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     protected HighamHall54FieldStateInterpolator<T>
  141.         createInterpolator(final boolean forward, T[][] yDotK,
  142.                            final FieldODEStateAndDerivative<T> globalPreviousState,
  143.                            final FieldODEStateAndDerivative<T> globalCurrentState, final FieldEquationsMapper<T> mapper) {
  144.         return new HighamHall54FieldStateInterpolator<>(getField(), forward, yDotK,
  145.                                                         globalPreviousState, globalCurrentState,
  146.                                                         globalPreviousState, globalCurrentState,
  147.                                                         mapper);
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public int getOrder() {
  152.         return 5;
  153.     }

  154.     /** {@inheritDoc} */
  155.     @Override
  156.     protected double estimateError(final T[][] yDotK, final T[] y0, final T[] y1, final T h) {

  157.         final StepsizeHelper helper = getStepSizeHelper();
  158.         double error = 0;

  159.         for (int j = 0; j < helper.getMainSetDimension(); ++j) {
  160.             double errSum = HighamHall54Integrator.STATIC_E[0] * yDotK[0][j].getReal();
  161.             for (int l = 1; l < HighamHall54Integrator.STATIC_E.length; ++l) {
  162.                 errSum += HighamHall54Integrator.STATIC_E[l] * yDotK[l][j].getReal();
  163.             }
  164.             final double tol   = helper.getTolerance(j, FastMath.max(FastMath.abs(y0[j].getReal()), FastMath.abs(y1[j].getReal())));
  165.             final double ratio = h.getReal() * errSum / tol;
  166.             error += ratio * ratio;

  167.         }

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

  169.     }

  170. }