RungeKuttaStateInterpolator.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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. package org.hipparchus.ode.nonstiff.interpolators;

  18. import org.hipparchus.ode.EquationsMapper;
  19. import org.hipparchus.ode.ODEStateAndDerivative;
  20. import org.hipparchus.ode.nonstiff.EmbeddedRungeKuttaIntegrator;
  21. import org.hipparchus.ode.nonstiff.FixedStepRungeKuttaIntegrator;
  22. import org.hipparchus.ode.sampling.AbstractODEStateInterpolator;

  23. /** This class represents an interpolator over the last step during an
  24.  * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators.
  25.  *
  26.  * @see FixedStepRungeKuttaIntegrator
  27.  * @see EmbeddedRungeKuttaIntegrator
  28.  *
  29.  */

  30. public abstract class RungeKuttaStateInterpolator extends AbstractODEStateInterpolator {

  31.     /** Serializable UID. */
  32.     private static final long serialVersionUID = 20160328L;

  33.     /** Slopes at the intermediate points */
  34.     protected double[][] yDotK;

  35.     /** Simple constructor.
  36.      * @param forward integration direction indicator
  37.      * @param yDotK slopes at the intermediate points
  38.      * @param globalPreviousState start of the global step
  39.      * @param globalCurrentState end of the global step
  40.      * @param softPreviousState start of the restricted step
  41.      * @param softCurrentState end of the restricted step
  42.      * @param mapper equations mapper for the all equations
  43.      */
  44.     protected RungeKuttaStateInterpolator(final boolean forward,
  45.                                           final double[][] yDotK,
  46.                                           final ODEStateAndDerivative globalPreviousState,
  47.                                           final ODEStateAndDerivative globalCurrentState,
  48.                                           final ODEStateAndDerivative softPreviousState,
  49.                                           final ODEStateAndDerivative softCurrentState,
  50.                                           final EquationsMapper mapper) {
  51.         super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
  52.         this.yDotK = new double[yDotK.length][];
  53.         for (int i = 0; i < yDotK.length; ++i) {
  54.             this.yDotK[i] = yDotK[i].clone();
  55.         }
  56.     }

  57.     /** {@inheritDoc} */
  58.     @Override
  59.     protected RungeKuttaStateInterpolator create(boolean newForward,
  60.                                                  ODEStateAndDerivative newGlobalPreviousState,
  61.                                                  ODEStateAndDerivative newGlobalCurrentState,
  62.                                                  ODEStateAndDerivative newSoftPreviousState,
  63.                                                  ODEStateAndDerivative newSoftCurrentState,
  64.                                                  EquationsMapper newMapper) {
  65.         return create(newForward, yDotK, newGlobalPreviousState, newGlobalCurrentState,
  66.                       newSoftPreviousState, newSoftCurrentState, newMapper);
  67.     }

  68.     /** Create a new instance.
  69.      * @param newForward integration direction indicator
  70.      * @param newYDotK slopes at the intermediate points
  71.      * @param newGlobalPreviousState start of the global step
  72.      * @param newGlobalCurrentState end of the global step
  73.      * @param newSoftPreviousState start of the restricted step
  74.      * @param newSoftCurrentState end of the restricted step
  75.      * @param newMapper equations mapper for the all equations
  76.      * @return a new instance
  77.      */
  78.     protected abstract RungeKuttaStateInterpolator create(boolean newForward, double[][] newYDotK,
  79.                                                           ODEStateAndDerivative newGlobalPreviousState,
  80.                                                           ODEStateAndDerivative newGlobalCurrentState,
  81.                                                           ODEStateAndDerivative newSoftPreviousState,
  82.                                                           ODEStateAndDerivative newSoftCurrentState,
  83.                                                           EquationsMapper newMapper);

  84.     /** Compute a state by linear combination added to previous state.
  85.      * @param coefficients coefficients to apply to the method staged derivatives
  86.      * @return combined state
  87.      */
  88.     protected final double[] previousStateLinearCombination(final double ... coefficients) {
  89.         return combine(getGlobalPreviousState().getCompleteState(),
  90.                        coefficients);
  91.     }

  92.     /** Compute a state by linear combination added to current state.
  93.      * @param coefficients coefficients to apply to the method staged derivatives
  94.      * @return combined state
  95.      */
  96.     protected double[] currentStateLinearCombination(final double ... coefficients) {
  97.         return combine(getGlobalCurrentState().getCompleteState(),
  98.                        coefficients);
  99.     }

  100.     /** Compute a state derivative by linear combination.
  101.      * @param coefficients coefficients to apply to the method staged derivatives
  102.      * @return combined state
  103.      */
  104.     protected double[] derivativeLinearCombination(final double ... coefficients) {
  105.         return combine(new double[yDotK[0].length], coefficients);
  106.     }

  107.     /** Linearly combine arrays.
  108.      * @param a array to add to
  109.      * @param coefficients coefficients to apply to the method staged derivatives
  110.      * @return a itself, as a conveniency for fluent API
  111.      */
  112.     private double[] combine(final double[] a, final double ... coefficients) {
  113.         for (int i = 0; i < a.length; ++i) {
  114.             for (int k = 0; k < coefficients.length; ++k) {
  115.                 a[i] += coefficients[k] * yDotK[k][i];
  116.             }
  117.         }
  118.         return a;
  119.     }

  120. }