HighamHall54StepInterpolator.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 represents an interpolator over the last step during an
  21.  * ODE integration for the 5(4) Higham and Hall integrator.
  22.  *
  23.  * @see HighamHall54Integrator
  24.  *
  25.  * @since 1.2
  26.  */

  27. class HighamHall54StepInterpolator
  28.   extends RungeKuttaStepInterpolator {

  29.   /** Serializable version identifier. */
  30.   private static final long serialVersionUID = 20111120L;

  31.   /** Simple constructor.
  32.    * This constructor builds an instance that is not usable yet, the
  33.    * {@link
  34.    * org.apache.commons.math4.legacy.ode.sampling.AbstractStepInterpolator#reinitialize}
  35.    * method should be called before using the instance in order to
  36.    * initialize the internal arrays. This constructor is used only
  37.    * in order to delay the initialization in some cases. The {@link
  38.    * EmbeddedRungeKuttaIntegrator} uses the prototyping design pattern
  39.    * to create the step interpolators by cloning an uninitialized model
  40.    * and later initializing the copy.
  41.    */
  42.   // CHECKSTYLE: stop RedundantModifier
  43.   // the public modifier here is needed for serialization
  44.   public HighamHall54StepInterpolator() {
  45.     super();
  46.   }
  47.   // CHECKSTYLE: resume RedundantModifier

  48.   /** Copy constructor.
  49.    * @param interpolator interpolator to copy from. The copy is a deep
  50.    * copy: its arrays are separated from the original arrays of the
  51.    * instance
  52.    */
  53.   HighamHall54StepInterpolator(final HighamHall54StepInterpolator interpolator) {
  54.     super(interpolator);
  55.   }

  56.   /** {@inheritDoc} */
  57.   @Override
  58.   protected StepInterpolator doCopy() {
  59.     return new HighamHall54StepInterpolator(this);
  60.   }


  61.   /** {@inheritDoc} */
  62.   @Override
  63.   protected void computeInterpolatedStateAndDerivatives(final double theta,
  64.                                           final double oneMinusThetaH) {

  65.     final double bDot0 = 1 + theta * (-15.0/2.0 + theta * (16.0 - 10.0 * theta));
  66.     final double bDot2 = theta * (459.0/16.0 + theta * (-729.0/8.0 + 135.0/2.0 * theta));
  67.     final double bDot3 = theta * (-44.0 + theta * (152.0 - 120.0 * theta));
  68.     final double bDot4 = theta * (375.0/16.0 + theta * (-625.0/8.0 + 125.0/2.0 * theta));
  69.     final double bDot5 = theta * 5.0/8.0 * (2 * theta - 1);

  70.     if (previousState != null && theta <= 0.5) {
  71.         final double hTheta = h * theta;
  72.         final double b0 = hTheta * (1.0 + theta * (-15.0/4.0  + theta * (16.0/3.0 - 5.0/2.0 * theta)));
  73.         final double b2 = hTheta * (      theta * (459.0/32.0 + theta * (-243.0/8.0 + theta * 135.0/8.0)));
  74.         final double b3 = hTheta * (      theta * (-22.0      + theta * (152.0/3.0  + theta * -30.0)));
  75.         final double b4 = hTheta * (      theta * (375.0/32.0 + theta * (-625.0/24.0 + theta * 125.0/8.0)));
  76.         final double b5 = hTheta * (      theta * (-5.0/16.0  + theta *  5.0/12.0));
  77.         for (int i = 0; i < interpolatedState.length; ++i) {
  78.             final double yDot0 = yDotK[0][i];
  79.             final double yDot2 = yDotK[2][i];
  80.             final double yDot3 = yDotK[3][i];
  81.             final double yDot4 = yDotK[4][i];
  82.             final double yDot5 = yDotK[5][i];
  83.             interpolatedState[i] =
  84.                     previousState[i] + b0 * yDot0 + b2 * yDot2 + b3 * yDot3 + b4 * yDot4 + b5 * yDot5;
  85.             interpolatedDerivatives[i] =
  86.                     bDot0 * yDot0 + bDot2 * yDot2 + bDot3 * yDot3 + bDot4 * yDot4 + bDot5 * yDot5;
  87.         }
  88.     } else {
  89.         final double theta2 = theta * theta;
  90.         final double b0 = h * (-1.0/12.0   + theta  * (1.0        + theta * (-15.0/4.0   + theta * (16.0/3.0 + theta * -5.0/2.0))));
  91.         final double b2 = h * (-27.0/32.0  + theta2 * (459.0/32.0 + theta * (-243.0/8.0  + theta * 135.0/8.0)));
  92.         final double b3 = h * (4.0/3.0     + theta2 * (-22.0      + theta * (152.0/3.0   + theta * -30.0)));
  93.         final double b4 = h * (-125.0/96.0 + theta2 * (375.0/32.0 + theta * (-625.0/24.0 + theta * 125.0/8.0)));
  94.         final double b5 = h * (-5.0/48.0   + theta2 * (-5.0/16.0  + theta *  5.0/12.0));
  95.         for (int i = 0; i < interpolatedState.length; ++i) {
  96.             final double yDot0 = yDotK[0][i];
  97.             final double yDot2 = yDotK[2][i];
  98.             final double yDot3 = yDotK[3][i];
  99.             final double yDot4 = yDotK[4][i];
  100.             final double yDot5 = yDotK[5][i];
  101.             interpolatedState[i] =
  102.                     currentState[i] + b0 * yDot0 + b2 * yDot2 + b3 * yDot3 + b4 * yDot4 + b5 * yDot5;
  103.             interpolatedDerivatives[i] =
  104.                     bDot0 * yDot0 + bDot2 * yDot2 + bDot3 * yDot3 + bDot4 * yDot4 + bDot5 * yDot5;
  105.         }
  106.     }
  107.   }
  108. }