DormandPrince853FieldStepInterpolator.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.exception.MaxCountExceededException;
  21. import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
  22. import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
  23. import org.apache.commons.math4.legacy.core.MathArrays;

  24. /**
  25.  * This class represents an interpolator over the last step during an
  26.  * ODE integration for the 8(5,3) Dormand-Prince integrator.
  27.  *
  28.  * @see DormandPrince853FieldIntegrator
  29.  *
  30.  * @param <T> the type of the field elements
  31.  * @since 3.6
  32.  */

  33. class DormandPrince853FieldStepInterpolator<T extends RealFieldElement<T>>
  34.     extends RungeKuttaFieldStepInterpolator<T> {

  35.     /** Interpolation weights.
  36.      * (beware that only the non-null values are in the table)
  37.      */
  38.     private final T[][] d;

  39.     /** Simple constructor.
  40.      * @param field field to which the time and state vector elements belong
  41.      * @param forward integration direction indicator
  42.      * @param yDotK slopes at the intermediate points
  43.      * @param globalPreviousState start of the global step
  44.      * @param globalCurrentState end of the global step
  45.      * @param softPreviousState start of the restricted step
  46.      * @param softCurrentState end of the restricted step
  47.      * @param mapper equations mapper for the all equations
  48.      */
  49.     DormandPrince853FieldStepInterpolator(final Field<T> field, final boolean forward,
  50.                                           final T[][] yDotK,
  51.                                           final FieldODEStateAndDerivative<T> globalPreviousState,
  52.                                           final FieldODEStateAndDerivative<T> globalCurrentState,
  53.                                           final FieldODEStateAndDerivative<T> softPreviousState,
  54.                                           final FieldODEStateAndDerivative<T> softCurrentState,
  55.                                           final FieldEquationsMapper<T> mapper) {
  56.         super(field, forward, yDotK,
  57.               globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
  58.               mapper);
  59.         // interpolation weights
  60.         d = MathArrays.buildArray(field, 7, 16);

  61.         // this row is the same as the b array
  62.         d[0][ 0] = fraction(field, 104257, 1920240);
  63.         d[0][ 1] = field.getZero();
  64.         d[0][ 2] = field.getZero();
  65.         d[0][ 3] = field.getZero();
  66.         d[0][ 4] = field.getZero();
  67.         d[0][ 5] = fraction(field,         3399327.0,          763840.0);
  68.         d[0][ 6] = fraction(field,        66578432.0,        35198415.0);
  69.         d[0][ 7] = fraction(field,     -1674902723.0,       288716400.0);
  70.         d[0][ 8] = fraction(field,  54980371265625.0, 176692375811392.0);
  71.         d[0][ 9] = fraction(field,         -734375.0,         4826304.0);
  72.         d[0][10] = fraction(field,       171414593.0,       851261400.0);
  73.         d[0][11] = fraction(field,          137909.0,         3084480.0);
  74.         d[0][12] = field.getZero();
  75.         d[0][13] = field.getZero();
  76.         d[0][14] = field.getZero();
  77.         d[0][15] = field.getZero();

  78.         d[1][ 0] = d[0][ 0].negate().add(1);
  79.         d[1][ 1] = d[0][ 1].negate();
  80.         d[1][ 2] = d[0][ 2].negate();
  81.         d[1][ 3] = d[0][ 3].negate();
  82.         d[1][ 4] = d[0][ 4].negate();
  83.         d[1][ 5] = d[0][ 5].negate();
  84.         d[1][ 6] = d[0][ 6].negate();
  85.         d[1][ 7] = d[0][ 7].negate();
  86.         d[1][ 8] = d[0][ 8].negate();
  87.         d[1][ 9] = d[0][ 9].negate();
  88.         d[1][10] = d[0][10].negate();
  89.         d[1][11] = d[0][11].negate();
  90.         d[1][12] = d[0][12].negate(); // really 0
  91.         d[1][13] = d[0][13].negate(); // really 0
  92.         d[1][14] = d[0][14].negate(); // really 0
  93.         d[1][15] = d[0][15].negate(); // really 0

  94.         d[2][ 0] = d[0][ 0].multiply(2).subtract(1);
  95.         d[2][ 1] = d[0][ 1].multiply(2);
  96.         d[2][ 2] = d[0][ 2].multiply(2);
  97.         d[2][ 3] = d[0][ 3].multiply(2);
  98.         d[2][ 4] = d[0][ 4].multiply(2);
  99.         d[2][ 5] = d[0][ 5].multiply(2);
  100.         d[2][ 6] = d[0][ 6].multiply(2);
  101.         d[2][ 7] = d[0][ 7].multiply(2);
  102.         d[2][ 8] = d[0][ 8].multiply(2);
  103.         d[2][ 9] = d[0][ 9].multiply(2);
  104.         d[2][10] = d[0][10].multiply(2);
  105.         d[2][11] = d[0][11].multiply(2);
  106.         d[2][12] = d[0][12].multiply(2).subtract(1); // really -1
  107.         d[2][13] = d[0][13].multiply(2);             // really  0
  108.         d[2][14] = d[0][14].multiply(2);             // really  0
  109.         d[2][15] = d[0][15].multiply(2);             // really  0

  110.         d[3][ 0] = fraction(field,         -17751989329.0, 2106076560.0);
  111.         d[3][ 1] = field.getZero();
  112.         d[3][ 2] = field.getZero();
  113.         d[3][ 3] = field.getZero();
  114.         d[3][ 4] = field.getZero();
  115.         d[3][ 5] = fraction(field,           4272954039.0, 7539864640.0);
  116.         d[3][ 6] = fraction(field,        -118476319744.0, 38604839385.0);
  117.         d[3][ 7] = fraction(field,         755123450731.0, 316657731600.0);
  118.         d[3][ 8] = fraction(field,  3692384461234828125.0, 1744130441634250432.0);
  119.         d[3][ 9] = fraction(field,          -4612609375.0, 5293382976.0);
  120.         d[3][10] = fraction(field,        2091772278379.0, 933644586600.0);
  121.         d[3][11] = fraction(field,           2136624137.0, 3382989120.0);
  122.         d[3][12] = fraction(field,              -126493.0, 1421424.0);
  123.         d[3][13] = fraction(field,             98350000.0, 5419179.0);
  124.         d[3][14] = fraction(field,            -18878125.0, 2053168.0);
  125.         d[3][15] = fraction(field,          -1944542619.0, 438351368.0);

  126.         d[4][ 0] = fraction(field,          32941697297.0, 3159114840.0);
  127.         d[4][ 1] = field.getZero();
  128.         d[4][ 2] = field.getZero();
  129.         d[4][ 3] = field.getZero();
  130.         d[4][ 4] = field.getZero();
  131.         d[4][ 5] = fraction(field,         456696183123.0, 1884966160.0);
  132.         d[4][ 6] = fraction(field,       19132610714624.0, 115814518155.0);
  133.         d[4][ 7] = fraction(field,     -177904688592943.0, 474986597400.0);
  134.         d[4][ 8] = fraction(field, -4821139941836765625.0, 218016305204281304.0);
  135.         d[4][ 9] = fraction(field,          30702015625.0, 3970037232.0);
  136.         d[4][10] = fraction(field,      -85916079474274.0, 2800933759800.0);
  137.         d[4][11] = fraction(field,          -5919468007.0, 634310460.0);
  138.         d[4][12] = fraction(field,              2479159.0, 157936.0);
  139.         d[4][13] = fraction(field,            -18750000.0, 602131.0);
  140.         d[4][14] = fraction(field,            -19203125.0, 2053168.0);
  141.         d[4][15] = fraction(field,          15700361463.0, 438351368.0);

  142.         d[5][ 0] = fraction(field,          12627015655.0, 631822968.0);
  143.         d[5][ 1] = field.getZero();
  144.         d[5][ 2] = field.getZero();
  145.         d[5][ 3] = field.getZero();
  146.         d[5][ 4] = field.getZero();
  147.         d[5][ 5] = fraction(field,         -72955222965.0, 188496616.0);
  148.         d[5][ 6] = fraction(field,      -13145744952320.0, 69488710893.0);
  149.         d[5][ 7] = fraction(field,       30084216194513.0, 56998391688.0);
  150.         d[5][ 8] = fraction(field,  -296858761006640625.0, 25648977082856624.0);
  151.         d[5][ 9] = fraction(field,            569140625.0, 82709109.0);
  152.         d[5][10] = fraction(field,         -18684190637.0, 18672891732.0);
  153.         d[5][11] = fraction(field,             69644045.0, 89549712.0);
  154.         d[5][12] = fraction(field,            -11847025.0, 4264272.0);
  155.         d[5][13] = fraction(field,           -978650000.0, 16257537.0);
  156.         d[5][14] = fraction(field,            519371875.0, 6159504.0);
  157.         d[5][15] = fraction(field,           5256837225.0, 438351368.0);

  158.         d[6][ 0] = fraction(field,           -450944925.0, 17550638.0);
  159.         d[6][ 1] = field.getZero();
  160.         d[6][ 2] = field.getZero();
  161.         d[6][ 3] = field.getZero();
  162.         d[6][ 4] = field.getZero();
  163.         d[6][ 5] = fraction(field,         -14532122925.0, 94248308.0);
  164.         d[6][ 6] = fraction(field,        -595876966400.0, 2573655959.0);
  165.         d[6][ 7] = fraction(field,         188748653015.0, 527762886.0);
  166.         d[6][ 8] = fraction(field,  2545485458115234375.0, 27252038150535163.0);
  167.         d[6][ 9] = fraction(field,          -1376953125.0, 36759604.0);
  168.         d[6][10] = fraction(field,          53995596795.0, 518691437.0);
  169.         d[6][11] = fraction(field,            210311225.0, 7047894.0);
  170.         d[6][12] = fraction(field,             -1718875.0, 39484.0);
  171.         d[6][13] = fraction(field,             58000000.0, 602131.0);
  172.         d[6][14] = fraction(field,             -1546875.0, 39484.0);
  173.         d[6][15] = fraction(field,          -1262172375.0, 8429834.0);
  174.     }

  175.     /** {@inheritDoc} */
  176.     @Override
  177.     protected DormandPrince853FieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  178.                                                                final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  179.                                                                final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  180.                                                                final FieldODEStateAndDerivative<T> newSoftPreviousState,
  181.                                                                final FieldODEStateAndDerivative<T> newSoftCurrentState,
  182.                                                                final FieldEquationsMapper<T> newMapper) {
  183.         return new DormandPrince853FieldStepInterpolator<>(newField, newForward, newYDotK,
  184.                                                             newGlobalPreviousState, newGlobalCurrentState,
  185.                                                             newSoftPreviousState, newSoftCurrentState,
  186.                                                             newMapper);
  187.     }

  188.     /** Create a fraction.
  189.      * @param field field to which the elements belong
  190.      * @param p numerator
  191.      * @param q denominator
  192.      * @return p/q computed in the instance field
  193.      */
  194.     private T fraction(final Field<T> field, final double p, final double q) {
  195.         return field.getZero().add(p).divide(q);
  196.     }

  197.     /** {@inheritDoc} */
  198.     @SuppressWarnings("unchecked")
  199.     @Override
  200.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  201.                                                                                    final T time, final T theta,
  202.                                                                                    final T thetaH, final T oneMinusThetaH)
  203.         throws MaxCountExceededException {

  204.         final T one      = time.getField().getOne();
  205.         final T eta      = one.subtract(theta);
  206.         final T twoTheta = theta.multiply(2);
  207.         final T theta2   = theta.multiply(theta);
  208.         final T dot1     = one.subtract(twoTheta);
  209.         final T dot2     = theta.multiply(theta.multiply(-3).add(2));
  210.         final T dot3     = twoTheta.multiply(theta.multiply(twoTheta.subtract(3)).add(1));
  211.         final T dot4     = theta2.multiply(theta.multiply(theta.multiply(5).subtract(8)).add(3));
  212.         final T dot5     = theta2.multiply(theta.multiply(theta.multiply(theta.multiply(-6).add(15)).subtract(12)).add(3));
  213.         final T dot6     = theta2.multiply(theta.multiply(theta.multiply(theta.multiply(theta.multiply(-7).add(18)).subtract(15)).add(4)));
  214.         final T[] interpolatedState;
  215.         final T[] interpolatedDerivatives;


  216.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  217.             final T f0 = thetaH;
  218.             final T f1 = f0.multiply(eta);
  219.             final T f2 = f1.multiply(theta);
  220.             final T f3 = f2.multiply(eta);
  221.             final T f4 = f3.multiply(theta);
  222.             final T f5 = f4.multiply(eta);
  223.             final T f6 = f5.multiply(theta);
  224.             final T[] p = MathArrays.buildArray(time.getField(), 16);
  225.             final T[] q = MathArrays.buildArray(time.getField(), 16);
  226.             for (int i = 0; i < p.length; ++i) {
  227.                 p[i] =     f0.multiply(d[0][i]).
  228.                        add(f1.multiply(d[1][i])).
  229.                        add(f2.multiply(d[2][i])).
  230.                        add(f3.multiply(d[3][i])).
  231.                        add(f4.multiply(d[4][i])).
  232.                        add(f5.multiply(d[5][i])).
  233.                        add(f6.multiply(d[6][i]));
  234.                 q[i] =                    d[0][i].
  235.                         add(dot1.multiply(d[1][i])).
  236.                         add(dot2.multiply(d[2][i])).
  237.                         add(dot3.multiply(d[3][i])).
  238.                         add(dot4.multiply(d[4][i])).
  239.                         add(dot5.multiply(d[5][i])).
  240.                         add(dot6.multiply(d[6][i]));
  241.             }
  242.             interpolatedState       = previousStateLinearCombination(p[0], p[1], p[ 2], p[ 3], p[ 4], p[ 5], p[ 6], p[ 7],
  243.                                                                      p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
  244.             interpolatedDerivatives = derivativeLinearCombination(q[0], q[1], q[ 2], q[ 3], q[ 4], q[ 5], q[ 6], q[ 7],
  245.                                                                   q[8], q[9], q[10], q[11], q[12], q[13], q[14], q[15]);
  246.         } else {
  247.             final T f0 = oneMinusThetaH.negate();
  248.             final T f1 = f0.multiply(theta).negate();
  249.             final T f2 = f1.multiply(theta);
  250.             final T f3 = f2.multiply(eta);
  251.             final T f4 = f3.multiply(theta);
  252.             final T f5 = f4.multiply(eta);
  253.             final T f6 = f5.multiply(theta);
  254.             final T[] p = MathArrays.buildArray(time.getField(), 16);
  255.             final T[] q = MathArrays.buildArray(time.getField(), 16);
  256.             for (int i = 0; i < p.length; ++i) {
  257.                 p[i] =     f0.multiply(d[0][i]).
  258.                        add(f1.multiply(d[1][i])).
  259.                        add(f2.multiply(d[2][i])).
  260.                        add(f3.multiply(d[3][i])).
  261.                        add(f4.multiply(d[4][i])).
  262.                        add(f5.multiply(d[5][i])).
  263.                        add(f6.multiply(d[6][i]));
  264.                 q[i] =                    d[0][i].
  265.                         add(dot1.multiply(d[1][i])).
  266.                         add(dot2.multiply(d[2][i])).
  267.                         add(dot3.multiply(d[3][i])).
  268.                         add(dot4.multiply(d[4][i])).
  269.                         add(dot5.multiply(d[5][i])).
  270.                         add(dot6.multiply(d[6][i]));
  271.             }
  272.             interpolatedState       = currentStateLinearCombination(p[0], p[1], p[ 2], p[ 3], p[ 4], p[ 5], p[ 6], p[ 7],
  273.                                                                     p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
  274.             interpolatedDerivatives = derivativeLinearCombination(q[0], q[1], q[ 2], q[ 3], q[ 4], q[ 5], q[ 6], q[ 7],
  275.                                                                   q[8], q[9], q[10], q[11], q[12], q[13], q[14], q[15]);
  276.         }

  277.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  278.     }
  279. }