AbstractODEStateInterpolator.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.ode.sampling;

  22. import org.hipparchus.exception.MathIllegalStateException;
  23. import org.hipparchus.ode.EquationsMapper;
  24. import org.hipparchus.ode.ODEStateAndDerivative;
  25. import org.hipparchus.util.FastMath;

  26. /** This abstract class represents an interpolator over the last step
  27.  * during an ODE integration.
  28.  *
  29.  * <p>The various ODE integrators provide objects extending this class
  30.  * to the step handlers. The handlers can use these objects to
  31.  * retrieve the state vector at intermediate times between the
  32.  * previous and the current grid points (dense output).</p>
  33.  *
  34.  * @see org.hipparchus.ode.ODEIntegrator
  35.  * @see ODEStepHandler
  36.  */

  37. public abstract class AbstractODEStateInterpolator
  38.     implements ODEStateInterpolator {

  39.     /** Serializable UID. */
  40.     private static final long serialVersionUID = 20160328L;

  41.     /** Global previous state. */
  42.     private final ODEStateAndDerivative globalPreviousState;

  43.     /** Global current state. */
  44.     private final ODEStateAndDerivative globalCurrentState;

  45.     /** Soft previous state. */
  46.     private final ODEStateAndDerivative softPreviousState;

  47.     /** Soft current state. */
  48.     private final ODEStateAndDerivative softCurrentState;

  49.     /** integration direction. */
  50.     private final boolean forward;

  51.     /** Mapper for ODE equations primary and secondary components. */
  52.     private final EquationsMapper mapper;

  53.     /** Simple constructor.
  54.      * @param isForward integration direction indicator
  55.      * @param globalPreviousState start of the global step
  56.      * @param globalCurrentState end of the global step
  57.      * @param softPreviousState start of the restricted step
  58.      * @param softCurrentState end of the restricted step
  59.      * @param equationsMapper mapper for ODE equations primary and secondary components
  60.      */
  61.     protected AbstractODEStateInterpolator(final boolean isForward,
  62.                                            final ODEStateAndDerivative globalPreviousState,
  63.                                            final ODEStateAndDerivative globalCurrentState,
  64.                                            final ODEStateAndDerivative softPreviousState,
  65.                                            final ODEStateAndDerivative softCurrentState,
  66.                                            final EquationsMapper equationsMapper) {
  67.         this.forward             = isForward;
  68.         this.globalPreviousState = globalPreviousState;
  69.         this.globalCurrentState  = globalCurrentState;
  70.         this.softPreviousState   = softPreviousState;
  71.         this.softCurrentState    = softCurrentState;
  72.         this.mapper              = equationsMapper;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public AbstractODEStateInterpolator restrictStep(final ODEStateAndDerivative previousState,
  77.                                                      final ODEStateAndDerivative currentState) {
  78.         return create(forward, globalPreviousState, globalCurrentState, previousState, currentState, mapper);
  79.     }

  80.     /** Create a new instance.
  81.      * @param newForward integration direction indicator
  82.      * @param newGlobalPreviousState start of the global step
  83.      * @param newGlobalCurrentState end of the global step
  84.      * @param newSoftPreviousState start of the restricted step
  85.      * @param newSoftCurrentState end of the restricted step
  86.      * @param newMapper equations mapper for the all equations
  87.      * @return a new instance
  88.      */
  89.     protected abstract AbstractODEStateInterpolator create(boolean newForward,
  90.                                                            ODEStateAndDerivative newGlobalPreviousState,
  91.                                                            ODEStateAndDerivative newGlobalCurrentState,
  92.                                                            ODEStateAndDerivative newSoftPreviousState,
  93.                                                            ODEStateAndDerivative newSoftCurrentState,
  94.                                                            EquationsMapper newMapper);

  95.     /**
  96.      * Get the previous global grid point state.
  97.      * @return previous global grid point state
  98.      */
  99.     public ODEStateAndDerivative getGlobalPreviousState() {
  100.         return globalPreviousState;
  101.     }

  102.     /**
  103.      * Get the current global grid point state.
  104.      * @return current global grid point state
  105.      */
  106.     public ODEStateAndDerivative getGlobalCurrentState() {
  107.         return globalCurrentState;
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public ODEStateAndDerivative getPreviousState() {
  112.         return softPreviousState;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public boolean isPreviousStateInterpolated() {
  117.         return softPreviousState != globalPreviousState;
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     public ODEStateAndDerivative getCurrentState() {
  122.         return softCurrentState;
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     public boolean isCurrentStateInterpolated() {
  127.         return softCurrentState != globalCurrentState;
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public ODEStateAndDerivative getInterpolatedState(final double time) {
  132.         if (FastMath.abs(globalCurrentState.getTime() - globalPreviousState.getTime()) <=
  133.                 FastMath.ulp(globalCurrentState.getTime())) {
  134.             return globalCurrentState;
  135.         }
  136.         final double thetaH         = time - globalPreviousState.getTime();
  137.         final double oneMinusThetaH = globalCurrentState.getTime() - time;
  138.         final double theta          = thetaH / (globalCurrentState.getTime() - globalPreviousState.getTime());
  139.         return computeInterpolatedStateAndDerivatives(mapper, time, theta, thetaH, oneMinusThetaH);
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     public boolean isForward() {
  144.         return forward;
  145.     }

  146.     /** Get the mapper for ODE equations primary and secondary components.
  147.      * @return mapper for ODE equations primary and secondary components
  148.      */
  149.     protected EquationsMapper getMapper() {
  150.         return mapper;
  151.     }

  152.     /** Compute the state and derivatives at the interpolated time.
  153.      * This is the main processing method that should be implemented by
  154.      * the derived classes to perform the interpolation.
  155.      * @param equationsMapper mapper for ODE equations primary and secondary components
  156.      * @param time interpolation time
  157.      * @param theta normalized interpolation abscissa within the step
  158.      * (theta is zero at the previous time step and one at the current time step)
  159.      * @param thetaH time gap between the previous time and the interpolated time
  160.      * @param oneMinusThetaH time gap between the interpolated time and
  161.      * the current time
  162.      * @return interpolated state and derivatives
  163.      * @exception MathIllegalStateException if the number of functions evaluations is exceeded
  164.      */
  165.     protected abstract ODEStateAndDerivative computeInterpolatedStateAndDerivatives(EquationsMapper equationsMapper,
  166.                                                                                     double time, double theta,
  167.                                                                                     double thetaH, double oneMinusThetaH)
  168.         throws MathIllegalStateException;

  169. }