ClassicalRungeKuttaFieldStepInterpolator.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 the classical 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 :
  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>) (y'<sub>2</sub> + 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 end:<br>
  38.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  39.  *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
  40.  *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
  41.  *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
  42.  *                                        ]
  43.  *   </li>
  44.  * </ul>
  45.  *
  46.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  47.  * evaluations of the derivatives already computed during the
  48.  * step.</p>
  49.  *
  50.  * @see ClassicalRungeKuttaFieldIntegrator
  51.  * @param <T> the type of the field elements
  52.  * @since 3.6
  53.  */

  54. class ClassicalRungeKuttaFieldStepInterpolator<T extends RealFieldElement<T>>
  55.     extends RungeKuttaFieldStepInterpolator<T> {

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

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected ClassicalRungeKuttaFieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  80.                                                                  final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  81.                                                                  final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  82.                                                                  final FieldODEStateAndDerivative<T> newSoftPreviousState,
  83.                                                                  final FieldODEStateAndDerivative<T> newSoftCurrentState,
  84.                                                                  final FieldEquationsMapper<T> newMapper) {
  85.         return new ClassicalRungeKuttaFieldStepInterpolator<>(newField, newForward, newYDotK,
  86.                                                                newGlobalPreviousState, newGlobalCurrentState,
  87.                                                                newSoftPreviousState, newSoftCurrentState,
  88.                                                                newMapper);
  89.     }

  90.     /** {@inheritDoc} */
  91.     @SuppressWarnings("unchecked")
  92.     @Override
  93.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  94.                                                                                    final T time, final T theta,
  95.                                                                                    final T thetaH, final T oneMinusThetaH) {

  96.         final T one                       = time.getField().getOne();
  97.         final T oneMinusTheta             = one.subtract(theta);
  98.         final T oneMinus2Theta            = one.subtract(theta.multiply(2));
  99.         final T coeffDot1                 = oneMinusTheta.multiply(oneMinus2Theta);
  100.         final T coeffDot23                = theta.multiply(oneMinusTheta).multiply(2);
  101.         final T coeffDot4                 = theta.multiply(oneMinus2Theta).negate();
  102.         final T[] interpolatedState;
  103.         final T[] interpolatedDerivatives;

  104.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  105.             final T fourTheta2      = theta.multiply(theta).multiply(4);
  106.             final T s               = thetaH.divide(6.0);
  107.             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
  108.             final T coeff23         = s.multiply(theta.multiply(6).subtract(fourTheta2));
  109.             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
  110.             interpolatedState       = previousStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  111.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  112.         } else {
  113.             final T fourTheta       = theta.multiply(4);
  114.             final T s               = oneMinusThetaH.divide(6);
  115.             final T coeff1          = s.multiply(theta.multiply(fourTheta.negate().add(5)).subtract(1));
  116.             final T coeff23         = s.multiply(theta.multiply(fourTheta.subtract(2)).subtract(2));
  117.             final T coeff4          = s.multiply(theta.multiply(fourTheta.negate().subtract(1)).subtract(1));
  118.             interpolatedState       = currentStateLinearCombination(coeff1, coeff23, coeff23, coeff4);
  119.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot23, coeffDot23, coeffDot4);
  120.         }

  121.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  122.     }
  123. }