diff --git a/src/main/java/org/apache/commons/math4/ode/AbstractFieldIntegrator.java b/src/main/java/org/apache/commons/math4/ode/AbstractFieldIntegrator.java index 56923d282..878f43547 100644 --- a/src/main/java/org/apache/commons/math4/ode/AbstractFieldIntegrator.java +++ b/src/main/java/org/apache/commons/math4/ode/AbstractFieldIntegrator.java @@ -340,17 +340,17 @@ public abstract class AbstractFieldIntegrator> imp return eventState; } - boolean needReset = false; + FieldODEState newState = null; for (final FieldEventState state : eventsStates) { - needReset = needReset || state.reset(eventState); - } - if (needReset) { - // some event handler has triggered changes that - // invalidate the derivatives, we need to recompute them - final T[] y = equations.getMapper().mapState(eventState); - final T[] yDot = computeDerivatives(eventState.getTime(), y); - resetOccurred = true; - return equations.getMapper().mapStateAndDerivative(eventState.getTime(), y, yDot); + newState = state.reset(eventState); + if (newState != null) { + // some event handler has triggered changes that + // invalidate the derivatives, we need to recompute them + final T[] y = equations.getMapper().mapState(newState); + final T[] yDot = computeDerivatives(newState.getTime(), y); + resetOccurred = true; + return equations.getMapper().mapStateAndDerivative(newState.getTime(), y, yDot); + } } // prepare handling of the remaining part of the step diff --git a/src/main/java/org/apache/commons/math4/ode/events/FieldEventState.java b/src/main/java/org/apache/commons/math4/ode/events/FieldEventState.java index 6987295aa..ffd0d7b63 100644 --- a/src/main/java/org/apache/commons/math4/ode/events/FieldEventState.java +++ b/src/main/java/org/apache/commons/math4/ode/events/FieldEventState.java @@ -23,6 +23,7 @@ import org.apache.commons.math4.analysis.solvers.AllowedSolution; import org.apache.commons.math4.analysis.solvers.BracketedRealFieldUnivariateSolver; import org.apache.commons.math4.exception.MaxCountExceededException; import org.apache.commons.math4.exception.NoBracketingException; +import org.apache.commons.math4.ode.FieldODEState; import org.apache.commons.math4.ode.FieldODEStateAndDerivative; import org.apache.commons.math4.ode.sampling.FieldStepInterpolator; import org.apache.commons.math4.util.FastMath; @@ -325,22 +326,27 @@ public class FieldEventState> { /** Let the event handler reset the state if it wants. * @param state state at the beginning of the next step - * @return true if the integrator should reset the derivatives too + * @return reset state (may by the same as initial state if only + * derivatives should be reset), or null if nothing is reset */ - public boolean reset(final FieldODEStateAndDerivative state) { + public FieldODEState reset(final FieldODEStateAndDerivative state) { if (!(pendingEvent && pendingEventTime.subtract(state.getTime()).abs().subtract(convergence).getReal() <= 0)) { - return false; + return null; } + final FieldODEState newState; if (nextAction == Action.RESET_STATE) { - handler.resetState(state); + newState = handler.resetState(state); + } else if (nextAction == Action.RESET_DERIVATIVES) { + newState = state; + } else { + newState = null; } pendingEvent = false; pendingEventTime = null; - return (nextAction == Action.RESET_STATE) || - (nextAction == Action.RESET_DERIVATIVES); + return newState; }