MidpointFieldStateInterpolator.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.MidpointFieldIntegrator;

  27. /**
  28.  * This class implements a step interpolator for second order
  29.  * Runge-Kutta integrator.
  30.  *
  31.  * <p>This interpolator computes 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>) + &theta; h [(1 - &theta;) y'<sub>1</sub> + &theta; y'<sub>2</sub>]
  37.  *   </li>
  38.  *   <li>Using reference point at step end:<br>
  39.  *   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>]
  40.  *   </li>
  41.  * </ul>
  42.  *
  43.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
  44.  * evaluations of the derivatives already computed during the
  45.  * step.</p>
  46.  *
  47.  * @see MidpointFieldIntegrator
  48.  * @param <T> the type of the field elements
  49.  */

  50. public class MidpointFieldStateInterpolator<T extends CalculusFieldElement<T>>
  51.     extends RungeKuttaFieldStateInterpolator<T> {

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

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     protected MidpointFieldStateInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  75.                                                        final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  76.                                                        final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  77.                                                        final FieldODEStateAndDerivative<T> newSoftPreviousState,
  78.                                                        final FieldODEStateAndDerivative<T> newSoftCurrentState,
  79.                                                        final FieldEquationsMapper<T> newMapper) {
  80.         return new MidpointFieldStateInterpolator<>(newField, newForward, newYDotK,
  81.                                                      newGlobalPreviousState, newGlobalCurrentState,
  82.                                                      newSoftPreviousState, newSoftCurrentState,
  83.                                                      newMapper);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @SuppressWarnings("unchecked")
  87.     @Override
  88.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  89.                                                                                    final T time, final T theta,
  90.                                                                                    final T thetaH, final T oneMinusThetaH) {

  91.         final T coeffDot2 = theta.multiply(2);
  92.         final T coeffDot1 = time.getField().getOne().subtract(coeffDot2);
  93.         final T[] interpolatedState;
  94.         final T[] interpolatedDerivatives;

  95.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  96.             final T coeff1 = theta.multiply(oneMinusThetaH);
  97.             final T coeff2 = theta.multiply(thetaH);
  98.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2);
  99.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
  100.         } else {
  101.             final T coeff1 = oneMinusThetaH.multiply(theta);
  102.             final T coeff2 = oneMinusThetaH.multiply(theta.add(1)).negate();
  103.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2);
  104.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2);
  105.         }

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

  107.     }

  108. }