Skip to content

Commit

Permalink
support boolean sub clocks for cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vwaurich authored and OpenModelica-Hudson committed Apr 26, 2017
1 parent 89d9bc6 commit f9882c3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -756,6 +756,7 @@ algorithm
off := off + basePartition.nSubClocks;
clockedPartitions := SimCode.CLOCKED_PARTITION(basePartition.clock, simSubPartitions)::clockedPartitions;
end for;
clockedPartitions := listReverse(clockedPartitions); // in order to keep the correct indexes for the correct clkfire-calls
end createClockedSimPartitions;

protected function collectClockedVars "author: rfranke
Expand Down Expand Up @@ -13175,5 +13176,26 @@ algorithm
end match;
end isModelTooBigForCSharpInOneFile;

public function absoluteClockIdxForBaseClock
input Integer baseClockIdx; // one-based
input list<SimCode.ClockedPartition> allBaseClockPartitions;
output Integer absBaseClockIdx;
protected
Integer i = 1;
algorithm
absBaseClockIdx := 1;
while i < baseClockIdx loop
absBaseClockIdx := absBaseClockIdx + listLength(getSubPartition(listGet(allBaseClockPartitions,i)));
i := i+1;
end while;
end absoluteClockIdxForBaseClock;

public function getClockedPartitions
input SimCode.SimCode simcode;
output list<SimCode.ClockedPartition> clockedPartitions;
algorithm
clockedPartitions := simcode.clockedPartitions;
end getClockedPartitions;

annotation(__OpenModelica_Interface="backend");
end SimCodeUtil;
45 changes: 42 additions & 3 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -12448,6 +12448,8 @@ end clockedPartFunctions;

template createEvaluateAll( list<SimEqSystem> allEquationsPlusWhen, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Context context, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation, Boolean createMeasureTime)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let className = lastIdentOfPathFromSimCode(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)

let equation_all_func_calls = (List.partition(allEquationsPlusWhen, 100) |> eqs hasindex i0 =>
Expand All @@ -12458,8 +12460,7 @@ template createEvaluateAll( list<SimEqSystem> allEquationsPlusWhen, SimCode simC
bool <%className%>::evaluateAll(const UPDATETYPE command)
{
<%if createMeasureTime then generateMeasureTimeStartCode("measuredFunctionStartValues", "evaluateAll", "MEASURETIME_MODELFUNCTIONS") else ""%>

<%createTimeConditionTreatments(timeEventLength(simCode))%>
<%createTimeConditionTreatments(timeEventLength(simCode), clockedPartitions)%>

// Evaluate Equations
<%equation_all_func_calls%>
Expand All @@ -12471,20 +12472,58 @@ template createEvaluateAll( list<SimEqSystem> allEquationsPlusWhen, SimCode simC
>>
end createEvaluateAll;

template createTimeConditionTreatments(String numberOfTimeEvents)
template createTimeConditionTreatments(String numberOfTimeEvents, list<ClockedPartition> clockPartitions)
::=
let booleanSubClocks = (clockPartitions |> baseClock hasindex i0 =>
booleanSubClockActivation1(absoluteClockIdxForBaseClock(intAdd(i0,1),clockPartitions), baseClock, numberOfTimeEvents)
;separator="\n")
<<
// treatment of clocks in model as time events
for (int i = <%numberOfTimeEvents%>; i < _dimTimeEvent; i++) {
if (_time_conditions[i]) {
evaluateClocked(i - <%numberOfTimeEvents%> + 1);
<%booleanSubClocks%>
_time_conditions[i] = false; // reset clock after one evaluation
_clockSubactive[i - <%numberOfTimeEvents%>] = false;
}
}
>>
end createTimeConditionTreatments;

template booleanSubClockActivation1(Integer absBaseClockIdx, ClockedPartition baseClock, String numberOfTimeEvents)
::=
match baseClock
case CLOCKED_PARTITION(baseClock = BOOLEAN_CLOCK(__)) then
let subClocks = (subPartitions |> subClock hasindex i0 =>
booleanSubClockActivation2(absBaseClockIdx, i0, subClock, numberOfTimeEvents)
;separator="\n")
<<
//the subclocks <%absBaseClockIdx%>
<%subClocks%>
>>
else
<<
//no subclock <%absBaseClockIdx%>
>>
end match
end booleanSubClockActivation1;

template booleanSubClockActivation2(Integer absClockIdx, Integer subClockIdx, SubPartition subPartition, String numberOfTimeEvents)
::=
if intNe(subClockIdx,0) then
let absSubClockIdx = intAdd(absClockIdx,subClockIdx)
<<
//activate boolean triggered subclock <%absSubClockIdx%> of the base sub-clock <%absClockIdx%> is triggered
if (_time_conditions[<%absClockIdx%> -1+<%numberOfTimeEvents%>] && (_simTime >= _clockShift[<%absSubClockIdx%> -1]*_clockInterval[<%absClockIdx%> -1])) {
_time_conditions[<%absSubClockIdx%> -1+<%numberOfTimeEvents%>] = (_simTime >= _clockShift[<%absSubClockIdx%> -1]*_clockInterval[<%absClockIdx%> -1]);
}
>>
else
<<

>>
end booleanSubClockActivation2;

template createEvaluateConditions( list<SimEqSystem> allEquationsPlusWhen, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Context context, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
::=
let className = lastIdentOfPathFromSimCode(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1710,7 +1710,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
'<%contextSystem(context)%>_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])'
'_time_conditions[<%absoluteClockIdxForBaseClock(arg.integer, getClockedPartitions(simCode))%> - 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
4 changes: 3 additions & 1 deletion Compiler/Template/CodegenCppHpcom.tpl
Expand Up @@ -773,6 +773,8 @@ template generateParallelEvaluate(list<SimEqSystem> allEquationsPlusWhen, Absyn.
SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Option<tuple<Schedule, Schedule, Schedule>> schedulesOpt, Context context, Text stateDerVectorName /*=__zDot*/,
String modelNamePrefixStr, Boolean useFlatArrayNotation)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let &varDecls = buffer "" /*BUFD*/
let measureTimeEvaluateOdeStart = if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then generateMeasureTimeStartCode("measuredFunctionStartValues", "evaluateODE", "MEASURETIME_MODELFUNCTIONS") else ""
let measureTimeEvaluateOdeEnd = if boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")) then generateMeasureTimeEndCode("measuredFunctionStartValues", "measuredFunctionEndValues", "(*measureTimeFunctionsArray)[0]", "evaluateODE", "MEASURETIME_MODELFUNCTIONS") else ""
Expand Down Expand Up @@ -801,7 +803,7 @@ template generateParallelEvaluate(list<SimEqSystem> allEquationsPlusWhen, Absyn.
{
<%measureTimeEvaluateAllStart%>
<%createTimeConditionTreatments(timeEventLength(simCode))%>
<%createTimeConditionTreatments(timeEventLength(simCode), clockedPartitions)%>
<%varDecls%>
Expand Down
11 changes: 11 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -811,6 +811,17 @@ end SimCodeFunction;

package SimCodeUtil

function absoluteClockIdxForBaseClock
input Integer baseClockIdx;
input list<SimCode.ClockedPartition> allBaseClockPartitions;
output Integer absBaseClockIdx;
end absoluteClockIdxForBaseClock;

function getClockedPartitions
input SimCode.SimCode simcode;
output list<SimCode.ClockedPartition> clockedPartitions;
end getClockedPartitions;

function functionInfo
input SimCodeFunction.Function fn;
output builtin.SourceInfo info;
Expand Down
3 changes: 1 addition & 2 deletions SimulationRuntime/cpp/Include/Core/System/ITime.h
Expand Up @@ -11,8 +11,7 @@ class ITime
virtual ~ITime() {};
virtual int getDimTimeEvent() const = 0;
virtual std::pair<double, double>* getTimeEventData() const = 0;
// gibt die Time events (Startzeit und Frequenz) zurück
virtual void initTimeEventData() = 0;
virtual void initTimeEventData() = 0;// sets the start time and interval length in the timeEventData
virtual void computeTimeEventConditions(double currTime) = 0;
virtual double computeNextTimeEvents(double currTime) = 0;
virtual void resetTimeConditions() = 0;
Expand Down

0 comments on commit f9882c3

Please sign in to comment.