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

  53. class GillFieldStepInterpolator<T extends RealFieldElement<T>>
  54.   extends RungeKuttaFieldStepInterpolator<T> {

  55.     /** First Gill coefficient. */
  56.     private final T one_minus_inv_sqrt_2;

  57.     /** Second Gill coefficient. */
  58.     private final T one_plus_inv_sqrt_2;

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

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

  96.     /** {@inheritDoc} */
  97.     @SuppressWarnings("unchecked")
  98.     @Override
  99.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  100.                                                                                    final T time, final T theta,
  101.                                                                                    final T thetaH, final T oneMinusThetaH) {

  102.         final T one        = time.getField().getOne();
  103.         final T twoTheta   = theta.multiply(2);
  104.         final T fourTheta2 = twoTheta.multiply(twoTheta);
  105.         final T coeffDot1  = theta.multiply(twoTheta.subtract(3)).add(1);
  106.         final T cDot23     = twoTheta.multiply(one.subtract(theta));
  107.         final T coeffDot2  = cDot23.multiply(one_minus_inv_sqrt_2);
  108.         final T coeffDot3  = cDot23.multiply(one_plus_inv_sqrt_2);
  109.         final T coeffDot4  = theta.multiply(twoTheta.subtract(1));
  110.         final T[] interpolatedState;
  111.         final T[] interpolatedDerivatives;

  112.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  113.             final T s               = thetaH.divide(6.0);
  114.             final T c23             = s.multiply(theta.multiply(6).subtract(fourTheta2));
  115.             final T coeff1          = s.multiply(fourTheta2.subtract(theta.multiply(9)).add(6));
  116.             final T coeff2          = c23.multiply(one_minus_inv_sqrt_2);
  117.             final T coeff3          = c23.multiply(one_plus_inv_sqrt_2);
  118.             final T coeff4          = s.multiply(fourTheta2.subtract(theta.multiply(3)));
  119.             interpolatedState       = previousStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  120.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  121.         } else {
  122.             final T s      = oneMinusThetaH.divide(-6.0);
  123.             final T c23    = s.multiply(twoTheta.add(2).subtract(fourTheta2));
  124.             final T coeff1 = s.multiply(fourTheta2.subtract(theta.multiply(5)).add(1));
  125.             final T coeff2 = c23.multiply(one_minus_inv_sqrt_2);
  126.             final T coeff3 = c23.multiply(one_plus_inv_sqrt_2);
  127.             final T coeff4 = s.multiply(fourTheta2.add(theta).add(1));
  128.             interpolatedState       = currentStateLinearCombination(coeff1, coeff2, coeff3, coeff4);
  129.             interpolatedDerivatives = derivativeLinearCombination(coeffDot1, coeffDot2, coeffDot3, coeffDot4);
  130.         }

  131.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  132.     }
  133. }