EventHandler.java

  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. package org.apache.commons.math4.legacy.ode.events;


  18. /** This interface represents a handler for discrete events triggered
  19.  * during ODE integration.
  20.  *
  21.  * <p>Some events can be triggered at discrete times as an ODE problem
  22.  * is solved. This occurs for example when the integration process
  23.  * should be stopped as some state is reached (G-stop facility) when the
  24.  * precise date is unknown a priori, or when the derivatives have
  25.  * discontinuities, or simply when the user wants to monitor some
  26.  * states boundaries crossings.
  27.  * </p>
  28.  *
  29.  * <p>These events are defined as occurring when a <code>g</code>
  30.  * switching function sign changes.</p>
  31.  *
  32.  * <p>Since events are only problem-dependent and are triggered by the
  33.  * independent <i>time</i> variable and the state vector, they can
  34.  * occur at virtually any time, unknown in advance. The integrators will
  35.  * take care to avoid sign changes inside the steps, they will reduce
  36.  * the step size when such an event is detected in order to put this
  37.  * event exactly at the end of the current step. This guarantees that
  38.  * step interpolation (which always has a one step scope) is relevant
  39.  * even in presence of discontinuities. This is independent from the
  40.  * stepsize control provided by integrators that monitor the local
  41.  * error (this event handling feature is available for all integrators,
  42.  * including fixed step ones).</p>
  43.  *
  44.  * @since 1.2
  45.  */

  46. public interface EventHandler  {

  47.     /** Enumerate for actions to be performed when an event occurs. */
  48.     enum Action {

  49.         /** Stop indicator.
  50.          * <p>This value should be used as the return value of the {@link
  51.          * #eventOccurred eventOccurred} method when the integration should be
  52.          * stopped after the event ending the current step.</p>
  53.          */
  54.         STOP,

  55.         /** Reset state indicator.
  56.          * <p>This value should be used as the return value of the {@link
  57.          * #eventOccurred eventOccurred} method when the integration should
  58.          * go on after the event ending the current step, with a new state
  59.          * vector (which will be retrieved thanks to the {@link #resetState
  60.          * resetState} method).</p>
  61.          */
  62.         RESET_STATE,

  63.         /** Reset derivatives indicator.
  64.          * <p>This value should be used as the return value of the {@link
  65.          * #eventOccurred eventOccurred} method when the integration should
  66.          * go on after the event ending the current step, with a new derivatives
  67.          * vector (which will be retrieved thanks to the {@link
  68.          * org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations#computeDerivatives}
  69.          * method).</p>
  70.          */
  71.         RESET_DERIVATIVES,

  72.         /** Continue indicator.
  73.          * <p>This value should be used as the return value of the {@link
  74.          * #eventOccurred eventOccurred} method when the integration should go
  75.          * on after the event ending the current step.</p>
  76.          */
  77.         CONTINUE;
  78.     }

  79.     /** Initialize event handler at the start of an ODE integration.
  80.      * <p>
  81.      * This method is called once at the start of the integration. It
  82.      * may be used by the event handler to initialize some internal data
  83.      * if needed.
  84.      * </p>
  85.      * @param t0 start value of the independent <i>time</i> variable
  86.      * @param y0 array containing the start value of the state vector
  87.      * @param t target time for the integration
  88.      */
  89.     void init(double t0, double[] y0, double t);

  90.   /** Compute the value of the switching function.

  91.    * <p>The discrete events are generated when the sign of this
  92.    * switching function changes. The integrator will take care to change
  93.    * the stepsize in such a way these events occur exactly at step boundaries.
  94.    * The switching function must be continuous in its roots neighborhood
  95.    * (but not necessarily smooth), as the integrator will need to find its
  96.    * roots to locate precisely the events.</p>
  97.    * <p>Also note that the integrator expect that once an event has occurred,
  98.    * the sign of the switching function at the start of the next step (i.e.
  99.    * just after the event) is the opposite of the sign just before the event.
  100.    * This consistency between the steps <strong>must</strong> be preserved,
  101.    * otherwise {@link org.apache.commons.math4.legacy.exception.NoBracketingException
  102.    * exceptions} related to root not being bracketed will occur.</p>
  103.    * <p>This need for consistency is sometimes tricky to achieve. A typical
  104.    * example is using an event to model a ball bouncing on the floor. The first
  105.    * idea to represent this would be to have {@code g(t) = h(t)} where h is the
  106.    * height above the floor at time {@code t}. When {@code g(t)} reaches 0, the
  107.    * ball is on the floor, so it should bounce and the typical way to do this is
  108.    * to reverse its vertical velocity. However, this would mean that before the
  109.    * event {@code g(t)} was decreasing from positive values to 0, and after the
  110.    * event {@code g(t)} would be increasing from 0 to positive values again.
  111.    * Consistency is broken here! The solution here is to have {@code g(t) = sign
  112.    * * h(t)}, where sign is a variable with initial value set to {@code +1}. Each
  113.    * time {@link #eventOccurred(double, double[], boolean) eventOccurred} is called,
  114.    * {@code sign} is reset to {@code -sign}. This allows the {@code g(t)}
  115.    * function to remain continuous (and even smooth) even across events, despite
  116.    * {@code h(t)} is not. Basically, the event is used to <em>fold</em> {@code h(t)}
  117.    * at bounce points, and {@code sign} is used to <em>unfold</em> it back, so the
  118.    * solvers sees a {@code g(t)} function which behaves smoothly even across events.</p>

  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.    * @return value of the g switching function
  122.    */
  123.   double g(double t, double[] y);

  124.   /** Handle an event and choose what to do next.

  125.    * <p>This method is called when the integrator has accepted a step
  126.    * ending exactly on a sign change of the function, just <em>before</em>
  127.    * the step handler itself is called (see below for scheduling). It
  128.    * allows the user to update his internal data to acknowledge the fact
  129.    * the event has been handled (for example setting a flag in the {@link
  130.    * org.apache.commons.math4.legacy.ode.FirstOrderDifferentialEquations
  131.    * differential equations} to switch the derivatives computation in
  132.    * case of discontinuity), or to direct the integrator to either stop
  133.    * or continue integration, possibly with a reset state or derivatives.</p>

  134.    * <ul>
  135.    *   <li>if {@link Action#STOP} is returned, the step handler will be called
  136.    *   with the <code>isLast</code> flag of the {@link
  137.    *   org.apache.commons.math4.legacy.ode.sampling.StepHandler#handleStep handleStep}
  138.    *   method set to true and the integration will be stopped,</li>
  139.    *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
  140.    *   resetState} method will be called once the step handler has
  141.    *   finished its task, and the integrator will also recompute the
  142.    *   derivatives,</li>
  143.    *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
  144.    *   will recompute the derivatives,
  145.    *   <li>if {@link Action#CONTINUE} is returned, no specific action will
  146.    *   be taken (apart from having called this method) and integration
  147.    *   will continue.</li>
  148.    * </ul>

  149.    * <p>The scheduling between this method and the {@link
  150.    * org.apache.commons.math4.legacy.ode.sampling.StepHandler StepHandler} method {@link
  151.    * org.apache.commons.math4.legacy.ode.sampling.StepHandler#handleStep(
  152.    * org.apache.commons.math4.legacy.ode.sampling.StepInterpolator, boolean)
  153.    * handleStep(interpolator, isLast)} is to call this method first and
  154.    * <code>handleStep</code> afterwards. This scheduling allows the integrator to
  155.    * pass <code>true</code> as the <code>isLast</code> parameter to the step
  156.    * handler to make it aware the step will be the last one if this method
  157.    * returns {@link Action#STOP}. As the interpolator may be used to navigate back
  158.    * throughout the last step (as {@link
  159.    * org.apache.commons.math4.legacy.ode.sampling.StepNormalizer StepNormalizer}
  160.    * does for example), user code called by this method and user
  161.    * code called by step handlers may experience apparently out of order values
  162.    * of the independent time variable. As an example, if the same user object
  163.    * implements both this {@link EventHandler EventHandler} interface and the
  164.    * {@link org.apache.commons.math4.legacy.ode.sampling.FixedStepHandler FixedStepHandler}
  165.    * interface, a <em>forward</em> integration may call its
  166.    * <code>eventOccurred</code> method with t = 10 first and call its
  167.    * <code>handleStep</code> method with t = 9 afterwards. Such out of order
  168.    * calls are limited to the size of the integration step for {@link
  169.    * org.apache.commons.math4.legacy.ode.sampling.StepHandler variable step handlers} and
  170.    * to the size of the fixed step for {@link
  171.    * org.apache.commons.math4.legacy.ode.sampling.FixedStepHandler fixed step handlers}.</p>

  172.    * @param t current value of the independent <i>time</i> variable
  173.    * @param y array containing the current value of the state vector
  174.    * @param increasing if true, the value of the switching function increases
  175.    * when times increases around event (note that increase is measured with respect
  176.    * to physical time, not with respect to integration which may go backward in time)
  177.    * @return indication of what the integrator should do next, this
  178.    * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
  179.    * {@link Action#RESET_DERIVATIVES} or {@link Action#CONTINUE}
  180.    */
  181.   Action eventOccurred(double t, double[] y, boolean increasing);

  182.   /** Reset the state prior to continue the integration.

  183.    * <p>This method is called after the step handler has returned and
  184.    * before the next step is started, but only when {@link
  185.    * #eventOccurred} has itself returned the {@link Action#RESET_STATE}
  186.    * indicator. It allows the user to reset the state vector for the
  187.    * next step, without perturbing the step handler of the finishing
  188.    * step. If the {@link #eventOccurred} never returns the {@link
  189.    * Action#RESET_STATE} indicator, this function will never be called, and it is
  190.    * safe to leave its body empty.</p>

  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. }