Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 83d2116

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Treat array flows as array equations and solve them for code generation
Note that ExpressionSolve.solveSimpleEquations does not cover array equations, even though it would work. This is why SimCodeUtil calls ExpressionSolve.solve2 now. Belonging to [master]: - #2776 - OpenModelica/OpenModelica-testsuite#1072
1 parent e8bea85 commit 83d2116

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

Compiler/NFFrontEnd/NFConnectEquations.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ algorithm
304304
end for;
305305
end if;
306306

307-
equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), Type.REAL(), src)};
307+
equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), c.ty, src)};
308308
end generateFlowEquations;
309309

310310
function makeFlowExp

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,7 @@ algorithm
15911591
(eqnlst, varlst,_) = BackendDAETransform.getEquationAndSolvedVar(comp, syst.orderedEqs, syst.orderedVars);
15921592
// States are solved for der(x) not x.
15931593
varlst = List.map(varlst, BackendVariable.transformXToXd);
1594-
(equations1,_, uniqueEqIndex1, tempvars) = createSingleArrayEqnCode(true, eqnlst, varlst, uniqueEqIndex, tempvars, shared.info);
1594+
(equations1,_, uniqueEqIndex1, tempvars) = createSingleArrayEqnCode(true, eqnlst, varlst, uniqueEqIndex, tempvars, shared);
15951595

15961596
eqSccMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, sccIndex, eqSccMapping);
15971597
eqBackendSimCodeMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, e, eqBackendSimCodeMapping);
@@ -1828,12 +1828,12 @@ algorithm
18281828
then (equations1, equations1, uniqueEqIndex, tempvars);
18291829

18301830
// A single array equation
1831-
case (_, _, _, BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns), BackendDAE.SHARED(info = ei), BackendDAE.SINGLEARRAY())
1831+
case (_, _, _, BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns), _, BackendDAE.SINGLEARRAY())
18321832
equation
18331833
(eqnlst, varlst,_) = BackendDAETransform.getEquationAndSolvedVar(comp, eqns, vars);
18341834
// States are solved for der(x) not x.
18351835
varlst = List.map(varlst, BackendVariable.transformXToXd);
1836-
(equations1, noDiscEquations1, uniqueEqIndex, tempvars) = createSingleArrayEqnCode(genDiscrete, eqnlst, varlst, iuniqueEqIndex, itempvars, ei);
1836+
(equations1, noDiscEquations1, uniqueEqIndex, tempvars) = createSingleArrayEqnCode(genDiscrete, eqnlst, varlst, iuniqueEqIndex, itempvars, shared);
18371837
then (equations1, noDiscEquations1, uniqueEqIndex, tempvars);
18381838

18391839
// A single algorithm section for several variables.
@@ -5477,7 +5477,7 @@ protected function createSingleArrayEqnCode
54775477
input list<BackendDAE.Var> inVars;
54785478
input Integer iuniqueEqIndex;
54795479
input list<SimCodeVar.SimVar> itempvars;
5480-
input BackendDAE.ExtraInfo iextra;
5480+
input BackendDAE.Shared shared;
54815481
output list<SimCode.SimEqSystem> equations_;
54825482
output list<SimCode.SimEqSystem> noDiscequations;
54835483
output Integer ouniqueEqIndex;
@@ -5493,7 +5493,7 @@ algorithm
54935493
inEquation.right := Expression.replaceDerOpInExp(inEquation.right);
54945494
then inEquation;
54955495
end match;
5496-
(equations_, noDiscequations, ouniqueEqIndex, otempvars) := matchcontinue(genDiscrete, inEquation, inVars, iuniqueEqIndex, itempvars, iextra)
5496+
(equations_, noDiscequations, ouniqueEqIndex, otempvars) := matchcontinue(genDiscrete, inEquation, inVars)
54975497
local
54985498
list<Integer> ds;
54995499
DAE.Exp e1, e2, lhse, rhse;
@@ -5502,35 +5502,24 @@ algorithm
55025502
list<BackendDAE.Var> vars;
55035503
DAE.ComponentRef cr, cr_1, left;
55045504
BackendDAE.Variables evars, vars1;
5505-
BackendDAE.EquationArray eeqns, eqns_1;
5506-
FCore.Cache cache;
5507-
FCore.Graph graph;
55085505
DAE.FunctionTree funcs;
55095506
DAE.ElementSource source;
5510-
BackendDAE.Variables av;
5511-
BackendDAE.BackendDAE subsystem_dae;
55125507
SimCode.SimEqSystem equation_;
55135508
list<SimCode.SimEqSystem> eqSystlst;
5514-
BackendDAE.StrongComponents comps;
5515-
BackendDAE.EqSystem syst;
5516-
BackendDAE.Shared shared;
55175509
Integer uniqueEqIndex;
55185510
String str;
55195511
list<DAE.Dimension> dims;
55205512
list<SimCodeVar.SimVar> tempvars;
55215513
BackendDAE.EquationAttributes eqAttr;
5522-
BackendDAE.Equation eq1;
5523-
HashSet.HashSet ht;
5524-
list<DAE.ComponentRef> crefs, crefstmp;
5514+
list<DAE.ComponentRef> crefstmp;
55255515
DAE.Type ty,basety;
55265516
list<tuple<DAE.Exp, DAE.Exp>> exptl;
55275517

5528-
55295518
// An array equation
55305519
// {z1,z2,..} = rhsexp -> solved for {z1,z2,..}
55315520
// => tmp = rhsexp;
55325521
// z1 = tmp[1]; z2 = tmp[2] ....
5533-
case (_, (BackendDAE.ARRAY_EQUATION(dimSize=ds, left=e1, right=e2, source=source, attr=eqAttr)), _, _, _, _)
5522+
case (_, (BackendDAE.ARRAY_EQUATION(dimSize=ds, left=e1, right=e2, source=source, attr=eqAttr)), _)
55345523
guard Expression.isMatrix(e1) or Expression.isArray(e1)
55355524
equation
55365525
// Flattne multi-dimensional ARRAY{ARRAY} expressions
@@ -5556,7 +5545,7 @@ algorithm
55565545

55575546
// An array equation
55585547
// cref = rhsexp
5559-
case (_, (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(cr_1, _), right=e2, source=source, attr=eqAttr)), BackendDAE.VAR(varName=cr)::_, _, _, _)
5548+
case (_, (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(cr_1, _), right=e2, source=source, attr=eqAttr)), BackendDAE.VAR(varName=cr)::_)
55605549
guard ComponentReference.crefEqual(cr_1, ComponentReference.crefStripLastSubs(cr))
55615550
equation
55625551
(e1, _) = BackendDAEUtil.collateArrExp(e1, NONE());
@@ -5567,7 +5556,7 @@ algorithm
55675556

55685557
// An array equation
55695558
// lhsexp = cref
5570-
case (_, (BackendDAE.ARRAY_EQUATION(left=e1, right=e2 as DAE.CREF(cr_1, _), source=source, attr=eqAttr)), BackendDAE.VAR(varName=cr)::_, _, _, _)
5559+
case (_, (BackendDAE.ARRAY_EQUATION(left=e1, right=e2 as DAE.CREF(cr_1, _), source=source, attr=eqAttr)), BackendDAE.VAR(varName=cr)::_)
55715560
guard ComponentReference.crefEqual(cr_1, ComponentReference.crefStripLastSubs(cr))
55725561
equation
55735562
(e1, _) = BackendDAEUtil.collateArrExp(e1, NONE());
@@ -5576,10 +5565,24 @@ algorithm
55765565
uniqueEqIndex = iuniqueEqIndex + 1;
55775566
then ({equation_}, {equation_}, uniqueEqIndex, itempvars);
55785567

5568+
// An array equation
5569+
// lhsexp = rhsexp
5570+
case (_, (BackendDAE.ARRAY_EQUATION(left=lhse, right=rhse, source=source, attr=eqAttr)), BackendDAE.VAR(varName=cr)::_)
5571+
equation
5572+
(lhse, _) = BackendDAEUtil.collateArrExp(lhse, NONE());
5573+
(rhse, _) = BackendDAEUtil.collateArrExp(rhse, NONE());
5574+
BackendDAE.SHARED(functionTree = funcs) = shared;
5575+
e1 = Expression.crefExp(cr);
5576+
e2 = ExpressionSolve.solve2(lhse, rhse, e1, SOME(funcs), SOME(iuniqueEqIndex), true, BackendDAEUtil.isSimulationDAE(shared));
5577+
equation_ = SimCode.SES_ARRAY_CALL_ASSIGN(iuniqueEqIndex, e1, e2, source, eqAttr);
5578+
uniqueEqIndex = iuniqueEqIndex + 1;
5579+
then ({equation_}, {equation_}, uniqueEqIndex, itempvars);
5580+
55795581
// failure
55805582
else equation
5583+
BackendDAE.VAR(varName = cr)::_ = inVars;
55815584
str = BackendDump.dumpEqnsStr(inEquations);
5582-
str = "for Eqn: " + str + "\narray equations currently only supported on form v = functioncall(...)";
5585+
str = "solving array equation: " + str + "\nfor variable: " + ComponentReference.crefStr(cr) + ".";
55835586
Error.addInternalError(str, sourceInfo());
55845587
then fail();
55855588
end matchcontinue;

0 commit comments

Comments
 (0)