View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.ode.events;
19  
20  import org.hipparchus.ode.ODEState;
21  import org.hipparchus.ode.ODEStateAndDerivative;
22  
23  /** This interface represents a handler for discrete events triggered
24   * during ODE integration.
25   *
26   * <p>Some events can be triggered at discrete times as an ODE problem
27   * is solved. This occurs for example when the integration process
28   * should be stopped as some state is reached (G-stop facility) when the
29   * precise date is unknown a priori, or when the derivatives have
30   * discontinuities, or simply when the user wants to monitor some
31   * states boundaries crossings.
32   * </p>
33   *
34   * <p>These events are defined as occurring when a <code>g</code>
35   * switching function sign changes.</p>
36   *
37   * <p>Since events are only problem-dependent and are triggered by the
38   * independent <i>time</i> variable and the state vector, they can
39   * occur at virtually any time, unknown in advance. The integrators will
40   * take care to avoid sign changes inside the steps, they will reduce
41   * the step size when such an event is detected in order to put this
42   * event exactly at the end of the current step. This guarantees that
43   * step interpolation (which always has a one step scope) is relevant
44   * even in presence of discontinuities. This is independent from the
45   * stepsize control provided by integrators that monitor the local
46   * error (this event handling feature is available for all integrators,
47   * including fixed step ones).</p>
48   *
49   * <p>
50   * Note that prior to Hipparchus 3.0, some of the methods that are now in
51   * {@link ODEEventDetector} were in this interface (and the remaining
52   * ones were in the defunct {@code EventHandlerConfiguration} interface).
53   * The interfaces have been reorganized to allow different objects to be
54   * used in event detection and event handling, hence allowing users to
55   * reuse predefined events detectors with custom handlers.
56   * </p>
57   * @see org.hipparchus.ode.events
58   * @since 3.0
59   */
60  @FunctionalInterface
61  public interface ODEEventHandler  {
62  
63      /** Initialize event handler at the start of an ODE integration.
64       * <p>
65       * This method is called once at the start of the integration. It
66       * may be used by the event handler to initialize some internal data
67       * if needed.
68       * </p>
69       * <p>
70       * The default implementation does nothing
71       * </p>
72       * @param initialState initial time, state vector and derivative
73       * @param finalTime target time for the integration
74       * @param detector event detector related to the event handler
75       */
76      default void init(ODEStateAndDerivative initialState, double finalTime, ODEEventDetector detector) {
77          // nothing by default
78      }
79  
80      /** Handle an event and choose what to do next.
81  
82       * <p>This method is called when the integrator has accepted a step
83       * ending exactly on a sign change of the function, just <em>after</em>
84       * the step handler itself is called (see below for scheduling). It
85       * allows the user to update his internal data to acknowledge the fact
86       * the event has been handled (for example setting a flag in the {@link
87       * org.hipparchus.ode.OrdinaryDifferentialEquation
88       * differential equations} to switch the derivatives computation in
89       * case of discontinuity), or to direct the integrator to either stop
90       * or continue integration, possibly with a reset state or derivatives.</p>
91       *
92       * <ul>
93       *   <li>if {@link Action#STOP} is returned, the integration will be stopped,</li>
94       *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
95       *   resetState} method will be called once the step handler has
96       *   finished its task, and the integrator will also recompute the
97       *   derivatives,</li>
98       *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
99       *   will recompute the derivatives,
100      *   <li>if {@link Action#RESET_EVENTS} is returned, the integrator
101      *   will recheck all event handlers,
102      *   <li>if {@link Action#CONTINUE} is returned, no specific action will
103      *   be taken (apart from having called this method) and integration
104      *   will continue.</li>
105      * </ul>
106      *
107      * <p>The scheduling between this method and the {@link
108      * org.hipparchus.ode.sampling.ODEStepHandler ODEStepHandler} method {@link
109      * org.hipparchus.ode.sampling.ODEStepHandler#handleStep(org.hipparchus.ode.sampling.ODEStateInterpolator)
110      * handleStep(interpolator)} is to call {@code handleStep} first and this method afterwards
111      * (this scheduling changed as of Hipparchus 2.0). This scheduling allows user code
112      * called by this method and user code called by step handlers to get values
113      * of the independent time variable consistent with integration direction.</p>
114      *
115      * @param state current value of the independent <i>time</i> variable, state vector
116      * and derivative
117      * @param detector detector that triggered the event
118      * @param increasing if true, the value of the switching function increases
119      * when times increases around event (note that increase is measured with respect
120      * to physical time, not with respect to integration which may go backward in time)
121      * @return indication of what the integrator should do next, this
122      * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
123      * {@link Action#RESET_DERIVATIVES}, {@link Action#RESET_EVENTS}, or
124      * {@link Action#CONTINUE}
125      */
126     Action eventOccurred(ODEStateAndDerivative state, ODEEventDetector detector, boolean increasing);
127 
128     /** Reset the state prior to continue the integration.
129      *
130      * <p>This method is called after the step handler has returned and
131      * before the next step is started, but only when {@link
132      * #eventOccurred} has itself returned the {@link Action#RESET_STATE}
133      * indicator. It allows the user to reset the state vector for the
134      * next step, without perturbing the step handler of the finishing
135      * step.</p>
136      * <p>The default implementation returns its argument.</p>
137      * @param detector detector that triggered the event
138      * @param state current value of the independent <i>time</i> variable, state vector
139      * and derivative
140      * @return reset state (note that it does not include the derivatives, they will
141      * be added automatically by the integrator afterwards)
142      */
143     default ODEState resetState(ODEEventDetector detector, ODEStateAndDerivative state) {
144         return state;
145     }
146 
147 }