ThreeEighthesFieldStepInterpolator.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 3/8 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/8) [ (8 - 15 &theta; +  8 &theta;<sup>2</sup>) y'<sub>1</sub>
  33.  *                                     +  3 * (15 &theta; - 12 &theta;<sup>2</sup>) y'<sub>2</sub>
  34.  *                                     +        3 &theta;                           y'<sub>3</sub>
  35.  *                                     +      (-3 &theta; +  4 &theta;<sup>2</sup>) y'<sub>4</sub>
  36.  *                                    ]
  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)
  40.  *                      - (1 - &theta;) (h/8) [(1 - 7 &theta; + 8 &theta;<sup>2</sup>) y'<sub>1</sub>
  41.  *                                         + 3 (1 +   &theta; - 4 &theta;<sup>2</sup>) y'<sub>2</sub>
  42.  *                                         + 3 (1 +   &theta;)                         y'<sub>3</sub>
  43.  *                                         +   (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  44.  *                                          ]
  45.  *   </li>
  46.  * </ul>
  47.  *
  48.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  49.  * evaluations of the derivatives already computed during the
  50.  * step.
  51.  *
  52.  * @see ThreeEighthesFieldIntegrator
  53.  * @param <T> the type of the field elements
  54.  * @since 3.6
  55.  */

  56. class ThreeEighthesFieldStepInterpolator<T extends RealFieldElement<T>>
  57.       extends RungeKuttaFieldStepInterpolator<T> {

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

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

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

  98.         final T coeffDot3  = theta.multiply(0.75);
  99.         final T coeffDot1  = coeffDot3.multiply(theta.multiply(4).subtract(5)).add(1);
  100.         final T coeffDot2  = coeffDot3.multiply(theta.multiply(-6).add(5));
  101.         final T coeffDot4  = coeffDot3.multiply(theta.multiply(2).subtract(1));
  102.         final T[] interpolatedState;
  103.         final T[] interpolatedDerivatives;

  104.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  105.             final T s          = thetaH.divide(8);
  106.             final T fourTheta2 = theta.multiply(theta).multiply(4);
  107.             final T coeff1     = s.multiply(fourTheta2.multiply(2).subtract(theta.multiply(15)).add(8));
  108.             final T coeff2     = s.multiply(theta.multiply(5).subtract(fourTheta2)).multiply(3);
  109.             final T coeff3     = s.multiply(theta).multiply(3);
  110.             final T coeff4     = s.multiply(fourTheta2.subtract(theta.multiply(3)));
  111.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  112.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  113.         } else {
  114.             final T s          = oneMinusThetaH.divide(-8);
  115.             final T fourTheta2 = theta.multiply(theta).multiply(4);
  116.             final T thetaPlus1 = theta.add(1);
  117.             final T coeff1     = s.multiply(fourTheta2.multiply(2).subtract(theta.multiply(7)).add(1));
  118.             final T coeff2     = s.multiply(thetaPlus1.subtract(fourTheta2)).multiply(3);
  119.             final T coeff3     = s.multiply(thetaPlus1).multiply(3);
  120.             final T coeff4     = s.multiply(thetaPlus1.add(fourTheta2));
  121.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  122.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  123.         }

  124.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  125.     }
  126. }