EventDetector.java

  1. /* Copyright 2002-2025 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 org.orekit.propagation.SpacecraftState;
  19. import org.orekit.propagation.events.handlers.EventHandler;
  20. import org.orekit.propagation.events.intervals.AdaptableInterval;
  21. import org.orekit.time.AbsoluteDate;

  22. /** This interface represents space-dynamics aware events detectors.
  23.  *
  24.  * <p>It mirrors the {@link org.hipparchus.ode.events.ODEEventHandler
  25.  * ODEEventHandler} interface from <a href="https://hipparchus.org/">
  26.  * Hipparchus</a> but provides a space-dynamics interface to the
  27.  * methods.</p>
  28.  *
  29.  * <p>Events detectors are a useful solution to meet the requirements
  30.  * of propagators concerning discrete conditions. The state of each
  31.  * event detector is queried by the propagator from time to time, at least
  32.  * once every {@link #getMaxCheckInterval() max check interval} but it may
  33.  * be more frequent. When the sign of the underlying g switching function
  34.  * changes, a root-finding algorithm is run to precisely locate the event,
  35.  * down to a configured {@link #getThreshold() convergence threshold}. The
  36.  * {@link #getMaxCheckInterval() max check interval} is therefore devoted to
  37.  * separate roots and is often much larger than the  {@link #getThreshold()
  38.  * convergence threshold}.</p>
  39.  *
  40.  * <p>The physical meaning of the g switching function is not really used
  41.  * by the event detection algorithms. Its varies from event detector to
  42.  * event detector. One example would be a visibility detector that could use the
  43.  * angular elevation of the satellite above horizon as a g switching function.
  44.  * In this case, the function would switch from negative to positive when the
  45.  * satellite raises above horizon and it would switch from positive to negative
  46.  * when it sets backs below horizon. Another example would be an apside detector
  47.  * that could use the dot product of position and velocity. In this case, the
  48.  * function would switch from negative to positive when the satellite crosses
  49.  * periapsis and it would switch from positive to negative when the satellite
  50.  * crosses apoapsis.</p>
  51.  *
  52.  * <p>When the precise state at which the g switching function changes has been
  53.  * located, the corresponding event is triggered, by calling the {@link
  54.  * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred}
  55.  * method from the associated {@link #getHandler() handler}.
  56.  * The method can do whatever it needs with the event (logging it, performing
  57.  * some processing, ignore it ...). The return value of the method will be used by
  58.  * the propagator to stop or resume propagation, possibly changing the state vector.</p>
  59.  *
  60.  * @author Luc Maisonobe
  61.  * @author V&eacute;ronique Pommier-Maurussane
  62.  */
  63. public interface EventDetector {

  64.     /** Initialize event detector at the start of a propagation.
  65.      * <p>
  66.      * This method is called once at the start of the propagation. It
  67.      * may be used by the event handler to initialize some internal data
  68.      * if needed.
  69.      * </p>
  70.      * <p>
  71.      * The default implementation initializes the handler.
  72.      * </p>
  73.      * @param s0 initial state
  74.      * @param t target time for the integration
  75.      *
  76.      */
  77.     default void init(SpacecraftState s0, AbsoluteDate t) {
  78.         getHandler().init(s0, t, this);
  79.     }

  80.     /** Reset the event detector during propagation when the state is modified by an event or an additional data provider.
  81.      * <p>
  82.      * The default implementation does nothing.
  83.      * </p>
  84.      * @param state current state
  85.      * @param target target time for the integration
  86.      * @since 13.0
  87.      */
  88.     default void reset(SpacecraftState state, AbsoluteDate target) {
  89.         // nothing by default
  90.     }

  91.     /**
  92.      * Method returning true if and only if the detection function g does not depend on dependent variables,
  93.      * just the independent one i.e. time. This information is used for performance in propagation.
  94.      * @return flag
  95.      * @since 13.1
  96.      */
  97.     default boolean dependsOnTimeOnly() {
  98.         return false;
  99.     }

  100.     /** Compute the value of the switching function.
  101.      * This function must be continuous (at least in its roots neighborhood),
  102.      * as the integrator will need to find its roots to locate the events.
  103.      * @param s the current state information: date, kinematics, attitude
  104.      * @return value of the switching function
  105.      */
  106.     double g(SpacecraftState s);

  107.     /** Get the convergence threshold in the event time search.
  108.      * @return convergence threshold (s)
  109.      */
  110.     default double getThreshold() {
  111.         return getDetectionSettings().getThreshold();
  112.     }

  113.     /** Get maximal time interval between switching function checks.
  114.      * @return maximal time interval (s) between switching function checks
  115.      */
  116.     default AdaptableInterval getMaxCheckInterval() {
  117.         return getDetectionSettings().getMaxCheckInterval();
  118.     }

  119.     /** Get maximal number of iterations in the event time search.
  120.      * @return maximal number of iterations in the event time search
  121.      */
  122.     default int getMaxIterationCount() {
  123.         return getDetectionSettings().getMaxIterationCount();
  124.     }

  125.     /** Get the handler.
  126.      * @return event handler to call at event occurrences
  127.      * @since 12.0
  128.      */
  129.     EventHandler getHandler();

  130.     /**
  131.      * This method finalizes the event detector's job.
  132.      * @param state state at propagation end
  133.      * @since 12.2
  134.      */
  135.     default void finish(SpacecraftState state) {
  136.         getHandler().finish(state, this);
  137.     }

  138.     /**
  139.      * Getter for the settings.
  140.      * @return detection settings
  141.      * @since 12.2
  142.      */
  143.     default EventDetectionSettings getDetectionSettings() {
  144.         return EventDetectionSettings.getDefaultEventDetectionSettings();
  145.     }
  146. }