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    
018    package org.apache.commons.math3.ode.events;
019    
020    /** This interface represents a handler for discrete events triggered
021     * during ODE integration.
022     *
023     * <p>Some events can be triggered at discrete times as an ODE problem
024     * is solved. This occurs for example when the integration process
025     * should be stopped as some state is reached (G-stop facility) when the
026     * precise date is unknown a priori, or when the derivatives have
027     * discontinuities, or simply when the user wants to monitor some
028     * states boundaries crossings.
029     * </p>
030     *
031     * <p>These events are defined as occurring when a <code>g</code>
032     * switching function sign changes.</p>
033     *
034     * <p>Since events are only problem-dependent and are triggered by the
035     * independent <i>time</i> variable and the state vector, they can
036     * occur at virtually any time, unknown in advance. The integrators will
037     * take care to avoid sign changes inside the steps, they will reduce
038     * the step size when such an event is detected in order to put this
039     * event exactly at the end of the current step. This guarantees that
040     * step interpolation (which always has a one step scope) is relevant
041     * even in presence of discontinuities. This is independent from the
042     * stepsize control provided by integrators that monitor the local
043     * error (this event handling feature is available for all integrators,
044     * including fixed step ones).</p>
045     *
046     * @version $Id: EventHandler.java 1416643 2012-12-03 19:37:14Z tn $
047     * @since 1.2
048     */
049    
050    public interface EventHandler  {
051    
052        /** Enumerate for actions to be performed when an event occurs. */
053        public enum Action {
054    
055            /** Stop indicator.
056             * <p>This value should be used as the return value of the {@link
057             * #eventOccurred eventOccurred} method when the integration should be
058             * stopped after the event ending the current step.</p>
059             */
060            STOP,
061    
062            /** Reset state indicator.
063             * <p>This value should be used as the return value of the {@link
064             * #eventOccurred eventOccurred} method when the integration should
065             * go on after the event ending the current step, with a new state
066             * vector (which will be retrieved thanks to the {@link #resetState
067             * resetState} method).</p>
068             */
069            RESET_STATE,
070    
071            /** Reset derivatives indicator.
072             * <p>This value should be used as the return value of the {@link
073             * #eventOccurred eventOccurred} method when the integration should
074             * go on after the event ending the current step, with a new derivatives
075             * vector (which will be retrieved thanks to the {@link
076             * org.apache.commons.math3.ode.FirstOrderDifferentialEquations#computeDerivatives}
077             * method).</p>
078             */
079            RESET_DERIVATIVES,
080    
081            /** Continue indicator.
082             * <p>This value should be used as the return value of the {@link
083             * #eventOccurred eventOccurred} method when the integration should go
084             * on after the event ending the current step.</p>
085             */
086            CONTINUE;
087    
088        }
089    
090        /** Initialize event handler at the start of an ODE integration.
091         * <p>
092         * This method is called once at the start of the integration. It
093         * may be used by the event handler to initialize some internal data
094         * if needed.
095         * </p>
096         * @param t0 start value of the independent <i>time</i> variable
097         * @param y0 array containing the start value of the state vector
098         * @param t target time for the integration
099         */
100        void init(double t0, double[] y0, double t);
101    
102      /** Compute the value of the switching function.
103    
104       * <p>The discrete events are generated when the sign of this
105       * switching function changes. The integrator will take care to change
106       * the stepsize in such a way these events occur exactly at step boundaries.
107       * The switching function must be continuous in its roots neighborhood
108       * (but not necessarily smooth), as the integrator will need to find its
109       * roots to locate precisely the events.</p>
110    
111       * @param t current value of the independent <i>time</i> variable
112       * @param y array containing the current value of the state vector
113       * @return value of the g switching function
114       */
115      double g(double t, double[] y);
116    
117      /** Handle an event and choose what to do next.
118    
119       * <p>This method is called when the integrator has accepted a step
120       * ending exactly on a sign change of the function, just <em>before</em>
121       * the step handler itself is called (see below for scheduling). It
122       * allows the user to update his internal data to acknowledge the fact
123       * the event has been handled (for example setting a flag in the {@link
124       * org.apache.commons.math3.ode.FirstOrderDifferentialEquations
125       * differential equations} to switch the derivatives computation in
126       * case of discontinuity), or to direct the integrator to either stop
127       * or continue integration, possibly with a reset state or derivatives.</p>
128    
129       * <ul>
130       *   <li>if {@link Action#STOP} is returned, the step handler will be called
131       *   with the <code>isLast</code> flag of the {@link
132       *   org.apache.commons.math3.ode.sampling.StepHandler#handleStep handleStep}
133       *   method set to true and the integration will be stopped,</li>
134       *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
135       *   resetState} method will be called once the step handler has
136       *   finished its task, and the integrator will also recompute the
137       *   derivatives,</li>
138       *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
139       *   will recompute the derivatives,
140       *   <li>if {@link Action#CONTINUE} is returned, no specific action will
141       *   be taken (apart from having called this method) and integration
142       *   will continue.</li>
143       * </ul>
144    
145       * <p>The scheduling between this method and the {@link
146       * org.apache.commons.math3.ode.sampling.StepHandler StepHandler} method {@link
147       * org.apache.commons.math3.ode.sampling.StepHandler#handleStep(
148       * org.apache.commons.math3.ode.sampling.StepInterpolator, boolean)
149       * handleStep(interpolator, isLast)} is to call this method first and
150       * <code>handleStep</code> afterwards. This scheduling allows the integrator to
151       * pass <code>true</code> as the <code>isLast</code> parameter to the step
152       * handler to make it aware the step will be the last one if this method
153       * returns {@link Action#STOP}. As the interpolator may be used to navigate back
154       * throughout the last step (as {@link
155       * org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer}
156       * does for example), user code called by this method and user
157       * code called by step handlers may experience apparently out of order values
158       * of the independent time variable. As an example, if the same user object
159       * implements both this {@link EventHandler EventHandler} interface and the
160       * {@link org.apache.commons.math3.ode.sampling.FixedStepHandler FixedStepHandler}
161       * interface, a <em>forward</em> integration may call its
162       * <code>eventOccurred</code> method with t = 10 first and call its
163       * <code>handleStep</code> method with t = 9 afterwards. Such out of order
164       * calls are limited to the size of the integration step for {@link
165       * org.apache.commons.math3.ode.sampling.StepHandler variable step handlers} and
166       * to the size of the fixed step for {@link
167       * org.apache.commons.math3.ode.sampling.FixedStepHandler fixed step handlers}.</p>
168    
169       * @param t current value of the independent <i>time</i> variable
170       * @param y array containing the current value of the state vector
171       * @param increasing if true, the value of the switching function increases
172       * when times increases around event (note that increase is measured with respect
173       * to physical time, not with respect to integration which may go backward in time)
174       * @return indication of what the integrator should do next, this
175       * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
176       * {@link Action#RESET_DERIVATIVES} or {@link Action#CONTINUE}
177       */
178      Action eventOccurred(double t, double[] y, boolean increasing);
179    
180      /** Reset the state prior to continue the integration.
181    
182       * <p>This method is called after the step handler has returned and
183       * before the next step is started, but only when {@link
184       * #eventOccurred} has itself returned the {@link Action#RESET_STATE}
185       * indicator. It allows the user to reset the state vector for the
186       * next step, without perturbing the step handler of the finishing
187       * step. If the {@link #eventOccurred} never returns the {@link
188       * Action#RESET_STATE} indicator, this function will never be called, and it is
189       * safe to leave its body empty.</p>
190    
191       * @param t current value of the independent <i>time</i> variable
192       * @param y array containing the current value of the state vector
193       * the new state should be put in the same array
194       */
195      void resetState(double t, double[] y);
196    
197    }