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  
19  import org.orekit.propagation.SpacecraftState;
20  import org.orekit.propagation.events.functions.EventFunction;
21  import org.orekit.propagation.events.handlers.ContinueOnEvent;
22  import org.orekit.propagation.events.handlers.EventHandler;
23  import org.orekit.propagation.events.intervals.AdaptableInterval;
24  import org.orekit.time.AbsoluteDate;
25  
26  /** This interface represents space-dynamics aware events detectors.
27   *
28   * <p>It mirrors the {@link org.hipparchus.ode.events.ODEEventHandler
29   * ODEEventHandler} interface from <a href="https://hipparchus.org/">
30   * Hipparchus</a> but provides a space-dynamics interface to the
31   * methods.</p>
32   *
33   * <p>Events detectors are a useful solution to meet the requirements
34   * of propagators concerning discrete conditions. The state of each
35   * event detector is queried by the propagator from time to time, at least
36   * once every {@link #getMaxCheckInterval() max check interval} but it may
37   * be more frequent. When the sign of the underlying g switching function
38   * changes, a root-finding algorithm is run to precisely locate the event,
39   * down to a configured {@link #getThreshold() convergence threshold}. The
40   * {@link #getMaxCheckInterval() max check interval} is therefore devoted to
41   * separate roots and is often much larger than the  {@link #getThreshold()
42   * convergence threshold}.</p>
43   *
44   * <p>The physical meaning of the g switching function is not really used
45   * by the event detection algorithms. Its varies from event detector to
46   * event detector. One example would be a visibility detector that could use the
47   * angular elevation of the satellite above horizon as a g switching function.
48   * In this case, the function would switch from negative to positive when the
49   * satellite raises above horizon and it would switch from positive to negative
50   * when it sets backs below horizon. Another example would be an apside detector
51   * that could use the dot product of position and velocity. In this case, the
52   * function would switch from negative to positive when the satellite crosses
53   * periapsis and it would switch from positive to negative when the satellite
54   * crosses apoapsis.</p>
55   *
56   * <p>When the precise state at which the g switching function changes has been
57   * located, the corresponding event is triggered, by calling the {@link
58   * EventHandler#eventOccurred(SpacecraftState, EventDetector, boolean) eventOccurred}
59   * method from the associated {@link #getHandler() handler}.
60   * The method can do whatever it needs with the event (logging it, performing
61   * some processing, ignore it ...). The return value of the method will be used by
62   * the propagator to stop or resume propagation, possibly changing the state vector.</p>
63   *
64   * @author Luc Maisonobe
65   * @author V&eacute;ronique Pommier-Maurussane
66   */
67  public interface EventDetector {
68  
69      /**
70       * Builds instance from event function only. Uses default handler and detection settings.
71       * @param eventFunction event function
72       * @return event detector
73       * @since 14.0
74       */
75      static EventDetector of(final EventFunction eventFunction) {
76          return of(eventFunction, new ContinueOnEvent());
77      }
78  
79      /**
80       * Builds instance from event function and handler. Uses default detection settings.
81       * @param eventFunction event function
82       * @param eventHandler handler
83       * @return event detector
84       * @since 14.0
85       */
86      static EventDetector of(final EventFunction eventFunction, final EventHandler eventHandler) {
87          return of(eventFunction, eventHandler, EventDetectionSettings.getDefaultEventDetectionSettings());
88      }
89  
90      /**
91       * Builds instance from event function, handler and detection settings.
92       * @param eventFunction event function
93       * @param eventHandler handler
94       * @param eventDetectionSettings detection settings
95       * @return event detector
96       * @since 14.0
97       */
98      static EventDetector of(final EventFunction eventFunction, final EventHandler eventHandler,
99                              final EventDetectionSettings eventDetectionSettings) {
100         return new EventDetector() {
101 
102             @Override
103             public double g(final SpacecraftState s) {
104                 return eventFunction.value(s);
105             }
106 
107             @Override
108             public EventFunction getEventFunction() {
109                 return eventFunction;
110             }
111 
112             @Override
113             public EventDetectionSettings getDetectionSettings() {
114                 return eventDetectionSettings;
115             }
116 
117             @Override
118             public EventHandler getHandler() {
119                 return eventHandler;
120             }
121         };
122     }
123 
124     /** Initialize event detector at the start of a propagation.
125      * <p>
126      * This method is called once at the start of the propagation. It
127      * may be used by the event handler to initialize some internal data
128      * if needed.
129      * </p>
130      * <p>
131      * The default implementation initializes the handler.
132      * </p>
133      * @param s0 initial state
134      * @param t target time for the integration
135      *
136      */
137     default void init(final SpacecraftState s0, final AbsoluteDate t) {
138         getHandler().init(s0, t, this);
139     }
140 
141     /** Reset the event detector during propagation when the state is modified by an event or an additional data provider.
142      * <p>
143      * The default implementation does nothing.
144      * </p>
145      * @param state current state
146      * @param target target time for the integration
147      * @since 13.0
148      */
149     default void reset(final SpacecraftState state, final AbsoluteDate target) {
150         // nothing by default
151     }
152 
153     /** Compute the value of the switching function.
154      * This function must be continuous (at least in its roots neighborhood),
155      * as the integrator will need to find its roots to locate the events.
156      * @param s the current state information: date, kinematics, attitude
157      * @return value of the switching function
158      */
159     double g(SpacecraftState s);
160 
161     /**
162      * Get the event function. It defines g both for double and Field.
163      * @return event function
164      * @since 14.0
165      */
166     default EventFunction getEventFunction() {
167         return this::g;
168     }
169 
170     /** Get the convergence threshold in the event time search.
171      * @return convergence threshold (s)
172      */
173     default double getThreshold() {
174         return getDetectionSettings().getThreshold();
175     }
176 
177     /** Get maximal time interval between switching function checks.
178      * @return maximal time interval (s) between switching function checks
179      */
180     default AdaptableInterval getMaxCheckInterval() {
181         return getDetectionSettings().getMaxCheckInterval();
182     }
183 
184     /** Get maximal number of iterations in the event time search.
185      * @return maximal number of iterations in the event time search
186      */
187     default int getMaxIterationCount() {
188         return getDetectionSettings().getMaxIterationCount();
189     }
190 
191     /** Get the handler.
192      * @return event handler to call at event occurrences
193      * @since 12.0
194      */
195     EventHandler getHandler();
196 
197     /**
198      * This method finalizes the event detector's job.
199      * @param state state at propagation end
200      * @since 12.2
201      */
202     default void finish(final SpacecraftState state) {
203         getHandler().finish(state, this);
204     }
205 
206     /**
207      * Getter for the settings.
208      * @return detection settings
209      * @since 12.2
210      */
211     default EventDetectionSettings getDetectionSettings() {
212         return EventDetectionSettings.getDefaultEventDetectionSettings();
213     }
214 }