Skip to content

Commit

Permalink
extend FMI2 Cpp interface with clock interval
Browse files Browse the repository at this point in the history
This is needed for the first tick in FMUs with inferred sample time.
  • Loading branch information
rfranke committed Oct 19, 2015
1 parent 9c388fa commit 0c1e4fb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
Expand Up @@ -288,6 +288,11 @@ void SystemDefaultImplementation::getClock(bool* z)
}
}

double *SystemDefaultImplementation::clockInterval()
{
return _clockInterval;
}

void SystemDefaultImplementation::getContinuousStates(double* z)
{
std::copy(__z ,__z + _dimContinuousStates, z);
Expand Down
Expand Up @@ -85,6 +85,9 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
/// Provide clocks
virtual void getClock(bool* z);

/// Provide clock intervals
virtual double *clockInterval();

/// Provide the right hand side
virtual void getRHS(double* f);

Expand Down
28 changes: 26 additions & 2 deletions SimulationRuntime/cpp/Include/FMU2/FMU2Interface.cpp
Expand Up @@ -238,6 +238,18 @@ extern "C"
CATCH_EXCEPTION(w);
}

fmi2Status fmi2GetInterval(fmi2Component c,
const fmi2Integer clockIndex[],
size_t nClockIndex, fmi2Real interval[])
{
FMU2Wrapper *w = reinterpret_cast<FMU2Wrapper*>(c);
LOG_CALL(w, "fmi2GetInterval(nClockIndex = %d)", nClockIndex);
try {
return w->getInterval(clockIndex, nClockIndex, interval);
}
CATCH_EXCEPTION(w);
}

fmi2Status fmi2SetReal(fmi2Component c,
const fmi2ValueReference vr[], size_t nvr,
const fmi2Real value[])
Expand Down Expand Up @@ -288,12 +300,24 @@ extern "C"

fmi2Status fmi2SetClock(fmi2Component c,
const fmi2Integer clockIndex[],
size_t nClockIndex)
size_t nClockIndex, const fmi2Boolean active[])
{
FMU2Wrapper *w = reinterpret_cast<FMU2Wrapper*>(c);
LOG_CALL(w, "fmi2SetClock(nClockIndex = %d)", nClockIndex);
try {
return w->setClock(clockIndex, nClockIndex);
return w->setClock(clockIndex, nClockIndex, active);
}
CATCH_EXCEPTION(w);
}

fmi2Status fmi2SetInterval(fmi2Component c,
const fmi2Integer clockIndex[],
size_t nClockIndex, const fmi2Real interval[])
{
FMU2Wrapper *w = reinterpret_cast<FMU2Wrapper*>(c);
LOG_CALL(w, "fmi2SetInterval(nClockIndex = %d)", nClockIndex);
try {
return w->setInterval(clockIndex, nClockIndex, interval);
}
CATCH_EXCEPTION(w);
}
Expand Down
25 changes: 23 additions & 2 deletions SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.cpp
Expand Up @@ -260,16 +260,27 @@ fmi2Status FMU2Wrapper::setString(const fmi2ValueReference vr[], size_t nvr,
}

fmi2Status FMU2Wrapper::setClock(const fmi2Integer clockIndex[],
size_t nClockIndex)
size_t nClockIndex, const fmi2Boolean active[])
{
for (int i = 0; i < nClockIndex; i++) {
_clock_buffer[clockIndex[i] - 1] = true;
_clock_buffer[clockIndex[i] - 1] = active[i];
_nclock_active ++;
}
_need_update = true;
return fmi2OK;
}

fmi2Status FMU2Wrapper::setInterval(const fmi2Integer clockIndex[],
size_t nClockIndex, const fmi2Real interval[])
{
double *clockInterval = _model->clockInterval();
for (int i = 0; i < nClockIndex; i++) {
clockInterval[clockIndex[i] - 1] = interval[i];
}
_need_update = true;
return fmi2OK;
}

fmi2Status FMU2Wrapper::getEventIndicators(fmi2Real eventIndicators[], size_t ni)
{
if (_need_update)
Expand Down Expand Up @@ -335,6 +346,16 @@ fmi2Status FMU2Wrapper::getClock(const fmi2Integer clockIndex[],
}
}

fmi2Status FMU2Wrapper::getInterval(const fmi2Integer clockIndex[],
size_t nClockIndex, fmi2Real interval[])
{
double *clockInterval = _model->clockInterval();
for (int i = 0; i < nClockIndex; i++) {
interval[i] = clockInterval[clockIndex[i] - 1];
}
return fmi2OK;
}

fmi2Status FMU2Wrapper::newDiscreteStates(fmi2EventInfo *eventInfo)
{
if (_need_update) {
Expand Down
6 changes: 5 additions & 1 deletion SimulationRuntime/cpp/Include/FMU2/FMU2Wrapper.h
Expand Up @@ -107,6 +107,8 @@ class FMU2Wrapper
fmi2String value[]);
virtual fmi2Status getClock (const fmi2Integer clockIndex[],
size_t nClockIndex, fmi2Boolean active[]);
virtual fmi2Status getInterval(const fmi2Integer clockIndex[],
size_t nClockIndex, fmi2Real interval[]);

virtual fmi2Status setReal (const fmi2ValueReference vr[], size_t nvr,
const fmi2Real value[]);
Expand All @@ -117,7 +119,9 @@ class FMU2Wrapper
virtual fmi2Status setString (const fmi2ValueReference vr[], size_t nvr,
const fmi2String value[]);
virtual fmi2Status setClock (const fmi2Integer clockIndex[],
size_t nClockIndex);
size_t nClockIndex, const fmi2Boolean active[]);
virtual fmi2Status setInterval(const fmi2Integer clockIndex[],
size_t nClockIndex, const fmi2Real interval[]);

// Enter and exit the different modes for Model Exchange
virtual fmi2Status newDiscreteStates (fmi2EventInfo *eventInfo);
Expand Down

0 comments on commit 0c1e4fb

Please sign in to comment.