GillStepInterpolator.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. import org.apache.commons.math4.core.jdkmath.JdkMath;

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

  50. class GillStepInterpolator
  51.   extends RungeKuttaStepInterpolator {

  52.     /** First Gill coefficient. */
  53.     private static final double ONE_MINUS_INV_SQRT_2 = 1 - JdkMath.sqrt(0.5);

  54.     /** Second Gill coefficient. */
  55.     private static final double ONE_PLUS_INV_SQRT_2 = 1 + JdkMath.sqrt(0.5);

  56.     /** Serializable version identifier. */
  57.     private static final long serialVersionUID = 20111120L;

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

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

  82.   /** {@inheritDoc} */
  83.   @Override
  84.   protected StepInterpolator doCopy() {
  85.     return new GillStepInterpolator(this);
  86.   }


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

  91.     final double twoTheta   = 2 * theta;
  92.     final double fourTheta2 = twoTheta * twoTheta;
  93.     final double coeffDot1  = theta * (twoTheta - 3) + 1;
  94.     final double cDot23     = twoTheta * (1 - theta);
  95.     final double coeffDot2  = cDot23  * ONE_MINUS_INV_SQRT_2;
  96.     final double coeffDot3  = cDot23  * ONE_PLUS_INV_SQRT_2;
  97.     final double coeffDot4  = theta * (twoTheta - 1);

  98.     if (previousState != null && theta <= 0.5) {
  99.         final double s         = theta * h / 6.0;
  100.         final double c23       = s * (6 * theta - fourTheta2);
  101.         final double coeff1    = s * (6 - 9 * theta + fourTheta2);
  102.         final double coeff2    = c23  * ONE_MINUS_INV_SQRT_2;
  103.         final double coeff3    = c23  * ONE_PLUS_INV_SQRT_2;
  104.         final double coeff4    = s * (-3 * theta + fourTheta2);
  105.         for (int i = 0; i < interpolatedState.length; ++i) {
  106.             final double yDot1 = yDotK[0][i];
  107.             final double yDot2 = yDotK[1][i];
  108.             final double yDot3 = yDotK[2][i];
  109.             final double yDot4 = yDotK[3][i];
  110.             interpolatedState[i] =
  111.                     previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4;
  112.             interpolatedDerivatives[i] =
  113.                     coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
  114.         }
  115.     } else {
  116.         final double s      = oneMinusThetaH / 6.0;
  117.         final double c23    = s * (2 + twoTheta - fourTheta2);
  118.         final double coeff1 = s * (1 - 5 * theta + fourTheta2);
  119.         final double coeff2 = c23  * ONE_MINUS_INV_SQRT_2;
  120.         final double coeff3 = c23  * ONE_PLUS_INV_SQRT_2;
  121.         final double coeff4 = s * (1 + theta + fourTheta2);
  122.         for (int i = 0; i < interpolatedState.length; ++i) {
  123.             final double yDot1 = yDotK[0][i];
  124.             final double yDot2 = yDotK[1][i];
  125.             final double yDot3 = yDotK[2][i];
  126.             final double yDot4 = yDotK[3][i];
  127.             interpolatedState[i] =
  128.                     currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4;
  129.             interpolatedDerivatives[i] =
  130.                     coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4;
  131.         }
  132.     }
  133.   }
  134. }