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.jacobians; 019 020 import org.apache.commons.math.ode.events.EventException; 021 022 /** This interface represents a handler for discrete events triggered 023 * during ODE integration. 024 * 025 * <p>Some events can be triggered at discrete times as an ODE problem 026 * is solved. This occurs for example when the integration process 027 * should be stopped as some state is reached (G-stop facility) when the 028 * precise date is unknown a priori, or when the derivatives have 029 * discontinuities, or simply when the user wants to monitor some 030 * states boundaries crossings. 031 * </p> 032 * 033 * <p>These events are defined as occurring when a <code>g</code> 034 * switching function sign changes.</p> 035 * 036 * <p>Since events are only problem-dependent and are triggered by the 037 * independent <i>time</i> variable and the state vector, they can 038 * occur at virtually any time, unknown in advance. The integrators will 039 * take care to avoid sign changes inside the steps, they will reduce 040 * the step size when such an event is detected in order to put this 041 * event exactly at the end of the current step. This guarantees that 042 * step interpolation (which always has a one step scope) is relevant 043 * even in presence of discontinuities. This is independent from the 044 * stepsize control provided by integrators that monitor the local 045 * error (this event handling feature is available for all integrators, 046 * including fixed step ones).</p> 047 * 048 * <p>Note that is is possible to register a {@link 049 * org.apache.commons.math.ode.events.EventHandler classical event handler} 050 * in the low level integrator used to build a {@link FirstOrderIntegratorWithJacobians} 051 * rather than implementing this class. The event handlers registered at low level 052 * will see the big compound state whether the event handlers defined by this interface 053 * see the original state, and its jacobians in separate arrays.</p> 054 * 055 * <p>The compound state is guaranteed to contain the original state in the first 056 * elements, followed by the jacobian with respect to initial state (in row order), 057 * followed by the jacobian with respect to parameters (in row order). If for example 058 * the original state dimension is 6 and there are 3 parameters, the compound state will 059 * be a 60 elements array. The first 6 elements will be the original state, the next 36 060 * elements will be the jacobian with respect to initial state, and the remaining 18 elements 061 * will be the jacobian with respect to parameters.</p> 062 * 063 * <p>Dealing with low level event handlers is cumbersome if one really needs the jacobians 064 * in these methods, but it also prevents many data being copied back and forth between 065 * state and jacobians on one side and compound state on the other side. So for performance 066 * reasons, it is recommended to use this interface <em>only</em> if jacobians are really 067 * needed and to use lower level handlers if only state is needed.</p> 068 * 069 * @version $Revision: 920131 $ $Date: 2010-03-07 17:19:18 -0500 (Sun, 07 Mar 2010) $ 070 * @since 2.1 071 */ 072 073 public interface EventHandlerWithJacobians { 074 075 /** Stop indicator. 076 * <p>This value should be used as the return value of the {@link 077 * #eventOccurred eventOccurred} method when the integration should be 078 * stopped after the event ending the current step.</p> 079 */ 080 int STOP = 0; 081 082 /** Reset state indicator. 083 * <p>This value should be used as the return value of the {@link 084 * #eventOccurred eventOccurred} method when the integration should 085 * go on after the event ending the current step, with a new state 086 * vector (which will be retrieved thanks to the {@link #resetState 087 * resetState} method).</p> 088 */ 089 int RESET_STATE = 1; 090 091 /** Reset derivatives indicator. 092 * <p>This value should be used as the return value of the {@link 093 * #eventOccurred eventOccurred} method when the integration should 094 * go on after the event ending the current step, with a new derivatives 095 * vector (which will be retrieved thanks to the {@link 096 * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives} 097 * method).</p> 098 */ 099 int RESET_DERIVATIVES = 2; 100 101 /** Continue indicator. 102 * <p>This value should be used as the return value of the {@link 103 * #eventOccurred eventOccurred} method when the integration should go 104 * on after the event ending the current step.</p> 105 */ 106 int CONTINUE = 3; 107 108 /** Compute the value of the switching function. 109 110 * <p>The discrete events are generated when the sign of this 111 * switching function changes. The integrator will take care to change 112 * the stepsize in such a way these events occur exactly at step boundaries. 113 * The switching function must be continuous in its roots neighborhood 114 * (but not necessarily smooth), as the integrator will need to find its 115 * roots to locate precisely the events.</p> 116 117 * @param t current value of the independent <i>time</i> variable 118 * @param y array containing the current value of the state vector 119 * @param dydy0 array containing the current value of the jacobian of 120 * the state vector with respect to initial state 121 * @param dydp array containing the current value of the jacobian of 122 * the state vector with respect to parameters 123 * @return value of the g switching function 124 * @exception EventException if the switching function cannot be evaluated 125 */ 126 double g(double t, double[] y, double[][] dydy0, double[][] dydp) 127 throws EventException; 128 129 /** Handle an event and choose what to do next. 130 131 * <p>This method is called when the integrator has accepted a step 132 * ending exactly on a sign change of the function, just <em>before</em> 133 * the step handler itself is called (see below for scheduling). It 134 * allows the user to update his internal data to acknowledge the fact 135 * the event has been handled (for example setting a flag in the {@link 136 * org.apache.commons.math.ode.jacobians.ODEWithJacobians 137 * differential equations} to switch the derivatives computation in 138 * case of discontinuity), or to direct the integrator to either stop 139 * or continue integration, possibly with a reset state or derivatives.</p> 140 141 * <ul> 142 * <li>if {@link #STOP} is returned, the step handler will be called 143 * with the <code>isLast</code> flag of the {@link 144 * org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians#handleStep( 145 * StepInterpolatorWithJacobians, boolean) handleStep} method set to true and 146 * the integration will be stopped,</li> 147 * <li>if {@link #RESET_STATE} is returned, the {@link #resetState 148 * resetState} method will be called once the step handler has 149 * finished its task, and the integrator will also recompute the 150 * derivatives,</li> 151 * <li>if {@link #RESET_DERIVATIVES} is returned, the integrator 152 * will recompute the derivatives, 153 * <li>if {@link #CONTINUE} is returned, no specific action will 154 * be taken (apart from having called this method) and integration 155 * will continue.</li> 156 * </ul> 157 158 * <p>The scheduling between this method and the {@link 159 * org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians 160 * StepHandlerWithJacobians} method {@link 161 * org.apache.commons.math.ode.jacobians.StepHandlerWithJacobians#handleStep( 162 * StepInterpolatorWithJacobians, boolean) handleStep(interpolator, isLast)} 163 * is to call this method first and <code>handleStep</code> afterwards. This 164 * scheduling allows the integrator to pass <code>true</code> as the 165 * <code>isLast</code> parameter to the step handler to make it aware the step 166 * will be the last one if this method returns {@link #STOP}. As the 167 * interpolator may be used to navigate back throughout the last step (as {@link 168 * org.apache.commons.math.ode.sampling.StepNormalizer StepNormalizer} 169 * does for example), user code called by this method and user 170 * code called by step handlers may experience apparently out of order values 171 * of the independent time variable. As an example, if the same user object 172 * implements both this {@link EventHandlerWithJacobians EventHandler} interface and the 173 * {@link org.apache.commons.math.ode.sampling.FixedStepHandler FixedStepHandler} 174 * interface, a <em>forward</em> integration may call its 175 * <code>eventOccurred</code> method with t = 10 first and call its 176 * <code>handleStep</code> method with t = 9 afterwards. Such out of order 177 * calls are limited to the size of the integration step for {@link 178 * org.apache.commons.math.ode.sampling.StepHandler variable step handlers} and 179 * to the size of the fixed step for {@link 180 * org.apache.commons.math.ode.sampling.FixedStepHandler fixed step handlers}.</p> 181 182 * @param t current value of the independent <i>time</i> variable 183 * @param y array containing the current value of the state vector 184 * @param dydy0 array containing the current value of the jacobian of 185 * the state vector with respect to initial state 186 * @param dydp array containing the current value of the jacobian of 187 * the state vector with respect to parameters 188 * @param increasing if true, the value of the switching function increases 189 * when times increases around event (note that increase is measured with respect 190 * to physical time, not with respect to integration which may go backward in time) 191 * @return indication of what the integrator should do next, this 192 * value must be one of {@link #STOP}, {@link #RESET_STATE}, 193 * {@link #RESET_DERIVATIVES} or {@link #CONTINUE} 194 * @exception EventException if the event occurrence triggers an error 195 */ 196 int eventOccurred(double t, double[] y, double[][] dydy0, double[][] dydp, 197 boolean increasing) throws EventException; 198 199 /** Reset the state prior to continue the integration. 200 201 * <p>This method is called after the step handler has returned and 202 * before the next step is started, but only when {@link 203 * #eventOccurred} has itself returned the {@link #RESET_STATE} 204 * indicator. It allows the user to reset the state vector for the 205 * next step, without perturbing the step handler of the finishing 206 * step. If the {@link #eventOccurred} never returns the {@link 207 * #RESET_STATE} indicator, this function will never be called, and it is 208 * safe to leave its body empty.</p> 209 210 * @param t current value of the independent <i>time</i> variable 211 * @param y array containing the current value of the state vector 212 * the new state should be put in the same array 213 * @param dydy0 array containing the current value of the jacobian of 214 * the state vector with respect to initial state, the new jacobian 215 * should be put in the same array 216 * @param dydp array containing the current value of the jacobian of 217 * the state vector with respect to parameters, the new jacobian 218 * should be put in the same array 219 * @exception EventException if the state cannot be reseted 220 */ 221 void resetState(double t, double[] y, double[][] dydy0, double[][] dydp) 222 throws EventException; 223 224 }