001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.ode.events;
019
020import org.apache.commons.math3.RealFieldElement;
021import org.apache.commons.math3.ode.FieldODEState;
022import org.apache.commons.math3.ode.FieldODEStateAndDerivative;
023
024/** This interface represents a handler for discrete events triggered
025 * during ODE integration.
026 *
027 * <p>Some events can be triggered at discrete times as an ODE problem
028 * is solved. This occurs for example when the integration process
029 * should be stopped as some state is reached (G-stop facility) when the
030 * precise date is unknown a priori, or when the derivatives have
031 * discontinuities, or simply when the user wants to monitor some
032 * states boundaries crossings.
033 * </p>
034 *
035 * <p>These events are defined as occurring when a <code>g</code>
036 * switching function sign changes.</p>
037 *
038 * <p>Since events are only problem-dependent and are triggered by the
039 * independent <i>time</i> variable and the state vector, they can
040 * occur at virtually any time, unknown in advance. The integrators will
041 * take care to avoid sign changes inside the steps, they will reduce
042 * the step size when such an event is detected in order to put this
043 * event exactly at the end of the current step. This guarantees that
044 * step interpolation (which always has a one step scope) is relevant
045 * even in presence of discontinuities. This is independent from the
046 * stepsize control provided by integrators that monitor the local
047 * error (this event handling feature is available for all integrators,
048 * including fixed step ones).</p>
049 *
050 * @param <T> the type of the field elements
051 * @since 3.6
052 */
053public interface FieldEventHandler<T extends RealFieldElement<T>>  {
054
055    /** Initialize event handler at the start of an ODE integration.
056     * <p>
057     * This method is called once at the start of the integration. It
058     * may be used by the event handler to initialize some internal data
059     * if needed.
060     * </p>
061     * @param initialState initial time, state vector and derivative
062     * @param finalTime target time for the integration
063     */
064    void init(FieldODEStateAndDerivative<T> initialState, T finalTime);
065
066    /** Compute the value of the switching function.
067
068     * <p>The discrete events are generated when the sign of this
069     * switching function changes. The integrator will take care to change
070     * the stepsize in such a way these events occur exactly at step boundaries.
071     * The switching function must be continuous in its roots neighborhood
072     * (but not necessarily smooth), as the integrator will need to find its
073     * roots to locate precisely the events.</p>
074     * <p>Also note that the integrator expect that once an event has occurred,
075     * the sign of the switching function at the start of the next step (i.e.
076     * just after the event) is the opposite of the sign just before the event.
077     * This consistency between the steps <string>must</strong> be preserved,
078     * otherwise {@link org.apache.commons.math3.exception.NoBracketingException
079     * exceptions} related to root not being bracketed will occur.</p>
080     * <p>This need for consistency is sometimes tricky to achieve. A typical
081     * example is using an event to model a ball bouncing on the floor. The first
082     * idea to represent this would be to have {@code g(t) = h(t)} where h is the
083     * height above the floor at time {@code t}. When {@code g(t)} reaches 0, the
084     * ball is on the floor, so it should bounce and the typical way to do this is
085     * to reverse its vertical velocity. However, this would mean that before the
086     * event {@code g(t)} was decreasing from positive values to 0, and after the
087     * event {@code g(t)} would be increasing from 0 to positive values again.
088     * Consistency is broken here! The solution here is to have {@code g(t) = sign
089     * * h(t)}, where sign is a variable with initial value set to {@code +1}. Each
090     * time {@link #eventOccurred(FieldODEStateAndDerivative, boolean) eventOccurred}
091     * method is called, {@code sign} is reset to {@code -sign}. This allows the
092     * {@code g(t)} function to remain continuous (and even smooth) even across events,
093     * despite {@code h(t)} is not. Basically, the event is used to <em>fold</em>
094     * {@code h(t)} at bounce points, and {@code sign} is used to <em>unfold</em> it
095     * back, so the solvers sees a {@code g(t)} function which behaves smoothly even
096     * across events.</p>
097
098     * @param state current value of the independent <i>time</i> variable, state vector
099     * 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.math3.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.math3.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.math3.ode.sampling.FieldStepHandler FieldStepHandler} method {@link
134     * org.apache.commons.math3.ode.sampling.FieldStepHandler#handleStep(
135     * org.apache.commons.math3.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.math3.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.math3.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
180}