StepNormalizer.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.math3.ode.sampling;

  18. import org.apache.commons.math3.exception.MaxCountExceededException;
  19. import org.apache.commons.math3.util.FastMath;
  20. import org.apache.commons.math3.util.Precision;

  21. /**
  22.  * This class wraps an object implementing {@link FixedStepHandler}
  23.  * into a {@link StepHandler}.

  24.  * <p>This wrapper allows to use fixed step handlers with general
  25.  * integrators which cannot guaranty their integration steps will
  26.  * remain constant and therefore only accept general step
  27.  * handlers.</p>
  28.  *
  29.  * <p>The stepsize used is selected at construction time. The {@link
  30.  * FixedStepHandler#handleStep handleStep} method of the underlying
  31.  * {@link FixedStepHandler} object is called at normalized times. The
  32.  * normalized times can be influenced by the {@link StepNormalizerMode} and
  33.  * {@link StepNormalizerBounds}.</p>
  34.  *
  35.  * <p>There is no constraint on the integrator, it can use any time step
  36.  * it needs (time steps longer or shorter than the fixed time step and
  37.  * non-integer ratios are all allowed).</p>
  38.  *
  39.  * <p>
  40.  * <table border="1" align="center">
  41.  * <tr BGCOLOR="#CCCCFF"><td colspan=6><font size="+2">Examples (step size = 0.5)</font></td></tr>
  42.  * <tr BGCOLOR="#EEEEFF"><font size="+1"><td>Start time</td><td>End time</td>
  43.  *  <td>Direction</td><td>{@link StepNormalizerMode Mode}</td>
  44.  *  <td>{@link StepNormalizerBounds Bounds}</td><td>Output</td></font></tr>
  45.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>0.8, 1.3, 1.8, 2.3, 2.8</td></tr>
  46.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>0.3, 0.8, 1.3, 1.8, 2.3, 2.8</td></tr>
  47.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>0.8, 1.3, 1.8, 2.3, 2.8, 3.1</td></tr>
  48.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>0.3, 0.8, 1.3, 1.8, 2.3, 2.8, 3.1</td></tr>
  49.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  50.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  51.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.1</td></tr>
  52.  * <tr><td>0.3</td><td>3.1</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.1</td></tr>
  53.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  54.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  55.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  56.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  57.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  58.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  59.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  60.  * <tr><td>0.0</td><td>3.0</td><td>forward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0</td></tr>
  61.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>2.6, 2.1, 1.6, 1.1, 0.6</td></tr>
  62.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>3.1, 2.6, 2.1, 1.6, 1.1, 0.6</td></tr>
  63.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>2.6, 2.1, 1.6, 1.1, 0.6, 0.3</td></tr>
  64.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>3.1, 2.6, 2.1, 1.6, 1.1, 0.6, 0.3</td></tr>
  65.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5</td></tr>
  66.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>3.1, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5</td></tr>
  67.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.3</td></tr>
  68.  * <tr><td>3.1</td><td>0.3</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>3.1, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.3</td></tr>
  69.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  70.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  71.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  72.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#INCREMENT INCREMENT}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  73.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#NEITHER NEITHER}</td><td>2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  74.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#FIRST FIRST}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  75.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#LAST LAST}</td><td>2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  76.  * <tr><td>3.0</td><td>0.0</td><td>backward</td><td>{@link StepNormalizerMode#MULTIPLES MULTIPLES}</td><td>{@link StepNormalizerBounds#BOTH BOTH}</td><td>3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0</td></tr>
  77.  * </table>
  78.  * </p>
  79.  *
  80.  * @see StepHandler
  81.  * @see FixedStepHandler
  82.  * @see StepNormalizerMode
  83.  * @see StepNormalizerBounds
  84.  * @since 1.2
  85.  */

  86. public class StepNormalizer implements StepHandler {
  87.     /** Fixed time step. */
  88.     private double h;

  89.     /** Underlying step handler. */
  90.     private final FixedStepHandler handler;

  91.     /** First step time. */
  92.     private double firstTime;

  93.     /** Last step time. */
  94.     private double lastTime;

  95.     /** Last state vector. */
  96.     private double[] lastState;

  97.     /** Last derivatives vector. */
  98.     private double[] lastDerivatives;

  99.     /** Integration direction indicator. */
  100.     private boolean forward;

  101.     /** The step normalizer bounds settings to use. */
  102.     private final StepNormalizerBounds bounds;

  103.     /** The step normalizer mode to use. */
  104.     private final StepNormalizerMode mode;

  105.     /** Simple constructor. Uses {@link StepNormalizerMode#INCREMENT INCREMENT}
  106.      * mode, and {@link StepNormalizerBounds#FIRST FIRST} bounds setting, for
  107.      * backwards compatibility.
  108.      * @param h fixed time step (sign is not used)
  109.      * @param handler fixed time step handler to wrap
  110.      */
  111.     public StepNormalizer(final double h, final FixedStepHandler handler) {
  112.         this(h, handler, StepNormalizerMode.INCREMENT,
  113.              StepNormalizerBounds.FIRST);
  114.     }

  115.     /** Simple constructor. Uses {@link StepNormalizerBounds#FIRST FIRST}
  116.      * bounds setting.
  117.      * @param h fixed time step (sign is not used)
  118.      * @param handler fixed time step handler to wrap
  119.      * @param mode step normalizer mode to use
  120.      * @since 3.0
  121.      */
  122.     public StepNormalizer(final double h, final FixedStepHandler handler,
  123.                           final StepNormalizerMode mode) {
  124.         this(h, handler, mode, StepNormalizerBounds.FIRST);
  125.     }

  126.     /** Simple constructor. Uses {@link StepNormalizerMode#INCREMENT INCREMENT}
  127.      * mode.
  128.      * @param h fixed time step (sign is not used)
  129.      * @param handler fixed time step handler to wrap
  130.      * @param bounds step normalizer bounds setting to use
  131.      * @since 3.0
  132.      */
  133.     public StepNormalizer(final double h, final FixedStepHandler handler,
  134.                           final StepNormalizerBounds bounds) {
  135.         this(h, handler, StepNormalizerMode.INCREMENT, bounds);
  136.     }

  137.     /** Simple constructor.
  138.      * @param h fixed time step (sign is not used)
  139.      * @param handler fixed time step handler to wrap
  140.      * @param mode step normalizer mode to use
  141.      * @param bounds step normalizer bounds setting to use
  142.      * @since 3.0
  143.      */
  144.     public StepNormalizer(final double h, final FixedStepHandler handler,
  145.                           final StepNormalizerMode mode,
  146.                           final StepNormalizerBounds bounds) {
  147.         this.h          = FastMath.abs(h);
  148.         this.handler    = handler;
  149.         this.mode       = mode;
  150.         this.bounds     = bounds;
  151.         firstTime       = Double.NaN;
  152.         lastTime        = Double.NaN;
  153.         lastState       = null;
  154.         lastDerivatives = null;
  155.         forward         = true;
  156.     }

  157.     /** {@inheritDoc} */
  158.     public void init(double t0, double[] y0, double t) {

  159.         firstTime       = Double.NaN;
  160.         lastTime        = Double.NaN;
  161.         lastState       = null;
  162.         lastDerivatives = null;
  163.         forward         = true;

  164.         // initialize the underlying handler
  165.         handler.init(t0, y0, t);

  166.     }

  167.     /**
  168.      * Handle the last accepted step
  169.      * @param interpolator interpolator for the last accepted step. For
  170.      * efficiency purposes, the various integrators reuse the same
  171.      * object on each call, so if the instance wants to keep it across
  172.      * all calls (for example to provide at the end of the integration a
  173.      * continuous model valid throughout the integration range), it
  174.      * should build a local copy using the clone method and store this
  175.      * copy.
  176.      * @param isLast true if the step is the last one
  177.      * @exception MaxCountExceededException if the interpolator throws one because
  178.      * the number of functions evaluations is exceeded
  179.      */
  180.     public void handleStep(final StepInterpolator interpolator, final boolean isLast)
  181.         throws MaxCountExceededException {
  182.         // The first time, update the last state with the start information.
  183.         if (lastState == null) {
  184.             firstTime = interpolator.getPreviousTime();
  185.             lastTime = interpolator.getPreviousTime();
  186.             interpolator.setInterpolatedTime(lastTime);
  187.             lastState = interpolator.getInterpolatedState().clone();
  188.             lastDerivatives = interpolator.getInterpolatedDerivatives().clone();

  189.             // Take the integration direction into account.
  190.             forward = interpolator.getCurrentTime() >= lastTime;
  191.             if (!forward) {
  192.                 h = -h;
  193.             }
  194.         }

  195.         // Calculate next normalized step time.
  196.         double nextTime = (mode == StepNormalizerMode.INCREMENT) ?
  197.                           lastTime + h :
  198.                           (FastMath.floor(lastTime / h) + 1) * h;
  199.         if (mode == StepNormalizerMode.MULTIPLES &&
  200.             Precision.equals(nextTime, lastTime, 1)) {
  201.             nextTime += h;
  202.         }

  203.         // Process normalized steps as long as they are in the current step.
  204.         boolean nextInStep = isNextInStep(nextTime, interpolator);
  205.         while (nextInStep) {
  206.             // Output the stored previous step.
  207.             doNormalizedStep(false);

  208.             // Store the next step as last step.
  209.             storeStep(interpolator, nextTime);

  210.             // Move on to the next step.
  211.             nextTime += h;
  212.             nextInStep = isNextInStep(nextTime, interpolator);
  213.         }

  214.         if (isLast) {
  215.             // There will be no more steps. The stored one should be given to
  216.             // the handler. We may have to output one more step. Only the last
  217.             // one of those should be flagged as being the last.
  218.             boolean addLast = bounds.lastIncluded() &&
  219.                               lastTime != interpolator.getCurrentTime();
  220.             doNormalizedStep(!addLast);
  221.             if (addLast) {
  222.                 storeStep(interpolator, interpolator.getCurrentTime());
  223.                 doNormalizedStep(true);
  224.             }
  225.         }
  226.     }

  227.     /**
  228.      * Returns a value indicating whether the next normalized time is in the
  229.      * current step.
  230.      * @param nextTime the next normalized time
  231.      * @param interpolator interpolator for the last accepted step, to use to
  232.      * get the end time of the current step
  233.      * @return value indicating whether the next normalized time is in the
  234.      * current step
  235.      */
  236.     private boolean isNextInStep(double nextTime,
  237.                                  StepInterpolator interpolator) {
  238.         return forward ?
  239.                nextTime <= interpolator.getCurrentTime() :
  240.                nextTime >= interpolator.getCurrentTime();
  241.     }

  242.     /**
  243.      * Invokes the underlying step handler for the current normalized step.
  244.      * @param isLast true if the step is the last one
  245.      */
  246.     private void doNormalizedStep(boolean isLast) {
  247.         if (!bounds.firstIncluded() && firstTime == lastTime) {
  248.             return;
  249.         }
  250.         handler.handleStep(lastTime, lastState, lastDerivatives, isLast);
  251.     }

  252.     /** Stores the interpolated information for the given time in the current
  253.      * state.
  254.      * @param interpolator interpolator for the last accepted step, to use to
  255.      * get the interpolated information
  256.      * @param t the time for which to store the interpolated information
  257.      * @exception MaxCountExceededException if the interpolator throws one because
  258.      * the number of functions evaluations is exceeded
  259.      */
  260.     private void storeStep(StepInterpolator interpolator, double t)
  261.         throws MaxCountExceededException {
  262.         lastTime = t;
  263.         interpolator.setInterpolatedTime(lastTime);
  264.         System.arraycopy(interpolator.getInterpolatedState(), 0,
  265.                          lastState, 0, lastState.length);
  266.         System.arraycopy(interpolator.getInterpolatedDerivatives(), 0,
  267.                          lastDerivatives, 0, lastDerivatives.length);
  268.     }
  269. }