Changed return type for eventOccurred from int to an enumerate in ODE.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1175379 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-09-25 12:39:09 +00:00
parent 39d86e32e2
commit c60827b0b7
9 changed files with 61 additions and 55 deletions

View File

@ -49,38 +49,43 @@ package org.apache.commons.math.ode.events;
public interface EventHandler { public interface EventHandler {
/** Stop indicator. /** Enumerate for actions to be performed when an event occurs. */
* <p>This value should be used as the return value of the {@link public enum Action {
* #eventOccurred eventOccurred} method when the integration should be
* stopped after the event ending the current step.</p>
*/
int STOP = 0;
/** Reset state indicator. /** Stop indicator.
* <p>This value should be used as the return value of the {@link * <p>This value should be used as the return value of the {@link
* #eventOccurred eventOccurred} method when the integration should * #eventOccurred eventOccurred} method when the integration should be
* go on after the event ending the current step, with a new state * stopped after the event ending the current step.</p>
* vector (which will be retrieved thanks to the {@link #resetState */
* resetState} method).</p> STOP,
*/
int RESET_STATE = 1;
/** Reset derivatives indicator. /** Reset state indicator.
* <p>This value should be used as the return value of the {@link * <p>This value should be used as the return value of the {@link
* #eventOccurred eventOccurred} method when the integration should * #eventOccurred eventOccurred} method when the integration should
* go on after the event ending the current step, with a new derivatives * go on after the event ending the current step, with a new state
* vector (which will be retrieved thanks to the {@link * vector (which will be retrieved thanks to the {@link #resetState
* org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives} * resetState} method).</p>
* method).</p> */
*/ RESET_STATE,
int RESET_DERIVATIVES = 2;
/** Continue indicator. /** Reset derivatives indicator.
* <p>This value should be used as the return value of the {@link * <p>This value should be used as the return value of the {@link
* #eventOccurred eventOccurred} method when the integration should go * #eventOccurred eventOccurred} method when the integration should
* on after the event ending the current step.</p> * go on after the event ending the current step, with a new derivatives
*/ * vector (which will be retrieved thanks to the {@link
int CONTINUE = 3; * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives}
* method).</p>
*/
RESET_DERIVATIVES,
/** Continue indicator.
* <p>This value should be used as the return value of the {@link
* #eventOccurred eventOccurred} method when the integration should go
* on after the event ending the current step.</p>
*/
CONTINUE;
}
/** Compute the value of the switching function. /** Compute the value of the switching function.
@ -158,7 +163,7 @@ public interface EventHandler {
* value must be one of {@link #STOP}, {@link #RESET_STATE}, * value must be one of {@link #STOP}, {@link #RESET_STATE},
* {@link #RESET_DERIVATIVES} or {@link #CONTINUE} * {@link #RESET_DERIVATIVES} or {@link #CONTINUE}
*/ */
int eventOccurred(double t, double[] y, boolean increasing); Action eventOccurred(double t, double[] y, boolean increasing);
/** Reset the state prior to continue the integration. /** Reset the state prior to continue the integration.

View File

@ -24,6 +24,7 @@ import org.apache.commons.math.analysis.solvers.PegasusSolver;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolver; import org.apache.commons.math.analysis.solvers.UnivariateRealSolver;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils; import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils;
import org.apache.commons.math.exception.ConvergenceException; import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.ode.events.EventHandler;
import org.apache.commons.math.ode.sampling.StepInterpolator; import org.apache.commons.math.ode.sampling.StepInterpolator;
import org.apache.commons.math.util.FastMath; import org.apache.commons.math.util.FastMath;
@ -81,7 +82,7 @@ public class EventState {
private boolean increasing; private boolean increasing;
/** Next action indicator. */ /** Next action indicator. */
private int nextAction; private EventHandler.Action nextAction;
/** Root-finding algorithm to use to detect state events. */ /** Root-finding algorithm to use to detect state events. */
private final UnivariateRealSolver solver; private final UnivariateRealSolver solver;
@ -113,7 +114,7 @@ public class EventState {
pendingEventTime = Double.NaN; pendingEventTime = Double.NaN;
previousEventTime = Double.NaN; previousEventTime = Double.NaN;
increasing = true; increasing = true;
nextAction = EventHandler.CONTINUE; nextAction = EventHandler.Action.CONTINUE;
} }
@ -304,7 +305,7 @@ public class EventState {
nextAction = handler.eventOccurred(t, y, !(increasing ^ forward)); nextAction = handler.eventOccurred(t, y, !(increasing ^ forward));
} else { } else {
g0Positive = g0 >= 0; g0Positive = g0 >= 0;
nextAction = EventHandler.CONTINUE; nextAction = EventHandler.Action.CONTINUE;
} }
} }
@ -313,7 +314,7 @@ public class EventState {
* @return true if the integration should be stopped * @return true if the integration should be stopped
*/ */
public boolean stop() { public boolean stop() {
return nextAction == EventHandler.STOP; return nextAction == EventHandler.Action.STOP;
} }
/** Let the event handler reset the state if it wants. /** Let the event handler reset the state if it wants.
@ -329,14 +330,14 @@ public class EventState {
return false; return false;
} }
if (nextAction == EventHandler.RESET_STATE) { if (nextAction == EventHandler.Action.RESET_STATE) {
handler.resetState(t, y); handler.resetState(t, y);
} }
pendingEvent = false; pendingEvent = false;
pendingEventTime = Double.NaN; pendingEventTime = Double.NaN;
return (nextAction == EventHandler.RESET_STATE) || return (nextAction == EventHandler.Action.RESET_STATE) ||
(nextAction == EventHandler.RESET_DERIVATIVES); (nextAction == EventHandler.Action.RESET_DERIVATIVES);
} }

View File

@ -119,10 +119,10 @@ public TestProblem4 copy() {
return sign * y[0]; return sign * y[0];
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
// this sign change is needed because the state will be reset soon // this sign change is needed because the state will be reset soon
sign = -sign; sign = -sign;
return EventHandler.RESET_STATE; return Action.RESET_STATE;
} }
public void resetState(double t, double[] y) { public void resetState(double t, double[] y) {
@ -141,8 +141,8 @@ public TestProblem4 copy() {
return t - 12.0; return t - 12.0;
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
return EventHandler.STOP; return Action.STOP;
} }
public void resetState(double t, double[] y) { public void resetState(double t, double[] y) {

View File

@ -39,8 +39,8 @@ public class EventStateTest {
public double g(double t, double[] y) { public double g(double t, double[] y) {
return (t - r1) * (r2 - t); return (t - r1) * (r2 - t);
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
return CONTINUE; return Action.CONTINUE;
} }
}; };

View File

@ -142,8 +142,8 @@ public class OverlappingEventsTest implements FirstOrderDifferentialEquations {
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
return STOP; return Action.STOP;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */

View File

@ -78,9 +78,9 @@ public class ClassicalRungeKuttaIntegratorTest {
return t - tEvent; return t - tEvent;
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
Assert.assertEquals(tEvent, t, 5.0e-6); Assert.assertEquals(tEvent, t, 5.0e-6);
return CONTINUE; return Action.CONTINUE;
} }
}, Double.POSITIVE_INFINITY, 1.0e-20, 100); }, Double.POSITIVE_INFINITY, 1.0e-20, 100);
finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y);

View File

@ -80,9 +80,9 @@ public class DormandPrince853IntegratorTest {
return t - tEvent; return t - tEvent;
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
Assert.assertEquals(tEvent, t, 5.0e-6); Assert.assertEquals(tEvent, t, 5.0e-6);
return CONTINUE; return Action.CONTINUE;
} }
}, Double.POSITIVE_INFINITY, 1.0e-20, 100); }, Double.POSITIVE_INFINITY, 1.0e-20, 100);
finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y);

View File

@ -202,8 +202,8 @@ public class HighamHall54IntegratorTest {
integ.addStepHandler(handler); integ.addStepHandler(handler);
integ.addEventHandler(new EventHandler() { integ.addEventHandler(new EventHandler() {
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
return EventHandler.CONTINUE; return Action.CONTINUE;
} }
public double g(double t, double[] y) { public double g(double t, double[] y) {
double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2;
@ -246,8 +246,8 @@ public class HighamHall54IntegratorTest {
integ.addStepHandler(handler); integ.addStepHandler(handler);
integ.addEventHandler(new EventHandler() { integ.addEventHandler(new EventHandler() {
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
return EventHandler.CONTINUE; return Action.CONTINUE;
} }
public double g(double t, double[] y) { public double g(double t, double[] y) {
double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2;

View File

@ -43,9 +43,9 @@ public class StepProblem
this.rate = rate; this.rate = rate;
} }
public int eventOccurred(double t, double[] y, boolean increasing) { public Action eventOccurred(double t, double[] y, boolean increasing) {
setRate(rateAfter); setRate(rateAfter);
return RESET_DERIVATIVES; return Action.RESET_DERIVATIVES;
} }
public double g(double t, double[] y) { public double g(double t, double[] y) {