1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/* 2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more 3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements. See the NOTICE file distributed with 4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership. 5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0 6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with 7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License. You may obtain a copy of the License at 8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * http://www.apache.org/licenses/LICENSE-2.0 10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software 12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS, 13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and 15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License. 16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.ode.events; 19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/** This interface represents a handler for discrete events triggered 21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * during ODE integration. 22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Some events can be triggered at discrete times as an ODE problem 24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * is solved. This occurs for example when the integration process 25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * should be stopped as some state is reached (G-stop facility) when the 26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * precise date is unknown a priori, or when the derivatives have 27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * discontinuities, or simply when the user wants to monitor some 28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * states boundaries crossings. 29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p> 30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>These events are defined as occurring when a <code>g</code> 32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * switching function sign changes.</p> 33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>Since events are only problem-dependent and are triggered by the 35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * independent <i>time</i> variable and the state vector, they can 36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * occur at virtually any time, unknown in advance. The integrators will 37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * take care to avoid sign changes inside the steps, they will reduce 38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the step size when such an event is detected in order to put this 39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * event exactly at the end of the current step. This guarantees that 40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * step interpolation (which always has a one step scope) is relevant 41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * even in presence of discontinuities. This is independent from the 42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * stepsize control provided by integrators that monitor the local 43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * error (this event handling feature is available for all integrators, 44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * including fixed step ones).</p> 45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1067500 $ $Date: 2011-02-05 21:11:30 +0100 (sam. 05 févr. 2011) $ 47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2 48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic interface EventHandler { 51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Stop indicator. 53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This value should be used as the return value of the {@link 54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #eventOccurred eventOccurred} method when the integration should be 55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * stopped after the event ending the current step.</p> 56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int STOP = 0; 58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Reset state indicator. 60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This value should be used as the return value of the {@link 61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #eventOccurred eventOccurred} method when the integration should 62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * go on after the event ending the current step, with a new state 63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * vector (which will be retrieved thanks to the {@link #resetState 64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * resetState} method).</p> 65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int RESET_STATE = 1; 67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Reset derivatives indicator. 69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This value should be used as the return value of the {@link 70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #eventOccurred eventOccurred} method when the integration should 71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * go on after the event ending the current step, with a new derivatives 72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * vector (which will be retrieved thanks to the {@link 73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives} 74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * method).</p> 75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int RESET_DERIVATIVES = 2; 77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Continue indicator. 79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This value should be used as the return value of the {@link 80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #eventOccurred eventOccurred} method when the integration should go 81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * on after the event ending the current step.</p> 82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int CONTINUE = 3; 84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Compute the value of the switching function. 86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The discrete events are generated when the sign of this 88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * switching function changes. The integrator will take care to change 89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the stepsize in such a way these events occur exactly at step boundaries. 90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The switching function must be continuous in its roots neighborhood 91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (but not necessarily smooth), as the integrator will need to find its 92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * roots to locate precisely the events.</p> 93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param t current value of the independent <i>time</i> variable 95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param y array containing the current value of the state vector 96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return value of the g switching function 97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception EventException if the switching function cannot be evaluated 98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond double g(double t, double[] y) throws EventException; 100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Handle an event and choose what to do next. 102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This method is called when the integrator has accepted a step 104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * ending exactly on a sign change of the function, just <em>before</em> 105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the step handler itself is called (see below for scheduling). It 106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * allows the user to update his internal data to acknowledge the fact 107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the event has been handled (for example setting a flag in the {@link 108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.FirstOrderDifferentialEquations 109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * differential equations} to switch the derivatives computation in 110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * case of discontinuity), or to direct the integrator to either stop 111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * or continue integration, possibly with a reset state or derivatives.</p> 112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul> 114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>if {@link #STOP} is returned, the step handler will be called 115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * with the <code>isLast</code> flag of the {@link 116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepHandler#handleStep handleStep} 117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * method set to true and the integration will be stopped,</li> 118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>if {@link #RESET_STATE} is returned, the {@link #resetState 119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * resetState} method will be called once the step handler has 120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * finished its task, and the integrator will also recompute the 121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * derivatives,</li> 122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>if {@link #RESET_DERIVATIVES} is returned, the integrator 123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * will recompute the derivatives, 124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>if {@link #CONTINUE} is returned, no specific action will 125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * be taken (apart from having called this method) and integration 126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * will continue.</li> 127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> 128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The scheduling between this method and the {@link 130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepHandler StepHandler} method {@link 131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepHandler#handleStep( 132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepInterpolator, boolean) 133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * handleStep(interpolator, isLast)} is to call this method first and 134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>handleStep</code> afterwards. This scheduling allows the integrator to 135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * pass <code>true</code> as the <code>isLast</code> parameter to the step 136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * handler to make it aware the step will be the last one if this method 137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * returns {@link #STOP}. As the interpolator may be used to navigate back 138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * throughout the last step (as {@link 139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepNormalizer StepNormalizer} 140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * does for example), user code called by this method and user 141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * code called by step handlers may experience apparently out of order values 142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * of the independent time variable. As an example, if the same user object 143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * implements both this {@link EventHandler EventHandler} interface and the 144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link org.apache.commons.math.ode.sampling.FixedStepHandler FixedStepHandler} 145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * interface, a <em>forward</em> integration may call its 146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>eventOccurred</code> method with t = 10 first and call its 147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>handleStep</code> method with t = 9 afterwards. Such out of order 148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * calls are limited to the size of the integration step for {@link 149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.StepHandler variable step handlers} and 150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * to the size of the fixed step for {@link 151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * org.apache.commons.math.ode.sampling.FixedStepHandler fixed step handlers}.</p> 152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param t current value of the independent <i>time</i> variable 154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param y array containing the current value of the state vector 155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param increasing if true, the value of the switching function increases 156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * when times increases around event (note that increase is measured with respect 157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * to physical time, not with respect to integration which may go backward in time) 158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @return indication of what the integrator should do next, this 159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * value must be one of {@link #STOP}, {@link #RESET_STATE}, 160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link #RESET_DERIVATIVES} or {@link #CONTINUE} 161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception EventException if the event occurrence triggers an error 162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond int eventOccurred(double t, double[] y, boolean increasing) throws EventException; 164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond /** Reset the state prior to continue the integration. 166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This method is called after the step handler has returned and 168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * before the next step is started, but only when {@link 169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #eventOccurred} has itself returned the {@link #RESET_STATE} 170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * indicator. It allows the user to reset the state vector for the 171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * next step, without perturbing the step handler of the finishing 172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * step. If the {@link #eventOccurred} never returns the {@link 173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #RESET_STATE} indicator, this function will never be called, and it is 174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * safe to leave its body empty.</p> 175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * 176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param t current value of the independent <i>time</i> variable 177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @param y array containing the current value of the state vector 178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the new state should be put in the same array 179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @exception EventException if the state cannot be reseted 180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */ 181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond void resetState(double t, double[] y) throws EventException; 182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond 183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond} 184