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.math.ode.jacobians;
019    
020    import org.apache.commons.math.ode.events.EventException;
021    
022    /** This interface represents a handler for discrete events triggered
023     * during ODE integration.
024     *
025     * <p>Some events can be triggered at discrete times as an ODE problem
026     * is solved. This occurs for example when the integration process
027     * should be stopped as some state is reached (G-stop facility) when the
028     * precise date is unknown a priori, or when the derivatives have
029     * discontinuities, or simply when the user wants to monitor some
030     * states boundaries crossings.
031     * </p>
032     *
033     * <p>These events are defined as occurring when a <code>g</code>
034     * switching function sign changes.</p>
035     *
036     * <p>Since events are only problem-dependent and are triggered by the
037     * independent <i>time</i> variable and the state vector, they can
038     * occur at virtually any time, unknown in advance. The integrators will
039     * take care to avoid sign changes inside the steps, they will reduce
040     * the step size when such an event is detected in order to put this
041     * event exactly at the end of the current step. This guarantees that
042     * step interpolation (which always has a one step scope) is relevant
043     * even in presence of discontinuities. This is independent from the
044     * stepsize control provided by integrators that monitor the local
045     * error (this event handling feature is available for all integrators,
046     * including fixed step ones).</p>
047     *
048     * <p>Note that is is possible to register a {@link
049     * org.apache.commons.math.ode.events.EventHandler classical event handler}
050     * in the low level integrator used to build a {@link FirstOrderIntegratorWithJacobians}
051     * rather than implementing this class. The event handlers registered at low level
052     * will see the big compound state whether the event handlers defined by this interface
053     * see the original state, and its jacobians in separate arrays.</p>
054     *
055     * <p>The compound state is guaranteed to contain the original state in the first
056     * elements, followed by the jacobian with respect to initial state (in row order),
057     * followed by the jacobian with respect to parameters (in row order). If for example
058     * the original state dimension is 6 and there are 3 parameters, the compound state will
059     * be a 60 elements array. The first 6 elements will be the original state, the next 36
060     * elements will be the jacobian with respect to initial state, and the remaining 18 elements
061     * will be the jacobian with respect to parameters.</p>
062     *
063     * <p>Dealing with low level event handlers is cumbersome if one really needs the jacobians
064     * in these methods, but it also prevents many data being copied back and forth between
065     * state and jacobians on one side and compound state on the other side. So for performance
066     * reasons, it is recommended to use this interface <em>only</em> if jacobians are really
067     * needed and to use lower level handlers if only state is needed.</p>
068     *
069     * @version $Revision: 1037341 $ $Date: 2010-11-20 22:58:35 +0100 (sam. 20 nov. 2010) $
070     * @since 2.1
071     * @deprecated as of 2.2 the complete package is deprecated, it will be replaced
072     * in 3.0 by a completely rewritten implementation
073     */
074    @Deprecated
075    public interface EventHandlerWithJacobians  {
076    
077        /** Stop indicator.
078         * <p>This value should be used as the return value of the {@link
079         * #eventOccurred eventOccurred} method when the integration should be
080         * stopped after the event ending the current step.</p>
081         */
082        int STOP = 0;
083    
084        /** Reset state indicator.
085         * <p>This value should be used as the return value of the {@link
086         * #eventOccurred eventOccurred} method when the integration should
087         * go on after the event ending the current step, with a new state
088         * vector (which will be retrieved thanks to the {@link #resetState
089         * resetState} method).</p>
090         */
091        int RESET_STATE = 1;
092    
093        /** Reset derivatives indicator.
094         * <p>This value should be used as the return value of the {@link
095         * #eventOccurred eventOccurred} method when the integration should
096         * go on after the event ending the current step, with a new derivatives
097         * vector (which will be retrieved thanks to the {@link
098         * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives}
099         * method).</p>
100         */
101        int RESET_DERIVATIVES = 2;
102    
103        /** Continue indicator.
104         * <p>This value should be used as the return value of the {@link
105         * #eventOccurred eventOccurred} method when the integration should go
106         * on after the event ending the current step.</p>
107         */
108        int CONTINUE = 3;
109    
110        /** Compute the value of the switching function.
111    
112         * <p>The discrete events are generated when the sign of this
113         * switching function changes. The integrator will take care to change
114         * the stepsize in such a way these events occur exactly at step boundaries.
115         * The switching function must be continuous in its roots neighborhood
116         * (but not necessarily smooth), as the integrator will need to find its
117         * roots to locate precisely the events.</p>
118    
119         * @param t current value of the independent <i>time</i> variable
120         * @param y array containing the current value of the state vector
121         * @param dydy0 array containing the current value of the jacobian of
122         * the state vector with respect to initial state
123         * @param dydp array containing the current value of the jacobian of
124         * the state vector with respect to parameters
125         * @return value of the g switching function
126         * @exception EventException if the switching function cannot be evaluated
127         */
128        double g(double t, double[] y, double[][] dydy0, double[][] dydp)
129            throws EventException;
130    
131        /** Handle an event and choose what to do next.
132    
133         * <p>This method is called when the integrator has accepted a step
134         * ending exactly on a sign change of the function, just <em>before</em>
135         * the step handler itself is called (see below for scheduling). It
136         * allows the user to update his internal data to acknowledge the fact
137         * the event has been handled (for example setting a flag in the {@link
138         * org.apache.commons.math.ode.jacobians.ODEWithJacobians
139         * differential equations} to switch the derivatives computation in
140         * case of discontinuity), or to direct the integrator to either stop
141         * or continue integration, possibly with a reset state or derivatives.</p>
142    
143         * <ul>
144         *   <li>if {@link #STOP} is returned, the step handler will be called
145         *   with the <code>isLast</code> flag of the {@link
146         *   org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians#handleStep(
147         *   StepInterpolatorWithJacobians, boolean) handleStep} method set to true and
148         *   the integration will be stopped,</li>
149         *   <li>if {@link #RESET_STATE} is returned, the {@link #resetState
150         *   resetState} method will be called once the step handler has
151         *   finished its task, and the integrator will also recompute the
152         *   derivatives,</li>
153         *   <li>if {@link #RESET_DERIVATIVES} is returned, the integrator
154         *   will recompute the derivatives,
155         *   <li>if {@link #CONTINUE} is returned, no specific action will
156         *   be taken (apart from having called this method) and integration
157         *   will continue.</li>
158         * </ul>
159    
160         * <p>The scheduling between this method and the {@link
161         * org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians
162         * StepHandlerWithJacobians} method {@link
163         * org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians#handleStep(
164         * StepInterpolatorWithJacobians, boolean) handleStep(interpolator, isLast)}
165         * is to call this method first and <code>handleStep</code> afterwards. This
166         * scheduling allows the integrator to pass <code>true</code> as the
167         * <code>isLast</code> parameter to the step handler to make it aware the step
168         * will be the last one if this method returns {@link #STOP}. As the
169         * interpolator may be used to navigate back throughout the last step (as {@link
170         * org.apache.commons.math.ode.sampling.StepNormalizer StepNormalizer}
171         * does for example), user code called by this method and user
172         * code called by step handlers may experience apparently out of order values
173         * of the independent time variable. As an example, if the same user object
174         * implements both this {@link EventHandlerWithJacobians EventHandler} interface and the
175         * {@link org.apache.commons.math.ode.sampling.FixedStepHandler FixedStepHandler}
176         * interface, a <em>forward</em> integration may call its
177         * <code>eventOccurred</code> method with t = 10 first and call its
178         * <code>handleStep</code> method with t = 9 afterwards. Such out of order
179         * calls are limited to the size of the integration step for {@link
180         * org.apache.commons.math.ode.sampling.StepHandler variable step handlers} and
181         * to the size of the fixed step for {@link
182         * org.apache.commons.math.ode.sampling.FixedStepHandler fixed step handlers}.</p>
183    
184         * @param t current value of the independent <i>time</i> variable
185         * @param y array containing the current value of the state vector
186         * @param dydy0 array containing the current value of the jacobian of
187         * the state vector with respect to initial state
188         * @param dydp array containing the current value of the jacobian of
189         * the state vector with respect to parameters
190         * @param increasing if true, the value of the switching function increases
191         * when times increases around event (note that increase is measured with respect
192         * to physical time, not with respect to integration which may go backward in time)
193         * @return indication of what the integrator should do next, this
194         * value must be one of {@link #STOP}, {@link #RESET_STATE},
195         * {@link #RESET_DERIVATIVES} or {@link #CONTINUE}
196         * @exception EventException if the event occurrence triggers an error
197         */
198        int eventOccurred(double t, double[] y, double[][] dydy0, double[][] dydp,
199                          boolean increasing) throws EventException;
200    
201        /** Reset the state prior to continue the integration.
202    
203         * <p>This method is called after the step handler has returned and
204         * before the next step is started, but only when {@link
205         * #eventOccurred} has itself returned the {@link #RESET_STATE}
206         * indicator. It allows the user to reset the state vector for the
207         * next step, without perturbing the step handler of the finishing
208         * step. If the {@link #eventOccurred} never returns the {@link
209         * #RESET_STATE} indicator, this function will never be called, and it is
210         * safe to leave its body empty.</p>
211    
212         * @param t current value of the independent <i>time</i> variable
213         * @param y array containing the current value of the state vector
214         * the new state should be put in the same array
215         * @param dydy0 array containing the current value of the jacobian of
216         * the state vector with respect to initial state, the new jacobian
217         * should be put in the same array
218         * @param dydp array containing the current value of the jacobian of
219         * the state vector with respect to parameters, the new jacobian
220         * should be put in the same array
221         * @exception EventException if the state cannot be reseted
222         */
223        void resetState(double t, double[] y, double[][] dydy0, double[][] dydp)
224        throws EventException;
225    
226    }