Skip to content

Commit

Permalink
extend Cpp runtime with sub clocks
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Nov 1, 2015
1 parent ceca57e commit 1edd09e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
51 changes: 25 additions & 26 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -3678,7 +3678,7 @@ match simCode
//Number of equations
<%dimension1(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>
_dimZeroFunc = <%zeroCrossLength(simCode)%>;
_dimClock = <%listLength(clockedPartitions)%>;
_dimClock = <%listLength(getSubPartitions(clockedPartitions))%>;
// simplified treatment of clocks in model as time events
_dimTimeEvent = <%timeEventLength(simCode)%> + _dimClock;
//Number of residues
Expand Down Expand Up @@ -9738,21 +9738,31 @@ end eventHandlingInit;

template clockIntervalsInit(SimCode simCode, Text& varDecls, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let i = tempDecl('int', &varDecls)
<<
<%(clockedPartitions |> partition hasindex i fromindex 0 =>
match partition
case CLOCKED_PARTITION(__) then
let &preExp = buffer "" /*BUFD*/
let intvl = daeExp(getClockInterval(baseClock), contextOther, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
<%i%> = 0;
<%(clockedPartitions |> partition =>
match partition
case CLOCKED_PARTITION(__) then
let &preExp = buffer "" /*BUFD*/
let intvl = daeExp(getClockInterval(baseClock), contextOther, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let subClocks = (subPartitions |> subPartition =>
match subPartition
case SUBPARTITION(subClock=SUBCLOCK(factor=RATIONAL(nom=fnom, denom=fres), shift=RATIONAL(nom=snom, denom=sres))) then
<<
<%preExp%>
_clockInterval[<%i%>] = <%intvl%>;
_clockTime[<%i%>] = _simTime;
_clockInterval[<%i%>] = <%intvl%> * <%fres%>.0 / <%fnom%>.0;
_clockShift[<%i%>] = <%snom%>.0 / <%sres%>.0;
_clockTime[<%i%>] = _simTime + _clockShift[<%i%>] * _clockInterval[<%i%>];
<%i%> ++;
>>
else ''
;separator="\n")%>
; separator="\n")
<<
<%subClocks%>
>>
; separator="\n")%>
>>
end clockIntervalsInit;

Expand Down Expand Up @@ -10436,24 +10446,13 @@ template generateTimeEvent(list<BackendDAE.TimeEvent> timeEvents, SimCode simCod
else ''
;separator="\n\n")%>
// simplified treatment of clocks in model as time events
<%(clockedPartitions |> partition =>
match partition
case CLOCKED_PARTITION(__) then
let &preExp = buffer "" /*BUFD*/
let intvl = daeExp(getClockInterval(baseClock), contextOther, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
<<
<%preExp%>
time_events.push_back(std::make_pair(0.0, <%intvl%>));
>>
else ''
;separator="\n\n")%>
for (int i = 0; i < _dimClock; i++)
time_events.push_back(std::make_pair(_clockShift[i] * _clockInterval[i], _clockInterval[i]));
}
>>
end generateTimeEvent;




template generateStepCompleted2(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
let &varDecls = buffer "" /*BUFD*/
Expand Down Expand Up @@ -12590,7 +12589,7 @@ template clockedPartFunctions(Integer i, list<tuple<SimCodeVar.SimVar, Boolean>>
void <%className%>::<%funcName%>(const UPDATETYPE command)
{
<%funcCalls%>
if (_simTime != _clockTime[<%idx%>]) {
if (_simTime > _clockTime[<%idx%>]) {
_clockInterval[<%idx%>] = _simTime - _clockTime[<%idx%>];
_clockTime[<%idx%>] = _simTime;
}
Expand Down
20 changes: 15 additions & 5 deletions SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp
Expand Up @@ -53,8 +53,9 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
, _dimClock (0)
, _dimAE (0)
, _time_event_counter (NULL)
, _clockTime (NULL)
, _clockInterval (NULL)
, _clockShift (NULL)
, _clockTime (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -85,8 +86,9 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
, _dimClock (0)
, _dimAE (0)
, _time_event_counter (NULL)
, _clockTime (NULL)
, _clockInterval (NULL)
, _clockShift (NULL)
, _clockTime (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -125,8 +127,9 @@ SystemDefaultImplementation::~SystemDefaultImplementation()
if(_time_conditions) delete [] _time_conditions ;
if(_time_event_counter) delete [] _time_event_counter;
if(_conditions0) delete [] _conditions0;
if(_clockTime) delete [] _clockTime;
if(_clockInterval) delete [] _clockInterval;
if(_clockShift) delete [] _clockShift;
if(_clockTime) delete [] _clockTime;
}

void SystemDefaultImplementation::Assert(bool cond,const string& msg)
Expand Down Expand Up @@ -223,10 +226,12 @@ void SystemDefaultImplementation::initialize()
}
if (_dimClock > 0)
{
if (_clockTime) delete [] _clockTime;
_clockTime = new double [_dimClock];
if (_clockInterval) delete [] _clockInterval;
_clockInterval = new double [_dimClock];
if (_clockShift) delete [] _clockShift;
_clockShift = new double [_dimClock];
if (_clockTime) delete [] _clockTime;
_clockTime = new double [_dimClock];
}
_start_time = 0.0;
_terminal = false;
Expand Down Expand Up @@ -293,6 +298,11 @@ double *SystemDefaultImplementation::clockInterval()
return _clockInterval;
}

double *SystemDefaultImplementation::clockShift()
{
return _clockShift;
}

void SystemDefaultImplementation::getContinuousStates(double* z)
{
std::copy(__z ,__z + _dimContinuousStates, z);
Expand Down
Expand Up @@ -88,6 +88,9 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
/// Provide clock intervals
virtual double *clockInterval();

/// Provide clock shifts
virtual double *clockShift();

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

Expand Down Expand Up @@ -169,8 +172,9 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation

int
* _time_event_counter;
double *_clockTime; ///< time of clock ticks
double *_clockInterval; ///< time interval between clock ticks
double *_clockShift; ///< time before first activation
double *_clockTime; ///< time of clock ticks
std::ostream *_outputStream; ///< Output stream for results

IContinuous::UPDATETYPE _callType;
Expand Down

0 comments on commit 1edd09e

Please sign in to comment.