FieldODEEventHandler.java

  1. /*
  2.  * Licensed to the Hipparchus project 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 Hipparchus project 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.  *      https://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.hipparchus.ode.events;

  18. import org.hipparchus.CalculusFieldElement;
  19. import org.hipparchus.ode.FieldODEState;
  20. import org.hipparchus.ode.FieldODEStateAndDerivative;

  21. /** This interface represents a handler for discrete events triggered
  22.  * during ODE integration.
  23.  *
  24.  * <p>Some events can be triggered at discrete times as an ODE problem
  25.  * is solved. This occurs for example when the integration process
  26.  * should be stopped as some state is reached (G-stop facility) when the
  27.  * precise date is unknown a priori, or when the derivatives have
  28.  * states boundaries crossings.
  29.  * </p>
  30.  *
  31.  * <p>These events are defined as occurring when a <code>g</code>
  32.  * switching function sign changes.</p>
  33.  *
  34.  * <p>Since events are only problem-dependent and are triggered by the
  35.  * independent <i>time</i> variable and the state vector, they can
  36.  * occur at virtually any time, unknown in advance. The integrators will
  37.  * take care to avoid sign changes inside the steps, they will reduce
  38.  * the step size when such an event is detected in order to put this
  39.  * event exactly at the end of the current step. This guarantees that
  40.  * step interpolation (which always has a one step scope) is relevant
  41.  * even in presence of discontinuities. This is independent from the
  42.  * stepsize control provided by integrators that monitor the local
  43.  * error (this event handling feature is available for all integrators,
  44.  * including fixed step ones).</p>
  45.  *
  46.  * <p>
  47.  * Note that prior to Hipparchus 3.0, some of the methods that are now in
  48.  * {@link FieldODEEventDetector} were in this interface (and the remaining
  49.  * ones were in the defunct {@code FieldEventHandlerConfiguration} interface).
  50.  * The interfaces have been reorganized to allow different objects to be
  51.  * used in event detection and event handling, hence allowing users to
  52.  * reuse predefined events detectors with custom handlers.
  53.  * </p>
  54.  * @see org.hipparchus.ode.events
  55.  * @param <T> the type of the field elements
  56.  */
  57. public interface FieldODEEventHandler<T extends CalculusFieldElement<T>>  {

  58.     /** Initialize event handler at the start of an ODE integration.
  59.      * <p>
  60.      * This method is called once at the start of the integration. It
  61.      * may be used by the event handler to initialize some internal data
  62.      * if needed.
  63.      * </p>
  64.      * <p>
  65.      * The default implementation does nothing
  66.      * </p>
  67.      * @param initialState initial time, state vector and derivative
  68.      * @param finalTime target time for the integration
  69.      * @param detector event detector related to the event handler
  70.      */
  71.     default void init(FieldODEStateAndDerivative<T> initialState, T finalTime, FieldODEEventDetector<T> detector) {
  72.         // nothing by default
  73.     }

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

  75.      * <p>This method is called when the integrator has accepted a step
  76.      * ending exactly on a sign change of the function, just <em>after</em>
  77.      * the step handler itself is called (see below for scheduling). It
  78.      * allows the user to update his internal data to acknowledge the fact
  79.      * the event has been handled (for example setting a flag in the {@link
  80.      * org.hipparchus.ode.FieldOrdinaryDifferentialEquation
  81.      * differential equations} to switch the derivatives computation in
  82.      * case of discontinuity), or to direct the integrator to either stop
  83.      * or continue integration, possibly with a reset state or derivatives.</p>
  84.      *
  85.      * <ul>
  86.      *   <li>if {@link Action#STOP} is returned, the integration will be stopped,</li>
  87.      *   <li>if {@link Action#RESET_STATE} is returned, the {@link #resetState
  88.      *   resetState} method will be called once the step handler has
  89.      *   finished its task, and the integrator will also recompute the
  90.      *   derivatives,</li>
  91.      *   <li>if {@link Action#RESET_DERIVATIVES} is returned, the integrator
  92.      *   will recompute the derivatives,
  93.      *   <li>if {@link Action#RESET_EVENTS} is returned, the integrator
  94.      *   will recheck all event handlers,
  95.      *   <li>if {@link Action#CONTINUE} is returned, no specific action will
  96.      *   be taken (apart from having called this method) and integration
  97.      *   will continue.</li>
  98.      * </ul>
  99.      *
  100.      * <p>The scheduling between this method and the {@link
  101.      * org.hipparchus.ode.sampling.FieldODEStepHandler FieldODEStepHandler} method {@link
  102.      * org.hipparchus.ode.sampling.FieldODEStepHandler#handleStep
  103.      * handleStep(interpolator, isLast)} is to call {@code handleStep} first and this method afterwards
  104.      * (this scheduling changed as of Hipparchus 2.0). This scheduling allows user code
  105.      * called by this method and user code called by step handlers to get values
  106.      * of the independent time variable consistent with integration direction.</p>
  107.      *
  108.      * @param state current value of the independent <i>time</i> variable, state vector
  109.      * and derivative
  110.      * @param detector detector that triggered the event
  111.      * @param increasing if true, the value of the switching function increases
  112.      * when times increases around event (note that increase is measured with respect
  113.      * to physical time, not with respect to integration which may go backward in time)
  114.      * @return indication of what the integrator should do next, this
  115.      * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE},
  116.      * {@link Action#RESET_DERIVATIVES}, {@link Action#RESET_EVENTS}, or
  117.      * {@link Action#CONTINUE}
  118.      */
  119.     Action eventOccurred(FieldODEStateAndDerivative<T> state, FieldODEEventDetector<T> detector, boolean increasing);

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

  121.      * <p>This method is called after the step handler has returned and
  122.      * before the next step is started, but only when {@link
  123.      * #eventOccurred(FieldODEStateAndDerivative, FieldODEEventDetector, boolean) eventOccurred}
  124.      * has itself returned the {@link Action#RESET_STATE} indicator. It allows the user to reset
  125.      * the state vector for the next step, without perturbing the step handler of the
  126.      * finishing step.</p>
  127.      * <p>The default implementation returns its argument.</p>
  128.      * @param detector detector that triggered the event
  129.      * @param state current value of the independent <i>time</i> variable, state vector
  130.      * and derivative
  131.      * @return reset state (note that it does not include the derivatives, they will
  132.      * be added automatically by the integrator afterwards)
  133.      */
  134.     default FieldODEState<T> resetState(FieldODEEventDetector<T> detector, FieldODEStateAndDerivative<T> state) {
  135.         return state;
  136.     }

  137. }