EulerStepInterpolator.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 linear interpolator for step.
  21.  *
  22.  * <p>This interpolator computes dense output inside the last
  23.  * step computed. The interpolation equation is consistent with the
  24.  * integration scheme :
  25.  * <ul>
  26.  *   <li>Using reference point at step start:<br>
  27.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub>) + &theta; h y'
  28.  *   </li>
  29.  *   <li>Using reference point at step end:<br>
  30.  *     y(t<sub>n</sub> + &theta; h) = y (t<sub>n</sub> + h) - (1-&theta;) h y'
  31.  *   </li>
  32.  * </ul>
  33.  *
  34.  * where &theta; belongs to [0 ; 1] and where y' is the evaluation of
  35.  * the derivatives already computed during the step.
  36.  *
  37.  * @see EulerIntegrator
  38.  * @since 1.2
  39.  */

  40. class EulerStepInterpolator
  41.   extends RungeKuttaStepInterpolator {

  42.   /** Serializable version identifier. */
  43.   private static final long serialVersionUID = 20111120L;

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

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

  68.   /** {@inheritDoc} */
  69.   @Override
  70.   protected StepInterpolator doCopy() {
  71.     return new EulerStepInterpolator(this);
  72.   }


  73.   /** {@inheritDoc} */
  74.   @Override
  75.   protected void computeInterpolatedStateAndDerivatives(final double theta,
  76.                                           final double oneMinusThetaH) {
  77.       if (previousState != null && theta <= 0.5) {
  78.           for (int i = 0; i < interpolatedState.length; ++i) {
  79.               interpolatedState[i] = previousState[i] + theta * h * yDotK[0][i];
  80.           }
  81.           System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length);
  82.       } else {
  83.           for (int i = 0; i < interpolatedState.length; ++i) {
  84.               interpolatedState[i] = currentState[i] - oneMinusThetaH * yDotK[0][i];
  85.           }
  86.           System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length);
  87.       }
  88.   }
  89. }