Avoid protected fields.

This commit is contained in:
Luc Maisonobe 2016-01-06 14:17:44 +01:00
parent 346a81d770
commit 355b55e4c6
5 changed files with 108 additions and 54 deletions

View File

@ -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;
@ -352,6 +355,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) {
@ -359,6 +363,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);
}
}
@ -411,4 +416,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;
}
}

View File

@ -399,6 +399,7 @@ public abstract class AbstractIntegrator implements FirstOrderIntegrator {
}
boolean needReset = false;
resetOccurred = false;
for (final EventState state : eventsStates) {
needReset = needReset || state.reset(eventT, eventYComplete);
}

View File

@ -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.

View File

@ -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;

View File

@ -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;
}