RungeKuttaFieldStateInterpolator.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.EmbeddedRungeKuttaFieldIntegrator;
  27. import org.hipparchus.ode.nonstiff.FixedStepRungeKuttaFieldIntegrator;
  28. import org.hipparchus.ode.sampling.AbstractFieldODEStateInterpolator;
  29. import org.hipparchus.util.MathArrays;

  30. /** This class represents an interpolator over the last step during an
  31.  * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
  32.  *
  33.  * @see FixedStepRungeKuttaFieldIntegrator
  34.  * @see EmbeddedRungeKuttaFieldIntegrator
  35.  *
  36.  * @param <T> the type of the field elements
  37.  */

  38. public abstract class RungeKuttaFieldStateInterpolator<T extends CalculusFieldElement<T>>
  39.     extends AbstractFieldODEStateInterpolator<T> {

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

  42.     /** Slopes at the intermediate points. */
  43.     private final T[][] yDotK;

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

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     protected RungeKuttaFieldStateInterpolator<T> create(boolean newForward,
  71.                                                          FieldODEStateAndDerivative<T> newGlobalPreviousState,
  72.                                                          FieldODEStateAndDerivative<T> newGlobalCurrentState,
  73.                                                          FieldODEStateAndDerivative<T> newSoftPreviousState,
  74.                                                          FieldODEStateAndDerivative<T> newSoftCurrentState,
  75.                                                          FieldEquationsMapper<T> newMapper) {
  76.         return create(field, newForward, yDotK,
  77.                       newGlobalPreviousState, newGlobalCurrentState,
  78.                       newSoftPreviousState, newSoftCurrentState,
  79.                       newMapper);
  80.     }

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

  98.     /** Compute a state by linear combination added to previous state.
  99.      * @param coefficients coefficients to apply to the method staged derivatives
  100.      * @return combined state
  101.      */
  102.     @SafeVarargs
  103.     protected final T[] previousStateLinearCombination(final T ... coefficients) {
  104.         return combine(getGlobalPreviousState().getCompleteState(),
  105.                        coefficients);
  106.     }

  107.     /** Compute a state by linear combination added to current state.
  108.      * @param coefficients coefficients to apply to the method staged derivatives
  109.      * @return combined state
  110.      */
  111.     @SuppressWarnings("unchecked")
  112.     protected T[] currentStateLinearCombination(final T ... coefficients) {
  113.         return combine(getGlobalCurrentState().getCompleteState(),
  114.                        coefficients);
  115.     }

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

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

  138. }