@@ -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
1045910469protected 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;
1046410475algorithm
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;
1051210531algorithm
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;
1221212240end 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+
1221412290public function getVariableIndex
1221512291 input SimCodeVar.SimVar inVar;
1221612292 output Integer outVariableIndex;
0 commit comments