Skip to content

Commit a605416

Browse files
authored
[OB] fix algorithm adjacency matrix (#13899)
- array variable outputs are assigned in their entirety - previous attempts of fixing this (#7853, #7875) were only partially correct - refers to ticket #7832
1 parent d4012cb commit a605416

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed

OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,17 +2734,21 @@ algorithm
27342734
Discrete output variables that do not occur get computed by pre().
27352735
Ticket #5659
27362736
*/
2737-
if indexTypeSolvable(inIndexType) then
2738-
crefLst := CheckModel.algorithmStatementListOutputs(statementLst, DAE.EXPAND()); // expand as we're in an algorithm
2739-
for cr in crefLst loop
2740-
try
2741-
(varslst, p) := BackendVariable.getVar(cr, vars);
2742-
res := adjacencyRowExp1Discrete(varslst, p, res);
2743-
else
2744-
/* Nothing to do, BackendVariable.getVar fails for $START, $PRE, time etc. */
2745-
end try;
2746-
end for;
2747-
end if;
2737+
crefLst := CheckModel.algorithmStatementListOutputs(statementLst, DAE.EXPAND()); // expand as we're in an algorithm
2738+
for cr in crefLst loop
2739+
try
2740+
(varslst, p) := BackendVariable.getVar(cr, vars);
2741+
res := adjacencyRowExp1DiscreteOrArray(varslst, p, res);
2742+
else
2743+
/* Nothing to do, BackendVariable.getVar fails for $START, $PRE, time etc. */
2744+
end try;
2745+
try
2746+
(varslst, p) := BackendVariable.getVar(ComponentReference.crefPrefixStart(cr), vars);
2747+
res := adjacencyRowExp1DiscreteOrArray(varslst, p, res);
2748+
else
2749+
/* Nothing to do, BackendVariable.getVar fails for $START, $PRE, time etc. */
2750+
end try;
2751+
end for;
27482752
then
27492753
(res,size);
27502754

@@ -3624,7 +3628,7 @@ algorithm
36243628
end match;
36253629
end adjacencyRowExp1;
36263630

3627-
protected function adjacencyRowExp1Discrete
3631+
protected function adjacencyRowExp1DiscreteOrArray
36283632
"Adds an adjacency matrix entry for all variables in the inVarLst, if they are discrete."
36293633
input list<BackendDAE.Var> inVarLst;
36303634
input list<Integer> inIntegerLst;
@@ -3638,14 +3642,14 @@ algorithm
36383642
AvlSetInt.Tree vars;
36393643
Integer i;
36403644
case ({}, {}) then inVarIndxLst;
3641-
case (BackendDAE.VAR(varKind = BackendDAE.DISCRETE())::rest, i::irest)
3645+
case (BackendDAE.VAR()::rest, i::irest)
36423646
equation
36433647
vars = AvlSetInt.add(inVarIndxLst, i);
3644-
then adjacencyRowExp1Discrete(rest,irest,vars);
3648+
then adjacencyRowExp1DiscreteOrArray(rest,irest,vars);
36453649
case (_::rest, _::irest)
3646-
then adjacencyRowExp1Discrete(rest,irest,inVarIndxLst);
3650+
then adjacencyRowExp1DiscreteOrArray(rest,irest,inVarIndxLst);
36473651
end match;
3648-
end adjacencyRowExp1Discrete;
3652+
end adjacencyRowExp1DiscreteOrArray;
36493653

36503654
public function traversingadjacencyRowExpFinderwithInput "Helper for statesAndVarsExp"
36513655
input DAE.Exp inExp;
@@ -4317,21 +4321,6 @@ protected function traverseStmts<ArgT> "Author: Frenkel TUD 2012-06
43174321
end FuncExpType;
43184322

43194323
protected
4320-
function removeSubscripts
4321-
"kabdelhak: remove left hand side subscripts
4322-
(Modelica Specification v3.5 : 11.1.2)
4323-
Fix: Do not do if it is a scalar variable with all constant subscripts.
4324-
It leads to a massive number of hash table accesses for big tensors."
4325-
input output DAE.Exp exp;
4326-
algorithm
4327-
exp := match exp
4328-
case DAE.CREF() guard(not ComponentReference.crefIsScalarWithAllConstSubs(exp.componentRef)) algorithm
4329-
exp.componentRef := ComponentReference.crefStripSubsExceptModelSubs(exp.componentRef);
4330-
then exp;
4331-
else exp;
4332-
end match;
4333-
end removeSubscripts;
4334-
43354324
DAE.Exp e,e2;
43364325
list<DAE.Exp> expl1;
43374326
DAE.ComponentRef cr;
@@ -4350,7 +4339,7 @@ algorithm
43504339
// (Modelica Specification v3.5 : 11.1.2)
43514340
// solves ticket #7832
43524341
extraArg = func(e, extraArg);
4353-
extraArg = func(removeSubscripts(e2), extraArg);
4342+
extraArg = func(e2, extraArg);
43544343
then
43554344
extraArg;
43564345

@@ -4360,7 +4349,7 @@ algorithm
43604349
// (Modelica Specification v3.5 : 11.1.2)
43614350
// solves ticket #7832
43624351
extraArg = func(e, extraArg);
4363-
extraArg = List.fold(list(removeSubscripts(ex) for ex in expl1),func,extraArg);
4352+
extraArg = List.fold(expl1,func,extraArg);
43644353
then
43654354
extraArg;
43664355

@@ -4370,7 +4359,7 @@ algorithm
43704359
// (Modelica Specification v3.5 : 11.1.2)
43714360
// solves ticket #7832
43724361
extraArg = func(e, extraArg);
4373-
extraArg = func(removeSubscripts(e2), extraArg);
4362+
extraArg = func(e2, extraArg);
43744363
then
43754364
extraArg;
43764365

@@ -10322,16 +10311,6 @@ algorithm
1032210311
end match;
1032310312
end containsHomotopyCall2;
1032410313

10325-
protected function indexTypeSolvable
10326-
input BackendDAE.IndexType indexType;
10327-
output Boolean b;
10328-
algorithm
10329-
b := match indexType
10330-
case BackendDAE.SOLVABLE() then true;
10331-
else false;
10332-
end match;
10333-
end indexTypeSolvable;
10334-
1033510314
public function doIndexReduction
1033610315
input BackendDAE.MatchingOptions opt;
1033710316
output Boolean b;

OMCompiler/Compiler/FrontEnd/CheckModel.mo

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ algorithm
549549
local
550550
DAE.Exp e, exp, e1, e2;
551551
HashSet.HashSet ht;
552-
DAE.ComponentRef cr;
552+
DAE.ComponentRef cr, first_cref;
553553
list<DAE.ComponentRef> crlst;
554554
list<DAE.Subscript> subs;
555555
DAE.Expand expand;
@@ -590,8 +590,13 @@ algorithm
590590
case (e as DAE.CREF(componentRef=cr), (expand,ht))
591591
equation
592592
cr = ComponentReference.crefStripSubsExceptModelSubs(cr);
593-
crlst = ComponentReference.expandCref(cr, true);
594-
ht = List.fold(crlst, BaseHashSet.add, ht);
593+
// check if first element already is in the set and only expand if not (saves a lot of expand work)
594+
// ticket #7832
595+
first_cref = ComponentReference.crefArrayGetFirstCref(cr);
596+
if not BaseHashSet.has(first_cref, ht) then
597+
crlst = ComponentReference.expandCref(cr, true);
598+
ht = List.fold(crlst, BaseHashSet.add, ht);
599+
end if;
595600
then (e, false, (expand,ht));
596601

597602
case (e as DAE.ASUB(exp=exp), _)

0 commit comments

Comments
 (0)