Skip to content

Commit

Permalink
devs: allows negative time in time advance
Browse files Browse the repository at this point in the history
If a vle::devs::Dynamics::timeAdvance returns a negative value, VLE
kernel will interpret this value as an absolute date instead of a
duration. Seems to be useful to (re)synchronize model with integer dates
and permits to reduce real representation problems. (closes: vle-forge#241).
  • Loading branch information
Gauthier Quesnel committed Sep 12, 2016
1 parent 519363e commit aa0fc98
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 102 deletions.
30 changes: 27 additions & 3 deletions src/vle/devs/Dynamics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,16 @@ class VLE_API Dynamics

/**
* @brief Process the initialization of the model by initializing the
* initial state and return the duration (or timeAdvance) of the initial
* state.
* initial state and return the duration of the initial state.
*
* If @e init returns 0 or a positive real, this value means the
* duration of the current state. But, if @e init returns a negative
* real, this value means an absolute value.
*
* @param time the time of the creation of this model.
* @return duration of the initial state.
*
* @return duration of the initial state ([0, +oo{) or an absolute
* date (]-oo, 0[).
*/
virtual Time init(Time /* time */)
{
Expand All @@ -121,6 +127,24 @@ class VLE_API Dynamics
/**
* @brief Process the time advance function: compute the duration of the
* current state.
*
* If @e timeAdvance returns 0 or a positive real, this value means
* the duration of the current state. But, if @e timeAdvance returns a
* negative real, this value means an absolute value.
*
* @code
* class Model : public vle::devs::Dynamics
* {
* void timeAdvance() const
* {
* return 0.1; // Wake up me in current time + 0.1.
*
* return -123.456; // Wake up me at date 123.456.
* }
* }
*
* @endcode
*
* @return duration of the current state.
*/
virtual Time timeAdvance() const
Expand Down
66 changes: 47 additions & 19 deletions src/vle/devs/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,59 +129,87 @@ void Simulator::output(Time time)
m_dynamics->output(time, m_result);
}

Time Simulator::timeAdvance()
{
Time tn = m_dynamics->timeAdvance();

if (tn < 0.0)
throw utils::ModellingError(
(fmt(_("Negative time advance in '%1%' (%2%)"))
% getName() % tn).str());

return tn;
}

Time Simulator::init(Time time)
{
Time tn = m_dynamics->init(time);

if (tn < 0.0)
if (tn >= 0)
return m_tn = tn + time;

if (-tn < time)
throw utils::ModellingError(
(fmt(_("Negative init function in '%1%' (%2%)"))
% getName() % tn).str());
_("Bad absolute date: %f must be greater than the current date %f"),
tn, time);

m_tn = -tn;

m_tn = tn + time;
return m_tn;
}

Time Simulator::confluentTransitions(Time time)
{
assert(not m_external_events.empty() and "Simulator d-conf error");
assert(m_have_internal == true and "Simulator d-conf error");

m_dynamics->confluentTransitions(time, m_external_events);
m_external_events.clear();

m_tn = timeAdvance() + time;
Time tn = m_dynamics->timeAdvance();

if (tn >= 0)
return m_tn = tn + time;

if (-tn < time)
throw utils::ModellingError(
_("Bad absolute date: %f must be greater than the current date %f"),
tn, time);

m_tn = -tn;

return m_tn;
}

Time Simulator::internalTransition(Time time)
{
assert(m_have_internal == true and "Simulator d-int error");

m_dynamics->internalTransition(time);
m_have_internal = false;

m_tn = timeAdvance() + time;
Time tn = m_dynamics->timeAdvance();

if (tn >= 0)
return m_tn = tn + time;

if (-tn < time)
throw utils::ModellingError(
_("Bad absolute date: %f must be greater than the current date %f"),
tn, time);

m_tn = -tn;

return m_tn;
}

Time Simulator::externalTransition(Time time)
{
assert(not m_external_events.empty() and "Simulator d-ext error");

m_dynamics->externalTransition(m_external_events, time);
m_external_events.clear();

m_tn = timeAdvance() + time;
Time tn = m_dynamics->timeAdvance();

if (tn >= 0)
return m_tn = tn + time;

if (-tn < time)
throw utils::ModellingError(
_("Bad absolute date: %f must be greater than the current date %f"),
tn, time);

m_tn = -tn;

return m_tn;
}

Expand Down
1 change: 0 additions & 1 deletion src/vle/devs/Simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class VLE_LOCAL Simulator
/*-*-*-*-*-*-*-*-*-*/

Time init(Time time);
Time timeAdvance();
void finish();
void output(Time time);
Time internalTransition(Time time);
Expand Down
Loading

0 comments on commit aa0fc98

Please sign in to comment.