Skip to content

Commit

Permalink
implement interval operator in Cpp runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Oct 13, 2015
1 parent c1357ac commit 55125bf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
40 changes: 34 additions & 6 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -6073,6 +6073,7 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th
let initFunctions = functionInitial(startValueEquations, varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let initZeroCrossings = functionOnlyZeroCrossing(zeroCrossings,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
let initEventHandling = eventHandlingInit(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
let initClockIntervals = clockIntervalsInit(simCode, &varDecls, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)

let initAlgloopSolvers = initAlgloopsolvers(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
let initAlgloopvars = initAlgloopVars(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
Expand Down Expand Up @@ -6186,6 +6187,7 @@ case SIMCODE(modelInfo = MODELINFO(__),makefileParams = MAKEFILE_PARAMS(__)) th

//init event handling
<%initEventHandling%>
<%initClockIntervals%>

//init equations
initEquations();
Expand Down Expand Up @@ -6325,7 +6327,7 @@ template functionInitialEquations(list<SimEqSystem> initalEquations, Text method
"Generates function in simulation file."
::=
let equation_func_calls = (initalEquations |> eq =>
equation_function_create_single_func(eq, contextOther, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace,methodName, "Initialize", stateDerVectorName, useFlatArrayNotation, createMeasureTime, assignToStartValues, overwriteOldStartValues)
equation_function_create_single_func(eq, contextOther, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace,methodName, "Initialize", stateDerVectorName, useFlatArrayNotation, createMeasureTime, assignToStartValues, overwriteOldStartValues, "")
;separator="\n")

<<
Expand Down Expand Up @@ -9938,6 +9940,27 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
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
<<
<%(clockedPartitions |> partition hasindex i fromindex 0 =>
match partition
case CLOCKED_PARTITION(__) then
let &preExp = buffer "" /*BUFD*/
let intvl = daeExp(getClockIntvl(baseClock), contextOther, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
<<
<%preExp%>
_clockInterval[<%i%>] = <%intvl%>;
_clockTime[<%i%>] = _simTime;
>>
else ''
;separator="\n")%>
>>
end clockIntervalsInit;


template dimension1(SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
match simCode
Expand Down Expand Up @@ -10244,11 +10267,11 @@ end equation_function_call;

template equation_function_create_single_func(SimEqSystem eq, Context context, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace,
Text method,Text classnameext, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation, Boolean createMeasureTime,
Boolean assignToStartValues, Boolean overwriteOldStartValue)
Boolean assignToStartValues, Boolean overwriteOldStartValue, String defaultVarDeclsLocal)
::=
let ix_str = equationIndex(eq)
let ix_str_array = intSub(stringInt(ix_str),1) //equation index - 1
let &varDeclsLocal = buffer "" /*BUFD*/
let &varDeclsLocal = buffer defaultVarDeclsLocal /*BUFD*/
let &additionalFuncs = buffer "" /*BUFD*/
let &measureTimeStartVar = buffer "" /*BUFD*/
let &measureTimeEndVar = buffer "" /*BUFD*/
Expand All @@ -10275,7 +10298,7 @@ template equation_function_create_single_func(SimEqSystem eq, Context context, S
case e as SES_MIXED(__)
then
/*<%equationMixed(e, context, &varDeclsLocal, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>*/
let &additionalFuncs += equation_function_create_single_func(e.cont, context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, method, classnameext, stateDerVectorName, useFlatArrayNotation, createMeasureTime, assignToStartValues, overwriteOldStartValue)
let &additionalFuncs += equation_function_create_single_func(e.cont, context, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, method, classnameext, stateDerVectorName, useFlatArrayNotation, createMeasureTime, assignToStartValues, overwriteOldStartValue, "")
"throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,\"Mixed systems are not supported yet\");"
case e as SES_FOR_LOOP(__)
then
Expand Down Expand Up @@ -12675,7 +12698,7 @@ end checkForDiscreteEvents;
template equationFunctions(list<SimEqSystem> allEquationsPlusWhen, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Context context, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation, Boolean enableMeasureTime)
::=
let equation_func_calls = (allEquationsPlusWhen |> eq =>
equation_function_create_single_func(eq, context/*BUFC*/, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,"evaluate","", stateDerVectorName, useFlatArrayNotation,enableMeasureTime,false,false)
equation_function_create_single_func(eq, context/*BUFC*/, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,"evaluate","", stateDerVectorName, useFlatArrayNotation,enableMeasureTime,false,false, "")
;separator="\n")
<<
<%equation_func_calls%>
Expand Down Expand Up @@ -12719,18 +12742,23 @@ template clockedPartFunctions(Integer i, list<tuple<SimCodeVar.SimVar, Boolean>>
::=
let className = lastIdentOfPathFromSimCode(simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace)
let funcs = equations |> eq =>
equation_function_create_single_func(eq, context/*BUFC*/, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, "evaluate", "", stateDerVectorName, useFlatArrayNotation, enableMeasureTime, false, false)
equation_function_create_single_func(eq, context/*BUFC*/, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, "evaluate", "", stateDerVectorName, useFlatArrayNotation, enableMeasureTime, false, false, 'const int clockIndex = <%i%>;')
; separator="\n"
let funcName = 'evaluateClocked<%i%>'
let funcCalls = (List.partition(equations, 100) |> eqs hasindex i0 =>
createEvaluateWithSplit(i0, context, eqs, funcName, className, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace)
; separator="\n")
let idx = intAdd(i, -1)
<<
<%funcs%>

void <%className%>::<%funcName%>(const UPDATETYPE command)
{
<%funcCalls%>
if (_simTime != _clockTime[<%idx%>]) {
_clockInterval[<%idx%>] = _simTime - _clockTime[<%idx%>];
_clockTime[<%idx%>] = _simTime;
}
}
>>
end clockedPartFunctions;
Expand Down
3 changes: 3 additions & 0 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1736,6 +1736,9 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
case CALL(path=IDENT(name="previous"), expLst={arg as CREF(__)}) then
'<%cref(crefPrefixPrevious(arg.componentRef), useFlatArrayNotation)%>'

case CALL(path=IDENT(name="interval")) then
'_clockInterval[clockIndex - 1]'

case CALL(path=IDENT(name="$getPart"), expLst={e1}) then
daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)

Expand Down
Expand Up @@ -50,8 +50,11 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
, _dimString (0)
, _dimZeroFunc (0)
, _dimTimeEvent (0)
, _dimClock (0)
, _dimAE (0)
, _time_event_counter (NULL)
, _clockTime (NULL)
, _clockInterval (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -79,8 +82,11 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
, _dimString (0)
, _dimZeroFunc (0)
, _dimTimeEvent (0)
, _dimClock (0)
, _dimAE (0)
, _time_event_counter (NULL)
, _clockTime (NULL)
, _clockInterval (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -119,7 +125,8 @@ 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;
}

void SystemDefaultImplementation::Assert(bool cond,const string& msg)
Expand Down Expand Up @@ -214,6 +221,13 @@ void SystemDefaultImplementation::initialize()
memset(_time_conditions,false,(_dimTimeEvent)*sizeof(bool));
memset(_time_event_counter,0,(_dimTimeEvent)*sizeof(int));
}
if (_dimClock > 0)
{
if (_clockTime) delete [] _clockTime;
_clockTime = new double [_dimClock];
if (_clockInterval) delete [] _clockInterval;
_clockInterval = new double [_dimClock];
}
_start_time = 0.0;
_terminal = false;
_terminate = false;
Expand Down
Expand Up @@ -166,6 +166,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation

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

IContinuous::UPDATETYPE _callType;
Expand Down

0 comments on commit 55125bf

Please sign in to comment.