FieldEventState.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.orekit.propagation.events;

  18. import java.util.function.DoubleFunction;

  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.Field;
  21. import org.hipparchus.analysis.UnivariateFunction;
  22. import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
  23. import org.hipparchus.analysis.solvers.BracketedUnivariateSolver.Interval;
  24. import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
  25. import org.hipparchus.exception.MathRuntimeException;
  26. import org.hipparchus.ode.events.Action;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.Precision;
  29. import org.orekit.errors.OrekitException;
  30. import org.orekit.errors.OrekitInternalError;
  31. import org.orekit.errors.OrekitMessages;
  32. import org.orekit.propagation.FieldSpacecraftState;
  33. import org.orekit.propagation.sampling.FieldOrekitStepInterpolator;
  34. import org.orekit.time.FieldAbsoluteDate;

  35. /** This class handles the state for one {@link FieldEventDetector
  36.  * event detector} during integration steps.
  37.  *
  38.  * <p>This class is heavily based on the class with the same name from the
  39.  * Hipparchus library. The changes performed consist in replacing
  40.  * raw types (double and double arrays) with space dynamics types
  41.  * ({@link FieldAbsoluteDate}, {@link FieldSpacecraftState}).</p>
  42.  * <p>Each time the propagator proposes a step, the event detector
  43.  * should be checked. This class handles the state of one detector
  44.  * during one propagation step, with references to the state at the
  45.  * end of the preceding step. This information is used to determine if
  46.  * the detector should trigger an event or not during the proposed
  47.  * step (and hence the step should be reduced to ensure the event
  48.  * occurs at a bound rather than inside the step).</p>
  49.  * @author Luc Maisonobe
  50.  * @param <D> class type for the generic version
  51.  */
  52. public class FieldEventState<D extends FieldEventDetector<T>, T extends CalculusFieldElement<T>> {

  53.     /** Event detector. */
  54.     private D detector;

  55.     /** Time of the previous call to g. */
  56.     private FieldAbsoluteDate<T> lastT;

  57.     /** Value from the previous call to g. */
  58.     private T lastG;

  59.     /** Time at the beginning of the step. */
  60.     private FieldAbsoluteDate<T> t0;

  61.     /** Value of the event detector at the beginning of the step. */
  62.     private T g0;

  63.     /** Simulated sign of g0 (we cheat when crossing events). */
  64.     private boolean g0Positive;

  65.     /** Indicator of event expected during the step. */
  66.     private boolean pendingEvent;

  67.     /** Occurrence time of the pending event. */
  68.     private FieldAbsoluteDate<T> pendingEventTime;

  69.     /**
  70.      * Time to stop propagation if the event is a stop event. Used to enable stopping at
  71.      * an event and then restarting after that event.
  72.      */
  73.     private FieldAbsoluteDate<T> stopTime;

  74.     /** Time after the current event. */
  75.     private FieldAbsoluteDate<T> afterEvent;

  76.     /** Value of the g function after the current event. */
  77.     private T afterG;

  78.     /** The earliest time considered for events. */
  79.     private FieldAbsoluteDate<T> earliestTimeConsidered;

  80.     /** Integration direction. */
  81.     private boolean forward;

  82.     /** Variation direction around pending event.
  83.      *  (this is considered with respect to the integration direction)
  84.      */
  85.     private boolean increasing;

  86.     /** Simple constructor.
  87.      * @param detector monitored event detector
  88.      */
  89.     public FieldEventState(final D detector) {

  90.         this.detector = detector;

  91.         // some dummy values ...
  92.         final Field<T> field   = detector.getMaxCheckInterval().getField();
  93.         final T nan            = field.getZero().add(Double.NaN);
  94.         lastT                  = FieldAbsoluteDate.getPastInfinity(field);
  95.         lastG                  = nan;
  96.         t0                     = null;
  97.         g0                     = nan;
  98.         g0Positive             = true;
  99.         pendingEvent           = false;
  100.         pendingEventTime       = null;
  101.         stopTime               = null;
  102.         increasing             = true;
  103.         earliestTimeConsidered = null;
  104.         afterEvent             = null;
  105.         afterG                 = nan;

  106.     }

  107.     /** Get the underlying event detector.
  108.      * @return underlying event detector
  109.      */
  110.     public D getEventDetector() {
  111.         return detector;
  112.     }

  113.     /** Initialize event handler at the start of a propagation.
  114.      * <p>
  115.      * This method is called once at the start of the propagation. It
  116.      * may be used by the event handler to initialize some internal data
  117.      * if needed.
  118.      * </p>
  119.      * @param s0 initial state
  120.      * @param t target time for the integration
  121.      *
  122.      */
  123.     public void init(final FieldSpacecraftState<T> s0,
  124.                      final FieldAbsoluteDate<T> t) {
  125.         detector.init(s0, t);
  126.         final Field<T> field = detector.getMaxCheckInterval().getField();
  127.         lastT = FieldAbsoluteDate.getPastInfinity(field);
  128.         lastG = field.getZero().add(Double.NaN);
  129.     }

  130.     /** Compute the value of the switching function.
  131.      * This function must be continuous (at least in its roots neighborhood),
  132.      * as the integrator will need to find its roots to locate the events.
  133.      * @param s the current state information: date, kinematics, attitude
  134.      * @return value of the switching function
  135.      */
  136.     private T g(final FieldSpacecraftState<T> s) {
  137.         if (!s.getDate().equals(lastT)) {
  138.             lastT = s.getDate();
  139.             lastG = detector.g(s);
  140.         }
  141.         return lastG;
  142.     }

  143.     /** Reinitialize the beginning of the step.
  144.      * @param interpolator interpolator valid for the current step
  145.      */
  146.     public void reinitializeBegin(final FieldOrekitStepInterpolator<T> interpolator) {
  147.         forward = interpolator.isForward();
  148.         final FieldSpacecraftState<T> s0 = interpolator.getPreviousState();
  149.         this.t0 = s0.getDate();
  150.         g0 = g(s0);
  151.         while (g0.getReal() == 0) {
  152.             // extremely rare case: there is a zero EXACTLY at interval start
  153.             // we will use the sign slightly after step beginning to force ignoring this zero
  154.             // try moving forward by half a convergence interval
  155.             final T dt = detector.getThreshold().multiply(forward ? 0.5 : -0.5);
  156.             FieldAbsoluteDate<T> startDate = t0.shiftedBy(dt);
  157.             // if convergence is too small move an ulp
  158.             if (t0.equals(startDate)) {
  159.                 startDate = nextAfter(startDate);
  160.             }
  161.             t0 = startDate;
  162.             g0 = g(interpolator.getInterpolatedState(t0));
  163.         }
  164.         g0Positive = g0.getReal() > 0;
  165.         // "last" event was increasing
  166.         increasing = g0Positive;
  167.     }

  168.     /** Evaluate the impact of the proposed step on the event detector.
  169.      * @param interpolator step interpolator for the proposed step
  170.      * @return true if the event detector triggers an event before
  171.      * the end of the proposed step (this implies the step should be
  172.      * rejected)
  173.      * @exception MathRuntimeException if an event cannot be located
  174.      */
  175.     public boolean evaluateStep(final FieldOrekitStepInterpolator<T> interpolator)
  176.         throws MathRuntimeException {
  177.         forward = interpolator.isForward();
  178.         final FieldSpacecraftState<T> s1 = interpolator.getCurrentState();
  179.         final FieldAbsoluteDate<T> t1 = s1.getDate();
  180.         final T dt = t1.durationFrom(t0);
  181.         if (FastMath.abs(dt.getReal()) < detector.getThreshold().getReal()) {
  182.             // we cannot do anything on such a small step, don't trigger any events
  183.             return false;
  184.         }
  185.         // number of points to check in the current step
  186.         final int n = FastMath.max(1, (int) FastMath.ceil(FastMath.abs(dt.getReal()) / detector.getMaxCheckInterval().getReal()));
  187.         final T h = dt.divide(n);


  188.         FieldAbsoluteDate<T> ta = t0;
  189.         T ga = g0;
  190.         for (int i = 0; i < n; ++i) {

  191.             // evaluate handler value at the end of the substep
  192.             final FieldAbsoluteDate<T> tb = (i == n - 1) ? t1 : t0.shiftedBy(h.multiply(i + 1));
  193.             final T gb = g(interpolator.getInterpolatedState(tb));

  194.             // check events occurrence
  195.             if (gb.getReal() == 0.0 || (g0Positive ^ (gb.getReal() > 0))) {
  196.                 // there is a sign change: an event is expected during this step
  197.                 if (findRoot(interpolator, ta, ga, tb, gb)) {
  198.                     return true;
  199.                 }
  200.             } else {
  201.                 // no sign change: there is no event for now
  202.                 ta = tb;
  203.                 ga = gb;
  204.             }
  205.         }

  206.         // no event during the whole step
  207.         pendingEvent     = false;
  208.         pendingEventTime = null;
  209.         return false;

  210.     }

  211.     /**
  212.      * Find a root in a bracketing interval.
  213.      *
  214.      * <p> When calling this method one of the following must be true. Either ga == 0, gb
  215.      * == 0, (ga < 0  and gb > 0), or (ga > 0 and gb < 0).
  216.      *
  217.      * @param interpolator that covers the interval.
  218.      * @param ta           earliest possible time for root.
  219.      * @param ga           g(ta).
  220.      * @param tb           latest possible time for root.
  221.      * @param gb           g(tb).
  222.      * @return if a zero crossing was found.
  223.      */
  224.     private boolean findRoot(final FieldOrekitStepInterpolator<T> interpolator,
  225.                              final FieldAbsoluteDate<T> ta, final T ga,
  226.                              final FieldAbsoluteDate<T> tb, final T gb) {

  227.         final T zero = ga.getField().getZero();

  228.         // check there appears to be a root in [ta, tb]
  229.         check(ga.getReal() == 0.0 || gb.getReal() == 0.0 || ga.getReal() > 0.0 && gb.getReal() < 0.0 || ga.getReal() < 0.0 && gb.getReal() > 0.0);
  230.         final T convergence = detector.getThreshold();
  231.         final int maxIterationCount = detector.getMaxIterationCount();
  232.         final BracketedUnivariateSolver<UnivariateFunction> solver =
  233.                 new BracketingNthOrderBrentSolver(0, convergence.getReal(), 0, 5);

  234.         // prepare loop below
  235.         FieldAbsoluteDate<T> loopT = ta;
  236.         T loopG = ga;

  237.         // event time, just at or before the actual root.
  238.         FieldAbsoluteDate<T> beforeRootT = null;
  239.         T beforeRootG = zero.add(Double.NaN);
  240.         // time on the other side of the root.
  241.         // Initialized the the loop below executes once.
  242.         FieldAbsoluteDate<T> afterRootT = ta;
  243.         T afterRootG = zero;

  244.         // check for some conditions that the root finders don't like
  245.         // these conditions cannot not happen in the loop below
  246.         // the ga == 0.0 case is handled by the loop below
  247.         if (ta.equals(tb)) {
  248.             // both non-zero but times are the same. Probably due to reset state
  249.             beforeRootT = ta;
  250.             beforeRootG = ga;
  251.             afterRootT = shiftedBy(beforeRootT, convergence);
  252.             afterRootG = g(interpolator.getInterpolatedState(afterRootT));
  253.         } else if (ga.getReal() != 0.0 && gb.getReal() == 0.0) {
  254.             // hard: ga != 0.0 and gb == 0.0
  255.             // look past gb by up to convergence to find next sign
  256.             // throw an exception if g(t) = 0.0 in [tb, tb + convergence]
  257.             beforeRootT = tb;
  258.             beforeRootG = gb;
  259.             afterRootT = shiftedBy(beforeRootT, convergence);
  260.             afterRootG = g(interpolator.getInterpolatedState(afterRootT));
  261.         } else if (ga.getReal() != 0.0) {
  262.             final T newGa = g(interpolator.getInterpolatedState(ta));
  263.             if (ga.getReal() > 0 != newGa.getReal() > 0) {
  264.                 // both non-zero, step sign change at ta, possibly due to reset state
  265.                 final FieldAbsoluteDate<T> nextT = minTime(shiftedBy(ta, convergence), tb);
  266.                 final T                    nextG = g(interpolator.getInterpolatedState(nextT));
  267.                 if (nextG.getReal() > 0.0 == g0Positive) {
  268.                     // the sign change between ga and newGa just moved the root less than one convergence
  269.                     // threshold later, we are still in a regular search for another root before tb,
  270.                     // we just need to fix the bracketing interval
  271.                     // (see issue https://github.com/Hipparchus-Math/hipparchus/issues/184)
  272.                     loopT = nextT;
  273.                     loopG = nextG;
  274.                 } else {
  275.                     beforeRootT = ta;
  276.                     beforeRootG = newGa;
  277.                     afterRootT  = nextT;
  278.                     afterRootG  = nextG;
  279.                 }
  280.             }
  281.         }

  282.         // loop to skip through "fake" roots, i.e. where g(t) = g'(t) = 0.0
  283.         // executed once if we didn't hit a special case above
  284.         while ((afterRootG.getReal() == 0.0 || afterRootG.getReal() > 0.0 == g0Positive) &&
  285.                 strictlyAfter(afterRootT, tb)) {
  286.             if (loopG.getReal() == 0.0) {
  287.                 // ga == 0.0 and gb may or may not be 0.0
  288.                 // handle the root at ta first
  289.                 beforeRootT = loopT;
  290.                 beforeRootG = loopG;
  291.                 afterRootT = minTime(shiftedBy(beforeRootT, convergence), tb);
  292.                 afterRootG = g(interpolator.getInterpolatedState(afterRootT));
  293.             } else {
  294.                 // both non-zero, the usual case, use a root finder.
  295.                 // time zero for evaluating the function f. Needs to be final
  296.                 final FieldAbsoluteDate<T> fT0 = loopT;
  297.                 final double tbDouble = tb.durationFrom(fT0).getReal();
  298.                 final double middle   = 0.5 * tbDouble;
  299.                 final DoubleFunction<FieldAbsoluteDate<T>> date = dt -> {
  300.                     // use either fT0 or tb as the base time for shifts
  301.                     // in order to ensure we reproduce exactly those times
  302.                     // using only one reference time like fT0 would imply
  303.                     // to use ft0.shiftedBy(tbDouble), which may be different
  304.                     // from tb due to numerical noise (see issue 921)
  305.                     if (forward == dt <= middle) {
  306.                         // use start of interval as reference
  307.                         return fT0.shiftedBy(dt);
  308.                     } else {
  309.                         // use end of interval as reference
  310.                         return tb.shiftedBy(dt - tbDouble);
  311.                     }
  312.                 };
  313.                 final UnivariateFunction f = dt -> g(interpolator.getInterpolatedState(date.apply(dt))).getReal();
  314.                 if (forward) {
  315.                     try {
  316.                         final Interval interval =
  317.                                 solver.solveInterval(maxIterationCount, f, 0, tbDouble);
  318.                         beforeRootT = date.apply(interval.getLeftAbscissa());
  319.                         beforeRootG = zero.add(interval.getLeftValue());
  320.                         afterRootT  = date.apply(interval.getRightAbscissa());
  321.                         afterRootG  = zero.add(interval.getRightValue());
  322.                         // CHECKSTYLE: stop IllegalCatch check
  323.                     } catch (RuntimeException e) {
  324.                         // CHECKSTYLE: resume IllegalCatch check
  325.                         throw new OrekitException(e, OrekitMessages.FIND_ROOT,
  326.                                 detector, loopT, loopG, tb, gb, lastT, lastG);
  327.                     }
  328.                 } else {
  329.                     try {
  330.                         final Interval interval =
  331.                                 solver.solveInterval(maxIterationCount, f, tbDouble, 0);
  332.                         beforeRootT = date.apply(interval.getRightAbscissa());
  333.                         beforeRootG = zero.add(interval.getRightValue());
  334.                         afterRootT  = date.apply(interval.getLeftAbscissa());
  335.                         afterRootG  = zero.add(interval.getLeftValue());
  336.                         // CHECKSTYLE: stop IllegalCatch check
  337.                     } catch (RuntimeException e) {
  338.                         // CHECKSTYLE: resume IllegalCatch check
  339.                         throw new OrekitException(e, OrekitMessages.FIND_ROOT,
  340.                                 detector, tb, gb, loopT, loopG, lastT, lastG);
  341.                     }
  342.                 }
  343.             }
  344.             // tolerance is set to less than 1 ulp
  345.             // assume tolerance is 1 ulp
  346.             if (beforeRootT.equals(afterRootT)) {
  347.                 afterRootT = nextAfter(afterRootT);
  348.                 afterRootG = g(interpolator.getInterpolatedState(afterRootT));
  349.             }
  350.             // check loop is making some progress
  351.             check(forward && afterRootT.compareTo(beforeRootT) > 0 ||
  352.                   !forward && afterRootT.compareTo(beforeRootT) < 0);
  353.             // setup next iteration
  354.             loopT = afterRootT;
  355.             loopG = afterRootG;
  356.         }

  357.         // figure out the result of root finding, and return accordingly
  358.         if (afterRootG.getReal() == 0.0 || afterRootG.getReal() > 0.0 == g0Positive) {
  359.             // loop gave up and didn't find any crossing within this step
  360.             return false;
  361.         } else {
  362.             // real crossing
  363.             check(beforeRootT != null && !Double.isNaN(beforeRootG.getReal()));
  364.             // variation direction, with respect to the integration direction
  365.             increasing = !g0Positive;
  366.             pendingEventTime = beforeRootT;
  367.             stopTime = beforeRootG.getReal() == 0.0 ? beforeRootT : afterRootT;
  368.             pendingEvent = true;
  369.             afterEvent = afterRootT;
  370.             afterG = afterRootG;

  371.             // check increasing set correctly
  372.             check(afterG.getReal() > 0 == increasing);
  373.             check(increasing == gb.getReal() >= ga.getReal());

  374.             return true;
  375.         }

  376.     }

  377.     /**
  378.      * Get the next number after the given number in the current propagation direction.
  379.      *
  380.      * @param t input time
  381.      * @return t +/- 1 ulp depending on the direction.
  382.      */
  383.     private FieldAbsoluteDate<T> nextAfter(final FieldAbsoluteDate<T> t) {
  384.         return t.shiftedBy(forward ? +Precision.EPSILON : -Precision.EPSILON);
  385.     }


  386.     /** Get the occurrence time of the event triggered in the current
  387.      * step.
  388.      * @return occurrence time of the event triggered in the current
  389.      * step.
  390.      */
  391.     public FieldAbsoluteDate<T> getEventDate() {
  392.         return pendingEventTime;
  393.     }

  394.     /**
  395.      * Try to accept the current history up to the given time.
  396.      *
  397.      * <p> It is not necessary to call this method before calling {@link
  398.      * #doEvent(FieldSpacecraftState)} with the same state. It is necessary to call this
  399.      * method before you call {@link #doEvent(FieldSpacecraftState)} on some other event
  400.      * detector.
  401.      *
  402.      * @param state        to try to accept.
  403.      * @param interpolator to use to find the new root, if any.
  404.      * @return if the event detector has an event it has not detected before that is on or
  405.      * before the same time as {@code state}. In other words {@code false} means continue
  406.      * on while {@code true} means stop and handle my event first.
  407.      */
  408.     public boolean tryAdvance(final FieldSpacecraftState<T> state,
  409.                               final FieldOrekitStepInterpolator<T> interpolator) {
  410.         final FieldAbsoluteDate<T> t = state.getDate();
  411.         // check this is only called before a pending event.
  412.         check(!pendingEvent || !strictlyAfter(pendingEventTime, t));

  413.         final boolean meFirst;

  414.         if (strictlyAfter(t, earliestTimeConsidered)) {
  415.             // just found an event and we know the next time we want to search again
  416.             meFirst = false;
  417.         } else {
  418.             // check g function to see if there is a new event
  419.             final T g = g(state);
  420.             final boolean positive = g.getReal() > 0;

  421.             if (positive == g0Positive) {
  422.                 // g function has expected sign
  423.                 g0 = g; // g0Positive is the same
  424.                 meFirst = false;
  425.             } else {
  426.                 // found a root we didn't expect -> find precise location
  427.                 final FieldAbsoluteDate<T> oldPendingEventTime = pendingEventTime;
  428.                 final boolean foundRoot = findRoot(interpolator, t0, g0, t, g);
  429.                 // make sure the new root is not the same as the old root, if one exists
  430.                 meFirst = foundRoot && !pendingEventTime.equals(oldPendingEventTime);
  431.             }
  432.         }

  433.         if (!meFirst) {
  434.             // advance t0 to the current time so we can't find events that occur before t
  435.             t0 = t;
  436.         }

  437.         return meFirst;
  438.     }

  439.     /**
  440.      * Notify the user's listener of the event. The event occurs wholly within this method
  441.      * call including a call to {@link FieldEventDetector#resetState(FieldSpacecraftState)}
  442.      * if necessary.
  443.      *
  444.      * @param state the state at the time of the event. This must be at the same time as
  445.      *              the current value of {@link #getEventDate()}.
  446.      * @return the user's requested action and the new state if the action is {@link
  447.      * Action#RESET_STATE}. Otherwise
  448.      * the new state is {@code state}. The stop time indicates what time propagation should
  449.      * stop if the action is {@link Action#STOP}.
  450.      * This guarantees the integration will stop on or after the root, so that integration
  451.      * may be restarted safely.
  452.      */
  453.     public EventOccurrence<T> doEvent(final FieldSpacecraftState<T> state) {
  454.         // check event is pending and is at the same time
  455.         check(pendingEvent);
  456.         check(state.getDate().equals(this.pendingEventTime));

  457.         final Action action = detector.eventOccurred(state, increasing == forward);
  458.         final FieldSpacecraftState<T> newState;
  459.         if (action == Action.RESET_STATE) {
  460.             newState = detector.resetState(state);
  461.         } else {
  462.             newState = state;
  463.         }
  464.         // clear pending event
  465.         pendingEvent     = false;
  466.         pendingEventTime = null;
  467.         // setup for next search
  468.         earliestTimeConsidered = afterEvent;
  469.         t0 = afterEvent;
  470.         g0 = afterG;
  471.         g0Positive = increasing;
  472.         // check g0Positive set correctly
  473.         check(g0.getReal() == 0.0 || g0Positive == g0.getReal() > 0);
  474.         return new EventOccurrence<T>(action, newState, stopTime);
  475.     }

  476.     /**
  477.      * Shift a time value along the current integration direction: {@link #forward}.
  478.      *
  479.      * @param t     the time to shift.
  480.      * @param delta the amount to shift.
  481.      * @return t + delta if forward, else t - delta. If the result has to be rounded it
  482.      * will be rounded to be before the true value of t + delta.
  483.      */
  484.     private FieldAbsoluteDate<T> shiftedBy(final FieldAbsoluteDate<T> t, final T delta) {
  485.         if (forward) {
  486.             final FieldAbsoluteDate<T> ret = t.shiftedBy(delta);
  487.             if (ret.durationFrom(t).getReal() > delta.getReal()) {
  488.                 return ret.shiftedBy(-Precision.EPSILON);
  489.             } else {
  490.                 return ret;
  491.             }
  492.         } else {
  493.             final FieldAbsoluteDate<T> ret = t.shiftedBy(delta.negate());
  494.             if (t.durationFrom(ret).getReal() > delta.getReal()) {
  495.                 return ret.shiftedBy(+Precision.EPSILON);
  496.             } else {
  497.                 return ret;
  498.             }
  499.         }
  500.     }

  501.     /**
  502.      * Get the time that happens first along the current propagation direction: {@link
  503.      * #forward}.
  504.      *
  505.      * @param a first time
  506.      * @param b second time
  507.      * @return min(a, b) if forward, else max (a, b)
  508.      */
  509.     private FieldAbsoluteDate<T> minTime(final FieldAbsoluteDate<T> a, final FieldAbsoluteDate<T> b) {
  510.         return (forward ^ (a.compareTo(b) > 0)) ? a : b;
  511.     }

  512.     /**
  513.      * Check the ordering of two times.
  514.      *
  515.      * @param t1 the first time.
  516.      * @param t2 the second time.
  517.      * @return true if {@code t2} is strictly after {@code t1} in the propagation
  518.      * direction.
  519.      */
  520.     private boolean strictlyAfter(final FieldAbsoluteDate<T> t1, final FieldAbsoluteDate<T> t2) {
  521.         if (t1 == null || t2 == null) {
  522.             return false;
  523.         } else {
  524.             return forward ? t1.compareTo(t2) < 0 : t2.compareTo(t1) < 0;
  525.         }
  526.     }

  527.     /**
  528.      * Same as keyword assert, but throw a {@link MathRuntimeException}.
  529.      *
  530.      * @param condition to check
  531.      * @throws MathRuntimeException if {@code condition} is false.
  532.      */
  533.     private void check(final boolean condition) throws MathRuntimeException {
  534.         if (!condition) {
  535.             throw new OrekitInternalError(null);
  536.         }
  537.     }

  538.     /**
  539.      * Class to hold the data related to an event occurrence that is needed to decide how
  540.      * to modify integration.
  541.      */
  542.     public static class EventOccurrence<T extends CalculusFieldElement<T>> {

  543.         /** User requested action. */
  544.         private final Action action;
  545.         /** New state for a reset action. */
  546.         private final FieldSpacecraftState<T> newState;
  547.         /** The time to stop propagation if the action is a stop event. */
  548.         private final FieldAbsoluteDate<T> stopDate;

  549.         /**
  550.          * Create a new occurrence of an event.
  551.          *
  552.          * @param action   the user requested action.
  553.          * @param newState for a reset event. Should be the current state unless the
  554.          *                 action is {@link Action#RESET_STATE}.
  555.          * @param stopDate to stop propagation if the action is {@link Action#STOP}. Used
  556.          *                 to move the stop time to just after the root.
  557.          */
  558.         EventOccurrence(final Action action,
  559.                         final FieldSpacecraftState<T> newState,
  560.                         final FieldAbsoluteDate<T> stopDate) {
  561.             this.action = action;
  562.             this.newState = newState;
  563.             this.stopDate = stopDate;
  564.         }

  565.         /**
  566.          * Get the user requested action.
  567.          *
  568.          * @return the action.
  569.          */
  570.         public Action getAction() {
  571.             return action;
  572.         }

  573.         /**
  574.          * Get the new state for a reset action.
  575.          *
  576.          * @return the new state.
  577.          */
  578.         public FieldSpacecraftState<T> getNewState() {
  579.             return newState;
  580.         }

  581.         /**
  582.          * Get the new time for a stop action.
  583.          *
  584.          * @return when to stop propagation.
  585.          */
  586.         public FieldAbsoluteDate<T> getStopDate() {
  587.             return stopDate;
  588.         }

  589.     }

  590.     /**Get PendingEvent.
  591.      * @return if there is a pending event or not
  592.      * */

  593.     public boolean getPendingEvent() {
  594.         return pendingEvent;
  595.     }

  596. }