diff --git a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo index 5bc6ef5f13b..5d4409b896e 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo @@ -847,8 +847,8 @@ public unique_dependencies := list(ComponentRef.simplifySubscripts(dep) for dep in dependencies); unique_dependencies := UnorderedSet.unique_list(unique_dependencies, ComponentRef.hash, ComponentRef.isEqual); if Flags.isSet(Flags.BLT_MATRIX_DUMP) then - print("Finding dependencies for:\n" + Equation.toString(eqn) + "\n"); - print("dependencies: " + List.toString(unique_dependencies, ComponentRef.toString) + "\n\n"); + print("\nFinding dependencies for:\n" + Equation.toString(eqn) + "\n"); + print("dependencies: " + List.toString(unique_dependencies, ComponentRef.toString) + "\n"); end if; () := match eqn local @@ -856,59 +856,53 @@ public case Equation.FOR_EQUATION() algorithm // get expanded matrix rows - fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, - function Slice.getDependentCrefIndicesPseudoFor(iter = eqn.iter)); + fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, function Slice.getDependentCrefIndicesPseudoFor(iter = eqn.iter)); then (); case Equation.ARRAY_EQUATION() algorithm - fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); + fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); then (); case Equation.RECORD_EQUATION() algorithm - fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); + fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); then (); case Equation.ALGORITHM() algorithm (eqn_scal_idx, eqn_size) := mapping.eqn_AtS[eqn_arr_idx]; row := Slice.getDependentCrefIndicesPseudoScalar(unique_dependencies, map, mapping); - for i in 0:eqn_size-1 loop - arrayUpdate(m, eqn_scal_idx+i, listAppend(row, m[eqn_scal_idx+i])); + for i in 1:eqn_size loop + updateIntegerRow(m, eqn_scal_idx+(i-1), row); end for; then (); case Equation.IF_EQUATION() algorithm - fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); + fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); then (); case Equation.WHEN_EQUATION() algorithm - fillMatrixArray(unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); + fillMatrixArray(eqn, unique_dependencies, map, mapping, eqn_arr_idx, m, modes, Slice.getDependentCrefIndicesPseudoArray); then (); else algorithm (eqn_scal_idx, _) := mapping.eqn_AtS[eqn_arr_idx]; row := Slice.getDependentCrefIndicesPseudoScalar(unique_dependencies, map, mapping); - arrayUpdate(m, eqn_scal_idx, listAppend(row, m[eqn_scal_idx])); + updateIntegerRow(m, eqn_scal_idx, row); then (); end match; end fillMatrix; function fillMatrixArray + "adds multiple rows to the adjacency matrix at once. + used for equations with size > 1" + input Equation eqn "only for debug purposes"; input list unique_dependencies; input UnorderedMap map; input Adjacency.Mapping mapping; input Integer eqn_arr_idx; input array> m; input CausalizeModes modes; - input getDependentCrefIndices func; + input Slice.getDependentCrefIndices func; protected - partial function getDependentCrefIndices - input list dependencies; - input UnorderedMap map; - input Adjacency.Mapping mapping; - input Integer eqn_arr_idx; - output array> m_part; - output array> mode_to_var_part; - end getDependentCrefIndices; Integer eqn_scal_idx, eqn_size; array> m_part; array> mode_to_var_part; @@ -916,8 +910,13 @@ public (eqn_scal_idx, eqn_size) := mapping.eqn_AtS[eqn_arr_idx]; (m_part, mode_to_var_part) := func(unique_dependencies, map, mapping, eqn_arr_idx); // check for arrayLength(m_part) == eqn_size ? + if not arrayLength(m_part) == eqn_size then + Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because equation size " + intString(eqn_size) + + " differs from adjacency matrix row size " + intString(arrayLength(m_part)) + " for equation:\n" + Equation.toString(eqn)}); + fail(); + end if; // add matrix rows to correct locations and update causalize modes - expandRows(m, eqn_scal_idx, m_part); + copyRows(m, eqn_scal_idx, m_part); if eqn_size > 1 then CausalizeModes.update(modes, eqn_scal_idx, eqn_arr_idx, mode_to_var_part, unique_dependencies); end if; @@ -998,15 +997,26 @@ public end if; end maxDimTraverse; - function expandRows + function copyRows input array> m; input Integer eqn_scal_idx; input array> m_part; algorithm for i in 1:arrayLength(m_part) loop - arrayUpdate(m, eqn_scal_idx+(i-1), listAppend(m_part[i], m[eqn_scal_idx+(i-1)])); + updateIntegerRow(m, eqn_scal_idx+(i-1), m_part[i]); end for; - end expandRows; + end copyRows; + + function updateIntegerRow + input array> m; + input Integer idx; + input list row; + algorithm + arrayUpdate(m, idx, listAppend(row, m[idx])); + if Flags.isSet(Flags.BLT_MATRIX_DUMP) then + print("Adding to row " + intString(idx) + " " + List.toString(row, intString) + "\n"); + end if; + end updateIntegerRow; end Matrix; annotation(__OpenModelica_Interface="backend"); diff --git a/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo b/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo index 79f71eb72ee..4e3638e9575 100644 --- a/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo +++ b/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo @@ -201,6 +201,15 @@ public input UnorderedSet acc; end filterCref; + partial function getDependentCrefIndices + input list dependencies "dependent var crefs"; + input UnorderedMap map "unordered map to check for relevance"; + input Mapping mapping "array <-> scalar index mapping"; + input Integer eqn_arr_idx; + output array> indices; + output array> mode_to_var; + end getDependentCrefIndices; + // ############################################################ // cref accumulation Functions // use with: @@ -334,12 +343,7 @@ public function getDependentCrefIndicesPseudoArray "[Adjacency.MatrixType.PSEUDO] Array equations. Turns cref dependencies into index lists, used for adjacency." - input list dependencies "dependent var crefs"; - input UnorderedMap map "unordered map to check for relevance"; - input Mapping mapping "array <-> scalar index mapping"; - input Integer eqn_arr_idx; - output array> indices; - output array> mode_to_var; + extends getDependentCrefIndices; protected ComponentRef stripped; Integer eqn_start, eqn_size, var_arr_idx, var_scal_idx, mode = 1; @@ -397,13 +401,8 @@ public function getDependentCrefIndicesPseudoFor "[Adjacency.MatrixType.PSEUDO] For-Loop equations. Turns cref dependencies into index lists, used for adjacency." - input list dependencies "dependent var crefs"; - input UnorderedMap map "unordered map to check for relevance"; - input Mapping mapping "array <-> scalar index mapping"; + extends getDependentCrefIndices; input Iterator iter "iterator frames"; - input Integer eqn_arr_idx; - output array> indices; - output array> mode_to_var; protected list names; list ranges; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo b/OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo index 99f20fe3205..6478ffbdbc0 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo @@ -746,7 +746,7 @@ public "Returns all subscripts of a cref as a flat list in the correct order. Ex: a[1, 2].b[4].c[6, 3] => {1, 2, 4, 6, 3}" input ComponentRef cref; - output list subscripts = List.flattenReverse(subscriptsAllWithWhole(cref)); + output list subscripts = List.flatten(subscriptsAllWithWhole(cref)); end subscriptsAllWithWholeFlat; function subscriptsAll