ThreeEighthesFieldStateInterpolator.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.ode.FieldEquationsMapper;
  25. import org.hipparchus.ode.FieldODEStateAndDerivative;
  26. import org.hipparchus.ode.nonstiff.ThreeEighthesFieldIntegrator;

  27. /**
  28.  * This class implements a step interpolator for the 3/8 fourth
  29.  * order Runge-Kutta integrator.
  30.  *
  31.  * <p>This interpolator allows to compute dense output inside the last
  32.  * step computed. The interpolation equation is consistent with the
  33.  * integration scheme :</p>
  34.  * <ul>
  35.  *   <li>Using reference point at step start:<br>
  36.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
  37.  *                      + &theta; (h/8) [ (8 - 15 &theta; +  8 &theta;<sup>2</sup>) y'<sub>1</sub>
  38.  *                                     +  3 * (15 &theta; - 12 &theta;<sup>2</sup>) y'<sub>2</sub>
  39.  *                                     +        3 &theta;                           y'<sub>3</sub>
  40.  *                                     +      (-3 &theta; +  4 &theta;<sup>2</sup>) y'<sub>4</sub>
  41.  *                                    ]
  42.  *   </li>
  43.  *   <li>Using reference point at step end:<br>
  44.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  45.  *                      - (1 - &theta;) (h/8) [(1 - 7 &theta; + 8 &theta;<sup>2</sup>) y'<sub>1</sub>
  46.  *                                         + 3 (1 +   &theta; - 4 &theta;<sup>2</sup>) y'<sub>2</sub>
  47.  *                                         + 3 (1 +   &theta;)                         y'<sub>3</sub>
  48.  *                                         +   (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  49.  *                                          ]
  50.  *   </li>
  51.  * </ul>
  52.  *
  53.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  54.  * evaluations of the derivatives already computed during the
  55.  * step.</p>
  56.  *
  57.  * @see ThreeEighthesFieldIntegrator
  58.  * @param <T> the type of the field elements
  59.  */

  60. public class ThreeEighthesFieldStateInterpolator<T extends CalculusFieldElement<T>>
  61.       extends RungeKuttaFieldStateInterpolator<T> {

  62.     /** Simple constructor.
  63.      * @param field field to which the time and state vector elements belong
  64.      * @param forward integration direction indicator
  65.      * @param yDotK slopes at the intermediate points
  66.      * @param globalPreviousState start of the global step
  67.      * @param globalCurrentState end of the global step
  68.      * @param softPreviousState start of the restricted step
  69.      * @param softCurrentState end of the restricted step
  70.      * @param mapper equations mapper for the all equations
  71.      */
  72.     public ThreeEighthesFieldStateInterpolator(final Field<T> field, final boolean forward,
  73.                                                final T[][] yDotK,
  74.                                                final FieldODEStateAndDerivative<T> globalPreviousState,
  75.                                                final FieldODEStateAndDerivative<T> globalCurrentState,
  76.                                                final FieldODEStateAndDerivative<T> softPreviousState,
  77.                                                final FieldODEStateAndDerivative<T> softCurrentState,
  78.                                                final FieldEquationsMapper<T> mapper) {
  79.         super(field, forward, yDotK, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
  80.                 mapper);
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     protected ThreeEighthesFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  85.                                                             final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  86.                                                             final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  87.                                                             final FieldODEStateAndDerivative<T> newSoftPreviousState,
  88.                                                             final FieldODEStateAndDerivative<T> newSoftCurrentState,
  89.                                                             final FieldEquationsMapper<T> newMapper) {
  90.         return new ThreeEighthesFieldStateInterpolator<>(newField, newForward, newYDotK,
  91.                                                           newGlobalPreviousState, newGlobalCurrentState,
  92.                                                           newSoftPreviousState, newSoftCurrentState,
  93.                                                           newMapper);
  94.     }

  95.     /** {@inheritDoc} */
  96.     @SuppressWarnings("unchecked")
  97.     @Override
  98.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  99.                                                                                    final T time, final T theta,
  100.                                                                                    final T thetaH, final T oneMinusThetaH) {

  101.         final T coeffDot3  = theta.multiply(0.75);
  102.         final T coeffDot1  = coeffDot3.multiply(theta.multiply(4).subtract(5)).add(1);
  103.         final T coeffDot2  = coeffDot3.multiply(theta.multiply(-6).add(5));
  104.         final T coeffDot4  = coeffDot3.multiply(theta.multiply(2).subtract(1));
  105.         final T[] interpolatedState;
  106.         final T[] interpolatedDerivatives;

  107.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  108.             final T s          = thetaH.divide(8);
  109.             final T fourTheta2 = theta.multiply(theta).multiply(4);
  110.             final T coeff1     = s.multiply(fourTheta2.multiply(2).subtract(theta.multiply(15)).add(8));
  111.             final T coeff2     = s.multiply(theta.multiply(5).subtract(fourTheta2)).multiply(3);
  112.             final T coeff3     = s.multiply(theta).multiply(3);
  113.             final T coeff4     = s.multiply(fourTheta2.subtract(theta.multiply(3)));
  114.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  115.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  116.         } else {
  117.             final T s          = oneMinusThetaH.divide(-8);
  118.             final T fourTheta2 = theta.multiply(theta).multiply(4);
  119.             final T thetaPlus1 = theta.add(1);
  120.             final T coeff1     = s.multiply(fourTheta2.multiply(2).subtract(theta.multiply(7)).add(1));
  121.             final T coeff2     = s.multiply(thetaPlus1.subtract(fourTheta2)).multiply(3);
  122.             final T coeff3     = s.multiply(thetaPlus1).multiply(3);
  123.             final T coeff4     = s.multiply(thetaPlus1.add(fourTheta2));
  124.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  125.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  126.         }

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

  128.     }

  129. }