Skip to content

Commit

Permalink
populate clockIndex to FMI 2 modelDescription.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed Oct 16, 2015
1 parent 3a9e48e commit fbb8233
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Compiler/SimCode/SimCode.mo
Expand Up @@ -123,6 +123,7 @@ uniontype SimCode
//*** a protected section *** not exported to SimCodeTV
HashTableCrILst.HashTable varToIndexMapping;
HashTableCrefToSimVar crefToSimVarHT "hidden from typeview - used by cref2simvar() for cref -> SIMVAR lookup available in templates.";
HashTable.HashTable crefToClockIndexHT "map variables to clock indices";
Option<BackendMapping> backendMapping;
//FMI 2.0 data for model structure
Option<FmiModelStructure> modelStructure;
Expand Down
59 changes: 59 additions & 0 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -186,6 +186,7 @@ protected
SimCode.HashTableCrefToSimVar crefToSimVarHT;
SimCode.MakefileParams makefileParams;
SimCode.ModelInfo modelInfo;
HashTable.HashTable crefToClockIndexHT;
array<Integer> systemIndexMap;
list<BackendDAE.EqSystem> clockedSysts, contSysts;
//list<BackendDAE.Equation> paramAsserts, remEqLst;
Expand Down Expand Up @@ -384,6 +385,8 @@ algorithm
//BaseHashTable.dumpHashTable(varToArrayIndexMapping);
//print("END MAPPING\n\n");

crefToClockIndexHT := List.fold(inBackendDAE.eqs, collectClockedVars, HashTable.emptyHashTable());

simCode := SimCode.SIMCODE(modelInfo,
{}, // Set by the traversal below...
recordDecls,
Expand Down Expand Up @@ -421,6 +424,7 @@ algorithm
varToArrayIndexMapping,
varToIndexMapping,
crefToSimVarHT,
crefToClockIndexHT,
SOME(backendMapping),
modelStruct);

Expand Down Expand Up @@ -616,6 +620,61 @@ algorithm
end for;
end createClockedSimPartitions;

public function collectClockedVars "author: rfranke
This function collects clocked variables along with their clockIndex"
input BackendDAE.EqSystem inEqSystem;
input HashTable.HashTable inHT;
output HashTable.HashTable outHT;
protected
Integer clockIndex;
algorithm
outHT := match inEqSystem
case BackendDAE.EQSYSTEM(partitionKind = BackendDAE.CLOCKED_PARTITION(subPartIdx=clockIndex)) equation
(outHT, _) = BackendVariable.traverseBackendDAEVars(inEqSystem.orderedVars, collectClockedVars1, (inHT, clockIndex));
then outHT;
else inHT;
end match;
end collectClockedVars;

protected function collectClockedVars1 "author: rfranke
Helper to collectClockedVars"
input BackendDAE.Var inVar;
input tuple<HashTable.HashTable, Integer> inTpl;
output BackendDAE.Var outVar;
output tuple<HashTable.HashTable, Integer> outTpl;
protected
HashTable.HashTable clkHT;
Integer clockIndex;
DAE.ComponentRef cref;
algorithm
(clkHT, clockIndex) := inTpl;
(outVar, outTpl) := match inVar
case BackendDAE.VAR(varName=cref) equation
clkHT = BaseHashTable.add((cref, clockIndex), clkHT);
clkHT = BaseHashTable.add((ComponentReference.crefPrefixPrevious(cref), clockIndex), clkHT);
then (inVar, (clkHT, clockIndex));
else (inVar, inTpl);
end match;
end collectClockedVars1;

public function getClockIndex "author: rfranke
Returns the index of the clock of a variable or zero non-clocked variables"
input SimCodeVar.SimVar simVar;
input SimCode.SimCode simCode;
output Integer clockIndex;
protected
DAE.ComponentRef cref;
HashTable.HashTable clkHT;
algorithm
cref := getSimVarCompRef(simVar);
clockIndex := match simCode
case SimCode.SIMCODE(crefToClockIndexHT=clkHT) then
if BaseHashTable.hasKey(cref, clkHT)
then BaseHashTable.get(cref, clkHT)
else 0;
end match;
end getClockIndex;

protected function getSimVarCompRef
input SimCodeVar.SimVar inVar;
output DAE.ComponentRef outComp;
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Template/CodegenFMUCommon.tpl
Expand Up @@ -446,10 +446,12 @@ match simVar
let variability = getVariability2(varKind, type_)
let caus = getCausality2(causality, varKind, isValueChangeable)
let initial = getInitialType2(variability, caus, initialValue)
let clockIndex = getClockIndex(simVar, simCode)
let previous = match varKind case CLOCKED_STATE(__) then '<%getVariableIndex(cref2simvar(previousName, simCode))%>'
<<
name="<%System.stringReplace(crefStrNoUnderscore(name),"$", "_D_")%>"
valueReference="<%valueReference%>"
<%if boolNot(stringEq(clockIndex, "0")) then 'clockIndex="'+clockIndex+'"' %>
<%if boolNot(stringEq(previous, "")) then 'previous="'+previous+'"' %>
<%description%>
variability="<%variability%>"
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -839,6 +839,12 @@ package SimCodeUtil
output list<SimCode.SubPartition> outSubPartitions;
end getSubPartitions;

function getClockIndex
input SimCodeVar.SimVar simVar;
input SimCode.SimCode simCode;
output Integer clockIndex;
end getClockIndex;

function computeDependencies
input list<SimCode.SimEqSystem> eqs;
input DAE.ComponentRef cref;
Expand Down

0 comments on commit fbb8233

Please sign in to comment.