Skip to content

Commit

Permalink
[NB] further work on tearing minimal (#12389)
Browse files Browse the repository at this point in the history
* [NB] further work on tearing minimal

* [NB] tearing. fix small oversight

* [NB] properly solve inner equations in loops

* [NB] update implicit equation solving

 - only use finalize tearing method for jacobian
 - throw warning instead of error if something has to be solved implicit while using -d=failtrace
 - rename UnorderedMap.subSet to UnorderedMap.subMap
  • Loading branch information
kabdelhak committed May 11, 2024
1 parent c0cd24f commit 08bd7dc
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 277 deletions.
8 changes: 4 additions & 4 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBCausalize.mo
Expand Up @@ -254,8 +254,8 @@ protected
// #################################################
// Phase I: match initial equations <-> unfixable vars
// #################################################
vn := UnorderedMap.subSet(system.unknowns.map, list(BVariable.getVarName(var) for var in unfixable));
en := UnorderedMap.subSet(system.equations.map, list(Equation.getEqnName(eqn) for eqn in initials));
vn := UnorderedMap.subMap(system.unknowns.map, list(BVariable.getVarName(var) for var in unfixable));
en := UnorderedMap.subMap(system.equations.map, list(Equation.getEqnName(eqn) for eqn in initials));

adj_matching := Adjacency.Matrix.fromFull(full, vn, en, system.equations, NBAdjacency.MatrixStrictness.MATCHING);
matching := Matching.regular(NBMatching.EMPTY_MATCHING, adj_matching, true, true);
Expand All @@ -266,7 +266,7 @@ protected
vo := vn;
eo := en;
vn := UnorderedMap.new<Integer>(ComponentRef.hash, ComponentRef.isEqual);
en := UnorderedMap.subSet(system.equations.map, list(Equation.getEqnName(eqn) for eqn in simulation));
en := UnorderedMap.subMap(system.equations.map, list(Equation.getEqnName(eqn) for eqn in simulation));

(adj_matching, full) := Adjacency.Matrix.expand(adj_matching, full, vo, vn, eo, en, system.unknowns, system.equations);
matching := Matching.regular(matching, adj_matching, true, true);
Expand All @@ -276,7 +276,7 @@ protected
// #################################################
vo := UnorderedMap.merge(vo, vn, sourceInfo());
eo := UnorderedMap.merge(eo, en, sourceInfo());
vn := UnorderedMap.subSet(system.unknowns.map, list(BVariable.getVarName(var) for var in fixable));
vn := UnorderedMap.subMap(system.unknowns.map, list(BVariable.getVarName(var) for var in fixable));
en := UnorderedMap.new<Integer>(ComponentRef.hash, ComponentRef.isEqual);
(adj_matching, full) := Adjacency.Matrix.expand(adj_matching, full, vo, vn, eo, en, system.unknowns, system.equations);
(matching, adj_matching, full, variables, equations, funcTree, varData, eqData) := Matching.singular(matching, adj_matching, full, system.unknowns, system.equations, funcTree, varData, eqData, system.systemType, false, true, false);
Expand Down
Expand Up @@ -375,7 +375,7 @@ public

// update adjacency matrices
vn := UnorderedMap.new<Integer>(ComponentRef.hash, ComponentRef.isEqual);
en := UnorderedMap.subSet(equations.map, list(Equation.getEqnName(eqn) for eqn in start_eqns));
en := UnorderedMap.subMap(equations.map, list(Equation.getEqnName(eqn) for eqn in start_eqns));
(adj, full) := Adjacency.Matrix.expand(adj, full, vo, vn, eo, en, variables, equations);

if Flags.isSet(Flags.INITIALIZATION) then
Expand Down
18 changes: 13 additions & 5 deletions OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBSolve.mo
Expand Up @@ -200,6 +200,8 @@ public
list<Pointer<Equation>> rest, sliced_eqns = {};
StrongComponent generic_comp;
list<StrongComponent> entwined_slices = {};
Tearing strict;
list<StrongComponent> tmp, inner_comps = {};

case StrongComponent.SINGLE_COMPONENT() algorithm
(eqn, funcTree, solve_status, implicit_index) := solveSingleStrongComponent(Pointer.access(comp.eqn), Pointer.access(comp.var), funcTree, systemType, implicit_index, slicing_map);
Expand All @@ -209,8 +211,13 @@ public
(eqn, funcTree, solve_status, implicit_index) := solveMultiStrongComponent(Pointer.access(comp.eqn), comp.vars, funcTree, systemType, implicit_index, slicing_map);
then ({StrongComponent.MULTI_COMPONENT(comp.vars, Pointer.create(eqn), solve_status)}, solve_status);

case StrongComponent.ALGEBRAIC_LOOP() algorithm
// do we need to do smth here? e.g. solve inner equations? call tearing from here?
case StrongComponent.ALGEBRAIC_LOOP(strict = strict) algorithm
for inner_comp in listReverse(arrayList(strict.innerEquations)) loop
(tmp, funcTree, implicit_index) := solveStrongComponent(inner_comp, funcTree, systemType, implicit_index, slicing_map);
inner_comps := listAppend(tmp, inner_comps);
end for;
strict.innerEquations := listArray(inner_comps);
comp.strict := strict;
comp.status := Status.IMPLICIT;
then ({comp}, Status.IMPLICIT);

Expand Down Expand Up @@ -540,8 +547,8 @@ public
else
// If eqn is non-linear in cref
if Flags.isSet(Flags.FAILTRACE) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " cref: "
+ ComponentRef.toString(fixed_cref) + " has to be solved implicitely in equation:\n" + Equation.toString(eqn)});
Error.addCompilerWarning(getInstanceName() + " cref: " + ComponentRef.toString(fixed_cref)
+ " has to be solved implicitely in equation:\n" + Equation.toString(eqn));
end if;
invertRelation := false;
status := Status.IMPLICIT;
Expand Down Expand Up @@ -583,7 +590,6 @@ public
end if;
end solveIfBody;

protected
function solveSimple
input output Equation eqn;
input ComponentRef cref;
Expand All @@ -601,11 +607,13 @@ protected
case Equation.WHEN_EQUATION() then (eqn, Status.EXPLICIT, false);

// ToDo: more cases
// ToDo: tuples, record elements, array constructors

else (eqn, Status.UNPROCESSED, false);
end match;
end solveSimple;

protected
function solveSimpleLhsRhs
input Expression lhs;
input Expression rhs;
Expand Down

0 comments on commit 08bd7dc

Please sign in to comment.