// synchronize the local time with the outside time.
CompositeActor container = (CompositeActor) getContainer();
Director executiveDirector = container.getExecutiveDirector();
_outsideTime = executiveDirector.getModelTime();
Time localTime = getModelTime();
// As an optimization, we try to keep the step size
// bounded by the time to the next event in the
// enclosing model.
Time outsideNextIterationTime = executiveDirector
.getModelNextIterationTime();
if (_debugging) {
_debug("The current time of outside model is " + _outsideTime,
" and its next iteration time is "
+ outsideNextIterationTime,
"\nThe current time of this director is " + localTime);
}
// Now, check the next iteration time.
if (outsideNextIterationTime.compareTo(_outsideTime) < 0) {
// NOTE: This check is redundant. The outside director should
// guarantee that this never happen.
throw new IllegalActionException(this, "Outside domain"
+ " time is going backward."
+ " Current outside time = " + _outsideTime
+ ", but the next iteration time = "
+ outsideNextIterationTime);
}
double aheadLength = _runAheadLength;
// Ideally, the outside time should equal the local time.
// If the outside time is less than the local time, then rollback
// is needed. If the outside time is greater than the local time,
// an exception will be thrown.
if (_outsideTime.compareTo(localTime) > 0) {
throw new IllegalActionException(this, executiveDirector,
"Outside time is later than the local time. "
+ "This should never happen.");
} else if (_outsideTime.compareTo(localTime) < 0) {
// Outside time less than the local time. Rollback!
// NOTE: This can happen, for example, if the CT model is
// inside a DE model, and it has advanced its time too
// far into the future. For example, if it was previously
// fired at time 0.0 and it advanced its local time
// to 0.1, but later it gets fired again at time 0.05.
// In that case, it has to restart the integration
// from time 0.0 and ensure that it doesn't progress
// its local time past 0.05.
// An example is to have two interacting CT subsystems
// embedded inside a DE model, where one system (called A)
// produces an event at time 0.1 and the other one (called B)
// produces an event at time 0.05. A may integrate with a step
// size of 0.1 but it has to roll back and use a step size 0.05
// such that the event produced by B can be handled.
if (_debugging) {
_debug(getName() + " rollback from: " + localTime + " to: "
+ _knownGoodTime + "due to outside time "
+ _outsideTime);
}
// The local time is set backwards to a known good time.
_rollback();
aheadLength = _outsideTime.subtract(getModelTime())
.getDoubleValue();
} else {
aheadLength = outsideNextIterationTime.subtract(_outsideTime)
.getDoubleValue();
}
if (_debugging) {
_debug(getName(), " local time = " + localTime,