Skip to content

Commit

Permalink
[NB] remove unused functions (#12040)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabdelhak committed Feb 28, 2024
1 parent 29bb463 commit 0d61675
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 577 deletions.
1 change: 0 additions & 1 deletion OMCompiler/Compiler/.cmake/meta_modelica_source_list.cmake
Expand Up @@ -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

Expand Down
92 changes: 5 additions & 87 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo
Expand Up @@ -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<SetVertex, SetEdge>;

uniontype Mapping
record MAPPING
Expand Down Expand Up @@ -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<SetVertex, Integer> vertexMap "map to get the vertex index";
UnorderedMap<SetEdge, Integer> 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<list<Integer>> m "eqn -> list<var>";
array<list<Integer>> mT "var -> list<eqn>";
Expand All @@ -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<FunctionTree> 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"
Expand Down Expand Up @@ -486,19 +466,14 @@ 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);
adj.m := m;
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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<FunctionTree> funcTree = NONE() "only needed for LINEAR without existing derivatives";
protected
BipartiteIncidenceList<SetVertex, SetEdge> graph;
Pointer<Integer> max_dim = Pointer.create(1);
Vector<Integer> vCount, eCount;
UnorderedMap<SetVertex, Integer> vertexMap;
UnorderedMap<SetEdge, Integer> 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<Integer>(SetVertex.hash, SetVertex.isEqual, VariablePointers.size(vars) + EquationPointers.size(eqns));
edgeMap := UnorderedMap.new<Integer>(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<Variable> var_ptr;
input Pointer<Integer> max_dim;
Expand Down
41 changes: 5 additions & 36 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo
Expand Up @@ -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;
Expand All @@ -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
// ############################################################
Expand Down Expand Up @@ -197,14 +185,13 @@ public
function simple
input VariablePointers vars;
input EquationPointers eqs;
input Adjacency.MatrixType matrixType = NBAdjacency.MatrixType.PSEUDO;
output list<StrongComponent> 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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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<StrongComponent> 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<list<StrongComponent>> acc = Pointer.create({});
Expand Down
28 changes: 8 additions & 20 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo
Expand Up @@ -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;
Expand Down Expand Up @@ -96,7 +91,7 @@ public
protected
list<list<Integer>> 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();
Expand Down Expand Up @@ -131,13 +126,12 @@ public
protected
list<list<Integer>> marked_eqns;
Option<Adjacency.Mapping> 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"
Expand All @@ -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;
Expand All @@ -175,28 +169,22 @@ public
input Boolean clear;
output list<list<Integer>> marked_eqns;
output Option<Adjacency.Mapping> mapping;
output Adjacency.MatrixType matrixType;
output Adjacency.MatrixStrictness matrixStrictness;
protected
array<Integer> 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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit 0d61675

Please sign in to comment.