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
18package org.apache.commons.math.ode.jacobians;
19
20import org.apache.commons.math.ode.events.EventException;
21
22/** This interface represents a handler for discrete events triggered
23 * during ODE integration.
24 *
25 * <p>Some events can be triggered at discrete times as an ODE problem
26 * is solved. This occurs for example when the integration process
27 * should be stopped as some state is reached (G-stop facility) when the
28 * precise date is unknown a priori, or when the derivatives have
29 * discontinuities, or simply when the user wants to monitor some
30 * states boundaries crossings.
31 * </p>
32 *
33 * <p>These events are defined as occurring when a <code>g</code>
34 * switching function sign changes.</p>
35 *
36 * <p>Since events are only problem-dependent and are triggered by the
37 * independent <i>time</i> variable and the state vector, they can
38 * occur at virtually any time, unknown in advance. The integrators will
39 * take care to avoid sign changes inside the steps, they will reduce
40 * the step size when such an event is detected in order to put this
41 * event exactly at the end of the current step. This guarantees that
42 * step interpolation (which always has a one step scope) is relevant
43 * even in presence of discontinuities. This is independent from the
44 * stepsize control provided by integrators that monitor the local
45 * error (this event handling feature is available for all integrators,
46 * including fixed step ones).</p>
47 *
48 * <p>Note that is is possible to register a {@link
49 * org.apache.commons.math.ode.events.EventHandler classical event handler}
50 * in the low level integrator used to build a {@link FirstOrderIntegratorWithJacobians}
51 * rather than implementing this class. The event handlers registered at low level
52 * will see the big compound state whether the event handlers defined by this interface
53 * see the original state, and its jacobians in separate arrays.</p>
54 *
55 * <p>The compound state is guaranteed to contain the original state in the first
56 * elements, followed by the jacobian with respect to initial state (in row order),
57 * followed by the jacobian with respect to parameters (in row order). If for example
58 * the original state dimension is 6 and there are 3 parameters, the compound state will
59 * be a 60 elements array. The first 6 elements will be the original state, the next 36
60 * elements will be the jacobian with respect to initial state, and the remaining 18 elements
61 * will be the jacobian with respect to parameters.</p>
62 *
63 * <p>Dealing with low level event handlers is cumbersome if one really needs the jacobians
64 * in these methods, but it also prevents many data being copied back and forth between
65 * state and jacobians on one side and compound state on the other side. So for performance
66 * reasons, it is recommended to use this interface <em>only</em> if jacobians are really
67 * needed and to use lower level handlers if only state is needed.</p>
68 *
69 * @version $Revision: 1037341 $ $Date: 2010-11-20 22:58:35 +0100 (sam. 20 nov. 2010) $
70 * @since 2.1
71 * @deprecated as of 2.2 the complete package is deprecated, it will be replaced
72 * in 3.0 by a completely rewritten implementation
73 */
74@Deprecated
75public interface EventHandlerWithJacobians  {
76
77    /** Stop indicator.
78     * <p>This value should be used as the return value of the {@link
79     * #eventOccurred eventOccurred} method when the integration should be
80     * stopped after the event ending the current step.</p>
81     */
82    int STOP = 0;
83
84    /** Reset state indicator.
85     * <p>This value should be used as the return value of the {@link
86     * #eventOccurred eventOccurred} method when the integration should
87     * go on after the event ending the current step, with a new state
88     * vector (which will be retrieved thanks to the {@link #resetState
89     * resetState} method).</p>
90     */
91    int RESET_STATE = 1;
92
93    /** Reset derivatives indicator.
94     * <p>This value should be used as the return value of the {@link
95     * #eventOccurred eventOccurred} method when the integration should
96     * go on after the event ending the current step, with a new derivatives
97     * vector (which will be retrieved thanks to the {@link
98     * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives}
99     * 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}
227