RungeKuttaFieldStepInterpolator.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. import org.apache.commons.math4.legacy.ode.sampling.AbstractFieldStepInterpolator;
  23. import org.apache.commons.math4.legacy.core.MathArrays;

  24. /** This class represents an interpolator over the last step during an
  25.  * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
  26.  *
  27.  * @see RungeKuttaFieldIntegrator
  28.  * @see EmbeddedRungeKuttaFieldIntegrator
  29.  *
  30.  * @param <T> the type of the field elements
  31.  * @since 3.6
  32.  */

  33. abstract class RungeKuttaFieldStepInterpolator<T extends RealFieldElement<T>>
  34.     extends AbstractFieldStepInterpolator<T> {

  35.     /** Field to which the time and state vector elements belong. */
  36.     private final Field<T> field;

  37.     /** Slopes at the intermediate points. */
  38.     private final T[][] yDotK;

  39.     /** Simple constructor.
  40.      * @param field field to which the time and state vector elements belong
  41.      * @param forward integration direction indicator
  42.      * @param yDotK slopes at the intermediate points
  43.      * @param globalPreviousState start of the global step
  44.      * @param globalCurrentState end of the global step
  45.      * @param softPreviousState start of the restricted step
  46.      * @param softCurrentState end of the restricted step
  47.      * @param mapper equations mapper for the all equations
  48.      */
  49.     protected RungeKuttaFieldStepInterpolator(final Field<T> field, final boolean forward,
  50.                                               final T[][] yDotK,
  51.                                               final FieldODEStateAndDerivative<T> globalPreviousState,
  52.                                               final FieldODEStateAndDerivative<T> globalCurrentState,
  53.                                               final FieldODEStateAndDerivative<T> softPreviousState,
  54.                                               final FieldODEStateAndDerivative<T> softCurrentState,
  55.                                               final FieldEquationsMapper<T> mapper) {
  56.         super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
  57.         this.field = field;
  58.         this.yDotK = MathArrays.buildArray(field, yDotK.length, -1);
  59.         for (int i = 0; i < yDotK.length; ++i) {
  60.             this.yDotK[i] = yDotK[i].clone();
  61.         }
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     protected RungeKuttaFieldStepInterpolator<T> create(boolean newForward,
  66.                                                         FieldODEStateAndDerivative<T> newGlobalPreviousState,
  67.                                                         FieldODEStateAndDerivative<T> newGlobalCurrentState,
  68.                                                         FieldODEStateAndDerivative<T> newSoftPreviousState,
  69.                                                         FieldODEStateAndDerivative<T> newSoftCurrentState,
  70.                                                         FieldEquationsMapper<T> newMapper) {
  71.         return create(field, newForward, yDotK,
  72.                       newGlobalPreviousState, newGlobalCurrentState,
  73.                       newSoftPreviousState, newSoftCurrentState,
  74.                       newMapper);
  75.     }

  76.     /** Create a new instance.
  77.      * @param newField field to which the time and state vector elements belong
  78.      * @param newForward integration direction indicator
  79.      * @param newYDotK slopes at the intermediate points
  80.      * @param newGlobalPreviousState start of the global step
  81.      * @param newGlobalCurrentState end of the global step
  82.      * @param newSoftPreviousState start of the restricted step
  83.      * @param newSoftCurrentState end of the restricted step
  84.      * @param newMapper equations mapper for the all equations
  85.      * @return a new instance
  86.      */
  87.     protected abstract RungeKuttaFieldStepInterpolator<T> create(Field<T> newField, boolean newForward, T[][] newYDotK,
  88.                                                                  FieldODEStateAndDerivative<T> newGlobalPreviousState,
  89.                                                                  FieldODEStateAndDerivative<T> newGlobalCurrentState,
  90.                                                                  FieldODEStateAndDerivative<T> newSoftPreviousState,
  91.                                                                  FieldODEStateAndDerivative<T> newSoftCurrentState,
  92.                                                                  FieldEquationsMapper<T> newMapper);

  93.     /** Compute a state by linear combination added to previous state.
  94.      * @param coefficients coefficients to apply to the method staged derivatives
  95.      * @return combined state
  96.      */
  97.     @SafeVarargs
  98.     protected final T[] previousStateLinearCombination(final T ... coefficients) {
  99.         return combine(getPreviousState().getState(),
  100.                        coefficients);
  101.     }

  102.     /** Compute a state by linear combination added to current state.
  103.      * @param coefficients coefficients to apply to the method staged derivatives
  104.      * @return combined state
  105.      */
  106.     @SuppressWarnings("unchecked")
  107.     protected T[] currentStateLinearCombination(final T ... coefficients) {
  108.         return combine(getCurrentState().getState(),
  109.                        coefficients);
  110.     }

  111.     /** Compute a state derivative by linear combination.
  112.      * @param coefficients coefficients to apply to the method staged derivatives
  113.      * @return combined state
  114.      */
  115.     @SuppressWarnings("unchecked")
  116.     protected T[] derivativeLinearCombination(final T ... coefficients) {
  117.         return combine(MathArrays.buildArray(field, yDotK[0].length), coefficients);
  118.     }

  119.     /** Linearly combine arrays.
  120.      * @param a array to add to
  121.      * @param coefficients coefficients to apply to the method staged derivatives
  122.      * @return a itself, as a convenience for fluent API
  123.      */
  124.     @SuppressWarnings("unchecked")
  125.     private T[] combine(final T[] a, final T ... coefficients) {
  126.         for (int i = 0; i < a.length; ++i) {
  127.             for (int k = 0; k < coefficients.length; ++k) {
  128.                 a[i] = a[i].add(coefficients[k].multiply(yDotK[k][i]));
  129.             }
  130.         }
  131.         return a;
  132.     }
  133. }