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  public interface ODEEventHandler  {
61  
62      /** Initialize event handler at the start of an ODE integration.
63       * <p>
64       * This method is called once at the start of the integration. It
65       * may be used by the event handler to initialize some internal data
66       * if needed.
67       * </p>
68       * <p>
69       * The default implementation does nothing
70       * </p>
71       * @param initialState initial time, state vector and derivative
72       * @param finalTime target time for the integration
73       * @param detector event detector related to the event handler
74       */
75      default void init(ODEStateAndDerivative initialState, double finalTime, ODEEventDetector detector) {
76          // nothing by default
77      }
78  
79      /** Handle an event and choose what to do next.
80  
81       * <p>This method is called when the integrator has accepted a step
82       * ending exactly on a sign change of the function, just <em>after</em>
83       * the step handler itself is called (see below for scheduling). It
84       * allows the user to update his internal data to acknowledge the fact
85       * the event has been handled (for example setting a flag in the {@link
86       * org.hipparchus.ode.OrdinaryDifferentialEquation
87       * differential equations} to switch the derivatives computation in
88       * case of discontinuity), or to direct the integrator to either stop
89       * or continue integration, possibly with a reset state or derivatives.</p>
90       *
91       * <ul>
92       *   <li>if {@link Action#STOP} is returned, the integration will be stopped,</li>
93       *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
94       *   resetState} method will be called once the step handler has
95       *   finished its task, and the integrator will also recompute the
96       *   derivatives,</li>
97       *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
98       *   will recompute the derivatives,
99       *   <li>if {@link Action#RESET_EVENTS} is returned, the integrator
100      *   will recheck all event handlers,
101      *   <li>if {@link Action#CONTINUE} is returned, no specific action will
102      *   be taken (apart from having called this method) and integration
103      *   will continue.</li>
104      * </ul>
105      *
106      * <p>The scheduling between this method and the {@link
107      * org.hipparchus.ode.sampling.ODEStepHandler ODEStepHandler} method {@link
108      * org.hipparchus.ode.sampling.ODEStepHandler#handleStep(org.hipparchus.ode.sampling.ODEStateInterpolator)
109      * handleStep(interpolator)} is to call {@code handleStep} first and this method afterwards
110      * (this scheduling changed as of Hipparchus 2.0). This scheduling allows user code
111      * called by this method and user code called by step handlers to get values
112      * of the independent time variable consistent with integration direction.</p>
113      *
114      * @param state current value of the independent <i>time</i> variable, state vector
115      * and derivative
116      * @param detector detector that triggered the event
117      * @param increasing if true, the value of the switching function increases
118      * when times increases around event (note that increase is measured with respect
119      * to physical time, not with respect to integration which may go backward in time)
120      * @return indication of what the integrator should do next, this
121      * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
122      * {@link Action#RESET_DERIVATIVES}, {@link Action#RESET_EVENTS}, or
123      * {@link Action#CONTINUE}
124      */
125     Action eventOccurred(ODEStateAndDerivative state, ODEEventDetector detector, boolean increasing);
126 
127     /** Reset the state prior to continue the integration.
128      *
129      * <p>This method is called after the step handler has returned and
130      * before the next step is started, but only when {@link
131      * #eventOccurred} has itself returned the {@link Action#RESET_STATE}
132      * indicator. It allows the user to reset the state vector for the
133      * next step, without perturbing the step handler of the finishing
134      * step.</p>
135      * <p>The default implementation returns its argument.</p>
136      * @param detector detector that triggered the event
137      * @param state current value of the independent <i>time</i> variable, state vector
138      * and derivative
139      * @return reset state (note that it does not include the derivatives, they will
140      * be added automatically by the integrator afterwards)
141      */
142     default ODEState resetState(ODEEventDetector detector, ODEStateAndDerivative state) {
143         return state;
144     }
145 
146 }