EventEnablingPredicateFilter.java

  1. /* Copyright 2002-2024 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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.Arrays;

  19. import org.hipparchus.ode.events.Action;
  20. import org.orekit.propagation.SpacecraftState;
  21. import org.orekit.propagation.events.handlers.EventHandler;
  22. import org.orekit.time.AbsoluteDate;

  23. /** Wrapper used to detect events only when enabled by an external predicated function.
  24.  *
  25.  * <p>General {@link EventDetector events} are defined implicitly
  26.  * by a {@link EventDetector#g(SpacecraftState) g function} crossing
  27.  * zero. This implies that during an orbit propagation, events are
  28.  * triggered at all zero crossings.
  29.  * </p>
  30.  *
  31.  * <p>Sometimes, users would like to enable or disable events by themselves,
  32.  * for example to trigger them only for certain orbits, or to check elevation
  33.  * maximums only when elevation itself is positive (i.e. they want to
  34.  * discard elevation maximums below ground). In these cases, looking precisely
  35.  * for all events location and triggering events that will later be ignored
  36.  * is a waste of computing time.</p>
  37.  *
  38.  * <p>Users can wrap a regular {@link EventDetector event detector} in
  39.  * an instance of this class and provide this wrapping instance to
  40.  * a {@link org.orekit.propagation.Propagator}
  41.  * in order to avoid wasting time looking for uninteresting events.
  42.  * The wrapper will intercept the calls to the {@link
  43.  * EventDetector#g(SpacecraftState) g function} and to the {@link
  44.  * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean)
  45.  * eventOccurred} method in order to ignore uninteresting events. The
  46.  * wrapped regular {@link EventDetector event detector} will the see only
  47.  * the interesting events, i.e. either only events that occur when a
  48.  * user-provided event enabling predicate function is true, ignoring all events
  49.  * that occur when the event enabling predicate function is false. The number of
  50.  * calls to the {@link EventDetector#g(SpacecraftState) g function} will also be
  51.  * reduced.</p>
  52.  * @see EventSlopeFilter
  53.  * @since 7.1
  54.  */

  55. public class EventEnablingPredicateFilter
  56.     extends AbstractDetector<EventEnablingPredicateFilter> {

  57.     /** Number of past transformers updates stored. */
  58.     private static final int HISTORY_SIZE = 100;

  59.     /** Wrapped event detector. */
  60.     private final EventDetector rawDetector;

  61.     /** Enabling predicate function. */
  62.     private final EnablingPredicate enabler;

  63.     /** Transformers of the g function. */
  64.     private final Transformer[] transformers;

  65.     /** Update time of the transformers. */
  66.     private final AbsoluteDate[] updates;

  67.     /** Indicator for forward integration. */
  68.     private boolean forward;

  69.     /** Extreme time encountered so far. */
  70.     private AbsoluteDate extremeT;

  71.     /** Detector function value at extremeT. */
  72.     private double extremeG;

  73.     /** Wrap an {@link EventDetector event detector}.
  74.      * @param rawDetector event detector to wrap
  75.      * @param enabler event enabling predicate function to use
  76.      */
  77.     public EventEnablingPredicateFilter(final EventDetector rawDetector,
  78.                                         final EnablingPredicate enabler) {
  79.         this(rawDetector.getMaxCheckInterval(), rawDetector.getThreshold(),
  80.              rawDetector.getMaxIterationCount(), new LocalHandler(),
  81.              rawDetector, enabler);
  82.     }

  83.     /** Protected constructor with full parameters.
  84.      * <p>
  85.      * This constructor is not public as users are expected to use the builder
  86.      * API with the various {@code withXxx()} methods to set up the instance
  87.      * in a readable manner without using a huge amount of parameters.
  88.      * </p>
  89.      * @param maxCheck maximum checking interval
  90.      * @param threshold convergence threshold (s)
  91.      * @param maxIter maximum number of iterations in the event time search
  92.      * @param handler event handler to call at event occurrences
  93.      * @param rawDetector event detector to wrap
  94.      * @param enabler event enabling function to use
  95.      */
  96.     protected EventEnablingPredicateFilter(final AdaptableInterval maxCheck, final double threshold,
  97.                                            final int maxIter, final EventHandler handler,
  98.                                            final EventDetector rawDetector,
  99.                                            final EnablingPredicate enabler) {
  100.         super(new EventDetectionSettings(maxCheck, threshold, maxIter), handler);
  101.         this.rawDetector  = rawDetector;
  102.         this.enabler      = enabler;
  103.         this.transformers = new Transformer[HISTORY_SIZE];
  104.         this.updates      = new AbsoluteDate[HISTORY_SIZE];
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     protected EventEnablingPredicateFilter create(final AdaptableInterval newMaxCheck, final double newThreshold,
  109.                                                   final int newMaxIter,
  110.                                                   final EventHandler newHandler) {
  111.         return new EventEnablingPredicateFilter(newMaxCheck, newThreshold, newMaxIter, newHandler, rawDetector, enabler);
  112.     }

  113.     /**
  114.      * Get the wrapped raw detector.
  115.      * @return the wrapped raw detector
  116.      * @since 11.1
  117.      */
  118.     public EventDetector getDetector() {
  119.         return rawDetector;
  120.     }

  121.     /**  {@inheritDoc} */
  122.     @Override
  123.     public void init(final SpacecraftState s0,
  124.                      final AbsoluteDate t) {
  125.         super.init(s0, t);

  126.         // delegate to raw detector
  127.         rawDetector.init(s0, t);

  128.         // initialize events triggering logic
  129.         forward  = t.compareTo(s0.getDate()) >= 0;
  130.         extremeT = forward ? AbsoluteDate.PAST_INFINITY : AbsoluteDate.FUTURE_INFINITY;
  131.         extremeG = Double.NaN;
  132.         Arrays.fill(transformers, Transformer.UNINITIALIZED);
  133.         Arrays.fill(updates, extremeT);

  134.     }

  135.     /**  {@inheritDoc} */
  136.     public double g(final SpacecraftState s) {

  137.         final double  rawG      = rawDetector.g(s);
  138.         final boolean isEnabled = enabler.eventIsEnabled(s, rawDetector, rawG);
  139.         if (Double.isNaN(extremeG)) {
  140.             extremeG = rawG;
  141.         }

  142.         // search which transformer should be applied to g
  143.         if (forward) {
  144.             final int last = transformers.length - 1;
  145.             if (extremeT.compareTo(s.getDate()) < 0) {
  146.                 // we are at the forward end of the history

  147.                 // check if enabled status has changed
  148.                 final Transformer previous = transformers[last];
  149.                 final Transformer next     = selectTransformer(previous, extremeG, isEnabled);
  150.                 if (next != previous) {
  151.                     // there is a status change somewhere between extremeT and t.
  152.                     // the new transformer is valid for t (this is how we have just computed
  153.                     // it above), but it is in fact valid on both sides of the change, so
  154.                     // it was already valid before t and even up to previous time. We store
  155.                     // the switch at extremeT for safety, to ensure the previous transformer
  156.                     // is not applied too close of the root
  157.                     System.arraycopy(updates,      1, updates,      0, last);
  158.                     System.arraycopy(transformers, 1, transformers, 0, last);
  159.                     updates[last]      = extremeT;
  160.                     transformers[last] = next;
  161.                 }

  162.                 extremeT = s.getDate();
  163.                 extremeG = rawG;

  164.                 // apply the transform
  165.                 return next.transformed(rawG);

  166.             } else {
  167.                 // we are in the middle of the history

  168.                 // select the transformer
  169.                 for (int i = last; i > 0; --i) {
  170.                     if (updates[i].compareTo(s.getDate()) <= 0) {
  171.                         // apply the transform
  172.                         return transformers[i].transformed(rawG);
  173.                     }
  174.                 }

  175.                 return transformers[0].transformed(rawG);

  176.             }
  177.         } else {
  178.             if (s.getDate().compareTo(extremeT) < 0) {
  179.                 // we are at the backward end of the history

  180.                 // check if a new rough root has been crossed
  181.                 final Transformer previous = transformers[0];
  182.                 final Transformer next     = selectTransformer(previous, extremeG, isEnabled);
  183.                 if (next != previous) {
  184.                     // there is a status change somewhere between extremeT and t.
  185.                     // the new transformer is valid for t (this is how we have just computed
  186.                     // it above), but it is in fact valid on both sides of the change, so
  187.                     // it was already valid before t and even up to previous time. We store
  188.                     // the switch at extremeT for safety, to ensure the previous transformer
  189.                     // is not applied too close of the root
  190.                     System.arraycopy(updates,      0, updates,      1, updates.length - 1);
  191.                     System.arraycopy(transformers, 0, transformers, 1, transformers.length - 1);
  192.                     updates[0]      = extremeT;
  193.                     transformers[0] = next;
  194.                 }

  195.                 extremeT = s.getDate();
  196.                 extremeG = rawG;

  197.                 // apply the transform
  198.                 return next.transformed(rawG);

  199.             } else {
  200.                 // we are in the middle of the history

  201.                 // select the transformer
  202.                 for (int i = 0; i < updates.length - 1; ++i) {
  203.                     if (s.getDate().compareTo(updates[i]) <= 0) {
  204.                         // apply the transform
  205.                         return transformers[i].transformed(rawG);
  206.                     }
  207.                 }

  208.                 return transformers[updates.length - 1].transformed(rawG);

  209.             }
  210.         }

  211.     }

  212.     /** Get next function transformer in the specified direction.
  213.      * @param previous transformer active on the previous point with respect
  214.      * to integration direction (may be null if no previous point is known)
  215.      * @param previousG value of the g function at the previous point
  216.      * @param isEnabled if true the event should be enabled now
  217.      * @return next transformer transformer
  218.      */
  219.     private Transformer selectTransformer(final Transformer previous, final double previousG, final boolean isEnabled) {
  220.         if (isEnabled) {
  221.             // we need to select a transformer that can produce zero crossings,
  222.             // so it is either Transformer.PLUS or Transformer.MINUS
  223.             switch (previous) {
  224.                 case UNINITIALIZED :
  225.                     return Transformer.PLUS; // this initial choice is arbitrary, it could have been Transformer.MINUS
  226.                 case MIN :
  227.                     return previousG >= 0 ? Transformer.MINUS : Transformer.PLUS;
  228.                 case MAX :
  229.                     return previousG >= 0 ? Transformer.PLUS : Transformer.MINUS;
  230.                 default :
  231.                     return previous;
  232.             }
  233.         } else {
  234.             // we need to select a transformer that cannot produce any zero crossings,
  235.             // so it is either Transformer.MAX or Transformer.MIN
  236.             switch (previous) {
  237.                 case UNINITIALIZED :
  238.                     return Transformer.MAX; // this initial choice is arbitrary, it could have been Transformer.MIN
  239.                 case PLUS :
  240.                     return previousG >= 0 ? Transformer.MAX : Transformer.MIN;
  241.                 case MINUS :
  242.                     return previousG >= 0 ? Transformer.MIN : Transformer.MAX;
  243.                 default :
  244.                     return previous;
  245.             }
  246.         }
  247.     }

  248.     /** Local handler. */
  249.     private static class LocalHandler implements EventHandler {

  250.         /** {@inheritDoc} */
  251.         public Action eventOccurred(final SpacecraftState s, final EventDetector detector, final boolean increasing) {
  252.             final EventEnablingPredicateFilter ef = (EventEnablingPredicateFilter) detector;
  253.             final Transformer transformer = ef.forward ? ef.transformers[ef.transformers.length - 1] : ef.transformers[0];
  254.             return ef.rawDetector.getHandler().eventOccurred(s, ef.rawDetector, transformer == Transformer.PLUS ? increasing : !increasing);
  255.         }

  256.         /** {@inheritDoc} */
  257.         @Override
  258.         public SpacecraftState resetState(final EventDetector detector, final SpacecraftState oldState) {
  259.             final EventEnablingPredicateFilter ef = (EventEnablingPredicateFilter) detector;
  260.             return ef.rawDetector.getHandler().resetState(ef.rawDetector, oldState);
  261.         }

  262.     }

  263. }