ClassicalRungeKuttaFieldStateInterpolator.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.ClassicalRungeKuttaFieldIntegrator;

  27. /**
  28.  * This class implements a step interpolator for the classical 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/6) [  (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
  38.  *                                     + (    6 &theta; - 4 &theta;<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
  39.  *                                     + (   -3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  40.  *                                    ]
  41.  *   </li>
  42.  *   <li>Using reference point at step end:<br>
  43.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  44.  *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
  45.  *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
  46.  *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
  47.  *                                        ]
  48.  *   </li>
  49.  * </ul>
  50.  *
  51.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  52.  * evaluations of the derivatives already computed during the
  53.  * step.</p>
  54.  *
  55.  * @see ClassicalRungeKuttaFieldIntegrator
  56.  * @param <T> the type of the field elements
  57.  */

  58. public class ClassicalRungeKuttaFieldStateInterpolator<T extends CalculusFieldElement<T>>
  59.     extends RungeKuttaFieldStateInterpolator<T> {

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

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

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

  98.         final T one                       = time.getField().getOne();
  99.         final T oneMinusTheta             = one.subtract(theta);
  100.         final T oneMinus2Theta            = one.subtract(theta.multiply(2));
  101.         final T coeffDot1                 = oneMinusTheta.multiply(oneMinus2Theta);
  102.         final T coeffDot23                = theta.multiply(oneMinusTheta).multiply(2);
  103.         final T coeffDot4                 = theta.multiply(oneMinus2Theta).negate();
  104.         final T[] interpolatedState;
  105.         final T[] interpolatedDerivatives;

  106.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  107.             final T fourTheta2      = theta.multiply(theta).multiply(4);
  108.             final T s               = thetaH.divide(6.0);
  109.             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
  110.             final T coeff23         = s.multiply(theta.multiply(6).subtract(fourTheta2));
  111.             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
  112.             interpolatedState       = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  113.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  114.         } else {
  115.             final T fourTheta       = theta.multiply(4);
  116.             final T s               = oneMinusThetaH.divide(6);
  117.             final T coeff1          = s.multiply(theta.multiply(fourTheta.negate().add(5)).subtract(1));
  118.             final T coeff23         = s.multiply(theta.multiply(fourTheta.subtract(2)).subtract(2));
  119.             final T coeff4          = s.multiply(theta.multiply(fourTheta.negate().subtract(1)).subtract(1));
  120.             interpolatedState       = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  121.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  122.         }

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

  124.     }

  125. }