diff --git a/OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake b/OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake index b7e2b2534d8..7d9e403d31f 100644 --- a/OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake +++ b/OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake @@ -304,7 +304,6 @@ set(OMC_MM_BACKEND_SOURCES # "NBackend Util"; ${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBBackendUtil.mo ${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBDifferentiate.mo - ${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBGraphUtil.mo ${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBReplacements.mo ${CMAKE_CURRENT_SOURCE_DIR}/NBackEnd/Util/NBSlice.mo diff --git a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo index 1c1aa23634b..3bbabe29b12 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo @@ -71,12 +71,9 @@ protected import SBMultiInterval; import SBPWLinearMap; import SBSet; - import NBGraphUtil.{SetVertex, SetEdge}; public - type MatrixType = enumeration(ARRAY, PSEUDO); type MatrixStrictness = enumeration(LINEAR, SOLVABLE, FULL); - type BipartiteGraph = BipartiteIncidenceList; uniontype Mapping record MAPPING @@ -421,15 +418,6 @@ public Mapping mapping; end FULL; - record ARRAY_ADJACENCY_MATRIX - "no transposed set matrix needed since the graph represents all vertices equally" - BipartiteGraph graph "set based graph"; - UnorderedMap vertexMap "map to get the vertex index"; - UnorderedMap edgeMap "map to get the edge index"; - MatrixStrictness st "strictness with which it was created"; - /* Maybe add optional markings here */ - end ARRAY_ADJACENCY_MATRIX; - record PSEUDO_ARRAY_ADJACENCY_MATRIX // ToDo: add optional solvability map for tearing array> m "eqn -> list"; array> mT "var -> list"; @@ -439,26 +427,18 @@ public end PSEUDO_ARRAY_ADJACENCY_MATRIX; record EMPTY_ADJACENCY_MATRIX - MatrixType ty; MatrixStrictness st; end EMPTY_ADJACENCY_MATRIX; function create input VariablePointers vars; input EquationPointers eqns; - input MatrixType ty; input MatrixStrictness st = MatrixStrictness.FULL; output Matrix adj; input output Option funcTree = NONE() "only needed for LINEAR without existing derivatives"; algorithm try - (adj, funcTree) := match ty - case MatrixType.ARRAY then createArray(vars, eqns, st, funcTree); - case MatrixType.PSEUDO then createPseudo(vars, eqns, st, funcTree); - else algorithm - Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of unknown adjacency matrix type."}); - then fail(); - end match; + (adj, funcTree) := createPseudo(vars, eqns, st, funcTree); else Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to create adjacency matrix for system:\n" + VariablePointers.toString(vars, "System") + "\n" @@ -486,8 +466,7 @@ public Mapping mapping; CausalizeModes modes; - case (ARRAY_ADJACENCY_MATRIX(), {-1}) then create(vars, eqns, MatrixType.ARRAY, adj.st); - case (PSEUDO_ARRAY_ADJACENCY_MATRIX(), {-1}) then create(vars, eqns, MatrixType.PSEUDO, adj.st); + case (PSEUDO_ARRAY_ADJACENCY_MATRIX(), {-1}) then create(vars, eqns, adj.st); case (PSEUDO_ARRAY_ADJACENCY_MATRIX(), _) algorithm (m, mT, _) := updatePseudo(adj.m, adj.st, adj.mapping, adj.modes, vars, eqns, idx_lst, funcTree); @@ -495,10 +474,6 @@ public adj.mT := mT; then (adj, funcTree); - case (ARRAY_ADJACENCY_MATRIX(), _) algorithm - // ToDo - then (adj, funcTree); - else algorithm Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of unknown adjacency matrix type."}); then fail(); @@ -530,14 +505,10 @@ public adj.modes := modes; then (adj, vars, eqns, funcTree); - case ARRAY_ADJACENCY_MATRIX() algorithm - // ToDo - then (adj, vars, eqns, funcTree); - case EMPTY_ADJACENCY_MATRIX() algorithm vars := VariablePointers.addList(new_vars, vars); eqns := EquationPointers.addList(new_eqns, eqns); - (adj, funcTree) := create(vars, eqns, adj.ty, adj.st, funcTree); + (adj, funcTree) := create(vars, eqns, adj.st, funcTree); then (adj, vars, eqns, funcTree); else algorithm @@ -643,8 +614,6 @@ public str := str + Mapping.toString(adj.mapping) + "\n"; then str; - case ARRAY_ADJACENCY_MATRIX() then str + "\n ARRAY NOT YET SUPPORTED \n"; - case PSEUDO_ARRAY_ADJACENCY_MATRIX() algorithm if arrayLength(adj.m) > 0 then str := str + StringUtil.headline_4("Normal Adjacency Matrix (row = equation)"); @@ -817,9 +786,6 @@ public count := match adj case PSEUDO_ARRAY_ADJACENCY_MATRIX() then BackendUtil.countElem(adj.m); case EMPTY_ADJACENCY_MATRIX() then 0; - case ARRAY_ADJACENCY_MATRIX() algorithm - Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because array adjacency matrix is not jet supported."}); - then fail(); else algorithm Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because of unknown matrix type."}); then fail(); @@ -914,7 +880,7 @@ public mapping := Mapping.create(eqns, vars); adj := FULL(equation_names, occurences, dependencies, solvabilities, repetitions, mapping); else - adj := EMPTY_ADJACENCY_MATRIX(MatrixType.PSEUDO, st); + adj := EMPTY_ADJACENCY_MATRIX(st); end if; end createFull; @@ -976,7 +942,7 @@ public adj := PSEUDO_ARRAY_ADJACENCY_MATRIX(m, mT, mapping, modes, st); else - adj := EMPTY_ADJACENCY_MATRIX(MatrixType.PSEUDO, st); + adj := EMPTY_ADJACENCY_MATRIX(st); end if; end createPseudo; @@ -1184,54 +1150,6 @@ public end for; end cleanMatrix; - function createArray - input VariablePointers vars; - input EquationPointers eqns; - input MatrixStrictness st = MatrixStrictness.FULL; - output Matrix adj; - input output Option funcTree = NONE() "only needed for LINEAR without existing derivatives"; - protected - BipartiteIncidenceList graph; - Pointer max_dim = Pointer.create(1); - Vector vCount, eCount; - UnorderedMap vertexMap; - UnorderedMap edgeMap; - algorithm - // reset unique tick index to 0 - BuiltinSystem.tmpTickReset(0); - - // create empty set based graph and map - graph := BipartiteIncidenceList.new(SetVertex.isEqual, SetEdge.isEqual, SetVertex.toString, SetEdge.toString); - vertexMap := UnorderedMap.new(SetVertex.hash, SetVertex.isEqual, VariablePointers.size(vars) + EquationPointers.size(eqns)); - edgeMap := UnorderedMap.new(SetEdge.hash, SetEdge.isEqual, VariablePointers.size(vars) + EquationPointers.size(eqns)); // make better size approx here - - // find maximum number of dimensions - VariablePointers.mapPtr(vars, function maxDimTraverse(max_dim = max_dim)); - EquationPointers.mapRes(eqns, function maxDimTraverse(max_dim = max_dim)); // maybe unnecessary? - vCount := Vector.newFill(Pointer.access(max_dim), 1); - eCount := Vector.newFill(Pointer.access(max_dim), 1); - - // create vertices for variables - VariablePointers.mapPtr(vars, function SetVertex.createTraverse(graph = graph, vCount = vCount, ST = SetType.U, vertexMap = vertexMap)); - - // create vertices for equations and create edges - EquationPointers.map(eqns, function SetEdge.fromEquation( - graph = graph, - vCount = vCount, - eCount = eCount, - map = vars.map, - vertexMap = vertexMap, - edgeMap = edgeMap, - eqn_tpl_opt = NONE() - )); - - if Flags.isSet(Flags.DUMP_SET_BASED_GRAPHS) then - print(BipartiteIncidenceList.toString(graph)); - end if; - - adj := ARRAY_ADJACENCY_MATRIX(graph, vertexMap, edgeMap, st); - end createArray; - function maxDimTraverse input Pointer var_ptr; input Pointer max_dim; diff --git a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo index 185ecae84f2..e07ca3e4323 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo @@ -45,7 +45,6 @@ protected import NFFlatten.{FunctionTree, FunctionTreeImpl}; import InstNode = NFInstNode.InstNode; import Prefixes = NFPrefixes; - import SBGraphUtil = NFSBGraphUtil; import Subscript = NFSubscript; import Type = NFType; import Variable = NFVariable; @@ -71,17 +70,6 @@ protected import StringUtil; import UnorderedSet; - // SetBased Graph imports - import SBGraph.BipartiteIncidenceList; - import SBGraph.VertexDescriptor; - import SBGraph.SetType; - import NBAdjacency.BipartiteGraph; - import SBInterval; - import SBMultiInterval; - import SBPWLinearMap; - import SBSet; - import NBGraphUtil.{SetVertex, SetEdge}; - // ############################################################ // Main Functions // ############################################################ @@ -197,14 +185,13 @@ public function simple input VariablePointers vars; input EquationPointers eqs; - input Adjacency.MatrixType matrixType = NBAdjacency.MatrixType.PSEUDO; output list comps; protected Adjacency.Matrix adj; Matching matching; algorithm // create scalar adjacency matrix for now - adj := Adjacency.Matrix.create(vars, eqs, matrixType); + adj := Adjacency.Matrix.create(vars, eqs); matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj); comps := Sorting.tarjan(adj, matching, vars, eqs); end simple; @@ -217,7 +204,6 @@ public algorithm (func) := match flag case "PFPlusExt" then causalizePseudoArray; - case "SBGraph" then causalizeArray; case "pseudo" then causalizePseudoArray; /* ... New causalize modules have to be added here */ else algorithm @@ -254,7 +240,7 @@ protected // ################################################# variables := VariablePointers.fromList(unfixable); equations := EquationPointers.fromList(initials); - adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.SOLVABLE); + adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixStrictness.SOLVABLE); // do not resolve potential singular systems in Phase I or II! -> regular matching matching := Matching.regular(matching, adj, true, true); @@ -271,7 +257,7 @@ protected (adj, variables, equations) := Adjacency.Matrix.expand(adj, variables, equations, fixable, {}); (matching, adj, variables, equations, funcTree, varData, eqData) := Matching.singular(matching, adj, variables, equations, funcTree, varData, eqData, system.systemType, false, true, false); - adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.FULL); + adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixStrictness.FULL); comps := Sorting.tarjan(adj, matching, variables, equations); then (variables, equations, adj, matching, comps); @@ -281,9 +267,9 @@ protected equations := EquationPointers.compress(system.equations); // create solvable adjacency matrix for matching and full for sorting - adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.SOLVABLE); + adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixStrictness.SOLVABLE); (matching, adj, variables, equations, funcTree, varData, eqData) := Matching.singular(NBMatching.EMPTY_MATCHING, adj, variables, equations, funcTree, varData, eqData, system.systemType, false, true); - adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.FULL); + adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixStrictness.FULL); comps := Sorting.tarjan(adj, matching, variables, equations); then (variables, equations, adj, matching, comps); end match; @@ -295,23 +281,6 @@ protected system.strongComponents := SOME(listArray(comps)); end causalizePseudoArray; - function causalizeArray extends Module.causalizeInterface; - protected - VariablePointers variables; - EquationPointers equations; - Adjacency.Matrix adj; - Matching matching; - list comps; - algorithm - // compress the arrays to remove gaps - variables := VariablePointers.compress(system.unknowns); - equations := EquationPointers.compress(system.equations); - - // create scalar adjacency matrix for now - adj := Adjacency.Matrix.create(variables, equations, NBAdjacency.MatrixType.ARRAY, NBAdjacency.MatrixStrictness.SOLVABLE); - matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj); - end causalizeArray; - function causalizeDAEMode extends Module.causalizeInterface; protected Pointer> acc = Pointer.create({}); diff --git a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo index 49db48315a8..fde54bbd4d4 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo @@ -37,11 +37,6 @@ encapsulated uniontype NBMatching import Matching = NBMatching; import GCExt; - // SetBased Graph imports - import NBAdjacency.BipartiteGraph; - import SBGraph.BipartiteIncidenceList; - import NBGraphUtil.{SetVertex, SetEdge}; - protected // NF import import NFFlatten.FunctionTree; @@ -96,7 +91,7 @@ public protected list> marked_eqns; algorithm - (matching, marked_eqns, _, _, _) := continue_(matching, adj, transposed, clear); + (matching, marked_eqns, _, _) := continue_(matching, adj, transposed, clear); if not partially and not listEmpty(List.flatten(marked_eqns)) then Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because the system is structurally singular."}); fail(); @@ -131,13 +126,12 @@ public protected list> marked_eqns; Option mapping; - Adjacency.MatrixType matrixType; Adjacency.MatrixStrictness matrixStrictness; Boolean changed; algorithm // 1. match the system try - (matching, marked_eqns, mapping, matrixType, matrixStrictness) := continue_(matching, adj, transposed, clear); + (matching, marked_eqns, mapping, matrixStrictness) := continue_(matching, adj, transposed, clear); else Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to match system:\n" + VariablePointers.toString(vars, "system vars") + "\n" @@ -150,20 +144,20 @@ public changed := match systemType case NBSystem.SystemType.INI algorithm // ####### BALANCE INITIALIZATION ####### - (vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.balanceInitialization(vars, eqns, varData, eqData, funcTree, adj, matching, mapping, matrixType); + (vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.balanceInitialization(vars, eqns, varData, eqData, funcTree, adj, matching, mapping); then changed; else algorithm // ####### INDEX REDUCTION ###### // for now no index reduction - (vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.noIndexReduction(vars, eqns, varData, eqData, funcTree, adj, matching, mapping, matrixType); + (vars, eqns, varData, eqData, funcTree, changed) := ResolveSingularities.noIndexReduction(vars, eqns, varData, eqData, funcTree, adj, matching, mapping); then changed; end match; // 3. Recompute adjacency and restart matching if something changed in step 2. if changed then // ToDo: keep more of old information by only updating changed stuff - adj := Adjacency.Matrix.create(vars, eqns, matrixType, matrixStrictness); + adj := Adjacency.Matrix.create(vars, eqns, matrixStrictness); (matching, adj, vars, eqns, funcTree, varData, eqData) := singular(EMPTY_MATCHING, adj, vars, eqns, funcTree, varData, eqData, systemType, false, true); end if; end singular; @@ -175,28 +169,22 @@ public input Boolean clear; output list> marked_eqns; output Option mapping; - output Adjacency.MatrixType matrixType; output Adjacency.MatrixStrictness matrixStrictness; protected array var_to_eqn, eqn_to_var; algorithm // 1. Match the system - (matching, marked_eqns, mapping, matrixType, matrixStrictness) := match adj + (matching, marked_eqns, mapping, matrixStrictness) := match adj // PSEUDO ARRAY case Adjacency.Matrix.PSEUDO_ARRAY_ADJACENCY_MATRIX() algorithm (var_to_eqn, eqn_to_var) := getAssignments(matching, adj.m, adj.mT); (var_to_eqn, eqn_to_var, marked_eqns) := PFPlusExternal(adj.m, var_to_eqn, eqn_to_var, clear); matching := MATCHING(var_to_eqn, eqn_to_var); - then (matching, marked_eqns, SOME(adj.mapping), NBAdjacency.MatrixType.PSEUDO, adj.st); - - // ARRAY - case Adjacency.Matrix.ARRAY_ADJACENCY_MATRIX() algorithm - Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because array matching is not yet supported."}); - then fail(); + then (matching, marked_eqns, SOME(adj.mapping), adj.st); // EMPTY case Adjacency.Matrix.EMPTY_ADJACENCY_MATRIX() - then (EMPTY_MATCHING, {}, NONE(), NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.FULL); + then (EMPTY_MATCHING, {}, NONE(), NBAdjacency.MatrixStrictness.FULL); // FAIL else algorithm diff --git a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBResolveSingularities.mo b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBResolveSingularities.mo index c5be652ecda..73bfa7fd6e5 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBResolveSingularities.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBResolveSingularities.mo @@ -177,7 +177,7 @@ public constraint_ptrs := EquationPointers.fromList(sliced_constraints); // create adjacency matrix and match with transposed matrix to respect variable priority - set_adj := Adjacency.Matrix.create(candidate_ptrs, constraint_ptrs, matrixType, NBAdjacency.MatrixStrictness.LINEAR); + set_adj := Adjacency.Matrix.create(candidate_ptrs, constraint_ptrs, NBAdjacency.MatrixStrictness.LINEAR); set_matching := Matching.regular(NBMatching.EMPTY_MATCHING, set_adj, true, true); if debug then diff --git a/OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo b/OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo index dae73efd795..8862919f517 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBTearing.mo @@ -355,7 +355,7 @@ protected discreteVars := VariablePointers.fromList(disc_vars); eqns := EquationPointers.fromList(disc_eqns); - (adj, SOME(funcTree)) := Adjacency.Matrix.create(discreteVars, eqns, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.LINEAR, SOME(funcTree)); + (adj, SOME(funcTree)) := Adjacency.Matrix.create(discreteVars, eqns, NBAdjacency.MatrixStrictness.LINEAR, SOME(funcTree)); matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj, true, false); (_, _, _, residual_lst) := Matching.getMatches(matching, NONE(), discreteVars, eqns); inner_comps := Sorting.tarjan(adj, matching, discreteVars, eqns); @@ -458,7 +458,7 @@ protected discreteVars := VariablePointers.fromList(disc_lst); eqns := EquationPointers.fromList(equations); - (adj, SOME(funcTree)) := Adjacency.Matrix.create(discreteVars, eqns, NBAdjacency.MatrixType.PSEUDO, NBAdjacency.MatrixStrictness.LINEAR, SOME(funcTree)); + (adj, SOME(funcTree)) := Adjacency.Matrix.create(discreteVars, eqns, NBAdjacency.MatrixStrictness.LINEAR, SOME(funcTree)); matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj, true, false); (_, _, _, residual_lst) := Matching.getMatches(matching, NONE(), discreteVars, eqns); inner_comps := Sorting.tarjan(adj, matching, discreteVars, eqns); diff --git a/OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo b/OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo index 76b23bd3614..49ea811fb65 100644 --- a/OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo +++ b/OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo @@ -139,7 +139,6 @@ public input Adjacency.Matrix adj; input Matching matching; input Option mapping_opt; - input Adjacency.MatrixType matrixType; output Boolean changed; end resolveSingularitiesInterface; diff --git a/OMCompiler/Compiler/NBackEnd/Util/NBGraphUtil.mo b/OMCompiler/Compiler/NBackEnd/Util/NBGraphUtil.mo deleted file mode 100644 index ba155c3428f..00000000000 --- a/OMCompiler/Compiler/NBackEnd/Util/NBGraphUtil.mo +++ /dev/null @@ -1,423 +0,0 @@ -/* -* This file is part of OpenModelica. -* -* Copyright (c) 1998-2020, Open Source Modelica Consortium (OSMC), -* c/o Linköpings universitet, Department of Computer and Information Science, -* SE-58183 Linköping, Sweden. -* -* All rights reserved. -* -* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR -* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2. -* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES -* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, -* ACCORDING TO RECIPIENTS CHOICE. -* -* The OpenModelica software and the Open Source Modelica -* Consortium (OSMC) Public License (OSMC-PL) are obtained -* from OSMC, either from the above address, -* from the URLs: http://www.ida.liu.se/projects/OpenModelica or -* http://www.openmodelica.org, and in the OpenModelica distribution. -* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. -* -* This program is distributed WITHOUT ANY WARRANTY; without -* even the implied warranty of MERCHANTABILITY or FITNESS -* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH -* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. -* -* See the full OSMC Public License conditions for more details. -* -*/ -encapsulated package NBGraphUtil -"file: NBGraphUtil.mo - package: NBGraphUtil - description: This file contains the functions which perform the causalization process; -" - -protected - // NF import - import ComponentRef = NFComponentRef; - import Dimension = NFDimension; - import Expression = NFExpression; - import InstNode = NFInstNode.InstNode; - import Subscript = NFSubscript; - import Variable = NFVariable; - - // NB import - import NBEquation.Equation; - import BVariable = NBVariable; - - // SetBased Graph imports - import SBGraphUtil = NFSBGraphUtil; - import SBGraph.BipartiteIncidenceList; - import SBGraph.VertexDescriptor; - import SBGraph.SetType; - import NBAdjacency.BipartiteGraph; - import SBInterval; - import SBMultiInterval; - import SBPWLinearMap; - import SBSet; - import NBGraphUtil.{SetVertex, SetEdge}; - -public - uniontype SetVertex - record SET_VERTEX - Pointer name "can represent variables as well as equations (residual var)"; - SBSet vs "corresponding set of vertices"; - end SET_VERTEX; - - function hash - input SetVertex v; - output Integer i = Variable.hash(Pointer.access(v.name)); - end hash; - - function isEqual - input SetVertex v1; - input SetVertex v2; - output Boolean equal = ComponentRef.isEqual(BVariable.getVarName(v1.name), BVariable.getVarName(v2.name)); - end isEqual; - - function isNamed - input SetVertex v; - input Pointer name; - output Boolean equal = ComponentRef.isEqual(BVariable.getVarName(v.name), BVariable.getVarName(name)); - end isNamed; - - function getSet - input SetVertex v; - output SBSet s = v.vs; - end getSet; - - function create - input Pointer var_ptr; - input BipartiteGraph graph; - input Vector vCount; - input SetType ST; - input UnorderedMap vertexMap; - output SBMultiInterval mi; - output Integer d; - protected - Option od; - list dims; - SBSet set; - SetVertex vertex; - algorithm - od := BipartiteIncidenceList.findVertex(graph, ST, function SetVertex.isNamed(name = var_ptr)); - - if isSome(od) then - // vertex already exists - SOME(d) := od; - vertex := BipartiteIncidenceList.getVertex(graph, d, ST); - mi := SBAtomicSet.aset(UnorderedSet.first(SBSet.asets(vertex.vs))); - else - // create new vertex - dims := BVariable.getDimensions(var_ptr); - mi := SBGraphUtil.multiIntervalFromDimensions(dims, vCount); - - set := SBSet.newEmpty(); - set := SBSet.addAtomicSet(SBAtomicSet.new(mi), set); - - vertex := SET_VERTEX(var_ptr, set); - d := BipartiteIncidenceList.addVertex(graph, vertex, ST); - UnorderedMap.add(vertex, d, vertexMap); - end if; - end create; - - function createTraverse - input Pointer var_ptr; - input BipartiteGraph graph; - input Vector vCount; - input SetType ST; - input UnorderedMap vertexMap; - algorithm - (_, _) := create(var_ptr, graph, vCount, ST, vertexMap); - end createTraverse; - - function toString - input SetVertex v; - output String str = Variable.toString(Pointer.access(v.name)) + "\n\t" + SBSet.toString(v.vs); - end toString; - end SetVertex; - - uniontype SetEdge - record SET_EDGE - String name "is always E_ + String(System.tmpTick())"; - SBPWLinearMap F; - SBPWLinearMap U; - end SET_EDGE; - - function hash - input SetEdge e; - output Integer i = stringHashDjb2(e.name); - end hash; - - function isEqual - input SetEdge e1; - input SetEdge e2; - output Boolean equal = e1.name == e2.name; - end isEqual; - - function getDomain - input SetEdge e; - output array domain = SBPWLinearMap.dom(e.F); - end getDomain; - - function fromEquation - input output Equation eqn; - input BipartiteGraph graph; - input Vector vCount; - input Vector eCount; - input UnorderedMap vertexMap; - input UnorderedMap edgeMap; - input UnorderedMap map "unordered map to check for relevance"; - input Option> eqn_tpl_opt; - protected - SBMultiInterval eqn_mi; - Integer eqn_d; - algorithm - // only get top level residual var - if not Util.isSome(eqn_tpl_opt) then - (eqn_mi, eqn_d) := SetVertex.create(Equation.getResidualVar(Pointer.create(eqn)), graph, vCount, SetType.F, vertexMap); - else - SOME((eqn_mi, eqn_d)) := eqn_tpl_opt; - end if; - - () := match eqn - local - Expression range; - Equation body; - - case Equation.SCALAR_EQUATION() algorithm - _ := Equation.map(eqn, function fromExpression( - eqn_tpl = (eqn_mi, eqn_d), - graph = graph, - vCount = vCount, - eCount = eCount, - vertexMap = vertexMap, - edgeMap = edgeMap, - map = map) - ); - then (); - - case Equation.ARRAY_EQUATION() algorithm - _ := Equation.map(eqn, function fromExpression( - eqn_tpl = (eqn_mi, eqn_d), - graph = graph, - vCount = vCount, - eCount = eCount, - vertexMap = vertexMap, - edgeMap = edgeMap, - map = map) - ); - then (); - - case Equation.FOR_EQUATION() algorithm - //body := applyIterator(eqn.iter, eqn.range, eqn.body); - for body in eqn.body loop - fromEquation(body, graph, vCount, eCount, vertexMap, edgeMap, map, SOME((eqn_mi, eqn_d))); - end for; - then (); - - else algorithm - Error.addMessage(Error.INTERNAL_ERROR, {getInstanceName() + " failed for " + Equation.toString(eqn)}); - then fail(); - end match; - end fromEquation; - -/* - function applyIterator - input InstNode iterator; - input Expression range; - input output Equation body; - algorithm - body := Equation.map(body, function Expression.replaceIterator(iterator = iterator, iteratorValue = range)); - end applyIterator; -*/ - - function fromExpression - input output Expression exp; - input tuple eqn_tpl; - input BipartiteGraph graph; - input Vector vCount; - input Vector eCount; - input UnorderedMap vertexMap; - input UnorderedMap edgeMap; - input UnorderedMap map "unordered map to check for relevance"; - algorithm - () := match exp - local - ComponentRef cref; - SBMultiInterval eqn_mi, var_mi; - VertexDescriptor eqn_d, var_d; - - case Expression.CREF(cref = cref) - guard(UnorderedMap.contains(ComponentRef.stripSubscriptsAll(cref), map)) - algorithm - (eqn_mi, eqn_d) := eqn_tpl; - (var_mi, var_d) := getVariableIntervals( - var_ptr = BVariable.getVarPointer(cref), - subs = ComponentRef.subscriptsAllFlat(cref), // mby wrong because of empty subs! - graph = graph, - vCount = vCount, - ST = SetType.U, - vertexMap = vertexMap - ); - updateGraph(eqn_d, var_d, eqn_mi, var_mi, graph, eCount, edgeMap); - then (); - - else (); - end match; - end fromExpression; - - function getVariableIntervals - input Pointer var_ptr; - input list subs; - input BipartiteGraph graph; - input Vector vCount; - input SetType ST; - input UnorderedMap vertexMap; - output SBMultiInterval outMI; - output VertexDescriptor d; - algorithm - (outMI, d) := SetVertex.create(var_ptr, graph, vCount, ST, vertexMap); - // if there are no subscripts just use full multi interval - if not listEmpty(subs) then - outMI := SBGraphUtil.multiIntervalFromSubscripts(subs, vCount, outMI); - end if; - end getVariableIntervals; - - function updateGraph - input VertexDescriptor d1; - input VertexDescriptor d2; - input SBMultiInterval mi1; - input SBMultiInterval mi2; - input BipartiteGraph graph; - input Vector eCount; - input UnorderedMap edgeMap; - protected - SBPWLinearMap pw1, pw2; - String name; - SetEdge se; - Integer edge_i; - algorithm - (name, pw1, pw2) := SBGraphUtil.linearMapFromIntervals(d1, d2, mi1, mi2, eCount); - se := SET_EDGE(name, pw1, pw2); - edge_i := BipartiteIncidenceList.addEdge(graph, d1, d2, se); - UnorderedMap.add(se, edge_i, edgeMap); - end updateGraph; - - function unmatchedU - "returns unmatched variable vertices U" - input SetEdge edge "edge for which to return vertices"; - input SBSet E_M "global matched edges"; - output SBSet unmatched_U = SBSet.newEmpty() "all matched F vertices of this edge"; - protected - SBPWLinearMap U = edge.U; - SBSet unmatched_edge; - algorithm - for i in 1:U.ndim loop - // find all edges from the i_th domain that are not matched - unmatched_edge := SBSet.complement(U.dom[i], E_M); - // apply map and add to unmatched U vertices - unmatched_U := SBSet.union(unmatched_U, SBLinearMap.apply(unmatched_edge,U.lmap[i])); - end for; - end unmatchedU; - - function minInvU - input SetEdge E "edge for which to return vertices"; - input SBSet U "Path vertex set to maximize"; - output SBSet P_max = SBSet.newEmpty() "Maximized path vertex set"; - protected - SBPWLinearMap invU = SBPWLinearMap.minInvCompact(E.U); - SBSet U_tmp; - algorithm - for i in 1:invU.ndim loop - // only consider indices from the current inverse domain - U_tmp := SBSet.intersection(invU.dom[i], U); - P_max := SBSet.union(P_max, SBLinearMap.apply(U_tmp, invU.lmap[i])); - end for; - end minInvU; - - function matchedF - "returns matched equation vertices F" - input SetEdge E "edge for which to return vertices"; - input SBSet E_M "global matched edges"; - output SBSet matched_F = SBSet.newEmpty() "all matched F vertices of this edge"; - protected - SBPWLinearMap F = E.F; - SBSet matched_E; - algorithm - for i in 1:F.ndim loop - // find all matched indices that are part of i_th domain - matched_E := SBSet.intersection(F.dom[i], E_M); - // apply map and add to matched F vertices - matched_F := SBSet.union(matched_F, SBLinearMap.apply(matched_E, F.lmap[i])); - end for; - end matchedF; - - function maxPath - input SetEdge E "edge for which to return vertices"; - input SBSet P1 "Path index set to maximize"; - input SetType ST "set type tow know which maps to use"; - output SBSet P1_max = SBSet.newEmpty() "Maximized path index set"; - algorithm - P1_max := match ST - case SetType.U then maxPathU(E, P1); - case SetType.F then maxPathF(E, P1); - else algorithm - Error.assertion(false, getInstanceName() + " failed for unknown SetType: " - + BipartiteIncidenceList.setTypeString(ST) + ". Allowed: U, F", sourceInfo()); - then fail(); - end match; - end maxPath; - - function maxPathU - input SetEdge E "edge for which to return vertices"; - input SBSet P1 "Path index set to maximize"; - output SBSet P1_max = SBSet.newEmpty() "Maximized path index set"; - protected - SBPWLinearMap U = E.U; - SBPWLinearMap invU = SBPWLinearMap.minInvCompact(U); - SBSet P1_tmp; - algorithm - for i in 1:U.ndim loop - // find all indices that are part of i_th domain - P1_tmp := SBSet.intersection(U.dom[i], P1); - // apply map and minimal inverse map - P1_tmp := SBLinearMap.apply(P1_tmp, U.lmap[i]); - P1_tmp := SBLinearMap.apply(P1_tmp, invU.lmap[i]); - // add to maximum path - P1_max := SBSet.union(P1_max, P1_tmp); - end for; - end maxPathU; - - function maxPathF - input SetEdge E "edge for which to return vertices"; - input SBSet P1 "Path index set to maximize"; - output SBSet P1_max = SBSet.newEmpty() "Maximized path index set"; - protected - SBPWLinearMap F = E.F; - SBPWLinearMap invF = SBPWLinearMap.minInvCompact(F); - SBSet P1_tmp; - algorithm - for i in 1:F.ndim loop - // find all indices that are part of i_th domain - P1_tmp := SBSet.intersection(F.dom[i], P1); - // apply map and minimal inverse map - P1_tmp := SBLinearMap.apply(P1_tmp, F.lmap[i]); - P1_tmp := SBLinearMap.apply(P1_tmp, invF.lmap[i]); - // add to maximum path - P1_max := SBSet.union(P1_max, P1_tmp); - end for; - end maxPathF; - - function toString - input SetEdge e; - output String str = e.name - + "\nmap F:\t" + SBPWLinearMap.toString(e.F) - + "\nmap U:\t" + SBPWLinearMap.toString(e.U) + "\n"; - end toString; - end SetEdge; - - annotation(__OpenModelica_Interface="backend"); -end NBGraphUtil; diff --git a/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo b/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo index c94217b4ba7..7390c301dab 100644 --- a/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo +++ b/OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo @@ -326,7 +326,7 @@ public end getUnsolvableExpCrefs; function getDependentCrefIndicesPseudoScalar - "[Adjacency.MatrixType.PSEUDO] Scalar equations. + "Scalar 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"; @@ -354,7 +354,7 @@ public end getDependentCrefIndicesPseudoScalar; function getDependentCrefIndicesPseudoFull - "[Adjacency.MatrixType.PSEUDO] equations that will get full dependency. + "equations that will get full dependency. Turns cref dependencies into index lists, used for adjacency." extends getDependentCrefIndices; protected @@ -413,7 +413,7 @@ public end getDependentCrefIndicesPseudoFull; function getDependentCrefIndicesPseudoFor - "[Adjacency.MatrixType.PSEUDO] For-Loop equations. + "For-Loop equations. Turns cref dependencies into index lists, used for adjacency." extends getDependentCrefIndices; input Iterator iter "iterator frames"; @@ -463,7 +463,7 @@ public end getDependentCrefIndicesPseudoFor; function getDependentCrefsPseudoForCausalized - "(Jacobian) [Adjacency.MatrixType.PSEUDO] For-Loop equations. + "(Jacobian) For-Loop equations. Turns cref dependencies into index lists, used for adjacency." input ComponentRef row_cref "cref representing the current row"; input list dependencies "dependent var crefs"; @@ -634,7 +634,7 @@ public end updateDependenciesInteger; function getDependentCrefsPseudoArrayCausalized - "[Adjacency.MatrixType.PSEUDO] Array equations. + "Array equations. Turns cref dependencies into index lists, used for adjacency." input ComponentRef row_cref "cref representing the current row"; input list dependencies "dependent var crefs"; diff --git a/OMCompiler/Compiler/boot/LoadCompilerSources.mos b/OMCompiler/Compiler/boot/LoadCompilerSources.mos index 6219d1cd13b..5a2d5a8e8f6 100644 --- a/OMCompiler/Compiler/boot/LoadCompilerSources.mos +++ b/OMCompiler/Compiler/boot/LoadCompilerSources.mos @@ -309,7 +309,6 @@ if true then /* Suppress output */ // "NBackend Util" "../NBackEnd/Util/NBBackendUtil.mo", "../NBackEnd/Util/NBDifferentiate.mo", - "../NBackEnd/Util/NBGraphUtil.mo", "../NBackEnd/Util/NBReplacements.mo", "../NBackEnd/Util/NBSlice.mo",