ClassicalRungeKuttaStepInterpolator.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.ode.sampling.StepInterpolator;

  19. /**
  20.  * This class implements a step interpolator for the classical fourth
  21.  * order Runge-Kutta integrator.
  22.  *
  23.  * <p>This interpolator allows to compute dense output inside the last
  24.  * step computed. The interpolation equation is consistent with the
  25.  * integration scheme :
  26.  * <ul>
  27.  *   <li>Using reference point at step start:<br>
  28.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>)
  29.  *                    + &theta; (h/6) [  (6 - 9 &theta; + 4 &theta;<sup>2</sup>) y'<sub>1</sub>
  30.  *                                     + (    6 &theta; - 4 &theta;<sup>2</sup>) (y'<sub>2</sub> + y'<sub>3</sub>)
  31.  *                                     + (   -3 &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  32.  *                                    ]
  33.  *   </li>
  34.  *   <li>Using reference point at step end:<br>
  35.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  36.  *                    + (1 - &theta;) (h/6) [ (-4 &theta;^2 + 5 &theta; - 1) y'<sub>1</sub>
  37.  *                                          +(4 &theta;^2 - 2 &theta; - 2) (y'<sub>2</sub> + y'<sub>3</sub>)
  38.  *                                          -(4 &theta;^2 +   &theta; + 1) y'<sub>4</sub>
  39.  *                                        ]
  40.  *   </li>
  41.  * </ul>
  42.  *
  43.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  44.  * evaluations of the derivatives already computed during the
  45.  * step.
  46.  *
  47.  * @see ClassicalRungeKuttaIntegrator
  48.  * @since 1.2
  49.  */

  50. class ClassicalRungeKuttaStepInterpolator
  51.     extends RungeKuttaStepInterpolator {

  52.     /** Serializable version identifier. */
  53.     private static final long serialVersionUID = 20111120L;

  54.     /** Simple constructor.
  55.      * This constructor builds an instance that is not usable yet, the
  56.      * {@link RungeKuttaStepInterpolator#reinitialize} method should be
  57.      * called before using the instance in order to initialize the
  58.      * internal arrays. This constructor is used only in order to delay
  59.      * the initialization in some cases. The {@link RungeKuttaIntegrator}
  60.      * class uses the prototyping design pattern to create the step
  61.      * interpolators by cloning an uninitialized model and latter initializing
  62.      * the copy.
  63.      */
  64.     // CHECKSTYLE: stop RedundantModifier
  65.     // the public modifier here is needed for serialization
  66.     public ClassicalRungeKuttaStepInterpolator() {
  67.     }
  68.     // CHECKSTYLE: resume RedundantModifier

  69.     /** Copy constructor.
  70.      * @param interpolator interpolator to copy from. The copy is a deep
  71.      * copy: its arrays are separated from the original arrays of the
  72.      * instance
  73.      */
  74.     ClassicalRungeKuttaStepInterpolator(final ClassicalRungeKuttaStepInterpolator interpolator) {
  75.         super(interpolator);
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected StepInterpolator doCopy() {
  80.         return new ClassicalRungeKuttaStepInterpolator(this);
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     protected void computeInterpolatedStateAndDerivatives(final double theta,
  85.                                             final double oneMinusThetaH) {

  86.         final double oneMinusTheta  = 1 - theta;
  87.         final double oneMinus2Theta = 1 - 2 * theta;
  88.         final double coeffDot1     = oneMinusTheta * oneMinus2Theta;
  89.         final double coeffDot23    = 2 * theta * oneMinusTheta;
  90.         final double coeffDot4     = -theta * oneMinus2Theta;
  91.         if (previousState != null && theta <= 0.5) {
  92.             final double fourTheta2     = 4 * theta * theta;
  93.             final double s             = theta * h / 6.0;
  94.             final double coeff1        = s * ( 6 - 9 * theta + fourTheta2);
  95.             final double coeff23       = s * ( 6 * theta - fourTheta2);
  96.             final double coeff4        = s * (-3 * theta + fourTheta2);
  97.             for (int i = 0; i < interpolatedState.length; ++i) {
  98.                 final double yDot1  = yDotK[0][i];
  99.                 final double yDot23 = yDotK[1][i] + yDotK[2][i];
  100.                 final double yDot4  = yDotK[3][i];
  101.                 interpolatedState[i] =
  102.                         previousState[i] + coeff1  * yDot1 + coeff23 * yDot23 + coeff4  * yDot4;
  103.                 interpolatedDerivatives[i] =
  104.                         coeffDot1 * yDot1 + coeffDot23 * yDot23 + coeffDot4 * yDot4;
  105.             }
  106.         } else {
  107.             final double fourTheta      = 4 * theta;
  108.             final double s             = oneMinusThetaH / 6.0;
  109.             final double coeff1        = s * ((-fourTheta + 5) * theta - 1);
  110.             final double coeff23       = s * (( fourTheta - 2) * theta - 2);
  111.             final double coeff4        = s * ((-fourTheta - 1) * theta - 1);
  112.             for (int i = 0; i < interpolatedState.length; ++i) {
  113.                 final double yDot1  = yDotK[0][i];
  114.                 final double yDot23 = yDotK[1][i] + yDotK[2][i];
  115.                 final double yDot4  = yDotK[3][i];
  116.                 interpolatedState[i] =
  117.                         currentState[i] + coeff1  * yDot1 + coeff23 * yDot23 + coeff4  * yDot4;
  118.                 interpolatedDerivatives[i] =
  119.                         coeffDot1 * yDot1 + coeffDot23 * yDot23 + coeffDot4 * yDot4;
  120.             }
  121.         }
  122.     }
  123. }