GillStateInterpolator.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.GillIntegrator;
  21. import org.hipparchus.util.FastMath;

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

  51. public class GillStateInterpolator extends RungeKuttaStateInterpolator {

  52.     /** First Gill coefficient. */
  53.     private static final double ONE_MINUS_INV_SQRT_2 = 1 - FastMath.sqrt(0.5);

  54.     /** Second Gill coefficient. */
  55.     private static final double ONE_PLUS_INV_SQRT_2 = 1 + FastMath.sqrt(0.5);

  56.     /** Serializable version identifier. */
  57.     private static final long serialVersionUID = 20160328L;

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

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     protected GillStateInterpolator create(final boolean newForward, final double[][] newYDotK,
  79.                                            final ODEStateAndDerivative newGlobalPreviousState,
  80.                                            final ODEStateAndDerivative newGlobalCurrentState,
  81.                                            final ODEStateAndDerivative newSoftPreviousState,
  82.                                            final ODEStateAndDerivative newSoftCurrentState,
  83.                                            final EquationsMapper newMapper) {
  84.         return new GillStateInterpolator(newForward, newYDotK,
  85.                                          newGlobalPreviousState, newGlobalCurrentState,
  86.                                          newSoftPreviousState, newSoftCurrentState,
  87.                                          newMapper);
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
  92.                                                                            final double time, final double theta,
  93.                                                                            final double thetaH, final double oneMinusThetaH) {

  94.         final double twoTheta   = 2 * theta;
  95.         final double fourTheta2 = twoTheta * twoTheta;
  96.         final double coeffDot1  = theta * (twoTheta - 3) + 1;
  97.         final double cDot23     = twoTheta * (1 - theta);
  98.         final double coeffDot2  = cDot23  * ONE_MINUS_INV_SQRT_2;
  99.         final double coeffDot3  = cDot23  * ONE_PLUS_INV_SQRT_2;
  100.         final double coeffDot4  = theta * (twoTheta - 1);

  101.         final double[] interpolatedState;
  102.         final double[] interpolatedDerivatives;
  103.         if (getGlobalPreviousState() != null && theta <= 0.5) {
  104.             final double s         = thetaH / 6.0;
  105.             final double c23       = s * (6 * theta - fourTheta2);
  106.             final double coeff1    = s * (6 - 9 * theta + fourTheta2);
  107.             final double coeff2    = c23  * ONE_MINUS_INV_SQRT_2;
  108.             final double coeff3    = c23  * ONE_PLUS_INV_SQRT_2;
  109.             final double coeff4    = s * (-3 * theta + fourTheta2);
  110.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  111.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3 , coeffDot4);
  112.         } else {
  113.             final double s      = oneMinusThetaH / -6.0;
  114.             final double c23    = s * (2 + twoTheta - fourTheta2);
  115.             final double coeff1 = s * (1 - 5 * theta + fourTheta2);
  116.             final double coeff2 = c23  * ONE_MINUS_INV_SQRT_2;
  117.             final double coeff3 = c23  * ONE_PLUS_INV_SQRT_2;
  118.             final double coeff4 = s * (1 + theta + fourTheta2);
  119.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  120.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3 , coeffDot4);
  121.         }

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

  123.     }

  124. }