Skip to content

Commit

Permalink
Roll out arrays for FMI model description and Cpp init XML files
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2679
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed Oct 4, 2018
1 parent 098c4ec commit efe296f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
90 changes: 83 additions & 7 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -10372,11 +10372,20 @@ algorithm
arrayName = ComponentReference.crefStripLastSubs(name);
end if;

if(ComponentReference.crefEqual(arrayName, name)) then
//print("Array not found\n");
_ = match iVar
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) equation
// store array dimensions and
// index of first element to indicate a contiguous array
arrayDimensions = List.map(List.lastN(numArrayElement, listLength(numArrayElement)), stringInt);
varIndices = arrayCreate(1, varIdx);
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, (arrayDimensions, varIndices)), tmpVarToArrayIndexMapping);
then ();
else equation if (ComponentReference.crefEqual(arrayName, name)) then
// scalar variable
varIndices = arrayCreate(1, varIdx);
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, ({1},varIndices)), tmpVarToArrayIndexMapping);
else
// store array dimensions and build up list of indices for elements
if(BaseHashTable.hasKey(arrayName, tmpVarToArrayIndexMapping)) then
((arrayDimensions,varIndices)) = BaseHashTable.get(arrayName, tmpVarToArrayIndexMapping);
else
Expand All @@ -10390,7 +10399,8 @@ algorithm
//print("VarIndices: " + intString(arrayLength(varIndices)) + " arrayIndex: " + intString(arrayIndex) + " varIndex: " + intString(varIdx) + "\n");
varIndices = arrayUpdate(varIndices, arrayIndex, varIdx);
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, (arrayDimensions,varIndices)), tmpVarToArrayIndexMapping);
end if;
end if; then ();
end match;
then ((tmpCurrentVarIndices, tmpVarToArrayIndexMapping, tmpVarToIndexMapping));
else
equation
Expand Down Expand Up @@ -10430,7 +10440,7 @@ algorithm
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NOALIAS()),_,tmpCurrentVarIndices)
equation
//print("getArrayIdxByVar: Handling common variable\n");
(varIdx,tmpCurrentVarIndices) = getVarToArrayIndexByType(iVarType, tmpCurrentVarIndices);
(varIdx,tmpCurrentVarIndices) = getVarToArrayIndexByType(iVar, iVarType, tmpCurrentVarIndices);
then varIdx;
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NEGATEDALIAS(varName)),_,_)
equation
Expand Down Expand Up @@ -10458,13 +10468,21 @@ end getArrayIdxByVar;

protected function getVarToArrayIndexByType "author: marcusw
Return the the current variable index of the given tuple, regarding the given type. The index-tuple is incremented and returned."
input SimCodeVar.SimVar iVar;
input Integer iVarType; //1 = real ; 2 = int ; 3 = bool ; 4 = string
output Integer oVarIdx;
input output array<Integer> iCurrentVarIndices;
algorithm
try
oVarIdx := arrayGet(iCurrentVarIndices, iVarType);
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx+1);
_ := match iVar
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) algorithm
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx + getNumElems(iVar));
then ();
else algorithm
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx + 1);
then ();
end match;
else
Error.addMessage(Error.INTERNAL_ERROR, {"GetVarToArrayIndexByType with unknown type called."});
oVarIdx := -1;
Expand Down Expand Up @@ -10509,12 +10527,18 @@ protected
list<DAE.Subscript> arraySubscripts;
list<Integer> arrayDimensions, arrayDimensionsReverse;
Boolean toColumnMajor;
Boolean isContiguous;
algorithm
arraySubscripts := ComponentReference.crefLastSubs(varName);
varName := ComponentReference.crefStripLastSubs(varName);//removeSubscripts(varName);
if(BaseHashTable.hasKey(varName, iVarToArrayIndexMapping)) then
((arrayDimensions,varIndices)) := BaseHashTable.get(varName, iVarToArrayIndexMapping); //varIndices are rowMajorOrder!
arraySize := arrayLength(varIndices);
isContiguous := arrayLength(varIndices) == 1;
if isContiguous then
arraySize := List.fold(arrayDimensions, intMul, 1);
else
arraySize := arrayLength(varIndices);
end if;
concreteVarIndex := getUnrolledArrayIndex(arraySubscripts,arrayDimensions);
toColumnMajor := iColumnMajor and listLength(arrayDimensions) > 1;
if toColumnMajor then
Expand All @@ -10528,7 +10552,11 @@ algorithm
// convert to row major so that column major access will give this idx
idx := convertIndexToColumnMajor(idx, arrayDimensionsReverse);
end if;
idx := arrayGet(varIndices, idx);
if isContiguous then
idx := arrayGet(varIndices, 1) + idx - 1;
else
idx := arrayGet(varIndices, idx);
end if;
if(intLt(idx, 0)) then
tmpVarIndexListNew := intString((intMul(idx, -1) - 1))::tmpVarIndexListNew;
//print("SimCodeUtil.tmpVarIndexListNew: Warning, negativ aliases (" + ComponentReference.printComponentRefStr(iVarName) + ") are not supported at the moment!\n");
Expand Down Expand Up @@ -12211,6 +12239,54 @@ algorithm
end match;
end getNumElems;

public
function getScalarElements
"Get scalar elements of an array in row major order. This is
needed by templates for XML files that only support scalar variables.
author: rfranke"
input SimCodeVar.SimVar var;
output list<SimCodeVar.SimVar> elts;
protected
list<Integer> dims;
SimCodeVar.SimVar elt;
list<DAE.Subscript> subs;
algorithm
elts := match var
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) algorithm
dims := List.map(List.lastN(var.numArrayElement, listLength(var.numArrayElement)), stringInt);
elt := var;
elt.type_ := Types.arrayElementType(var.type_);
subs := {};
elts := {};
then fillScalarElements(elt, dims, 1, subs, elts);
else then {var};
end match;
end getScalarElements;

protected
function fillScalarElements
"Helper to getScalarElements.
author: rfranke"
input SimCodeVar.SimVar eltIn;
input list<Integer> dims;
input Integer dimIdx;
input list<DAE.Subscript> subsIn;
input output list<SimCodeVar.SimVar> elts;
protected
SimCodeVar.SimVar elt = eltIn;
list<DAE.Subscript> subs;
algorithm
for i in listGet(dims, dimIdx):-1:1 loop
subs := DAE.INDEX(DAE.ICONST(i))::subsIn;
if dimIdx < listLength(dims) then
elts := fillScalarElements(eltIn, dims, dimIdx + 1, subs, elts);
else
elt.name := ComponentReference.crefSetLastSubs(elt.name, listReverse(subs));
elts := elt::elts;
end if;
end for;
end fillScalarElements;

public function getVariableIndex
input SimCodeVar.SimVar inVar;
output Integer outVariableIndex;
Expand Down
12 changes: 11 additions & 1 deletion Compiler/Template/CodegenCppInit.tpl
Expand Up @@ -118,6 +118,15 @@ template scalarVariableXML(SimCode simCode, SimVar simVar, HashTableCrIListArray
"Generates code for ScalarVariable file for FMU target."
::=
match simVar
case SIMVAR(type_ = T_ARRAY()) then
/* call ScalarVariableType for array to update complexStartExpressions */
let _ = if not generateFMUModelDescription then
ScalarVariableType(simCode, name, aliasvar, unit, displayUnit, minValue, maxValue, initialValue, nominalValue, isFixed, Types.arrayElementType(type_), complexStartExpressions, stateDerVectorName)
/* roll out array as XML file only supports scalars */
let &complexStartExpressionsForScalarsUnused = buffer ""
'<%getScalarElements(simVar) |> var =>
scalarVariableXML(simCode, var, varToArrayIndexMapping, indexForUndefinedReferences, generateFMUModelDescription, complexStartExpressionsForScalarsUnused, stateDerVectorName)
;separator="\n"%>'
case SIMVAR(__) then
let variableCode = if generateFMUModelDescription then CodegenFMUCommon.ScalarVariableType(simVar) else
ScalarVariableType(simCode, name, aliasvar, unit, displayUnit, minValue, maxValue, initialValue, nominalValue, isFixed, type_, complexStartExpressions, stateDerVectorName)
Expand Down Expand Up @@ -168,7 +177,8 @@ template ScalarVariableType(SimCode simCode, DAE.ComponentRef simVarCref, AliasV
end ScalarVariableType;

template ScalarVariableTypeStartAttribute(SimCode simCode, DAE.ComponentRef simVarCref, AliasVariable simVarAlias, Option<DAE.Exp> startValue, Text type, Text& complexStartExpressions, Text stateDerVectorName)
"generates code for start attribute"
"generates code for start attribute,
adds non-constant start values to complexStartExpressions"
::=
match startValue
case SOME(exp) then
Expand Down
5 changes: 5 additions & 0 deletions Compiler/Template/CodegenFMUCommon.tpl
Expand Up @@ -149,6 +149,11 @@ template ScalarVariable(SimVar simVar, SimCode simCode, list<SimVar> stateVars,
"Generates code for ScalarVariable file for FMU target."
::=
match simVar
case SIMVAR(type_ = T_ARRAY()) then
/* roll out array as XML file only supports scalars */
'<%getScalarElements(simVar) |> var =>
ScalarVariable(var, simCode, stateVars, FMUVersion)
;separator="\n"%>'
case SIMVAR(__) then
if stringEq(crefStr(name),"$dummy") then
<<>>
Expand Down
9 changes: 9 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -910,6 +910,11 @@ package SimCodeUtil
output Integer outVariableIndex;
end getStateSimVarIndexFromIndex;

function getScalarElements
input SimCodeVar.SimVar var;
output list<SimCodeVar.SimVar> elts;
end getScalarElements;

function getVariableIndex
input SimCodeVar.SimVar inVar;
output Integer outVariableIndex;
Expand Down Expand Up @@ -3666,6 +3671,10 @@ package DAEUtil
end DAEUtil;

package Types
function arrayElementType
input DAE.Type inType;
output DAE.Type outType;
end arrayElementType;
function getDimensionSizes
input DAE.Type inType;
output list<Integer> outIntegerLst;
Expand Down

0 comments on commit efe296f

Please sign in to comment.