From c60827b0b79a5da5da681f6b6f63e745f13fc1e3 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sun, 25 Sep 2011 12:39:09 +0000 Subject: [PATCH] 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 --- .../commons/math/ode/events/EventHandler.java | 65 ++++++++++--------- .../commons/math/ode/events/EventState.java | 15 +++-- .../apache/commons/math/ode/TestProblem4.java | 8 +-- .../math/ode/events/EventStateTest.java | 4 +- .../ode/events/OverlappingEventsTest.java | 4 +- .../ClassicalRungeKuttaIntegratorTest.java | 4 +- .../DormandPrince853IntegratorTest.java | 4 +- .../nonstiff/HighamHall54IntegratorTest.java | 8 +-- .../math/ode/nonstiff/StepProblem.java | 4 +- 9 files changed, 61 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/apache/commons/math/ode/events/EventHandler.java b/src/main/java/org/apache/commons/math/ode/events/EventHandler.java index d3697d20f..c25edee02 100644 --- a/src/main/java/org/apache/commons/math/ode/events/EventHandler.java +++ b/src/main/java/org/apache/commons/math/ode/events/EventHandler.java @@ -49,38 +49,43 @@ package org.apache.commons.math.ode.events; public interface EventHandler { - /** Stop indicator. - *

This value should be used as the return value of the {@link - * #eventOccurred eventOccurred} method when the integration should be - * stopped after the event ending the current step.

- */ - int STOP = 0; + /** Enumerate for actions to be performed when an event occurs. */ + public enum Action { - /** Reset state indicator. - *

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, with a new state - * vector (which will be retrieved thanks to the {@link #resetState - * resetState} method).

- */ - int RESET_STATE = 1; + /** Stop indicator. + *

This value should be used as the return value of the {@link + * #eventOccurred eventOccurred} method when the integration should be + * stopped after the event ending the current step.

+ */ + STOP, - /** Reset derivatives indicator. - *

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, with a new derivatives - * vector (which will be retrieved thanks to the {@link - * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives} - * method).

- */ - int RESET_DERIVATIVES = 2; + /** Reset state indicator. + *

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, with a new state + * vector (which will be retrieved thanks to the {@link #resetState + * resetState} method).

+ */ + RESET_STATE, - /** Continue indicator. - *

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.

- */ - int CONTINUE = 3; + /** Reset derivatives indicator. + *

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, with a new derivatives + * vector (which will be retrieved thanks to the {@link + * org.apache.commons.math.ode.FirstOrderDifferentialEquations#computeDerivatives} + * method).

+ */ + RESET_DERIVATIVES, + + /** Continue indicator. + *

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.

+ */ + CONTINUE; + + } /** Compute the value of the switching function. @@ -158,7 +163,7 @@ public interface EventHandler { * value must be one of {@link #STOP}, {@link #RESET_STATE}, * {@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. diff --git a/src/main/java/org/apache/commons/math/ode/events/EventState.java b/src/main/java/org/apache/commons/math/ode/events/EventState.java index 39c10e240..8e9ff46a5 100644 --- a/src/main/java/org/apache/commons/math/ode/events/EventState.java +++ b/src/main/java/org/apache/commons/math/ode/events/EventState.java @@ -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.UnivariateRealSolverUtils; 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.util.FastMath; @@ -81,7 +82,7 @@ public class EventState { private boolean increasing; /** Next action indicator. */ - private int nextAction; + private EventHandler.Action nextAction; /** Root-finding algorithm to use to detect state events. */ private final UnivariateRealSolver solver; @@ -113,7 +114,7 @@ public class EventState { pendingEventTime = Double.NaN; previousEventTime = Double.NaN; increasing = true; - nextAction = EventHandler.CONTINUE; + nextAction = EventHandler.Action.CONTINUE; } @@ -304,7 +305,7 @@ public class EventState { nextAction = handler.eventOccurred(t, y, !(increasing ^ forward)); } else { 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 */ public boolean stop() { - return nextAction == EventHandler.STOP; + return nextAction == EventHandler.Action.STOP; } /** Let the event handler reset the state if it wants. @@ -329,14 +330,14 @@ public class EventState { return false; } - if (nextAction == EventHandler.RESET_STATE) { + if (nextAction == EventHandler.Action.RESET_STATE) { handler.resetState(t, y); } pendingEvent = false; pendingEventTime = Double.NaN; - return (nextAction == EventHandler.RESET_STATE) || - (nextAction == EventHandler.RESET_DERIVATIVES); + return (nextAction == EventHandler.Action.RESET_STATE) || + (nextAction == EventHandler.Action.RESET_DERIVATIVES); } diff --git a/src/test/java/org/apache/commons/math/ode/TestProblem4.java b/src/test/java/org/apache/commons/math/ode/TestProblem4.java index 9c6f17d88..44fee21ee 100644 --- a/src/test/java/org/apache/commons/math/ode/TestProblem4.java +++ b/src/test/java/org/apache/commons/math/ode/TestProblem4.java @@ -119,10 +119,10 @@ public TestProblem4 copy() { 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 sign = -sign; - return EventHandler.RESET_STATE; + return Action.RESET_STATE; } public void resetState(double t, double[] y) { @@ -141,8 +141,8 @@ public TestProblem4 copy() { return t - 12.0; } - public int eventOccurred(double t, double[] y, boolean increasing) { - return EventHandler.STOP; + public Action eventOccurred(double t, double[] y, boolean increasing) { + return Action.STOP; } public void resetState(double t, double[] y) { diff --git a/src/test/java/org/apache/commons/math/ode/events/EventStateTest.java b/src/test/java/org/apache/commons/math/ode/events/EventStateTest.java index 3e1880729..07178e52b 100644 --- a/src/test/java/org/apache/commons/math/ode/events/EventStateTest.java +++ b/src/test/java/org/apache/commons/math/ode/events/EventStateTest.java @@ -39,8 +39,8 @@ public class EventStateTest { public double g(double t, double[] y) { return (t - r1) * (r2 - t); } - public int eventOccurred(double t, double[] y, boolean increasing) { - return CONTINUE; + public Action eventOccurred(double t, double[] y, boolean increasing) { + return Action.CONTINUE; } }; diff --git a/src/test/java/org/apache/commons/math/ode/events/OverlappingEventsTest.java b/src/test/java/org/apache/commons/math/ode/events/OverlappingEventsTest.java index d4f3da6aa..30b166f95 100644 --- a/src/test/java/org/apache/commons/math/ode/events/OverlappingEventsTest.java +++ b/src/test/java/org/apache/commons/math/ode/events/OverlappingEventsTest.java @@ -142,8 +142,8 @@ public class OverlappingEventsTest implements FirstOrderDifferentialEquations { } /** {@inheritDoc} */ - public int eventOccurred(double t, double[] y, boolean increasing) { - return STOP; + public Action eventOccurred(double t, double[] y, boolean increasing) { + return Action.STOP; } /** {@inheritDoc} */ diff --git a/src/test/java/org/apache/commons/math/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java b/src/test/java/org/apache/commons/math/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java index 91b0ac6de..b92dc00c1 100644 --- a/src/test/java/org/apache/commons/math/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java +++ b/src/test/java/org/apache/commons/math/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.java @@ -78,9 +78,9 @@ public class ClassicalRungeKuttaIntegratorTest { 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); - return CONTINUE; + return Action.CONTINUE; } }, Double.POSITIVE_INFINITY, 1.0e-20, 100); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); diff --git a/src/test/java/org/apache/commons/math/ode/nonstiff/DormandPrince853IntegratorTest.java b/src/test/java/org/apache/commons/math/ode/nonstiff/DormandPrince853IntegratorTest.java index 91799e490..723d16b5b 100644 --- a/src/test/java/org/apache/commons/math/ode/nonstiff/DormandPrince853IntegratorTest.java +++ b/src/test/java/org/apache/commons/math/ode/nonstiff/DormandPrince853IntegratorTest.java @@ -80,9 +80,9 @@ public class DormandPrince853IntegratorTest { 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); - return CONTINUE; + return Action.CONTINUE; } }, Double.POSITIVE_INFINITY, 1.0e-20, 100); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); diff --git a/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java b/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java index 052738541..74dc08640 100644 --- a/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java +++ b/src/test/java/org/apache/commons/math/ode/nonstiff/HighamHall54IntegratorTest.java @@ -202,8 +202,8 @@ public class HighamHall54IntegratorTest { integ.addStepHandler(handler); integ.addEventHandler(new EventHandler() { - public int eventOccurred(double t, double[] y, boolean increasing) { - return EventHandler.CONTINUE; + public Action eventOccurred(double t, double[] y, boolean increasing) { + return Action.CONTINUE; } public double g(double t, double[] y) { double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; @@ -246,8 +246,8 @@ public class HighamHall54IntegratorTest { integ.addStepHandler(handler); integ.addEventHandler(new EventHandler() { - public int eventOccurred(double t, double[] y, boolean increasing) { - return EventHandler.CONTINUE; + public Action eventOccurred(double t, double[] y, boolean increasing) { + return Action.CONTINUE; } public double g(double t, double[] y) { double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; diff --git a/src/test/java/org/apache/commons/math/ode/nonstiff/StepProblem.java b/src/test/java/org/apache/commons/math/ode/nonstiff/StepProblem.java index dad205241..00f7cdd6c 100644 --- a/src/test/java/org/apache/commons/math/ode/nonstiff/StepProblem.java +++ b/src/test/java/org/apache/commons/math/ode/nonstiff/StepProblem.java @@ -43,9 +43,9 @@ public class StepProblem this.rate = rate; } - public int eventOccurred(double t, double[] y, boolean increasing) { + public Action eventOccurred(double t, double[] y, boolean increasing) { setRate(rateAfter); - return RESET_DERIVATIVES; + return Action.RESET_DERIVATIVES; } public double g(double t, double[] y) {