MidpointStepInterpolator.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 second order
  21.  * Runge-Kutta integrator.
  22.  *
  23.  * <p>This interpolator computes 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>) + &theta; h [(1 - &theta;) y'<sub>1</sub> + &theta; y'<sub>2</sub>]
  29.  *   </li>
  30.  *   <li>Using reference point at step end:<br>
  31.  *   y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) + (1-&theta;) h [&theta; y'<sub>1</sub> - (1+&theta;) y'<sub>2</sub>]
  32.  *   </li>
  33.  * </ul>
  34.  *
  35.  * where &theta; belongs to [0 ; 1] and where y'<sub>1</sub> and y'<sub>2</sub> are the two
  36.  * evaluations of the derivatives already computed during the
  37.  * step.
  38.  *
  39.  * @see MidpointIntegrator
  40.  * @since 1.2
  41.  */

  42. class MidpointStepInterpolator
  43.   extends RungeKuttaStepInterpolator {

  44.   /** Serializable version identifier. */
  45.   private static final long serialVersionUID = 20111120L;

  46.   /** Simple constructor.
  47.    * This constructor builds an instance that is not usable yet, the
  48.    * {@link
  49.    * org.apache.commons.math4.legacy.ode.sampling.AbstractStepInterpolator#reinitialize}
  50.    * method should be called before using the instance in order to
  51.    * initialize the internal arrays. This constructor is used only
  52.    * in order to delay the initialization in some cases. The {@link
  53.    * RungeKuttaIntegrator} class uses the prototyping design pattern
  54.    * to create the step interpolators by cloning an uninitialized model
  55.    * and later initializing the copy.
  56.    */
  57.   // CHECKSTYLE: stop RedundantModifier
  58.   // the public modifier here is needed for serialization
  59.   public MidpointStepInterpolator() {
  60.   }
  61.   // CHECKSTYLE: resume RedundantModifier

  62.   /** Copy constructor.
  63.    * @param interpolator interpolator to copy from. The copy is a deep
  64.    * copy: its arrays are separated from the original arrays of the
  65.    * instance
  66.    */
  67.   MidpointStepInterpolator(final MidpointStepInterpolator interpolator) {
  68.     super(interpolator);
  69.   }

  70.   /** {@inheritDoc} */
  71.   @Override
  72.   protected StepInterpolator doCopy() {
  73.     return new MidpointStepInterpolator(this);
  74.   }


  75.   /** {@inheritDoc} */
  76.   @Override
  77.   protected void computeInterpolatedStateAndDerivatives(final double theta,
  78.                                           final double oneMinusThetaH) {

  79.     final double coeffDot2 = 2 * theta;
  80.     final double coeffDot1 = 1 - coeffDot2;

  81.     if (previousState != null && theta <= 0.5) {
  82.         final double coeff1    = theta * oneMinusThetaH;
  83.         final double coeff2    = theta * theta * h;
  84.         for (int i = 0; i < interpolatedState.length; ++i) {
  85.             final double yDot1 = yDotK[0][i];
  86.             final double yDot2 = yDotK[1][i];
  87.             interpolatedState[i] = previousState[i] + coeff1 * yDot1 + coeff2 * yDot2;
  88.             interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2;
  89.         }
  90.     } else {
  91.         final double coeff1    = oneMinusThetaH * theta;
  92.         final double coeff2    = oneMinusThetaH * (1.0 + theta);
  93.         for (int i = 0; i < interpolatedState.length; ++i) {
  94.             final double yDot1 = yDotK[0][i];
  95.             final double yDot2 = yDotK[1][i];
  96.             interpolatedState[i] = currentState[i] + coeff1 * yDot1 - coeff2 * yDot2;
  97.             interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2;
  98.         }
  99.     }
  100.   }
  101. }