Skip to content

Commit 4bd69db

Browse files
authored
[NB] remove dummy equations after cleanup (#13135)
- Initialization cleanup might create dummy equations for when-equations that are only active during initialization - remove corresponding strong components
1 parent b09ec70 commit 4bd69db

File tree

7 files changed

+68
-3
lines changed

7 files changed

+68
-3
lines changed

OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ public
809809
function hash
810810
"only hashes the name"
811811
input Pointer<Equation> eqn;
812-
output Integer i = Variable.hash(Pointer.access(getResidualVar(eqn)));
812+
output Integer i = if isDummy(Pointer.access(eqn)) then 0 else ComponentRef.hash(getEqnName(eqn));
813813
end hash;
814814

815815
function equalName

OMCompiler/Compiler/NBackEnd/Classes/NBStrongComponent.mo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,17 @@ public
782782
end match;
783783
end isDiscrete;
784784

785+
function isDummy
786+
input StrongComponent comp;
787+
output Boolean b;
788+
algorithm
789+
b := match comp
790+
case SINGLE_COMPONENT() then Equation.isDummy(Pointer.access(comp.eqn));
791+
case MULTI_COMPONENT() then Equation.isDummy(Pointer.access(Slice.getT(comp.eqn)));
792+
else false;
793+
end match;
794+
end isDummy;
795+
785796
function isAlias
786797
input StrongComponent comp;
787798
output Boolean b;

OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public
3939
import BEquation = NBEquation;
4040
import NBEquation.{Equation, EquationPointer, EquationPointers, EqData, EquationAttributes, EquationKind, IfEquationBody, Iterator};
4141
import NBVariable.{VariablePointer, VariablePointers, VarData};
42+
import Evaluation = NBEvaluation;
4243
import Events = NBEvents;
4344
import NFFlatten.FunctionTree;
4445
import Jacobian = NBJacobian;
@@ -277,6 +278,7 @@ public
277278

278279
// (do not change order SOLVE -> JACOBIAN)
279280
postOptModules := {
281+
(Evaluation.removeDummies, "Remove Dummies"),
280282
(function Tearing.main(kind = NBPartition.Kind.ODE), "Tearing"),
281283
(Partitioning.categorize, "Categorize"),
282284
(Solve.main, "Solve"),
@@ -1510,7 +1512,7 @@ public
15101512
c := Pointer.access(collector_ptr);
15111513
single_sc := intString(c.single_scalar + c.single_array + c.single_record) + " (scalar:" + intString(c.single_scalar) + ", array:" + intString(c.single_array) + ", record:" + intString(c.single_record) + ")";
15121514
multi_sc := intString(c.multi_algorithm + c.multi_when + c.multi_if) + " (algorithm:" + intString(c.multi_algorithm) + ", when:" + intString(c.multi_when) + ", if:" + intString(c.multi_if) + ", tuple:" + intString(c.multi_tpl) + ")";
1513-
for_sc := intString(c.resizable_for + c.generic_for + c.entwined_for) + " (resizable: " + intString(c.resizable_for) + "generic: " + intString(c.generic_for) + ", entwined:" + intString(c.entwined_for) + ")";
1515+
for_sc := intString(c.resizable_for + c.generic_for + c.entwined_for) + " (resizable: " + intString(c.resizable_for) + ", generic: " + intString(c.generic_for) + ", entwined:" + intString(c.entwined_for) + ")";
15141516
alg_sc := intString(c.loop_lin + c.loop_nlin) + " (linear: " + intString(c.loop_lin) + ", nonlinear:" + intString(c.loop_nlin) + ")";
15151517

15161518
Error.addCompilerNotification(

OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBInitialization.mo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ public
578578
bdae.init_0 := SOME(list(Partition.clone(par, false) for par in bdae.init));
579579
bdae.init_0 := SOME(list(Partition.mapExp(par, function cleanupHomotopy(init = true, hasHom = hasHom)) for par in Util.getOption(bdae.init_0)));
580580
end if;
581-
582581
then bdae;
583582

584583
else bdae;

OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBEvaluation.mo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ encapsulated package NBEvaluation
3939
public
4040
import BackendDAE = NBackendDAE;
4141
import Module = NBModule;
42+
import Partition = NBPartition.Partition;
43+
import StrongComponent = NBStrongComponent;
4244

4345
protected
4446
// Old Backend imports
@@ -67,5 +69,27 @@ public
6769

6870
constant Stages DEFAULT_STAGES = STAGES(true, true, false, true);
6971

72+
function removeDummies
73+
"removes the dummy components in a partition
74+
Note: will be expanded into removeConstantComponents()"
75+
input output BackendDAE bdae;
76+
algorithm
77+
bdae := match bdae
78+
case BackendDAE.MAIN() algorithm
79+
bdae.ode := list(removeDummyComponents(p) for p in bdae.ode);
80+
bdae.algebraic := list(removeDummyComponents(p) for p in bdae.algebraic);
81+
bdae.ode_event := list(removeDummyComponents(p) for p in bdae.ode_event);
82+
bdae.alg_event := list(removeDummyComponents(p) for p in bdae.alg_event);
83+
then bdae;
84+
else bdae;
85+
end match;
86+
end removeDummies;
87+
88+
function removeDummyComponents
89+
input output Partition part;
90+
algorithm
91+
part.strongComponents := Util.applyOption(part.strongComponents, function Array.filter(fun = StrongComponent.isDummy));
92+
end removeDummyComponents;
93+
7094
annotation(__OpenModelica_Interface="backend");
7195
end NBEvaluation;

OMCompiler/Compiler/NBackEnd/Modules/3_Post/NBSolve.mo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ public
484484
end match;
485485
then (Slice.SLICE(Pointer.create(solved_eqn), eqn_slice.indices), funcTree, status);
486486

487+
// dummy equation implies removed equation (occurs only in simulation systems)
488+
case Equation.DUMMY_EQUATION() then (eqn_slice, funcTree, Status.EXPLICIT);
489+
487490
else algorithm
488491
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for equation:\n" + Slice.toString(eqn_slice, function Equation.pointerToString(str = ""))});
489492
then fail();
@@ -525,6 +528,9 @@ public
525528
+ "cref: " + ComponentRef.toString(cref) + " in equation:\n" + Equation.toString(eqn)});
526529
then fail();
527530

531+
// dummy equation implies removed equation (occurs only in simulation systems)
532+
case Equation.DUMMY_EQUATION() then (eqn, funcTree, Status.EXPLICIT, false);
533+
528534
else solveBody(eqn, cref, funcTree);
529535
end match;
530536
end solveEquation;

OMCompiler/Compiler/Util/Array.mo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,5 +1367,28 @@ algorithm
13671367
end if;
13681368
end generate;
13691369

1370+
function filter<T>
1371+
input array<T> arr;
1372+
input filterFunc fun;
1373+
output array<T> new_arr;
1374+
partial function filterFunc
1375+
input T t;
1376+
output Boolean b "if b=true then 't' gets removed";
1377+
end filterFunc;
1378+
protected
1379+
Integer new_size;
1380+
T dummy = dummy; // Fool the compiler into thinking dummy is initialized.
1381+
Integer index = 1;
1382+
algorithm
1383+
new_size := arrayLength(arr) - sum(1 for e guard fun(e) in arr);
1384+
new_arr := arrayCreateNoInit(new_size, dummy);
1385+
for e in arr loop
1386+
if not fun(e) then
1387+
arrayUpdateNoBoundsChecking(new_arr, index, e);
1388+
index := index + 1;
1389+
end if;
1390+
end for;
1391+
end filter;
1392+
13701393
annotation(__OpenModelica_Interface="util");
13711394
end Array;

0 commit comments

Comments
 (0)