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.  *      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. package org.apache.commons.math4.legacy.ode.nonstiff;

  18. import org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.RealFieldElement;
  20. import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
  21. import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
  22. import org.apache.commons.math4.legacy.core.MathArrays;


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

  36. public class HighamHall54FieldIntegrator<T extends RealFieldElement<T>>
  37.     extends EmbeddedRungeKuttaFieldIntegrator<T> {

  38.     /** Integrator method name. */
  39.     private static final String METHOD_NAME = "Higham-Hall 5(4)";

  40.     /** Error weights Butcher array. */
  41.     private final T[] e ;

  42.     /** Simple constructor.
  43.      * Build a fifth order Higham and Hall integrator with the given step bounds
  44.      * @param field field to which the time and state vector elements belong
  45.      * @param minStep minimal step (sign is irrelevant, regardless of
  46.      * integration direction, forward or backward), the last step can
  47.      * be smaller than this
  48.      * @param maxStep maximal step (sign is irrelevant, regardless of
  49.      * integration direction, forward or backward), the last step can
  50.      * be smaller than this
  51.      * @param scalAbsoluteTolerance allowed absolute error
  52.      * @param scalRelativeTolerance allowed relative error
  53.      */
  54.     public HighamHall54FieldIntegrator(final Field<T> field,
  55.                                        final double minStep, final double maxStep,
  56.                                        final double scalAbsoluteTolerance,
  57.                                        final double scalRelativeTolerance) {
  58.         super(field, METHOD_NAME, -1,
  59.               minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance);
  60.         e = MathArrays.buildArray(field, 7);
  61.         e[0] = fraction(-1,  20);
  62.         e[1] = field.getZero();
  63.         e[2] = fraction(81, 160);
  64.         e[3] = fraction(-6,   5);
  65.         e[4] = fraction(25,  32);
  66.         e[5] = fraction( 1,  16);
  67.         e[6] = fraction(-1,  10);
  68.     }

  69.     /** Simple constructor.
  70.      * Build a fifth order Higham and Hall integrator with the given step bounds
  71.      * @param field field to which the time and state vector elements belong
  72.      * @param minStep minimal step (sign is irrelevant, regardless of
  73.      * integration direction, forward or backward), the last step can
  74.      * be smaller than this
  75.      * @param maxStep maximal step (sign is irrelevant, regardless of
  76.      * integration direction, forward or backward), the last step can
  77.      * be smaller than this
  78.      * @param vecAbsoluteTolerance allowed absolute error
  79.      * @param vecRelativeTolerance allowed relative error
  80.      */
  81.     public HighamHall54FieldIntegrator(final Field<T> field,
  82.                                        final double minStep, final double maxStep,
  83.                                        final double[] vecAbsoluteTolerance,
  84.                                        final double[] vecRelativeTolerance) {
  85.         super(field, METHOD_NAME, -1,
  86.               minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance);
  87.         e = MathArrays.buildArray(field, 7);
  88.         e[0] = fraction(-1,  20);
  89.         e[1] = field.getZero();
  90.         e[2] = fraction(81, 160);
  91.         e[3] = fraction(-6,   5);
  92.         e[4] = fraction(25,  32);
  93.         e[5] = fraction( 1,  16);
  94.         e[6] = fraction(-1,  10);
  95.     }

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

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public T[][] getA() {
  111.         final T[][] a = MathArrays.buildArray(getField(), 6, -1);
  112.         for (int i = 0; i < a.length; ++i) {
  113.             a[i] = MathArrays.buildArray(getField(), i + 1);
  114.         }
  115.         a[0][0] = fraction(     2,     9);
  116.         a[1][0] = fraction(     1,    12);
  117.         a[1][1] = fraction(     1,     4);
  118.         a[2][0] = fraction(     1,     8);
  119.         a[2][1] = getField().getZero();
  120.         a[2][2] = fraction(     3,     8);
  121.         a[3][0] = fraction(    91,   500);
  122.         a[3][1] = fraction(   -27,   100);
  123.         a[3][2] = fraction(    78,   125);
  124.         a[3][3] = fraction(     8,   125);
  125.         a[4][0] = fraction(   -11,    20);
  126.         a[4][1] = fraction(    27,    20);
  127.         a[4][2] = fraction(    12,     5);
  128.         a[4][3] = fraction(   -36,     5);
  129.         a[4][4] = fraction(     5,     1);
  130.         a[5][0] = fraction(     1,    12);
  131.         a[5][1] = getField().getZero();
  132.         a[5][2] = fraction(    27,    32);
  133.         a[5][3] = fraction(    -4,     3);
  134.         a[5][4] = fraction(   125,    96);
  135.         a[5][5] = fraction(     5,    48);
  136.         return a;
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     public T[] getB() {
  141.         final T[] b = MathArrays.buildArray(getField(), 7);
  142.         b[0] = fraction(  1, 12);
  143.         b[1] = getField().getZero();
  144.         b[2] = fraction( 27, 32);
  145.         b[3] = fraction( -4,  3);
  146.         b[4] = fraction(125, 96);
  147.         b[5] = fraction(  5, 48);
  148.         b[6] = getField().getZero();
  149.         return b;
  150.     }

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

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

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

  170.         T error = getField().getZero();

  171.         for (int j = 0; j < mainSetDimension; ++j) {
  172.             T errSum = yDotK[0][j].multiply(e[0]);
  173.             for (int l = 1; l < e.length; ++l) {
  174.                 errSum = errSum.add(yDotK[l][j].multiply(e[l]));
  175.             }

  176.             final T yScale = RealFieldElement.max(y0[j].abs(), y1[j].abs());
  177.             final T tol    = (vecAbsoluteTolerance == null) ?
  178.                              yScale.multiply(scalRelativeTolerance).add(scalAbsoluteTolerance) :
  179.                              yScale.multiply(vecRelativeTolerance[j]).add(vecAbsoluteTolerance[j]);
  180.             final T ratio  = h.multiply(errSum).divide(tol);
  181.             error = error.add(ratio.multiply(ratio));
  182.         }

  183.         return error.divide(mainSetDimension).sqrt();
  184.     }
  185. }