MidpointFieldStepInterpolator.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.  *      http://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.apache.commons.math4.legacy.ode.nonstiff;

  18. import org.apache.commons.math4.legacy.core.Field;
  19. import org.apache.commons.math4.legacy.core.RealFieldElement;
  20. import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
  21. import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;

  22. /**
  23.  * This class implements a step interpolator for second order
  24.  * Runge-Kutta integrator.
  25.  *
  26.  * <p>This interpolator computes dense output inside the last
  27.  * step computed. The interpolation equation is consistent with the
  28.  * integration scheme :
  29.  * <ul>
  30.  *   <li>Using reference point at step start:<br>
  31.  *   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>]
  32.  *   </li>
  33.  *   <li>Using reference point at step end:<br>
  34.  *   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>]
  35.  *   </li>
  36.  * </ul>
  37.  *
  38.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
  39.  * evaluations of the derivatives already computed during the
  40.  * step.</p>
  41.  *
  42.  * @see MidpointFieldIntegrator
  43.  * @param <T> the type of the field elements
  44.  * @since 3.6
  45.  */

  46. class MidpointFieldStepInterpolator<T extends RealFieldElement<T>>
  47.     extends RungeKuttaFieldStepInterpolator<T> {

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

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

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

  88.         final T coeffDot2 = theta.multiply(2);
  89.         final T coeffDot1 = time.getField().getOne().subtract(coeffDot2);
  90.         final T[] interpolatedState;
  91.         final T[] interpolatedDerivatives;

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

  103.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  104.     }
  105. }