HighamHall54FieldStepInterpolator.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 represents an interpolator over the last step during an
  24.  * ODE integration for the 5(4) Higham and Hall integrator.
  25.  *
  26.  * @see HighamHall54FieldIntegrator
  27.  *
  28.  * @param <T> the type of the field elements
  29.  * @since 3.6
  30.  */

  31. class HighamHall54FieldStepInterpolator<T extends RealFieldElement<T>>
  32.     extends RungeKuttaFieldStepInterpolator<T> {

  33.     /** Simple constructor.
  34.      * @param field field to which the time and state vector elements belong
  35.      * @param forward integration direction indicator
  36.      * @param yDotK slopes at the intermediate points
  37.      * @param globalPreviousState start of the global step
  38.      * @param globalCurrentState end of the global step
  39.      * @param softPreviousState start of the restricted step
  40.      * @param softCurrentState end of the restricted step
  41.      * @param mapper equations mapper for the all equations
  42.      */
  43.     HighamHall54FieldStepInterpolator(final Field<T> field, final boolean forward,
  44.                                       final T[][] yDotK,
  45.                                       final FieldODEStateAndDerivative<T> globalPreviousState,
  46.                                       final FieldODEStateAndDerivative<T> globalCurrentState,
  47.                                       final FieldODEStateAndDerivative<T> softPreviousState,
  48.                                       final FieldODEStateAndDerivative<T> softCurrentState,
  49.                                       final FieldEquationsMapper<T> mapper) {
  50.         super(field, forward, yDotK,
  51.               globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
  52.               mapper);
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     protected HighamHall54FieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  57.                                                           final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  58.                                                           final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  59.                                                           final FieldODEStateAndDerivative<T> newSoftPreviousState,
  60.                                                           final FieldODEStateAndDerivative<T> newSoftCurrentState,
  61.                                                           final FieldEquationsMapper<T> newMapper) {
  62.         return new HighamHall54FieldStepInterpolator<>(newField, newForward, newYDotK,
  63.                                                         newGlobalPreviousState, newGlobalCurrentState,
  64.                                                         newSoftPreviousState, newSoftCurrentState,
  65.                                                         newMapper);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @SuppressWarnings("unchecked")
  69.     @Override
  70.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  71.                                                                                    final T time, final T theta,
  72.                                                                                    final T thetaH, final T oneMinusThetaH) {

  73.         final T bDot0 = theta.multiply(theta.multiply(theta.multiply( -10.0      ).add( 16.0       )).add(-15.0 /  2.0)).add(1);
  74.         final T bDot1 = time.getField().getZero();
  75.         final T bDot2 = theta.multiply(theta.multiply(theta.multiply( 135.0 / 2.0).add(-729.0 / 8.0)).add(459.0 / 16.0));
  76.         final T bDot3 = theta.multiply(theta.multiply(theta.multiply(-120.0      ).add( 152.0      )).add(-44.0       ));
  77.         final T bDot4 = theta.multiply(theta.multiply(theta.multiply( 125.0 / 2.0).add(-625.0 / 8.0)).add(375.0 / 16.0));
  78.         final T bDot5 = theta.multiply(  5.0 /  8.0).multiply(theta.multiply(2).subtract(1));
  79.         final T[] interpolatedState;
  80.         final T[] interpolatedDerivatives;

  81.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  82.             final T b0 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply( -5.0 / 2.0).add(  16.0 /  3.0)).add(-15.0 /  4.0)).add(1));
  83.             final T b1 = time.getField().getZero();
  84.             final T b2 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(135.0 / 8.0).add(-243.0 /  8.0)).add(459.0 / 32.0)));
  85.             final T b3 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(-30.0      ).add( 152.0 /  3.0)).add(-22.0       )));
  86.             final T b4 = thetaH.multiply(theta.multiply(theta.multiply(theta.multiply(125.0 / 8.0).add(-625.0 / 24.0)).add(375.0 / 32.0)));
  87.             final T b5 = thetaH.multiply(theta.multiply(theta.multiply(                                   5.0 / 12.0 ).add( -5.0 / 16.0)));
  88.             interpolatedState       = previousStateLinearCombination(b0, b1, b2, b3, b4, b5);
  89.             interpolatedDerivatives = derivativeLinearCombination(bDot0, bDot1, bDot2, bDot3, bDot4, bDot5);
  90.         } else {
  91.             final T theta2 = theta.multiply(theta);
  92.             final T h      = thetaH.divide(theta);
  93.             final T b0 = h.multiply( theta.multiply(theta.multiply(theta.multiply(theta.multiply(-5.0 / 2.0).add( 16.0 / 3.0)).add( -15.0 /  4.0)).add(  1.0       )).add(  -1.0 / 12.0));
  94.             final T b1 = time.getField().getZero();
  95.             final T b2 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               135.0 / 8.0 ).add(-243.0 /  8.0)).add(459.0 / 32.0)).add( -27.0 / 32.0));
  96.             final T b3 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               -30.0       ).add( 152.0 /  3.0)).add(-22.0       )).add(  4.0  /  3.0));
  97.             final T b4 = h.multiply(theta2.multiply(theta.multiply(theta.multiply(                               125.0 / 8.0 ).add(-625.0 / 24.0)).add(375.0 / 32.0)).add(-125.0 / 96.0));
  98.             final T b5 = h.multiply(theta2.multiply(theta.multiply(                                                                   5.0 / 12.0 ).add(-5.0  / 16.0)).add(  -5.0 / 48.0));
  99.             interpolatedState       = currentStateLinearCombination(b0, b1, b2, b3, b4, b5);
  100.             interpolatedDerivatives = derivativeLinearCombination(bDot0, bDot1, bDot2, bDot3, bDot4, bDot5);
  101.         }

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