DormandPrince853FieldStateInterpolator.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.interpolators;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.Field;
  24. import org.hipparchus.exception.MathIllegalStateException;
  25. import org.hipparchus.ode.FieldEquationsMapper;
  26. import org.hipparchus.ode.FieldODEStateAndDerivative;
  27. import org.hipparchus.ode.nonstiff.DormandPrince853FieldIntegrator;
  28. import org.hipparchus.util.MathArrays;

  29. /**
  30.  * This class represents an interpolator over the last step during an
  31.  * ODE integration for the 8(5,3) Dormand-Prince integrator.
  32.  *
  33.  * @see DormandPrince853FieldIntegrator
  34.  *
  35.  * @param <T> the type of the field elements
  36.  */

  37. public class DormandPrince853FieldStateInterpolator<T extends CalculusFieldElement<T>>
  38.     extends RungeKuttaFieldStateInterpolator<T> {

  39.     /** Interpolation weights.
  40.      * (beware that only the non-null values are in the table)
  41.      */
  42.     private final T[][] d;

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

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

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

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

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

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

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

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

  177.     }

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

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

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

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


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

  280.         return mapper.mapStateAndDerivative(time, interpolatedState, interpolatedDerivatives);

  281.     }

  282. }