ThreeEighthesStepInterpolator.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 3/8 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/8) [ (8 - 15 &theta; +  8 &theta;<sup>2</sup>) y'<sub>1</sub>
  30.  *                                     +  3 * (15 &theta; - 12 &theta;<sup>2</sup>) y'<sub>2</sub>
  31.  *                                     +        3 &theta;                           y'<sub>3</sub>
  32.  *                                     +      (-3 &theta; +  4 &theta;<sup>2</sup>) y'<sub>4</sub>
  33.  *                                    ]
  34.  *   </li>
  35.  *   <li>Using reference point at step end:<br>
  36.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h)
  37.  *                      - (1 - &theta;) (h/8) [(1 - 7 &theta; + 8 &theta;<sup>2</sup>) y'<sub>1</sub>
  38.  *                                         + 3 (1 +   &theta; - 4 &theta;<sup>2</sup>) y'<sub>2</sub>
  39.  *                                         + 3 (1 +   &theta;)                         y'<sub>3</sub>
  40.  *                                         +   (1 +   &theta; + 4 &theta;<sup>2</sup>) y'<sub>4</sub>
  41.  *                                          ]
  42.  *   </li>
  43.  * </ul>
  44.  *
  45.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> to y'<sub>4</sub> are the four
  46.  * evaluations of the derivatives already computed during the
  47.  * step.
  48.  *
  49.  * @see ThreeEighthesIntegrator
  50.  * @since 1.2
  51.  */

  52. class ThreeEighthesStepInterpolator
  53.   extends RungeKuttaStepInterpolator {

  54.   /** Serializable version identifier. */
  55.   private static final long serialVersionUID = 20111120L;

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

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

  80.   /** {@inheritDoc} */
  81.   @Override
  82.   protected StepInterpolator doCopy() {
  83.     return new ThreeEighthesStepInterpolator(this);
  84.   }


  85.   /** {@inheritDoc} */
  86.   @Override
  87.   protected void computeInterpolatedStateAndDerivatives(final double theta,
  88.                                           final double oneMinusThetaH) {

  89.       final double coeffDot3  = 0.75 * theta;
  90.       final double coeffDot1  = coeffDot3 * (4 * theta - 5) + 1;
  91.       final double coeffDot2  = coeffDot3 * (5 - 6 * theta);
  92.       final double coeffDot4  = coeffDot3 * (2 * theta - 1);

  93.       if (previousState != null && theta <= 0.5) {
  94.           final double s          = theta * h / 8.0;
  95.           final double fourTheta2 = 4 * theta * theta;
  96.           final double coeff1     = s * (8 - 15 * theta + 2 * fourTheta2);
  97.           final double coeff2     = 3 * s * (5 * theta - fourTheta2);
  98.           final double coeff3     = 3 * s * theta;
  99.           final double coeff4     = s * (-3 * theta + fourTheta2);
  100.           for (int i = 0; i < interpolatedState.length; ++i) {
  101.               final double yDot1 = yDotK[0][i];
  102.               final double yDot2 = yDotK[1][i];
  103.               final double yDot3 = yDotK[2][i];
  104.               final double yDot4 = yDotK[3][i];
  105.               interpolatedState[i] =
  106.                       previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4;
  107.               interpolatedDerivatives[i] =
  108.                       coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
  109.           }
  110.       } else {
  111.           final double s          = oneMinusThetaH / 8.0;
  112.           final double fourTheta2 = 4 * theta * theta;
  113.           final double coeff1     = s * (1 - 7 * theta + 2 * fourTheta2);
  114.           final double coeff2     = 3 * s * (1 + theta - fourTheta2);
  115.           final double coeff3     = 3 * s * (1 + theta);
  116.           final double coeff4     = s * (1 + theta + fourTheta2);
  117.           for (int i = 0; i < interpolatedState.length; ++i) {
  118.               final double yDot1 = yDotK[0][i];
  119.               final double yDot2 = yDotK[1][i];
  120.               final double yDot3 = yDotK[2][i];
  121.               final double yDot4 = yDotK[3][i];
  122.               interpolatedState[i] =
  123.                       currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4;
  124.               interpolatedDerivatives[i] =
  125.                       coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
  126.           }
  127.       }
  128.   }
  129. }