AbstractFieldODEStateInterpolator.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.CalculusFieldElement;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.ode.FieldEquationsMapper;
  25. import org.hipparchus.ode.FieldODEStateAndDerivative;
  26. import org.hipparchus.util.FastMath;

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

  40. public abstract class AbstractFieldODEStateInterpolator<T extends CalculusFieldElement<T>>
  41.     implements FieldODEStateInterpolator<T> {

  42.     /** Global previous state. */
  43.     private final FieldODEStateAndDerivative<T> globalPreviousState;

  44.     /** Global current state. */
  45.     private final FieldODEStateAndDerivative<T> globalCurrentState;

  46.     /** Soft previous state. */
  47.     private final FieldODEStateAndDerivative<T> softPreviousState;

  48.     /** Soft current state. */
  49.     private final FieldODEStateAndDerivative<T> softCurrentState;

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

  52.     /** Mapper for ODE equations primary and secondary components. */
  53.     private final FieldEquationsMapper<T> mapper;

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

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public AbstractFieldODEStateInterpolator<T> restrictStep(final FieldODEStateAndDerivative<T> previousState,
  78.                                                              final FieldODEStateAndDerivative<T> currentState) {
  79.         return create(forward, globalPreviousState, globalCurrentState, previousState, currentState, mapper);
  80.     }

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

  96.     /**
  97.      * Get the previous global grid point state.
  98.      * @return previous global grid point state
  99.      */
  100.     public FieldODEStateAndDerivative<T> getGlobalPreviousState() {
  101.         return globalPreviousState;
  102.     }

  103.     /**
  104.      * Get the current global grid point state.
  105.      * @return current global grid point state
  106.      */
  107.     public FieldODEStateAndDerivative<T> getGlobalCurrentState() {
  108.         return globalCurrentState;
  109.     }

  110.     /** {@inheritDoc} */
  111.     @Override
  112.     public FieldODEStateAndDerivative<T> getPreviousState() {
  113.         return softPreviousState;
  114.     }

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

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public FieldODEStateAndDerivative<T> getCurrentState() {
  123.         return softCurrentState;
  124.     }

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

  130.     /** {@inheritDoc} */
  131.     @Override
  132.     public FieldODEStateAndDerivative<T> getInterpolatedState(final T time) {
  133.         if (FastMath.abs(globalCurrentState.getTime().subtract(globalPreviousState.getTime()).getReal()) <=
  134.             FastMath.ulp(globalCurrentState.getTime().getReal())) {
  135.             return globalCurrentState;
  136.         }
  137.         final T thetaH         = time.subtract(globalPreviousState.getTime());
  138.         final T oneMinusThetaH = globalCurrentState.getTime().subtract(time);
  139.         final T theta          = thetaH.divide(globalCurrentState.getTime().subtract(globalPreviousState.getTime()));
  140.         return computeInterpolatedStateAndDerivatives(mapper, time, theta, thetaH, oneMinusThetaH);
  141.     }

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

  147.     /** Get the mapper for ODE equations primary and secondary components.
  148.      * @return mapper for ODE equations primary and secondary components
  149.      */
  150.     protected FieldEquationsMapper<T> getMapper() {
  151.         return mapper;
  152.     }

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

  170. }