Repository: commons-math Updated Branches: refs/heads/MATH_3_X 98b7057e0 -> 84fd5fe3e
Avoid protected fields. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/34b136cf Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/34b136cf Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/34b136cf Branch: refs/heads/MATH_3_X Commit: 34b136cfbf8a0132e44ce8d4a54a58aeb611505b Parents: 98b7057 Author: Luc Maisonobe <l...@apache.org> Authored: Mon Dec 28 20:43:55 2015 +0100 Committer: Luc Maisonobe <l...@apache.org> Committed: Mon Dec 28 22:12:08 2015 +0100 ---------------------------------------------------------------------- .../math3/ode/AbstractFieldIntegrator.java | 61 ++++++++++++++++++-- .../commons/math3/ode/AbstractIntegrator.java | 1 + .../AdaptiveStepsizeFieldIntegrator.java | 4 +- .../EmbeddedRungeKuttaFieldIntegrator.java | 50 ++++++++-------- .../ode/nonstiff/RungeKuttaFieldIntegrator.java | 46 +++++++-------- 5 files changed, 108 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/34b136cf/src/main/java/org/apache/commons/math3/ode/AbstractFieldIntegrator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/ode/AbstractFieldIntegrator.java b/src/main/java/org/apache/commons/math3/ode/AbstractFieldIntegrator.java index d450d11..cce777e 100644 --- a/src/main/java/org/apache/commons/math3/ode/AbstractFieldIntegrator.java +++ b/src/main/java/org/apache/commons/math3/ode/AbstractFieldIntegrator.java @@ -56,16 +56,19 @@ public abstract class AbstractFieldIntegrator<T extends RealFieldElement<T>> imp private static final double DEFAULT_FUNCTION_VALUE_ACCURACY = 1e-15; /** Step handler. */ - protected Collection<FieldStepHandler<T>> stepHandlers; + private Collection<FieldStepHandler<T>> stepHandlers; /** Current step start. */ - protected FieldODEStateAndDerivative<T> stepStart; + private FieldODEStateAndDerivative<T> stepStart; /** Current stepsize. */ - protected T stepSize; + private T stepSize; /** Indicator for last step. */ - protected boolean isLastStep; + private boolean isLastStep; + + /** Indicator that a state or derivative reset was triggered by some event. */ + private boolean resetOccurred; /** Field to which the time and state vector elements belong. */ private final Field<T> field; @@ -338,6 +341,7 @@ public abstract class AbstractFieldIntegrator<T extends RealFieldElement<T>> imp } FieldODEState<T> newState = null; + resetOccurred = false; for (final FieldEventState<T> state : eventsStates) { newState = state.reset(eventState); if (newState != null) { @@ -345,6 +349,7 @@ public abstract class AbstractFieldIntegrator<T extends RealFieldElement<T>> imp // 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); } } @@ -397,4 +402,52 @@ public abstract class AbstractFieldIntegrator<T extends RealFieldElement<T>> imp } + /** Check if a reset occurred while last step was accepted. + * @return true if a reset occurred while last step was accepted + */ + protected boolean resetOccurred() { + return resetOccurred; + } + + /** Set the current step size. + * @param stepSize step size to set + */ + protected void setStepSize(final T stepSize) { + this.stepSize = stepSize; + } + + /** Get the current step size. + * @return current step size + */ + protected T getStepSize() { + return stepSize; + } + /** Set current step start. + * @param stepStart step start + */ + protected void setStepStart(final FieldODEStateAndDerivative<T> stepStart) { + this.stepStart = stepStart; + } + + /** Getcurrent step start. + * @return current step start + */ + protected FieldODEStateAndDerivative<T> getStepStart() { + return stepStart; + } + + /** Set the last state flag. + * @param isLastStep if true, this step is the last one + */ + protected void setIsLastStep(final boolean isLastStep) { + this.isLastStep = isLastStep; + } + + /** Check if this step is the last one. + * @return true if this step is the last one + */ + protected boolean isLastStep() { + return isLastStep; + } + } http://git-wip-us.apache.org/repos/asf/commons-math/blob/34b136cf/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java b/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java index e080722..f5d7260 100644 --- a/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java +++ b/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java @@ -394,6 +394,7 @@ public abstract class AbstractIntegrator implements FirstOrderIntegrator { } boolean needReset = false; + resetOccurred = false; for (final EventState state : eventsStates) { needReset = needReset || state.reset(eventT, eventYComplete); } http://git-wip-us.apache.org/repos/asf/commons-math/blob/34b136cf/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeFieldIntegrator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeFieldIntegrator.java b/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeFieldIntegrator.java index 49e00f3..c8e592b 100644 --- a/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeFieldIntegrator.java +++ b/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeFieldIntegrator.java @@ -345,8 +345,8 @@ public abstract class AdaptiveStepsizeFieldIntegrator<T extends RealFieldElement /** Reset internal state to dummy values. */ protected void resetInternalState() { - stepStart = null; - stepSize = minStep.multiply(maxStep).sqrt(); + setStepStart(null); + setStepSize(minStep.multiply(maxStep).sqrt()); } /** Get the minimal step. http://git-wip-us.apache.org/repos/asf/commons-math/blob/34b136cf/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaFieldIntegrator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaFieldIntegrator.java b/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaFieldIntegrator.java index 29fa5dd..ae8c074 100644 --- a/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaFieldIntegrator.java +++ b/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaFieldIntegrator.java @@ -222,7 +222,7 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme sanityChecks(initialState, finalTime); final T t0 = initialState.getTime(); final T[] y0 = equations.getMapper().mapState(initialState); - stepStart = initIntegration(equations, t0, y0, finalTime); + setStepStart(initIntegration(equations, t0, y0, finalTime)); final boolean forward = finalTime.subtract(initialState.getTime()).getReal() > 0; // create some internal working arrays @@ -236,7 +236,7 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme boolean firstTime = true; // main integration loop - isLastStep = false; + setIsLastStep(false); do { // iterate over step size, ensuring local normalized error is smaller than 1 @@ -244,8 +244,8 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme while (error.subtract(1.0).getReal() >= 0) { // first stage - y = equations.getMapper().mapState(stepStart); - yDotK[0] = equations.getMapper().mapDerivative(stepStart); + y = equations.getMapper().mapState(getStepStart()); + yDotK[0] = equations.getMapper().mapDerivative(getStepStart()); if (firstTime) { final T[] scale = MathArrays.buildArray(getField(), mainSetDimension); @@ -258,18 +258,18 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme scale[i] = y[i].abs().multiply(vecRelativeTolerance[i]).add(vecAbsoluteTolerance[i]); } } - hNew = initializeStep(forward, getOrder(), scale, stepStart, equations.getMapper()); + hNew = initializeStep(forward, getOrder(), scale, getStepStart(), equations.getMapper()); firstTime = false; } - stepSize = hNew; + setStepSize(hNew); if (forward) { - if (stepStart.getTime().add(stepSize).subtract(finalTime).getReal() >= 0) { - stepSize = finalTime.subtract(stepStart.getTime()); + if (getStepStart().getTime().add(getStepSize()).subtract(finalTime).getReal() >= 0) { + setStepSize(finalTime.subtract(getStepStart().getTime())); } } else { - if (stepStart.getTime().add(stepSize).subtract(finalTime).getReal() <= 0) { - stepSize = finalTime.subtract(stepStart.getTime()); + if (getStepStart().getTime().add(getStepSize()).subtract(finalTime).getReal() <= 0) { + setStepSize(finalTime.subtract(getStepStart().getTime())); } } @@ -281,10 +281,10 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme for (int l = 1; l < k; ++l) { sum = sum.add(yDotK[l][j].multiply(a[k-1][l])); } - yTmp[j] = y[j].add(stepSize.multiply(sum)); + yTmp[j] = y[j].add(getStepSize().multiply(sum)); } - yDotK[k] = computeDerivatives(stepStart.getTime().add(stepSize.multiply(c[k-1])), yTmp); + yDotK[k] = computeDerivatives(getStepStart().getTime().add(getStepSize().multiply(c[k-1])), yTmp); } @@ -294,53 +294,53 @@ public abstract class EmbeddedRungeKuttaFieldIntegrator<T extends RealFieldEleme for (int l = 1; l < stages; ++l) { sum = sum.add(yDotK[l][j].multiply(b[l])); } - yTmp[j] = y[j].add(stepSize.multiply(sum)); + yTmp[j] = y[j].add(getStepSize().multiply(sum)); } // estimate the error at the end of the step - error = estimateError(yDotK, y, yTmp, stepSize); + error = estimateError(yDotK, y, yTmp, getStepSize()); if (error.subtract(1.0).getReal() >= 0) { // reject the step and attempt to reduce error by stepsize control final T factor = MathUtils.min(maxGrowth, MathUtils.max(minReduction, safety.multiply(error.pow(exp)))); - hNew = filterStep(stepSize.multiply(factor), forward, false); + hNew = filterStep(getStepSize().multiply(factor), forward, false); } } - final T stepEnd = stepStart.getTime().add(stepSize); + final T stepEnd = getStepStart().getTime().add(getStepSize()); final T[] yDotTmp = (fsal >= 0) ? yDotK[fsal] : computeDerivatives(stepEnd, yTmp); final FieldODEStateAndDerivative<T> stateTmp = new FieldODEStateAndDerivative<T>(stepEnd, yTmp, yDotTmp); // local error is small enough: accept the step, trigger events and step handlers System.arraycopy(yTmp, 0, y, 0, y0.length); - stepStart = acceptStep(createInterpolator(forward, yDotK, stepStart, stateTmp, equations.getMapper()), - finalTime); + setStepStart(acceptStep(createInterpolator(forward, yDotK, getStepStart(), stateTmp, equations.getMapper()), + finalTime)); - if (!isLastStep) { + if (!isLastStep()) { // stepsize control for next step final T factor = MathUtils.min(maxGrowth, MathUtils.max(minReduction, safety.multiply(error.pow(exp)))); - final T scaledH = stepSize.multiply(factor); - final T nextT = stepStart.getTime().add(scaledH); + final T scaledH = getStepSize().multiply(factor); + final T nextT = getStepStart().getTime().add(scaledH); final boolean nextIsLast = forward ? nextT.subtract(finalTime).getReal() >= 0 : nextT.subtract(finalTime).getReal() <= 0; hNew = filterStep(scaledH, forward, nextIsLast); - final T filteredNextT = stepStart.getTime().add(hNew); + final T filteredNextT = getStepStart().getTime().add(hNew); final boolean filteredNextIsLast = forward ? filteredNextT.subtract(finalTime).getReal() >= 0 : filteredNextT.subtract(finalTime).getReal() <= 0; if (filteredNextIsLast) { - hNew = finalTime.subtract(stepStart.getTime()); + hNew = finalTime.subtract(getStepStart().getTime()); } } - } while (!isLastStep); + } while (!isLastStep()); - final FieldODEStateAndDerivative<T> finalState = stepStart; + final FieldODEStateAndDerivative<T> finalState = getStepStart(); resetInternalState(); return finalState; http://git-wip-us.apache.org/repos/asf/commons-math/blob/34b136cf/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaFieldIntegrator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaFieldIntegrator.java b/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaFieldIntegrator.java index cabb9a0..97ba805 100644 --- a/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaFieldIntegrator.java +++ b/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaFieldIntegrator.java @@ -120,7 +120,7 @@ public abstract class RungeKuttaFieldIntegrator<T extends RealFieldElement<T>> sanityChecks(initialState, finalTime); final T t0 = initialState.getTime(); final T[] y0 = equations.getMapper().mapState(initialState); - stepStart = initIntegration(equations, t0, y0, finalTime); + setStepStart(initIntegration(equations, t0, y0, finalTime)); final boolean forward = finalTime.subtract(initialState.getTime()).getReal() > 0; // create some internal working arrays @@ -131,26 +131,26 @@ public abstract class RungeKuttaFieldIntegrator<T extends RealFieldElement<T>> // set up integration control objects if (forward) { - if (stepStart.getTime().add(step).subtract(finalTime).getReal() >= 0) { - stepSize = finalTime.subtract(stepStart.getTime()); + if (getStepStart().getTime().add(step).subtract(finalTime).getReal() >= 0) { + setStepSize(finalTime.subtract(getStepStart().getTime())); } else { - stepSize = step; + setStepSize(step); } } else { - if (stepStart.getTime().subtract(step).subtract(finalTime).getReal() <= 0) { - stepSize = finalTime.subtract(stepStart.getTime()); + if (getStepStart().getTime().subtract(step).subtract(finalTime).getReal() <= 0) { + setStepSize(finalTime.subtract(getStepStart().getTime())); } else { - stepSize = step.negate(); + setStepSize(step.negate()); } } // main integration loop - isLastStep = false; + setIsLastStep(false); do { // first stage - y = equations.getMapper().mapState(stepStart); - yDotK[0] = equations.getMapper().mapDerivative(stepStart); + y = equations.getMapper().mapState(getStepStart()); + yDotK[0] = equations.getMapper().mapDerivative(getStepStart()); // next stages for (int k = 1; k < stages; ++k) { @@ -160,10 +160,10 @@ public abstract class RungeKuttaFieldIntegrator<T extends RealFieldElement<T>> for (int l = 1; l < k; ++l) { sum = sum.add(yDotK[l][j].multiply(a[k-1][l])); } - yTmp[j] = y[j].add(stepSize.multiply(sum)); + yTmp[j] = y[j].add(getStepSize().multiply(sum)); } - yDotK[k] = computeDerivatives(stepStart.getTime().add(stepSize.multiply(c[k-1])), yTmp); + yDotK[k] = computeDerivatives(getStepStart().getTime().add(getStepSize().multiply(c[k-1])), yTmp); } @@ -173,34 +173,34 @@ public abstract class RungeKuttaFieldIntegrator<T extends RealFieldElement<T>> for (int l = 1; l < stages; ++l) { sum = sum.add(yDotK[l][j].multiply(b[l])); } - yTmp[j] = y[j].add(stepSize.multiply(sum)); + yTmp[j] = y[j].add(getStepSize().multiply(sum)); } - final T stepEnd = stepStart.getTime().add(stepSize); + final T stepEnd = getStepStart().getTime().add(getStepSize()); final T[] yDotTmp = computeDerivatives(stepEnd, yTmp); final FieldODEStateAndDerivative<T> stateTmp = new FieldODEStateAndDerivative<T>(stepEnd, yTmp, yDotTmp); // discrete events handling System.arraycopy(yTmp, 0, y, 0, y0.length); - stepStart = acceptStep(createInterpolator(forward, yDotK, stepStart, stateTmp, equations.getMapper()), - finalTime); + setStepStart(acceptStep(createInterpolator(forward, yDotK, getStepStart(), stateTmp, equations.getMapper()), + finalTime)); - if (!isLastStep) { + if (!isLastStep()) { // stepsize control for next step - final T nextT = stepStart.getTime().add(stepSize); + final T nextT = getStepStart().getTime().add(getStepSize()); final boolean nextIsLast = forward ? (nextT.subtract(finalTime).getReal() >= 0) : (nextT.subtract(finalTime).getReal() <= 0); if (nextIsLast) { - stepSize = finalTime.subtract(stepStart.getTime()); + setStepSize(finalTime.subtract(getStepStart().getTime())); } } - } while (!isLastStep); + } while (!isLastStep()); - final FieldODEStateAndDerivative<T> finalState = stepStart; - stepStart = null; - stepSize = null; + final FieldODEStateAndDerivative<T> finalState = getStepStart(); + setStepStart(null); + setStepSize(null); return finalState; }