MidpointStateInterpolator.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.MidpointIntegrator;

  21. /**
  22.  * This class implements a step interpolator for second order
  23.  * Runge-Kutta integrator.
  24.  *
  25.  * <p>This interpolator computes 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>) + &theta; h [(1 - &theta;) y'<sub>1</sub> + &theta; y'<sub>2</sub>]
  31.  *   </li>
  32.  *   <li>Using reference point at step end:<br>
  33.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) + (1-&theta;) h [&theta; y'<sub>1</sub> - (1+&theta;) y'<sub>2</sub>]
  34.  *   </li>
  35.  * </ul>
  36.  *
  37.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
  38.  * evaluations of the derivatives already computed during the
  39.  * step.</p>
  40.  *
  41.  * @see MidpointIntegrator
  42.  */

  43. public class MidpointStateInterpolator extends RungeKuttaStateInterpolator {

  44.     /** Serializable version identifier. */
  45.     private static final long serialVersionUID = 20160328L;

  46.     /** Simple constructor.
  47.      * @param forward integration direction indicator
  48.      * @param yDotK slopes at the intermediate points
  49.      * @param globalPreviousState start of the global step
  50.      * @param globalCurrentState end of the global step
  51.      * @param softPreviousState start of the restricted step
  52.      * @param softCurrentState end of the restricted step
  53.      * @param mapper equations mapper for the all equations
  54.      */
  55.     public MidpointStateInterpolator(final boolean forward,
  56.                                      final double[][] yDotK,
  57.                                      final ODEStateAndDerivative globalPreviousState,
  58.                                      final ODEStateAndDerivative globalCurrentState,
  59.                                      final ODEStateAndDerivative softPreviousState,
  60.                                      final ODEStateAndDerivative softCurrentState,
  61.                                      final EquationsMapper mapper) {
  62.         super(forward, yDotK, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     protected MidpointStateInterpolator create(final boolean newForward, final double[][] newYDotK,
  67.                                                final ODEStateAndDerivative newGlobalPreviousState,
  68.                                                final ODEStateAndDerivative newGlobalCurrentState,
  69.                                                final ODEStateAndDerivative newSoftPreviousState,
  70.                                                final ODEStateAndDerivative newSoftCurrentState,
  71.                                                final EquationsMapper newMapper) {
  72.         return new MidpointStateInterpolator(newForward, newYDotK,
  73.                                              newGlobalPreviousState, newGlobalCurrentState,
  74.                                              newSoftPreviousState, newSoftCurrentState,
  75.                                              newMapper);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected ODEStateAndDerivative computeInterpolatedStateAndDerivatives(final EquationsMapper mapper,
  80.                                                                            final double time, final double theta,
  81.                                                                            final double thetaH, final double oneMinusThetaH) {
  82.         final double coeffDot2 = 2 * theta;
  83.         final double coeffDot1 = 1 - coeffDot2;

  84.         final double[] interpolatedState;
  85.         final double[] interpolatedDerivatives;
  86.         if (getGlobalPreviousState() != null && theta <= 0.5) {

  87.             final double coeff1     = theta * oneMinusThetaH;
  88.             final double coeff2     = theta * thetaH;
  89.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2);
  90.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
  91.         } else {
  92.             final double coeff1     =  oneMinusThetaH * theta;
  93.             final double coeff2     = -oneMinusThetaH * (1.0 + theta);
  94.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2);
  95.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
  96.         }

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

  98.     }

  99. }