View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) 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 ASF 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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.migration.ode.events;
24  
25  import org.hipparchus.ode.ODEState;
26  import org.hipparchus.ode.ODEStateAndDerivative;
27  import org.hipparchus.ode.events.ODEEventDetector;
28  import org.hipparchus.ode.events.ODEEventHandler;
29  
30  /** This interface represents a handler for discrete events triggered
31   * during ODE integration.
32   *
33   * <p>Some events can be triggered at discrete times as an ODE problem
34   * is solved. This occurs for example when the integration process
35   * should be stopped as some state is reached (G-stop facility) when the
36   * precise date is unknown a priori, or when the derivatives have
37   * discontinuities, or simply when the user wants to monitor some
38   * states boundaries crossings.
39   * </p>
40   *
41   * <p>These events are defined as occurring when a <code>g</code>
42   * switching function sign changes.</p>
43   *
44   * <p>Since events are only problem-dependent and are triggered by the
45   * independent <i>time</i> variable and the state vector, they can
46   * occur at virtually any time, unknown in advance. The integrators will
47   * take care to avoid sign changes inside the steps, they will reduce
48   * the step size when such an event is detected in order to put this
49   * event exactly at the end of the current step. This guarantees that
50   * step interpolation (which always has a one step scope) is relevant
51   * even in presence of discontinuities. This is independent from the
52   * stepsize control provided by integrators that monitor the local
53   * error (this event handling feature is available for all integrators,
54   * including fixed step ones).</p>
55   * @deprecated as of 1.0, replaced with {@link ODEEventDetector}
56   */
57  @Deprecated
58  public interface EventHandler extends ODEEventDetector {
59  
60      /** {@inheritDoc} */
61      @Override
62      default void init(final ODEStateAndDerivative initialState, final double finalTime) {
63          init(initialState.getTime(), initialState.getPrimaryState(), finalTime);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      default double g(final ODEStateAndDerivative state) {
69          return g(state.getTime(), state.getPrimaryState());
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      default ODEEventHandler getHandler() {
75          return new ODEEventHandler() {
76              /** {@inheritDoc} */
77              @Override
78              public org.hipparchus.ode.events.Action eventOccurred(ODEStateAndDerivative state, ODEEventDetector detector, boolean increasing) {
79                  switch (EventHandler.this.eventOccurred(state.getTime(), state.getPrimaryState(), increasing)) {
80                      case CONTINUE:
81                          return org.hipparchus.ode.events.Action.CONTINUE;
82                      case RESET_DERIVATIVES:
83                          return org.hipparchus.ode.events.Action.RESET_DERIVATIVES;
84                      case RESET_STATE:
85                          return org.hipparchus.ode.events.Action.RESET_STATE;
86                      default:
87                          return org.hipparchus.ode.events.Action.STOP;
88                  }
89              }
90  
91              /** {@inheritDoc} */
92              @Override
93              public ODEState resetState(final ODEEventDetector detector, final ODEStateAndDerivative state) {
94                  final double   t = state.getTime();
95                  final double[] y = state.getPrimaryState();
96                  EventHandler.this.resetState(t, y);
97                  return new ODEState(t, y);
98              }
99          };
100     }
101 
102     /** Enumerate for actions to be performed when an event occurs.
103      * @deprecated as of 1.0, replaced with {@link org.hipparchus.ode.events.Action}
104      */
105     @Deprecated
106     enum Action {
107 
108         /** Stop indicator.
109          * <p>This value should be used as the return value of the {@link
110          * #eventOccurred eventOccurred} method when the integration should be
111          * stopped after the event ending the current step.</p>
112          */
113         STOP,
114 
115         /** Reset state indicator.
116          * <p>This value should be used as the return value of the {@link
117          * #eventOccurred eventOccurred} method when the integration should
118          * go on after the event ending the current step, with a new state
119          * vector (which will be retrieved thanks to the {@link #resetState
120          * resetState} method).</p>
121          */
122         RESET_STATE,
123 
124         /** Reset derivatives indicator.
125          * <p>This value should be used as the return value of the {@link
126          * #eventOccurred eventOccurred} method when the integration should
127          * go on after the event ending the current step, with a new derivatives
128          * vector (which will be retrieved thanks to the {@link
129          * org.hipparchus.ode.OrdinaryDifferentialEquation#computeDerivatives}
130          * method).</p>
131          */
132         RESET_DERIVATIVES,
133 
134         /** Continue indicator.
135          * <p>This value should be used as the return value of the {@link
136          * #eventOccurred eventOccurred} method when the integration should go
137          * on after the event ending the current step.</p>
138          */
139         CONTINUE;
140 
141     }
142 
143     /** Initialize event handler at the start of an ODE integration.
144      * <p>
145      * This method is called once at the start of the integration. It
146      * may be used by the event handler to initialize some internal data
147      * if needed.
148      * </p>
149      * @param t0 start value of the independent <i>time</i> variable
150      * @param y0 array containing the start value of the state vector
151      * @param t target time for the integration
152      */
153     void init(double t0, double[] y0, double t);
154 
155     /** Compute the value of the switching function.
156 
157      * <p>The discrete events are generated when the sign of this
158      * switching function changes. The integrator will take care to change
159      * the stepsize in such a way these events occur exactly at step boundaries.
160      * The switching function must be continuous in its roots neighborhood
161      * (but not necessarily smooth), as the integrator will need to find its
162      * roots to locate precisely the events.</p>
163      * <p>Also note that the integrator expect that once an event has occurred,
164      * the sign of the switching function at the start of the next step (i.e.
165      * just after the event) is the opposite of the sign just before the event.
166      * This consistency between the steps <strong>must</strong> be preserved,
167      * otherwise {@link org.hipparchus.exception.MathIllegalArgumentException
168      * exceptions} related to root not being bracketed will occur.</p>
169      * <p>This need for consistency is sometimes tricky to achieve. A typical
170      * example is using an event to model a ball bouncing on the floor. The first
171      * idea to represent this would be to have {@code g(t) = h(t)} where h is the
172      * height above the floor at time {@code t}. When {@code g(t)} reaches 0, the
173      * ball is on the floor, so it should bounce and the typical way to do this is
174      * to reverse its vertical velocity. However, this would mean that before the
175      * event {@code g(t)} was decreasing from positive values to 0, and after the
176      * event {@code g(t)} would be increasing from 0 to positive values again.
177      * Consistency is broken here! The solution here is to have {@code g(t) = sign
178      * * h(t)}, where sign is a variable with initial value set to {@code +1}. Each
179      * time {@link #eventOccurred(double, double[], boolean) eventOccurred} is called,
180      * {@code sign} is reset to {@code -sign}. This allows the {@code g(t)}
181      * function to remain continuous (and even smooth) even across events, despite
182      * {@code h(t)} is not. Basically, the event is used to <em>fold</em> {@code h(t)}
183      * at bounce points, and {@code sign} is used to <em>unfold</em> it back, so the
184      * solvers sees a {@code g(t)} function which behaves smoothly even across events.</p>
185 
186      * @param t current value of the independent <i>time</i> variable
187      * @param y array containing the current value of the state vector
188      * @return value of the g switching function
189      */
190     double g(double t, double[] y);
191 
192     /** Handle an event and choose what to do next.
193 
194      * <p>This method is called when the integrator has accepted a step
195      * ending exactly on a sign change of the function, just <em>before</em>
196      * the step handler itself is called (see below for scheduling). It
197      * allows the user to update his internal data to acknowledge the fact
198      * the event has been handled (for example setting a flag in the {@link
199      * org.hipparchus.migration.ode.FirstOrderDifferentialEquations
200      * differential equations} to switch the derivatives computation in
201      * case of discontinuity), or to direct the integrator to either stop
202      * or continue integration, possibly with a reset state or derivatives.</p>
203 
204      * <ul>
205      *   <li>if {@link Action#STOP} is returned, the integration will be stopped,</li>
206      *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
207      *   resetState} method will be called once the step handler has
208      *   finished its task, and the integrator will also recompute the
209      *   derivatives,</li>
210      *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
211      *   will recompute the derivatives,
212      *   <li>if {@link Action#CONTINUE} is returned, no specific action will
213      *   be taken (apart from having called this method) and integration
214      *   will continue.</li>
215      * </ul>
216 
217      * @param t current value of the independent <i>time</i> variable
218      * @param y array containing the current value of the state vector
219      * @param increasing if true, the value of the switching function increases
220      * when times increases around event (note that increase is measured with respect
221      * to physical time, not with respect to integration which may go backward in time)
222      * @return indication of what the integrator should do next, this
223      * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
224      * {@link Action#RESET_DERIVATIVES} or {@link Action#CONTINUE}
225      */
226     Action eventOccurred(double t, double[] y, boolean increasing);
227 
228     /** Reset the state prior to continue the integration.
229 
230      * <p>This method is called after the step handler has returned and
231      * before the next step is started, but only when {@link
232      * #eventOccurred} has itself returned the {@link Action#RESET_STATE}
233      * indicator. It allows the user to reset the state vector for the
234      * next step, without perturbing the step handler of the finishing
235      * step. If the {@link #eventOccurred} never returns the {@link
236      * Action#RESET_STATE} indicator, this function will never be called, and it is
237      * safe to leave its body empty.</p>
238 
239      * @param t current value of the independent <i>time</i> variable
240      * @param y array containing the current value of the state vector
241      * the new state should be put in the same array
242      */
243     void resetState(double t, double[] y);
244 
245 }