ClassicalRungeKuttaStateInterpolator.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.ode.nonstiff.interpolators;

  18. import org.hipparchus.ode.EquationsMapper;
  19. import org.hipparchus.ode.ODEStateAndDerivative;
  20. import org.hipparchus.ode.nonstiff.ClassicalRungeKuttaIntegrator;

  21. /**
  22.  * This class implements a step interpolator for the classical fourth
  23.  * order Runge-Kutta integrator.
  24.  *
  25.  * <p>This interpolator allows to compute dense output inside the last
  26.  * step computed. The interpolation equation is consistent with the
  27.  * integration scheme :</p>
  28.  * <ul>
  29.  *   <li>Using reference point at step start:<br>
  30.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
  31.  *                    + &theta; (h/6) [  (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
  32.  *                                     + (    6 &theta; - 4 &theta;<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
  33.  *                                     + (   -3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  34.  *                                    ]
  35.  *   </li>
  36.  *   <li>Using reference point at step end:<br>
  37.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  38.  *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
  39.  *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
  40.  *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
  41.  *                                        ]
  42.  *   </li>
  43.  * </ul>
  44.  *
  45.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  46.  * evaluations of the derivatives already computed during the
  47.  * step.</p>
  48.  *
  49.  * @see ClassicalRungeKuttaIntegrator
  50.  */

  51. public class ClassicalRungeKuttaStateInterpolator extends RungeKuttaStateInterpolator {

  52.     /** Serializable version identifier. */
  53.     private static final long serialVersionUID = 20160328L;

  54.     /** Simple constructor.
  55.      * @param forward integration direction indicator
  56.      * @param yDotK slopes at the intermediate points
  57.      * @param globalPreviousState start of the global step
  58.      * @param globalCurrentState end of the global step
  59.      * @param softPreviousState start of the restricted step
  60.      * @param softCurrentState end of the restricted step
  61.      * @param mapper equations mapper for the all equations
  62.      */
  63.     public ClassicalRungeKuttaStateInterpolator(final boolean forward,
  64.                                                 final double[][] yDotK,
  65.                                                 final ODEStateAndDerivative globalPreviousState,
  66.                                                 final ODEStateAndDerivative globalCurrentState,
  67.                                                 final ODEStateAndDerivative softPreviousState,
  68.                                                 final ODEStateAndDerivative softCurrentState,
  69.                                                 final EquationsMapper mapper) {
  70.         super(forward, yDotK, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     protected ClassicalRungeKuttaStateInterpolator create(final boolean newForward, final double[][] newYDotK,
  75.                                                           final ODEStateAndDerivative newGlobalPreviousState,
  76.                                                           final ODEStateAndDerivative newGlobalCurrentState,
  77.                                                           final ODEStateAndDerivative newSoftPreviousState,
  78.                                                           final ODEStateAndDerivative newSoftCurrentState,
  79.                                                           final EquationsMapper newMapper) {
  80.         return new ClassicalRungeKuttaStateInterpolator(newForward, newYDotK,
  81.                 newGlobalPreviousState, newGlobalCurrentState,
  82.                 newSoftPreviousState, newSoftCurrentState,
  83.                 newMapper);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override
  87.     protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
  88.                                                                            final double time, final double theta,
  89.                                                                            final double thetaH, final double oneMinusThetaH) {

  90.         final double oneMinusTheta             = 1.0 - theta;
  91.         final double oneMinus2Theta            = 1.0 - theta * 2.0;
  92.         final double coeffDot1                 = oneMinusTheta * oneMinus2Theta;
  93.         final double coeffDot23                =  theta * oneMinusTheta * 2;
  94.         final double coeffDot4                 = -theta * oneMinus2Theta;
  95.         final double[] interpolatedState;
  96.         final double[] interpolatedDerivatives;

  97.         if (getGlobalPreviousState() != null && theta <= 0.5) {
  98.             final double fourTheta2      = theta * theta * 4;
  99.             final double s               = thetaH / 6.0;
  100.             final double coeff1          = s * (fourTheta2 - theta * 9 + 6);
  101.             final double coeff23         = s * (theta * 6 - fourTheta2);
  102.             final double coeff4          = s * (fourTheta2 - theta * 3);
  103.             interpolatedState       = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  104.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  105.         } else {
  106.             final double fourTheta       = theta * 4;
  107.             final double s               = oneMinusThetaH / 6.0;
  108.             final double coeff1          = s * (theta * (-fourTheta + 5) - 1);
  109.             final double coeff23         = s * (theta * ( fourTheta - 2) - 2);
  110.             final double coeff4          = s * (theta * (-fourTheta - 1) - 1);
  111.             interpolatedState       = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  112.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  113.         }

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

  115.     }

  116. }