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    *      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  
18  package org.apache.commons.math4.legacy.ode.events;
19  
20  import org.apache.commons.math4.legacy.core.RealFieldElement;
21  import org.apache.commons.math4.legacy.ode.FieldODEState;
22  import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
23  
24  /** This interface represents a handler for discrete events triggered
25   * during ODE integration.
26   *
27   * <p>Some events can be triggered at discrete times as an ODE problem
28   * is solved. This occurs for example when the integration process
29   * should be stopped as some state is reached (G-stop facility) when the
30   * precise date is unknown a priori, or when the derivatives have
31   * discontinuities, or simply when the user wants to monitor some
32   * states boundaries crossings.
33   * </p>
34   *
35   * <p>These events are defined as occurring when a <code>g</code>
36   * switching function sign changes.</p>
37   *
38   * <p>Since events are only problem-dependent and are triggered by the
39   * independent <i>time</i> variable and the state vector, they can
40   * occur at virtually any time, unknown in advance. The integrators will
41   * take care to avoid sign changes inside the steps, they will reduce
42   * the step size when such an event is detected in order to put this
43   * event exactly at the end of the current step. This guarantees that
44   * step interpolation (which always has a one step scope) is relevant
45   * even in presence of discontinuities. This is independent from the
46   * stepsize control provided by integrators that monitor the local
47   * error (this event handling feature is available for all integrators,
48   * including fixed step ones).</p>
49   *
50   * @param <T> the type of the field elements
51   * @since 3.6
52   */
53  public interface FieldEventHandler<T extends RealFieldElement<T>>  {
54  
55      /** Initialize event handler at the start of an ODE integration.
56       * <p>
57       * This method is called once at the start of the integration. It
58       * may be used by the event handler to initialize some internal data
59       * if needed.
60       * </p>
61       * @param initialState initial time, state vector and derivative
62       * @param finalTime target time for the integration
63       */
64      void init(FieldODEStateAndDerivative<T> initialState, T finalTime);
65  
66      /** Compute the value of the switching function.
67  
68       * <p>The discrete events are generated when the sign of this
69       * switching function changes. The integrator will take care to change
70       * the stepsize in such a way these events occur exactly at step boundaries.
71       * The switching function must be continuous in its roots neighborhood
72       * (but not necessarily smooth), as the integrator will need to find its
73       * roots to locate precisely the events.</p>
74       * <p>Also note that the integrator expect that once an event has occurred,
75       * the sign of the switching function at the start of the next step (i.e.
76       * just after the event) is the opposite of the sign just before the event.
77       * This consistency between the steps <strong>must</strong> be preserved,
78       * otherwise {@link org.apache.commons.math4.legacy.exception.NoBracketingException
79       * exceptions} related to root not being bracketed will occur.</p>
80       * <p>This need for consistency is sometimes tricky to achieve. A typical
81       * example is using an event to model a ball bouncing on the floor. The first
82       * idea to represent this would be to have {@code g(t) = h(t)} where h is the
83       * height above the floor at time {@code t}. When {@code g(t)} reaches 0, the
84       * ball is on the floor, so it should bounce and the typical way to do this is
85       * to reverse its vertical velocity. However, this would mean that before the
86       * event {@code g(t)} was decreasing from positive values to 0, and after the
87       * event {@code g(t)} would be increasing from 0 to positive values again.
88       * Consistency is broken here! The solution here is to have {@code g(t) = sign
89       * * h(t)}, where sign is a variable with initial value set to {@code +1}. Each
90       * time {@link #eventOccurred(FieldODEStateAndDerivative, boolean) eventOccurred}
91       * method is called, {@code sign} is reset to {@code -sign}. This allows the
92       * {@code g(t)} function to remain continuous (and even smooth) even across events,
93       * despite {@code h(t)} is not. Basically, the event is used to <em>fold</em>
94       * {@code h(t)} at bounce points, and {@code sign} is used to <em>unfold</em> it
95       * back, so the solvers sees a {@code g(t)} function which behaves smoothly even
96       * across events.</p>
97  
98       * @param state current value of the independent <i>time</i> variable, state vector
99       * and derivative
100      * @return value of the g switching function
101      */
102     T g(FieldODEStateAndDerivative<T> state);
103 
104     /** Handle an event and choose what to do next.
105 
106      * <p>This method is called when the integrator has accepted a step
107      * ending exactly on a sign change of the function, just <em>before</em>
108      * the step handler itself is called (see below for scheduling). It
109      * allows the user to update his internal data to acknowledge the fact
110      * the event has been handled (for example setting a flag in the {@link
111      * org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations
112      * differential equations} to switch the derivatives computation in
113      * case of discontinuity), or to direct the integrator to either stop
114      * or continue integration, possibly with a reset state or derivatives.</p>
115 
116      * <ul>
117      *   <li>if {@link Action#STOP} is returned, the step handler will be called
118      *   with the <code>isLast</code> flag of the {@link
119      *   org.apache.commons.math4.legacy.ode.sampling.StepHandler#handleStep handleStep}
120      *   method set to true and the integration will be stopped,</li>
121      *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
122      *   resetState} method will be called once the step handler has
123      *   finished its task, and the integrator will also recompute the
124      *   derivatives,</li>
125      *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
126      *   will recompute the derivatives,
127      *   <li>if {@link Action#CONTINUE} is returned, no specific action will
128      *   be taken (apart from having called this method) and integration
129      *   will continue.</li>
130      * </ul>
131 
132      * <p>The scheduling between this method and the {@link
133      * org.apache.commons.math4.legacy.ode.sampling.FieldStepHandler FieldStepHandler} method {@link
134      * org.apache.commons.math4.legacy.ode.sampling.FieldStepHandler#handleStep(
135      * org.apache.commons.math4.legacy.ode.sampling.FieldStepInterpolator, boolean)
136      * handleStep(interpolator, isLast)} is to call this method first and
137      * <code>handleStep</code> afterwards. This scheduling allows the integrator to
138      * pass <code>true</code> as the <code>isLast</code> parameter to the step
139      * handler to make it aware the step will be the last one if this method
140      * returns {@link Action#STOP}. As the interpolator may be used to navigate back
141      * throughout the last step, user code called by this method and user
142      * code called by step handlers may experience apparently out of order values
143      * of the independent time variable. As an example, if the same user object
144      * implements both this {@link FieldEventHandler FieldEventHandler} interface and the
145      * {@link org.apache.commons.math4.legacy.ode.sampling.FieldStepHandler FieldStepHandler}
146      * interface, a <em>forward</em> integration may call its
147      * {code eventOccurred} method with t = 10 first and call its
148      * {code handleStep} method with t = 9 afterwards. Such out of order
149      * calls are limited to the size of the integration step for {@link
150      * org.apache.commons.math4.legacy.ode.sampling.FieldStepHandler variable step handlers}.</p>
151 
152      * @param state current value of the independent <i>time</i> variable, state vector
153      * and derivative
154      * @param increasing if true, the value of the switching function increases
155      * when times increases around event (note that increase is measured with respect
156      * to physical time, not with respect to integration which may go backward in time)
157      * @return indication of what the integrator should do next, this
158      * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
159      * {@link Action#RESET_DERIVATIVES} or {@link Action#CONTINUE}
160      */
161     Action eventOccurred(FieldODEStateAndDerivative<T> state, boolean increasing);
162 
163     /** Reset the state prior to continue the integration.
164 
165      * <p>This method is called after the step handler has returned and
166      * before the next step is started, but only when {@link
167      * #eventOccurred(FieldODEStateAndDerivative, boolean) eventOccurred} has itself
168      * returned the {@link Action#RESET_STATE} indicator. It allows the user to reset
169      * the state vector for the next step, without perturbing the step handler of the
170      * finishing step. If the {@link #eventOccurred(FieldODEStateAndDerivative, boolean)
171      * eventOccurred} never returns the {@link Action#RESET_STATE} indicator, this
172      * function will never be called, and it is safe to leave its body empty.</p>
173      * @param state current value of the independent <i>time</i> variable, state vector
174      * and derivative
175      * @return reset state (note that it does not include the derivatives, they will
176      * be added automatically by the integrator afterwards)
177      */
178     FieldODEState<T> resetState(FieldODEStateAndDerivative<T> state);
179 }