GillFieldStateInterpolator.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.GillFieldIntegrator;

  27. /**
  28.  * This class implements a step interpolator for the Gill fourth
  29.  * order Runge-Kutta integrator.
  30.  *
  31.  * <p>This interpolator allows to compute 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>)
  37.  *                    + &theta; (h/6) [ (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
  38.  *                                    + (    6 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
  39.  *                                    + (  - 3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  40.  *                                    ]
  41.  *   </li>
  42.  *   <li>Using reference point at step start:<br>
  43.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  44.  *                    - (1 - &theta;) (h/6) [ (1 - 5 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
  45.  *                                          + (2 + 2 &theta; - 4 &theta;<sup>2</sup>) ((1-1/&radic;2) y'<sub>2</sub> + (1+1/&radic;2)) y'<sub>3</sub>)
  46.  *                                          + (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  47.  *                                          ]
  48.  *   </li>
  49.  * </ul>
  50.  * <p>where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub>
  51.  * are the four evaluations of the derivatives already computed during
  52.  * the step.</p>
  53.  *
  54.  * @see GillFieldIntegrator
  55.  * @param <T> the type of the field elements
  56.  */

  57. public class GillFieldStateInterpolator<T extends CalculusFieldElement<T>>
  58.     extends RungeKuttaFieldStateInterpolator<T> {

  59.     /** First Gill coefficient. */
  60.     private final T one_minus_inv_sqrt_2;

  61.     /** Second Gill coefficient. */
  62.     private final T one_plus_inv_sqrt_2;

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

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

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

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

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

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

  135.     }

  136. }