Skip to content

Commit 7c4868e

Browse files
author
Jens Frenkel
committed
- handle if equations in incidence matrix
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12293 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent fdf9c94 commit 7c4868e

File tree

1 file changed

+93
-31
lines changed

1 file changed

+93
-31
lines changed

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 93 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,7 +4052,7 @@ algorithm
40524052
// get the equation
40534053
e = equationNth(inEqsArr, index);
40544054
// compute the row
4055-
(row,_) = incidenceRow(vars, e, inWhenClause, inIndexType);
4055+
(row,_) = incidenceRow(e, vars, inWhenClause, inIndexType, {});
40564056
i1 = index+1;
40574057
// put it in the arrays
40584058
iArr = row::iArr;
@@ -4111,7 +4111,7 @@ algorithm
41114111
// get the equation
41124112
e = equationNth(inEqsArr, index);
41134113
// compute the row
4114-
(row,size) = incidenceRow(vars, e, inWhenClause, inIndexType);
4114+
(row,size) = incidenceRow(e, vars, inWhenClause, inIndexType, {});
41154115
rowSize = inRowSize + size;
41164116
rowindxs = List.intRange2(inRowSize+1, rowSize);
41174117
i1 = index+1;
@@ -4183,20 +4183,21 @@ algorithm
41834183
end matchcontinue;
41844184
end fillincidenceMatrixT;
41854185

4186-
public function incidenceRow
4186+
protected function incidenceRow
41874187
"function: incidenceRow
41884188
author: PA
41894189
Helper function to incidenceMatrix. Calculates the indidence row
41904190
in the matrix for one equation."
4191-
input Variables inVariables;
41924191
input BackendDAE.Equation inEquation;
4192+
input Variables vars;
41934193
input list<WhenClause> inWhenClause;
41944194
input BackendDAE.IndexType inIndexType;
4195+
input list<Integer> iRow;
41954196
output list<Integer> outIntegerLst;
41964197
output Integer rowSize;
41974198
algorithm
41984199
(outIntegerLst,rowSize) :=
4199-
matchcontinue (inVariables,inEquation,inWhenClause,inIndexType)
4200+
matchcontinue (inEquation,vars,inWhenClause,inIndexType,iRow)
42004201
local
42014202
list<Integer> lst1,lst2,res,dimsize;
42024203
Variables vars;
@@ -4213,61 +4214,61 @@ algorithm
42134214
list<BackendDAE.Equation> eqns;
42144215

42154216
// EQUATION
4216-
case (vars,BackendDAE.EQUATION(exp = e1,scalar = e2),_,_)
4217+
case (BackendDAE.EQUATION(exp = e1,scalar = e2),_,_,_,_)
42174218
equation
4218-
lst1 = incidenceRowExp(e1, vars, {},inIndexType);
4219+
lst1 = incidenceRowExp(e1, vars, iRow,inIndexType);
42194220
res = incidenceRowExp(e2, vars, lst1,inIndexType);
42204221
then
42214222
(res,1);
42224223

42234224
// COMPLEX_EQUATION
4224-
case (vars,BackendDAE.COMPLEX_EQUATION(size=size,left=e1,right=e2),_,_)
4225+
case (BackendDAE.COMPLEX_EQUATION(size=size,left=e1,right=e2),_,_,_,_)
42254226
equation
4226-
lst1 = incidenceRowExp(e1, vars, {},inIndexType);
4227+
lst1 = incidenceRowExp(e1, vars, iRow,inIndexType);
42274228
res = incidenceRowExp(e2, vars, lst1,inIndexType);
42284229
then
42294230
(res,size);
42304231

42314232
// ARRAY_EQUATION
4232-
case (vars,BackendDAE.ARRAY_EQUATION(dimSize=dimsize,left=e1,right=e2),_,_)
4233+
case (BackendDAE.ARRAY_EQUATION(dimSize=dimsize,left=e1,right=e2),_,_,_,_)
42334234
equation
42344235
size = List.reduce(dimsize, intMul);
4235-
lst1 = incidenceRowExp(e1, vars, {},inIndexType);
4236+
lst1 = incidenceRowExp(e1, vars, iRow,inIndexType);
42364237
res = incidenceRowExp(e2, vars, lst1,inIndexType);
42374238
then
42384239
(res,size);
42394240

42404241
// SOLVED_EQUATION
4241-
case (vars,BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e),_,_)
4242+
case (BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e),_,_,_,_)
42424243
equation
42434244
expCref = Expression.crefExp(cr);
4244-
lst1 = incidenceRowExp(expCref, vars, {},inIndexType);
4245+
lst1 = incidenceRowExp(expCref, vars, iRow,inIndexType);
42454246
res = incidenceRowExp(e, vars, lst1,inIndexType);
42464247
then
42474248
(res,1);
42484249

42494250
// RESIDUAL_EQUATION
4250-
case (vars,BackendDAE.RESIDUAL_EQUATION(exp = e),_,_)
4251+
case (BackendDAE.RESIDUAL_EQUATION(exp = e),_,_,_,_)
42514252
equation
4252-
res = incidenceRowExp(e, vars, {},inIndexType);
4253+
res = incidenceRowExp(e, vars, iRow,inIndexType);
42534254
then
42544255
(res,1);
42554256

42564257
// WHEN_EQUATION
4257-
case (vars,BackendDAE.WHEN_EQUATION(size=size,whenEquation = we as BackendDAE.WHEN_EQ(condition=cond,index=wc_index,left=cr,right=e2,elsewhenPart=NONE())),wc,_)
4258+
case (BackendDAE.WHEN_EQUATION(size=size,whenEquation = we as BackendDAE.WHEN_EQ(condition=cond,index=wc_index,left=cr,right=e2,elsewhenPart=NONE())),_,wc,_,_)
42584259
equation
42594260
expl = BackendEquation.getWhenCondition(wc,wc_index);
4260-
lst1 = incidenceRow1(expl, incidenceRowExp, vars, {},inIndexType);
4261+
lst1 = incidenceRow1(expl, incidenceRowExp, vars, iRow,inIndexType);
42614262
e1 = Expression.crefExp(cr);
42624263
lst1 = incidenceRowExp(cond, vars, lst1,inIndexType);
42634264
lst2 = incidenceRowExp(e1, vars, lst1,inIndexType);
42644265
res = incidenceRowExp(e2, vars, lst2,inIndexType);
42654266
then
42664267
(res,size);
4267-
case (vars,BackendDAE.WHEN_EQUATION(size=size,whenEquation = we as BackendDAE.WHEN_EQ(condition=cond,index=wc_index,left=cr,right=e2,elsewhenPart=SOME(elsewe))),wc,_)
4268+
case (BackendDAE.WHEN_EQUATION(size=size,whenEquation = we as BackendDAE.WHEN_EQ(condition=cond,index=wc_index,left=cr,right=e2,elsewhenPart=SOME(elsewe))),_,wc,_,_)
42684269
equation
42694270
expl = BackendEquation.getWhenCondition(wc,wc_index);
4270-
lst1 = incidenceRow1(expl, incidenceRowExp, vars, {},inIndexType);
4271+
lst1 = incidenceRow1(expl, incidenceRowExp, vars, iRow,inIndexType);
42714272
e1 = Expression.crefExp(cr);
42724273
lst1 = incidenceRowExp(cond, vars, lst1,inIndexType);
42734274
lst2 = incidenceRowExp(e1, vars, lst1,inIndexType);
@@ -4282,23 +4283,22 @@ algorithm
42824283
// If algorithm later on needs to be inverted, i.e. solved for
42834284
// different variables than calculated, a non linear solver or
42844285
// analysis of algorithm itself needs to be implemented.
4285-
case (vars,BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS(statementLst = statementLst)),_,_)
4286+
case (BackendDAE.ALGORITHM(size=size,alg=DAE.ALGORITHM_STMTS(statementLst = statementLst)),_,_,_,_)
42864287
equation
4287-
((_,res,_)) = traverseStmts(statementLst, incidenceRowAlgorithm, (vars, {},inIndexType));
4288+
((_,res,_)) = traverseStmts(statementLst, incidenceRowAlgorithm, (vars, iRow,inIndexType));
42884289
then
42894290
(res,size);
42904291

42914292
// if Equation
4292-
// TODO : how to handle this?
4293-
case(vars,BackendDAE.IF_EQUATION(conditions=expl,eqnstrue=eqnslst,eqnsfalse=eqns),_,_)
4293+
case(BackendDAE.IF_EQUATION(conditions=expl,eqnstrue=eqnslst,eqnsfalse=eqns),_,_,_,_)
42944294
equation
4295-
res = incidenceRow1(expl, incidenceRowExp, vars, {},inIndexType);
4296-
print("Warning: BackendDAEUtil.incidenceRow does not handle if-equations propper!\n");
4297-
4295+
res = incidenceRow1(expl, incidenceRowExp, vars, iRow,inIndexType);
4296+
(res,size) = incidenceRowLstLst(eqnslst,vars,inWhenClause,inIndexType,res,1);
4297+
(res,size) = incidenceRowLst(eqns,vars,inWhenClause,inIndexType,res,size);
42984298
then
4299-
(res,1);
4299+
(res,size);
43004300

4301-
case (_,_,_,_)
4301+
case (_,_,_,_,_)
43024302
equation
43034303
eqnstr = BackendDump.equationStr(inEquation);
43044304
print("- BackendDAE.incidenceRow failed for eqn: ");
@@ -4309,6 +4309,68 @@ algorithm
43094309
end matchcontinue;
43104310
end incidenceRow;
43114311

4312+
protected function incidenceRowLst
4313+
"function: incidenceRowLst
4314+
author: Frenkel TUD
4315+
Helper function to incidenceMatrix. Calculates the indidence row
4316+
in the matrix for if equation."
4317+
input list<BackendDAE.Equation> inEquation;
4318+
input Variables inVariables;
4319+
input list<WhenClause> inWhenClause;
4320+
input BackendDAE.IndexType inIndexType;
4321+
input list<Integer> inIntegerLst;
4322+
input Integer inRowSize;
4323+
output list<Integer> outIntegerLst;
4324+
output Integer rowSize;
4325+
algorithm
4326+
(outIntegerLst,rowSize) :=
4327+
match (inEquation,inVariables,inWhenClause,inIndexType,inIntegerLst,inRowSize)
4328+
local
4329+
Integer size;
4330+
list<Integer> row;
4331+
BackendDAE.Equation eqn;
4332+
list<BackendDAE.Equation> eqns;
4333+
case({},_,_,_,_,_) then (inIntegerLst,inRowSize);
4334+
case(eqn::eqns,_,_,_,_,_)
4335+
equation
4336+
(row,size) = incidenceRow(eqn,inVariables,inWhenClause,inIndexType,inIntegerLst);
4337+
(row,size) = incidenceRowLst(eqns,inVariables,inWhenClause,inIndexType,row,inRowSize+size);
4338+
then
4339+
(row,size);
4340+
end match;
4341+
end incidenceRowLst;
4342+
4343+
protected function incidenceRowLstLst
4344+
"function: incidenceRowLst
4345+
author: Frenkel TUD
4346+
Helper function to incidenceMatrix. Calculates the indidence row
4347+
in the matrix for if equation."
4348+
input list<list<BackendDAE.Equation>> inEquation;
4349+
input Variables inVariables;
4350+
input list<WhenClause> inWhenClause;
4351+
input BackendDAE.IndexType inIndexType;
4352+
input list<Integer> inIntegerLst;
4353+
input Integer inRowSize;
4354+
output list<Integer> outIntegerLst;
4355+
output Integer rowSize;
4356+
algorithm
4357+
(outIntegerLst,rowSize) :=
4358+
match (inEquation,inVariables,inWhenClause,inIndexType,inIntegerLst,inRowSize)
4359+
local
4360+
Integer size;
4361+
list<Integer> row;
4362+
list<BackendDAE.Equation> eqn;
4363+
list<list<BackendDAE.Equation>> eqns;
4364+
case({},_,_,_,_,_) then (inIntegerLst,inRowSize);
4365+
case(eqn::eqns,_,_,_,_,_)
4366+
equation
4367+
(row,size) = incidenceRowLst(eqn,inVariables,inWhenClause,inIndexType,inIntegerLst,inRowSize);
4368+
(row,size) = incidenceRowLstLst(eqns,inVariables,inWhenClause,inIndexType,row,size);
4369+
then
4370+
(row,size);
4371+
end match;
4372+
end incidenceRowLstLst;
4373+
43124374
protected function incidenceRowWhen
43134375
"function: incidenceRowWhen
43144376
author: Frenkel TUD
@@ -4973,7 +5035,7 @@ algorithm
49735035
abse = intAbs(e);
49745036
e_1 = abse - 1;
49755037
eqn = equationNth(daeeqns, e_1);
4976-
(row,_) = incidenceRow(vars, eqn,wc,BackendDAE.NORMAL());
5038+
(row,_) = incidenceRow(eqn,vars,wc,BackendDAE.NORMAL(),{});
49775039
oldvars = getOldVars(m,abse);
49785040
m_1 = Util.arrayReplaceAtWithFill(abse,row,{},m);
49795041
(_,outvars,invars) = List.intersection1OnTrue(oldvars,row,intEq);
@@ -5089,7 +5151,7 @@ algorithm
50895151
e_1 = abse - 1;
50905152
eqn = equationNth(daeeqns, e_1);
50915153
size = BackendEquation.equationSize(eqn);
5092-
(row,_) = incidenceRow(vars, eqn,wc,BackendDAE.NORMAL());
5154+
(row,_) = incidenceRow(eqn,vars,wc,BackendDAE.NORMAL(),{});
50935155
scalarindxs = iMapEqnIncRow[abse];
50945156
oldvars = getOldVars(m,listGet(scalarindxs,1));
50955157
(_,outvars,invars) = List.intersection1OnTrue(oldvars,row,intEq);
@@ -5138,7 +5200,7 @@ algorithm
51385200
e_1 = abse - 1;
51395201
eqn = equationNth(daeeqns, e_1);
51405202
rowsize = BackendEquation.equationSize(eqn);
5141-
(row,_) = incidenceRow(vars, eqn,wc,BackendDAE.NORMAL());
5203+
(row,_) = incidenceRow(eqn,vars,wc,BackendDAE.NORMAL(),{});
51425204
new_size = size+rowsize;
51435205
scalarindxs = List.intRange2(size+1,new_size);
51445206
mapEqnIncRow = arrayUpdate(iMapEqnIncRow,abse,scalarindxs);

0 commit comments

Comments
 (0)