    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.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 $Revision: 1067500 $ $Date: 2011-02-05 21:11:30 +0100 (sam. 05 f??vr. 2011) $
    047     * @since 1.2
    048     */
    049
    050    public interface EventHandler  {
    051
    052      /** Stop indicator.
    053       * <p>This value should be used as the return value of the {@link
    054       * #eventOccurred eventOccurred} method when the integration should be
    055       * stopped after the event ending the current step.</p>
    056       */
    057      int STOP = 0;
    058
    059      /** Reset state indicator.
    060       * <p>This value should be used as the return value of the {@link
    061       * #eventOccurred eventOccurred} method when the integration should
    062       * go on after the event ending the current step, with a new state
    063       * vector (which will be retrieved thanks to the {@link #resetState
    064       * resetState} method).</p>
    065       */
    066      int RESET_STATE = 1;
    067
    068      /** Reset derivatives indicator.
    069       * <p>This value should be used as the return value of the {@link
    070       * #eventOccurred eventOccurred} method when the integration should
    071       * go on after the event ending the current step, with a new derivatives
    072       * vector (which will be retrieved thanks to the {@link
    073       * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives}
    074       * method).</p>
    075       */
    076      int RESET_DERIVATIVES = 2;
    077
    078      /** Continue indicator.
    079       * <p>This value should be used as the return value of the {@link
    080       * #eventOccurred eventOccurred} method when the integration should go
    081       * on after the event ending the current step.</p>
    082       */
    083      int CONTINUE = 3;
    084
    085      /** Compute the value of the switching function.
    086
    087       * <p>The discrete events are generated when the sign of this
    088       * switching function changes. The integrator will take care to change
    089       * the stepsize in such a way these events occur exactly at step boundaries.
    090       * The switching function must be continuous in its roots neighborhood
    091       * (but not necessarily smooth), as the integrator will need to find its
    092       * roots to locate precisely the events.</p>
    093       *
    094       * @param t current value of the independent <i>time</i> variable
    095       * @param y array containing the current value of the state vector
    096       * @return value of the g switching function
    097       * @exception EventException if the switching function cannot be evaluated
    098       */
    099      double g(double t, double[] y) throws EventException;
    100
    101      /** Handle an event and choose what to do next.
    102
    103       * <p>This method is called when the integrator has accepted a step
    104       * ending exactly on a sign change of the function, just <em>before</em>
    105       * the step handler itself is called (see below for scheduling). It
    106       * allows the user to update his internal data to acknowledge the fact
    107       * the event has been handled (for example setting a flag in the {@link
    108       * org.apache.commons.math.ode.FirstOrderDifferentialEquations
    109       * differential equations} to switch the derivatives computation in
    110       * case of discontinuity), or to direct the integrator to either stop
    111       * or continue integration, possibly with a reset state or derivatives.</p>
    112       *
    113       * <ul>
    114       *   <li>if {@link #STOP} is returned, the step handler will be called
    115       *   with the <code>isLast</code> flag of the {@link
    116       *   org.apache.commons.math.ode.sampling.StepHandler#handleStep handleStep}
    117       *   method set to true and the integration will be stopped,</li>
    118       *   <li>if {@link #RESET_STATE} is returned, the {@link #resetState
    119       *   resetState} method will be called once the step handler has
    120       *   finished its task, and the integrator will also recompute the
    121       *   derivatives,</li>
    122       *   <li>if {@link #RESET_DERIVATIVES} is returned, the integrator
    123       *   will recompute the derivatives,
    124       *   <li>if {@link #CONTINUE} is returned, no specific action will
    125       *   be taken (apart from having called this method) and integration
    126       *   will continue.</li>
    127       * </ul>
    128       *
    129       * <p>The scheduling between this method and the {@link
    130       * org.apache.commons.math.ode.sampling.StepHandler StepHandler} method {@link
    131       * org.apache.commons.math.ode.sampling.StepHandler#handleStep(
    132       * org.apache.commons.math.ode.sampling.StepInterpolator, boolean)
    133       * handleStep(interpolator, isLast)} is to call this method first and
    134       * <code>handleStep</code> afterwards. This scheduling allows the integrator to
    135       * pass <code>true</code> as the <code>isLast</code> parameter to the step
    136       * handler to make it aware the step will be the last one if this method
    137       * returns {@link #STOP}. As the interpolator may be used to navigate back
    138       * throughout the last step (as {@link
    139       * org.apache.commons.math.ode.sampling.StepNormalizer StepNormalizer}
    140       * does for example), user code called by this method and user
    141       * code called by step handlers may experience apparently out of order values
    142       * of the independent time variable. As an example, if the same user object
    143       * implements both this {@link EventHandler EventHandler} interface and the
    144       * {@link org.apache.commons.math.ode.sampling.FixedStepHandler FixedStepHandler}
    145       * interface, a <em>forward</em> integration may call its
    146       * <code>eventOccurred</code> method with t = 10 first and call its
    147       * <code>handleStep</code> method with t = 9 afterwards. Such out of order
    148       * calls are limited to the size of the integration step for {@link
    149       * org.apache.commons.math.ode.sampling.StepHandler variable step handlers} and
    150       * to the size of the fixed step for {@link
    151       * org.apache.commons.math.ode.sampling.FixedStepHandler fixed step handlers}.</p>
    152       *
    153       * @param t current value of the independent <i>time</i> variable
    154       * @param y array containing the current value of the state vector
    155       * @param increasing if true, the value of the switching function increases
    156       * when times increases around event (note that increase is measured with respect
    157       * to physical time, not with respect to integration which may go backward in time)
    158       * @return indication of what the integrator should do next, this
    159       * value must be one of {@link #STOP}, {@link #RESET_STATE},
    160       * {@link #RESET_DERIVATIVES} or {@link #CONTINUE}
    161       * @exception EventException if the event occurrence triggers an error
    162       */
    163      int eventOccurred(double t, double[] y, boolean increasing) throws EventException;
    164
    165      /** Reset the state prior to continue the integration.
    166
    167       * <p>This method is called after the step handler has returned and
    168       * before the next step is started, but only when {@link
    169       * #eventOccurred} has itself returned the {@link #RESET_STATE}
    170       * indicator. It allows the user to reset the state vector for the
    171       * next step, without perturbing the step handler of the finishing
    172       * step. If the {@link #eventOccurred} never returns the {@link
    173       * #RESET_STATE} indicator, this function will never be called, and it is
    174       * safe to leave its body empty.</p>
    175       *
    176       * @param t current value of the independent <i>time</i> variable
    177       * @param y array containing the current value of the state vector
    178       * the new state should be put in the same array
    179       * @exception EventException if the state cannot be reseted
    180       */
    181      void resetState(double t, double[] y) throws EventException;
    182
    183    }