Skip to content

Commit

Permalink
fix for event clocks in cpp template
Browse files Browse the repository at this point in the history
  • Loading branch information
niwokr committed Apr 26, 2016
1 parent 7733e05 commit c57105d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 13 deletions.
17 changes: 14 additions & 3 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -7750,10 +7750,13 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then

//Provide number (dimension) of zero functions
virtual int getDimZeroFunc();
//Provide number (dimension) of zero functions
virtual int getDimClock();
//Provides current values of root/zero functions
virtual void getZeroFunc(double* f);
virtual void setConditions(bool* c);
virtual void getConditions(bool* c);
virtual void getClockConditions(bool* c);

//Called to handle an event
virtual void handleEvent(const bool* events);
Expand Down Expand Up @@ -12178,7 +12181,11 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<<
int <%lastIdentOfPath(modelInfo.name)%>::getDimZeroFunc()
{
return _dimZeroFunc + _dimClock;;
return _dimZeroFunc + _dimClock;
}
int <%lastIdentOfPath(modelInfo.name)%>::getDimClock()
{
return _dimClock;
}
>>
end dimZeroFunc;
Expand Down Expand Up @@ -12499,9 +12506,9 @@ template giveZeroFunc1(list<ZeroCrossing> zeroCrossings,SimCode simCode ,Text& e
{
double nextClockTime = _clockTime[i]+_clockInterval[i];
if(_simTime < nextClockTime)
f[i]= (_simTime - nextClockTime)- 1e-9;
f[i+_dimZeroFunc]= (_simTime - nextClockTime)- 1e-9;
else
f[i]=1.0;
f[i+_dimZeroFunc]=1.0;
}

}
Expand Down Expand Up @@ -12529,6 +12536,10 @@ template getConditions(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Te
{
SystemDefaultImplementation::getConditions(c);
}
void <%lastIdentOfPath(modelInfo.name)%>::getClockConditions(bool* c)
{
SystemDefaultImplementation::getClockConditions(c);
}
>>
end getConditions;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1701,7 +1701,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
'_clockInterval[clockIndex - 1]'

case CALL(path=IDENT(name="$_clkfire"), expLst={arg as ICONST(__)}) then
'_time_conditions[<%arg.integer%> - 1 + <%timeEventLength(simCode)%>] = (_simTime > _clockTime[<%arg.integer%> - 1])'
'_clockCondition[<%arg.integer%> - 1 + <%timeEventLength(simCode)%>] = (_simTime > _clockTime[<%arg.integer%> - 1])'

case CALL(path=IDENT(name="$getPart"), expLst={e1}) then
daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
Expand Down
34 changes: 32 additions & 2 deletions SimulationRuntime/cpp/Core/System/ContinuousEvents.cpp
Expand Up @@ -9,7 +9,15 @@
#include <Core/Math/Functions.h>


ContinuousEvents::ContinuousEvents() : _event_system(NULL), _countinous_system(NULL), _mixed_system(NULL), _conditions0(NULL), _conditions1(NULL)
ContinuousEvents::ContinuousEvents()
: _event_system(NULL)
, _countinous_system(NULL)
, _mixed_system(NULL)
, _conditions0(NULL)
, _conditions1(NULL)
,_clockconditions0(NULL)
,_clockconditions1(NULL)

{
}

Expand All @@ -20,6 +28,14 @@ ContinuousEvents::~ContinuousEvents(void)
delete[] _conditions0;
if(_conditions1)
delete[] _conditions1;


if(_clockconditions0)
delete[] _clockconditions0;
if(_clockconditions1)
delete[] _clockconditions1;


}

/**
Expand All @@ -38,8 +54,15 @@ void ContinuousEvents::initialize(IEvent* system)
if(_conditions1)
delete[] _conditions1;

if(_clockconditions0)
delete[] _clockconditions0;
if(_clockconditions1)
delete[] _clockconditions1;

_conditions0 = new bool[_event_system->getDimZeroFunc()];
_conditions1 = new bool[_event_system->getDimZeroFunc()];
_clockconditions0 = new bool[_event_system->getDimClock()];
_clockconditions1 = new bool[_event_system->getDimClock()];
}


Expand All @@ -53,8 +76,11 @@ bool ContinuousEvents::startEventIteration(bool& state_vars_reinitialized)
//Deactivated: _event_system->saveDiscreteVars(); // store values of discrete vars vor next check

unsigned int dim = _event_system->getDimZeroFunc();
unsigned int dimClock = _event_system->getDimClock();

_event_system->getConditions(_conditions0);
_event_system->getConditions(_clockconditions0);

//Handle all events

state_vars_reinitialized = _countinous_system->evaluateConditions();
Expand All @@ -65,9 +91,13 @@ bool ContinuousEvents::startEventIteration(bool& state_vars_reinitialized)


_event_system->getConditions(_conditions1);
_event_system->getConditions(_clockconditions1);

bool crestart = !std::equal (_conditions1, _conditions1+dim,_conditions0);
//check for event clocks
bool eventclocksrestart = !std::equal (_clockconditions1, _clockconditions1+dimClock,_clockconditions0);

return((drestart||crestart)); //returns true if new events occurred
return((drestart||crestart||eventclocksrestart)); //returns true if new events occurred
}
/** @} */ // end of coreSystem
/*
Expand Down
Expand Up @@ -314,11 +314,6 @@ double *SystemDefaultImplementation::clockShift()
void SystemDefaultImplementation::getContinuousStates(double* z)
{
std::copy(__z ,__z + _dimContinuousStates, z);
/*for(int i=0; i< _dimContinuousStates; ++i)
{
z[i] = __z[i];
}*/

}

IGlobalSettings* SystemDefaultImplementation::getGlobalSettings()
Expand Down Expand Up @@ -375,7 +370,10 @@ void SystemDefaultImplementation::getConditions(bool* c)
{
memcpy(c,_conditions,_dimZeroFunc*sizeof(bool));
}

void SystemDefaultImplementation::getClockConditions(bool* c)
{
memcpy(c,_clockCondition,_dimClock*sizeof(bool));
}
/// setter for variables of different types

void SystemDefaultImplementation::setBoolean(const bool* z)
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Core/System/ContinuousEvents.h
Expand Up @@ -24,5 +24,7 @@ class BOOST_EXTENSION_EVENTHANDLING_DECL ContinuousEvents
IMixedSystem* _mixed_system; //just a cast of _event_system -> required in IterateEventQueue
bool* _conditions0;
bool* _conditions1;
bool* _clockconditions0;
bool* _clockconditions1;
};
/** @} */ // end of coreSystem
4 changes: 3 additions & 1 deletion SimulationRuntime/cpp/Include/Core/System/IEvent.h
Expand Up @@ -26,11 +26,13 @@ class IEvent

/// Provide number (dimension) of zero functions
virtual int getDimZeroFunc() = 0;
virtual int getDimClock() = 0;
/// Provides current values of root/zero functions
virtual void getZeroFunc(double* f) = 0;

virtual void setConditions(bool* c) = 0;
virtual void setConditions(bool* c) = 0;
virtual void getConditions(bool* c) = 0;
virtual void getClockConditions(bool* c) = 0;
//Deactivated: virtual void saveDiscreteVars() = 0;
//Saves all variables before an event is handled, is needed for the pre, edge and change operator
virtual void saveAll() = 0;
Expand Down
Expand Up @@ -96,6 +96,7 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation

virtual void setConditions(bool* c);
virtual void getConditions(bool* c);
virtual void getClockConditions(bool* c);
/// Provide boolean variables
virtual void setBoolean(const bool* z);

Expand Down

0 comments on commit c57105d

Please sign in to comment.