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

Commit dd5a6fd

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Treat variable index for array elements in FMI model description
Belonging to [master]: - #2679
1 parent efe296f commit dd5a6fd

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7979,7 +7979,7 @@ protected function setVariableIndexHelper2
79797979
input SimCodeVar.SimVar inVar;
79807980
input Integer inIndex;
79817981
output SimCodeVar.SimVar outVar = inVar;
7982-
output Integer outIndex = inIndex + 1;
7982+
output Integer outIndex = inIndex + getNumElems(inVar);
79837983
algorithm
79847984
outVar.variable_index := SOME(inIndex);
79857985
end setVariableIndexHelper2;
@@ -10395,7 +10395,7 @@ algorithm
1039510395
varIndices = arrayCreate(List.fold(arrayDimensions, intMul, 1), 0);
1039610396
end if;
1039710397
//print("Num of array elements {" + stringDelimitList(List.map(arrayDimensions, intString), ",") + "} : " + intString(listLength(arraySubscripts)) + " arraySubs "+ExpressionDump.printSubscriptLstStr(arraySubscripts) + " arrayDimensions[ "+stringDelimitList(List.map(arrayDimensions,intString),",")+"]\n");
10398-
arrayIndex = getUnrolledArrayIndex(arraySubscripts,arrayDimensions);
10398+
arrayIndex = getScalarElementIndex(arraySubscripts, arrayDimensions);
1039910399
//print("VarIndices: " + intString(arrayLength(varIndices)) + " arrayIndex: " + intString(arrayIndex) + " varIndex: " + intString(varIdx) + "\n");
1040010400
varIndices = arrayUpdate(varIndices, arrayIndex, varIdx);
1040110401
tmpVarToArrayIndexMapping = BaseHashTable.add((arrayName, (arrayDimensions,varIndices)), tmpVarToArrayIndexMapping);
@@ -10539,7 +10539,7 @@ algorithm
1053910539
else
1054010540
arraySize := arrayLength(varIndices);
1054110541
end if;
10542-
concreteVarIndex := getUnrolledArrayIndex(arraySubscripts,arrayDimensions);
10542+
concreteVarIndex := getScalarElementIndex(arraySubscripts, arrayDimensions);
1054310543
toColumnMajor := iColumnMajor and listLength(arrayDimensions) > 1;
1054410544
if toColumnMajor then
1054510545
concreteVarIndex := convertIndexToColumnMajor(concreteVarIndex, arrayDimensions);
@@ -10652,7 +10652,7 @@ algorithm
1065210652
oIsConsecutive := consecutive;
1065310653
end isVarIndexListConsecutive;
1065410654

10655-
protected function getUnrolledArrayIndex
10655+
protected function getScalarElementIndex
1065610656
"Calculate the one based memory offset for consecutive row major storage,
1065710657
author: rfranke"
1065810658
input list<DAE.Subscript> arraySubscripts;
@@ -10668,7 +10668,7 @@ algorithm
1066810668
arrayIndex := arrayIndex + (idx - 1) * fac;
1066910669
fac := fac * listGet(arrayDimensions, i);
1067010670
end for;
10671-
end getUnrolledArrayIndex;
10671+
end getScalarElementIndex;
1067210672

1067310673
public function createIdxSCVarMapping "author: marcusw
1067410674
Create a mapping from the SCVar-Index (array-Index) to the SCVariable, as it is used in the c-runtime."
@@ -12218,7 +12218,7 @@ function getNumScalars
1221812218
input list<SimCodeVar.SimVar> vars;
1221912219
output Integer numScalars;
1222012220
algorithm
12221-
numScalars := List.fold(List.map(vars, getNumElems), intAdd, 0);
12221+
numScalars := List.applyAndFold(vars, intAdd, getNumElems, 0);
1222212222
end getNumScalars;
1222312223

1222412224
protected
@@ -12250,7 +12250,9 @@ protected
1225012250
list<Integer> dims;
1225112251
SimCodeVar.SimVar elt;
1225212252
list<DAE.Subscript> subs;
12253+
Integer index = 0;
1225312254
algorithm
12255+
// create list of elements
1225412256
elts := match var
1225512257
case SimCodeVar.SIMVAR(type_ = DAE.T_ARRAY()) algorithm
1225612258
dims := List.map(List.lastN(var.numArrayElement, listLength(var.numArrayElement)), stringInt);
@@ -12261,11 +12263,17 @@ algorithm
1226112263
then fillScalarElements(elt, dims, 1, subs, elts);
1226212264
else then {var};
1226312265
end match;
12266+
// set variable indices
12267+
(elts, index) := match var
12268+
case SimCodeVar.SIMVAR(variable_index = SOME(index))
12269+
then setVariableIndexHelper(elts, index);
12270+
else (elts, index);
12271+
end match;
1226412272
end getScalarElements;
1226512273

1226612274
protected
1226712275
function fillScalarElements
12268-
"Helper to getScalarElements.
12276+
"Helper for getScalarElements, called recursively for each dimension.
1226912277
author: rfranke"
1227012278
input SimCodeVar.SimVar eltIn;
1227112279
input list<Integer> dims;
@@ -12277,12 +12285,25 @@ protected
1227712285
list<DAE.Subscript> subs;
1227812286
algorithm
1227912287
for i in listGet(dims, dimIdx):-1:1 loop
12280-
subs := DAE.INDEX(DAE.ICONST(i))::subsIn;
12288+
subs := DAE.INDEX(DAE.ICONST(i)) :: subsIn;
1228112289
if dimIdx < listLength(dims) then
1228212290
elts := fillScalarElements(eltIn, dims, dimIdx + 1, subs, elts);
1228312291
else
12284-
elt.name := ComponentReference.crefSetLastSubs(elt.name, listReverse(subs));
12285-
elts := elt::elts;
12292+
// add subscripts to array element
12293+
subs := listReverse(subs);
12294+
elt.name := ComponentReference.crefSetLastSubs(elt.name, subs);
12295+
// add subscripts to previousName
12296+
_ := match elt
12297+
local
12298+
DAE.ComponentRef cref;
12299+
Boolean fixed;
12300+
case SimCodeVar.SIMVAR(varKind = BackendDAE.CLOCKED_STATE(previousName = cref, isStartFixed = fixed))
12301+
algorithm
12302+
elt.varKind := BackendDAE.CLOCKED_STATE(ComponentReference.crefSetLastSubs(cref, subs), fixed);
12303+
then ();
12304+
else ();
12305+
end match;
12306+
elts := elt :: elts;
1228612307
end if;
1228712308
end for;
1228812309
end fillScalarElements;
@@ -12726,11 +12747,23 @@ algorithm
1272612747
SimCodeVar.SimVar sv;
1272712748
SimCode.HashTableCrefToSimVar crefToSimVarHT;
1272812749
String errstr;
12729-
12750+
list<DAE.Subscript> subs;
12751+
Integer index;
1273012752
case (cref, SimCode.SIMCODE(crefToSimVarHT = crefToSimVarHT) )
12731-
equation
12732-
sv = BaseHashTable.get(cref, crefToSimVarHT);
12733-
sv = match sv.aliasvar
12753+
algorithm
12754+
if BaseHashTable.hasKey(cref, crefToSimVarHT) then
12755+
sv := BaseHashTable.get(cref, crefToSimVarHT);
12756+
else
12757+
// lookup array variable and add offset for array element
12758+
sv := BaseHashTable.get(ComponentReference.crefStripLastSubs(cref), crefToSimVarHT);
12759+
subs := ComponentReference.crefLastSubs(cref);
12760+
sv.name := ComponentReference.crefSetLastSubs(sv.name, subs);
12761+
sv.variable_index := match sv.variable_index
12762+
case SOME(index)
12763+
then SOME(index + getScalarElementIndex(subs, List.map(sv.numArrayElement, stringInt)) - 1);
12764+
end match;
12765+
end if;
12766+
sv := match sv.aliasvar
1273412767
case SimCodeVar.NOALIAS() then sv;
1273512768
case SimCodeVar.ALIAS(varName=cref) then cref2simvar(cref, simCode); /* Possibly not needed; can't really hurt that much though */
1273612769
case SimCodeVar.NEGATEDALIAS() then sv;

0 commit comments

Comments
 (0)