EulerFieldStepInterpolator.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 linear interpolator for step.
  24.  *
  25.  * <p>This interpolator computes dense output inside the last
  26.  * step computed. The interpolation equation is consistent with the
  27.  * integration scheme :
  28.  * <ul>
  29.  *   <li>Using reference point at step start:<br>
  30.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>) + &theta; h y'
  31.  *   </li>
  32.  *   <li>Using reference point at step end:<br>
  33.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) - (1-&theta;) h y'
  34.  *   </li>
  35.  * </ul>
  36.  *
  37.  * where &theta; belongs to [0 ; 1] and where y' is the evaluation of
  38.  * the derivatives already computed during the step.
  39.  *
  40.  * @see EulerFieldIntegrator
  41.  * @param <T> the type of the field elements
  42.  * @since 3.6
  43.  */

  44. class EulerFieldStepInterpolator<T extends RealFieldElement<T>>
  45.     extends RungeKuttaFieldStepInterpolator<T> {

  46.     /** Simple constructor.
  47.      * @param field field to which the time and state vector elements belong
  48.      * @param forward integration direction indicator
  49.      * @param yDotK slopes at the intermediate points
  50.      * @param globalPreviousState start of the global step
  51.      * @param globalCurrentState end of the global step
  52.      * @param softPreviousState start of the restricted step
  53.      * @param softCurrentState end of the restricted step
  54.      * @param mapper equations mapper for the all equations
  55.      */
  56.     EulerFieldStepInterpolator(final Field<T> field, final boolean forward,
  57.                                              final T[][] yDotK,
  58.                                              final FieldODEStateAndDerivative<T> globalPreviousState,
  59.                                              final FieldODEStateAndDerivative<T> globalCurrentState,
  60.                                              final FieldODEStateAndDerivative<T> softPreviousState,
  61.                                              final FieldODEStateAndDerivative<T> softCurrentState,
  62.                                              final FieldEquationsMapper<T> mapper) {
  63.         super(field, forward, yDotK,
  64.               globalPreviousState, globalCurrentState, softPreviousState, softCurrentState,
  65.               mapper);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     protected EulerFieldStepInterpolator<T> create(final Field<T> newField, final boolean newForward, final T[][] newYDotK,
  70.                                                                  final FieldODEStateAndDerivative<T> newGlobalPreviousState,
  71.                                                                  final FieldODEStateAndDerivative<T> newGlobalCurrentState,
  72.                                                                  final FieldODEStateAndDerivative<T> newSoftPreviousState,
  73.                                                                  final FieldODEStateAndDerivative<T> newSoftCurrentState,
  74.                                                                  final FieldEquationsMapper<T> newMapper) {
  75.         return new EulerFieldStepInterpolator<>(newField, newForward, newYDotK,
  76.                                                  newGlobalPreviousState, newGlobalCurrentState,
  77.                                                  newSoftPreviousState, newSoftCurrentState,
  78.                                                  newMapper);
  79.     }

  80.     /** {@inheritDoc} */
  81.     @SuppressWarnings("unchecked")
  82.     @Override
  83.     protected FieldODEStateAndDerivative<T> computeInterpolatedStateAndDerivatives(final FieldEquationsMapper<T> mapper,
  84.                                                                                    final T time, final T theta,
  85.                                                                                    final T thetaH, final T oneMinusThetaH) {
  86.         final T[] interpolatedState;
  87.         final T[] interpolatedDerivatives;
  88.         if (getGlobalPreviousState() != null && theta.getReal() <= 0.5) {
  89.             interpolatedState       = previousStateLinearCombination(thetaH);
  90.             interpolatedDerivatives = derivativeLinearCombination(time.getField().getOne());
  91.         } else {
  92.             interpolatedState       = currentStateLinearCombination(oneMinusThetaH.negate());
  93.             interpolatedDerivatives = derivativeLinearCombination(time.getField().getOne());
  94.         }

  95.         return new FieldODEStateAndDerivative<>(time, interpolatedState, interpolatedDerivatives);
  96.     }
  97. }