Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit efe296f

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Roll out arrays for FMI model description and Cpp init XML files
Belonging to [master]: - #2679
1 parent 098c4ec commit efe296f

File tree

4 files changed

+108
-8
lines changed

4 files changed

+108
-8
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10372,11 +10372,20 @@ algorithm
1037210372
arrayName = ComponentReference.crefStripLastSubs(name);
1037310373
end if;
1037410374

10375-
if(ComponentReference.crefEqual(arrayName, name)) then
10376-
//print("Array not found\n");
10375+
_ = match iVar
10376+
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) equation
10377+
// store array dimensions and
10378+
// index of first element to indicate a contiguous array
10379+
arrayDimensions = List.map(List.lastN(numArrayElement, listLength(numArrayElement)), stringInt);
10380+
varIndices = arrayCreate(1, varIdx);
10381+
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, (arrayDimensions, varIndices)), tmpVarToArrayIndexMapping);
10382+
then ();
10383+
else equation if (ComponentReference.crefEqual(arrayName, name)) then
10384+
// scalar variable
1037710385
varIndices = arrayCreate(1, varIdx);
1037810386
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, ({1},varIndices)), tmpVarToArrayIndexMapping);
1037910387
else
10388+
// store array dimensions and build up list of indices for elements
1038010389
if(BaseHashTable.hasKey(arrayName, tmpVarToArrayIndexMapping)) then
1038110390
((arrayDimensions,varIndices)) = BaseHashTable.get(arrayName, tmpVarToArrayIndexMapping);
1038210391
else
@@ -10390,7 +10399,8 @@ algorithm
1039010399
//print("VarIndices: " + intString(arrayLength(varIndices)) + " arrayIndex: " + intString(arrayIndex) + " varIndex: " + intString(varIdx) + "\n");
1039110400
varIndices = arrayUpdate(varIndices, arrayIndex, varIdx);
1039210401
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, (arrayDimensions,varIndices)), tmpVarToArrayIndexMapping);
10393-
end if;
10402+
end if; then ();
10403+
end match;
1039410404
then ((tmpCurrentVarIndices, tmpVarToArrayIndexMapping, tmpVarToIndexMapping));
1039510405
else
1039610406
equation
@@ -10430,7 +10440,7 @@ algorithm
1043010440
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NOALIAS()),_,tmpCurrentVarIndices)
1043110441
equation
1043210442
//print("getArrayIdxByVar: Handling common variable\n");
10433-
(varIdx,tmpCurrentVarIndices) = getVarToArrayIndexByType(iVarType, tmpCurrentVarIndices);
10443+
(varIdx,tmpCurrentVarIndices) = getVarToArrayIndexByType(iVar, iVarType, tmpCurrentVarIndices);
1043410444
then varIdx;
1043510445
case(SimCodeVar.SIMVAR(name=name, aliasvar=SimCodeVar.NEGATEDALIAS(varName)),_,_)
1043610446
equation
@@ -10458,13 +10468,21 @@ end getArrayIdxByVar;
1045810468

1045910469
protected function getVarToArrayIndexByType "author: marcusw
1046010470
Return the the current variable index of the given tuple, regarding the given type. The index-tuple is incremented and returned."
10471+
input SimCodeVar.SimVar iVar;
1046110472
input Integer iVarType; //1 = real ; 2 = int ; 3 = bool ; 4 = string
1046210473
output Integer oVarIdx;
1046310474
input output array<Integer> iCurrentVarIndices;
1046410475
algorithm
1046510476
try
1046610477
oVarIdx := arrayGet(iCurrentVarIndices, iVarType);
10467-
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx+1);
10478+
_ := match iVar
10479+
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) algorithm
10480+
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx + getNumElems(iVar));
10481+
then ();
10482+
else algorithm
10483+
arrayUpdate(iCurrentVarIndices, iVarType, oVarIdx + 1);
10484+
then ();
10485+
end match;
1046810486
else
1046910487
Error.addMessage(Error.INTERNAL_ERROR, {"GetVarToArrayIndexByType with unknown type called."});
1047010488
oVarIdx := -1;
@@ -10509,12 +10527,18 @@ protected
1050910527
list<DAE.Subscript> arraySubscripts;
1051010528
list<Integer> arrayDimensions, arrayDimensionsReverse;
1051110529
Boolean toColumnMajor;
10530+
Boolean isContiguous;
1051210531
algorithm
1051310532
arraySubscripts := ComponentReference.crefLastSubs(varName);
1051410533
varName := ComponentReference.crefStripLastSubs(varName);//removeSubscripts(varName);
1051510534
if(BaseHashTable.hasKey(varName, iVarToArrayIndexMapping)) then
1051610535
((arrayDimensions,varIndices)) := BaseHashTable.get(varName, iVarToArrayIndexMapping); //varIndices are rowMajorOrder!
10517-
arraySize := arrayLength(varIndices);
10536+
isContiguous := arrayLength(varIndices) == 1;
10537+
if isContiguous then
10538+
arraySize := List.fold(arrayDimensions, intMul, 1);
10539+
else
10540+
arraySize := arrayLength(varIndices);
10541+
end if;
1051810542
concreteVarIndex := getUnrolledArrayIndex(arraySubscripts,arrayDimensions);
1051910543
toColumnMajor := iColumnMajor and listLength(arrayDimensions) > 1;
1052010544
if toColumnMajor then
@@ -10528,7 +10552,11 @@ algorithm
1052810552
// convert to row major so that column major access will give this idx
1052910553
idx := convertIndexToColumnMajor(idx, arrayDimensionsReverse);
1053010554
end if;
10531-
idx := arrayGet(varIndices, idx);
10555+
if isContiguous then
10556+
idx := arrayGet(varIndices, 1) + idx - 1;
10557+
else
10558+
idx := arrayGet(varIndices, idx);
10559+
end if;
1053210560
if(intLt(idx, 0)) then
1053310561
tmpVarIndexListNew := intString((intMul(idx, -1) - 1))::tmpVarIndexListNew;
1053410562
//print("SimCodeUtil.tmpVarIndexListNew: Warning, negativ aliases (" + ComponentReference.printComponentRefStr(iVarName) + ") are not supported at the moment!\n");
@@ -12211,6 +12239,54 @@ algorithm
1221112239
end match;
1221212240
end getNumElems;
1221312241

12242+
public
12243+
function getScalarElements
12244+
"Get scalar elements of an array in row major order. This is
12245+
needed by templates for XML files that only support scalar variables.
12246+
author: rfranke"
12247+
input SimCodeVar.SimVar var;
12248+
output list<SimCodeVar.SimVar> elts;
12249+
protected
12250+
list<Integer> dims;
12251+
SimCodeVar.SimVar elt;
12252+
list<DAE.Subscript> subs;
12253+
algorithm
12254+
elts := match var
12255+
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) algorithm
12256+
dims := List.map(List.lastN(var.numArrayElement, listLength(var.numArrayElement)), stringInt);
12257+
elt := var;
12258+
elt.type_ := Types.arrayElementType(var.type_);
12259+
subs := {};
12260+
elts := {};
12261+
then fillScalarElements(elt, dims, 1, subs, elts);
12262+
else then {var};
12263+
end match;
12264+
end getScalarElements;
12265+
12266+
protected
12267+
function fillScalarElements
12268+
"Helper to getScalarElements.
12269+
author: rfranke"
12270+
input SimCodeVar.SimVar eltIn;
12271+
input list<Integer> dims;
12272+
input Integer dimIdx;
12273+
input list<DAE.Subscript> subsIn;
12274+
input output list<SimCodeVar.SimVar> elts;
12275+
protected
12276+
SimCodeVar.SimVar elt = eltIn;
12277+
list<DAE.Subscript> subs;
12278+
algorithm
12279+
for i in listGet(dims, dimIdx):-1:1 loop
12280+
subs := DAE.INDEX(DAE.ICONST(i))::subsIn;
12281+
if dimIdx < listLength(dims) then
12282+
elts := fillScalarElements(eltIn, dims, dimIdx + 1, subs, elts);
12283+
else
12284+
elt.name := ComponentReference.crefSetLastSubs(elt.name, listReverse(subs));
12285+
elts := elt::elts;
12286+
end if;
12287+
end for;
12288+
end fillScalarElements;
12289+
1221412290
public function getVariableIndex
1221512291
input SimCodeVar.SimVar inVar;
1221612292
output Integer outVariableIndex;

Compiler/Template/CodegenCppInit.tpl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ template scalarVariableXML(SimCode simCode, SimVar simVar, HashTableCrIListArray
118118
"Generates code for ScalarVariable file for FMU target."
119119
::=
120120
match simVar
121+
case SIMVAR(type_ = T_ARRAY()) then
122+
/* call ScalarVariableType for array to update complexStartExpressions */
123+
let _ = if not generateFMUModelDescription then
124+
ScalarVariableType(simCode, name, aliasvar, unit, displayUnit, minValue, maxValue, initialValue, nominalValue, isFixed, Types.arrayElementType(type_), complexStartExpressions, stateDerVectorName)
125+
/* roll out array as XML file only supports scalars */
126+
let &complexStartExpressionsForScalarsUnused = buffer ""
127+
'<%getScalarElements(simVar) |> var =>
128+
scalarVariableXML(simCode, var, varToArrayIndexMapping, indexForUndefinedReferences, generateFMUModelDescription, complexStartExpressionsForScalarsUnused, stateDerVectorName)
129+
;separator="\n"%>'
121130
case SIMVAR(__) then
122131
let variableCode = if generateFMUModelDescription then CodegenFMUCommon.ScalarVariableType(simVar) else
123132
ScalarVariableType(simCode, name, aliasvar, unit, displayUnit, minValue, maxValue, initialValue, nominalValue, isFixed, type_, complexStartExpressions, stateDerVectorName)
@@ -168,7 +177,8 @@ template ScalarVariableType(SimCode simCode, DAE.ComponentRef simVarCref, AliasV
168177
end ScalarVariableType;
169178

170179
template ScalarVariableTypeStartAttribute(SimCode simCode, DAE.ComponentRef simVarCref, AliasVariable simVarAlias, Option<DAE.Exp> startValue, Text type, Text& complexStartExpressions, Text stateDerVectorName)
171-
"generates code for start attribute"
180+
"generates code for start attribute,
181+
adds non-constant start values to complexStartExpressions"
172182
::=
173183
match startValue
174184
case SOME(exp) then

Compiler/Template/CodegenFMUCommon.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ template ScalarVariable(SimVar simVar, SimCode simCode, list<SimVar> stateVars,
149149
"Generates code for ScalarVariable file for FMU target."
150150
::=
151151
match simVar
152+
case SIMVAR(type_ = T_ARRAY()) then
153+
/* roll out array as XML file only supports scalars */
154+
'<%getScalarElements(simVar) |> var =>
155+
ScalarVariable(var, simCode, stateVars, FMUVersion)
156+
;separator="\n"%>'
152157
case SIMVAR(__) then
153158
if stringEq(crefStr(name),"$dummy") then
154159
<<>>

Compiler/Template/SimCodeTV.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,11 @@ package SimCodeUtil
910910
output Integer outVariableIndex;
911911
end getStateSimVarIndexFromIndex;
912912

913+
function getScalarElements
914+
input SimCodeVar.SimVar var;
915+
output list<SimCodeVar.SimVar> elts;
916+
end getScalarElements;
917+
913918
function getVariableIndex
914919
input SimCodeVar.SimVar inVar;
915920
output Integer outVariableIndex;
@@ -3666,6 +3671,10 @@ package DAEUtil
36663671
end DAEUtil;
36673672

36683673
package Types
3674+
function arrayElementType
3675+
input DAE.Type inType;
3676+
output DAE.Type outType;
3677+
end arrayElementType;
36693678
function getDimensionSizes
36703679
input DAE.Type inType;
36713680
output list<Integer> outIntegerLst;

0 commit comments

Comments
 (0)