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

Commit ab3e53f

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
fix daeMode for records fucntions
1 parent b6789ac commit ab3e53f

File tree

1 file changed

+73
-92
lines changed

1 file changed

+73
-92
lines changed

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 73 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ algorithm
448448

449449
// add residuals vars from DAE creation
450450
if Flags.getConfigEnum(Flags.DAE_MODE)>1 then
451-
daeModeSP := createDaeModeSparsePattern(bdaeModeVars, bdaeModeEqns, shared, crefToSimVarHT);
451+
daeModeSP := createDaeModeSparsePattern(bdaeModeVars, bdaeModeEqns, residualVars, shared, crefToSimVarHT);
452452

453453
residualVars := rewriteIndex(residualVars, 0);
454454
crefToSimVarHT:= List.fold(residualVars,addSimVarToHashTable,crefToSimVarHT);
@@ -1815,7 +1815,7 @@ algorithm
18151815
if debug then
18161816
print("DAE Mode debug output for one system:\n");
18171817
print("DAE Mode equations:\n");
1818-
print(Tpl.tplString3(TaskSystemDump.dumpEqs, daeEquations1, 0, false));
1818+
dumpSimEqSystemLst(daeEquations1,"\n");
18191819
print("DAE Mode residual variables:\n");
18201820
print(Tpl.tplString(SimCodeDump.dumpVarsShort, resVars1));
18211821
print("DAE Mode algebraic variables:\n");
@@ -1897,7 +1897,7 @@ protected
18971897
list<SimCodeVar.SimVar> resVarsTmp = {};
18981898
list<SimCodeVar.SimVar> algVarsTmp = {};
18991899

1900-
list<BackendDAE.Equation> eqnlst, alglst;
1900+
list<BackendDAE.Equation> eqnlst, orgeqnLst, alglst;
19011901
list<BackendDAE.Var> varlst, algVarlst, tmpVarsLst;
19021902
BackendDAE.Var dummyVar;
19031903

@@ -1908,17 +1908,17 @@ algorithm
19081908
(stateeqnsmark, syst, shared) := inArg;
19091909
(daeEquations, resVars, algVars, uniqueEqIndex, varIndex, tempvars, bdaeVars, bdaeEqns) := inFold;
19101910
emptyVars := BackendVariable.emptyVars();
1911-
(varlst,varNums,eqnlst,eqnNums) := BackendDAEUtil.getStrongComponentVarsAndEquations(comp, syst.orderedVars, syst.orderedEqs);
1911+
(varlst,varNums,orgeqnLst,eqnNums) := BackendDAEUtil.getStrongComponentVarsAndEquations(comp, syst.orderedVars, syst.orderedEqs);
19121912
skip := false;
19131913

19141914
if debug then
19151915
print("Proceed component: " + BackendDump.strongComponentString(comp) + "\n");
1916-
BackendDump.dumpEquationList(eqnlst,"Equations:");
1916+
BackendDump.dumpEquationList(orgeqnLst,"Equations:");
19171917
BackendDump.dumpVarList(varlst,"Variables:");
19181918
end if;
19191919

19201920
// skip is when equations
1921-
skip := Util.boolAndList(List.map(eqnlst, BackendEquation.isWhenEquation));
1921+
skip := Util.boolAndList(List.map(orgeqnLst, BackendEquation.isWhenEquation));
19221922
// skip is discrete
19231923
skip := Util.boolAndList(List.map(varlst, BackendVariable.isVarDiscrete)) or skip;
19241924
// skip algebraic equation if dynamic option is selected
@@ -1927,13 +1927,14 @@ algorithm
19271927
// convert only dynamic block here
19281928
if skipEquations and not skip then
19291929

1930-
// try as is should fallback case is a hack for complex record equations
1930+
// try as it is
19311931
try
19321932
// make residual equations => 0 = f(x,xd,y)
1933-
eqnlst := List.flattenReverse(List.map(eqnlst, BackendEquation.equationToScalarResidualForm));
1933+
eqnlst := List.flattenReverse(List.map(orgeqnLst, BackendEquation.equationToScalarResidualForm));
19341934
tmpEqns := {};
1935+
// this fallback case is a hack for complex record equations
19351936
else
1936-
(eqnlst, tmpEqns, uniqueEqIndex, tempvars) := createDAEResidualComplexEquation(eqnlst, uniqueEqIndex, tempvars);
1937+
(eqnlst, tmpEqns, uniqueEqIndex, tempvars) := createDAEResidualComplexEquation(orgeqnLst, uniqueEqIndex, tempvars, shared.functionTree);
19371938
end try;
19381939

19391940
// add residual var => $DAEres = f(x,xd,y)
@@ -1954,12 +1955,14 @@ algorithm
19541955

19551956
//collect also the BackendDAE versions for sparsity and jacobains
19561957
bdaeVars := BackendVariable.addVars(varlst, bdaeVars);
1957-
bdaeEqns := BackendEquation.addEquations(eqnlst, bdaeEqns);
1958+
bdaeEqns := BackendEquation.addEquations(listReverse(orgeqnLst), bdaeEqns);
19581959

19591960
// result --> resVarsTmp, algVarsTmp, daeEquationsTmp, bdaeVars, bdaeEqns
19601961
end if;
19611962

19621963
if debug then
1964+
print("Result residual equations\n");
1965+
dumpSimEqSystemLst(daeEquationsTmp,"\n");
19631966
print("Result residual variables\n");
19641967
print(Tpl.tplString(SimCodeDump.dumpVarsShort, resVarsTmp));
19651968
print("Result algebraic variables\n");
@@ -1984,6 +1987,7 @@ protected function createDaeModeSparsePattern
19841987
since no other dependencies are necessary "
19851988
input BackendDAE.Variables inVars;
19861989
input BackendDAE.EquationArray inEqns;
1990+
input list<SimCodeVar.SimVar> inResidualVarsLst;
19871991
input BackendDAE.Shared inShared;
19881992
input SimCode.HashTableCrefToSimVar crefToSimVarHT;
19891993
output Option<SimCode.JacobianMatrix> outSimCodeJac;
@@ -1997,7 +2001,7 @@ protected
19972001
list<list<DAE.ComponentRef>> translated;
19982002
list<tuple<DAE.ComponentRef,list<DAE.ComponentRef>>> sparsetuple, sparsetupleT;
19992003
array<DAE.ComponentRef> varCrefs;
2000-
list<DAE.ComponentRef> varCrefsList;
2004+
list<DAE.ComponentRef> varCrefsList, resVarCrefsList;
20012005
array<list<Integer>> coloredArray;
20022006
BackendDAE.SparseColoring coloring;
20032007
SimCode.JacobianMatrix simCodeJac;
@@ -2009,21 +2013,23 @@ protected
20092013
SimCodeVar.SimVar svar; BackendDAE.Var bvar;
20102014
algorithm
20112015
try
2016+
resVarCrefsList := List.map(inResidualVarsLst, getSimVarCompRef);
20122017
// sort variables in SimCode order
20132018
inVarsLst := BackendVariable.varList(inVars);
2014-
varCrefsList := listReverse(List.map(inVarsLst, BackendVariable.varCref));
2015-
inSimVars := getSimVars2Crefs(varCrefsList, crefToSimVarHT);
2019+
varCrefsList := List.map(inVarsLst, BackendVariable.varCref);
2020+
inSimVars := listReverse(getSimVars2Crefs(varCrefsList, crefToSimVarHT));
20162021
tupleVars := List.threadTuple(inSimVars, inVarsLst);
20172022
tupleVars := List.sort(tupleVars, compareSimVarTupleIndexGt);
20182023
inVarsLst := List.unzipSecond(tupleVars);
2019-
sortedVars := BackendVariable.listVar(listReverse(inVarsLst));
20202024
varCrefsList := List.map(inVarsLst, BackendVariable.varCref);
2025+
sortedVars := BackendVariable.listVar1(inVarsLst);
20212026

20222027
// create adjacency matrix
20232028
funcs := BackendDAEUtil.getFunctions(inShared);
20242029
system := BackendDAEUtil.createEqSystem(sortedVars, inEqns);
20252030
(system,_,_,_,_) := BackendDAEUtil.getIncidenceMatrixScalar(system, BackendDAE.SOLVABLE(), SOME(funcs));
20262031
if debug then
2032+
dumpVarLst(inSimVars,"sortedVars ("+intString(listLength(inSimVars))+")");
20272033
print("createDaeModeSparsePattern DAE-System:\n");
20282034
BackendDump.printEqSystem(system);
20292035
end if;
@@ -2053,6 +2059,10 @@ algorithm
20532059
// build sparse pattern
20542060
bdaeSP :=(sparsetuple, sparsetupleT, (varCrefsList, varCrefsList), nonZeroElements);
20552061

2062+
if debug then
2063+
BackendDump.dumpSparsityPattern(bdaeSP, "DAE mode sparsity pattern");
2064+
end if;
2065+
20562066
// get coloring
20572067
coloredArray := SymbolicJacobian.createColoring(intsp, intspT, listLength(varCrefsList), listLength(varCrefsList));
20582068
coloring := list(list(arrayGet(varCrefs, i) for i in lst) for lst in coloredArray);
@@ -2100,24 +2110,25 @@ protected function createDAEResidualComplexEquation
21002110
input list<BackendDAE.Equation> inEquation;
21012111
input Integer iuniqueEqIndex;
21022112
input list<SimCodeVar.SimVar> itempvars;
2113+
input DAE.FunctionTree funcsIn;
21032114
output list<BackendDAE.Equation> outResEqn;
21042115
output list<SimCode.SimEqSystem> equations_;
21052116
output Integer ouniqueEqIndex;
21062117
output list<SimCodeVar.SimVar> otempvars;
21072118
protected
2108-
DAE.Exp inExp, inExp1;
2119+
DAE.Exp right, left;
21092120
DAE.ElementSource source;
21102121
BackendDAE.EquationAttributes inEqAttr;
21112122
BackendDAE.Equation eqn;
21122123
algorithm
21132124
eqn::{} := inEquation;
2114-
BackendDAE.COMPLEX_EQUATION(left = inExp, right = inExp1, source=source, attr=inEqAttr) := eqn;
2115-
(equations_, ouniqueEqIndex, otempvars) := matchcontinue (inExp, inExp1)
2125+
BackendDAE.COMPLEX_EQUATION(left = left, right = right, source=source, attr=inEqAttr) := eqn;
2126+
(equations_, ouniqueEqIndex, otempvars) := matchcontinue (left, right)
21162127
local
21172128
DAE.ComponentRef cr, crtmp;
21182129
list<DAE.ComponentRef> crlst;
21192130
list<list<DAE.ComponentRef>> crlstlst;
2120-
DAE.Exp e1, e2, e1_1, e2_1, etmp;
2131+
DAE.Exp etmp, crefExp, recordExp, callExp;
21212132
DAE.Statement stms;
21222133
DAE.Type tp;
21232134
list<DAE.Type> tplst;
@@ -2132,39 +2143,28 @@ algorithm
21322143
String ident, s, s1, s2;
21332144
list<SimCodeVar.SimVar> tempvars;
21342145

2135-
/* a = f() */
2136-
case (DAE.CREF(componentRef=cr), _) equation
2137-
// ((e1_1, _)) = Expression.extendArrExp((inExp, false));
2138-
(e2_1, _) = Expression.extendArrExp(inExp1, false);
2139-
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
2140-
(tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path))) = Expression.typeof(inExp);
2141-
// tmp
2142-
ident = Absyn.pathStringUnquoteReplaceDot(path, "_");
2143-
crtmp = ComponentReference.makeCrefIdent("$TMP_" + ident + intString(iuniqueEqIndex), tp, {});
2144-
tempvars = createTempVars(varLst, crtmp, itempvars);
2145-
// 0 = a - tmp
2146-
e1lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, cr);
2147-
e2lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, crtmp);
2148-
exptl = List.threadTuple(e1lst, e2lst);
2149-
outResEqn = List.map2(exptl, BackendEquation.generateRESIDUAL_EQUATION1, source, inEqAttr);
2150-
uniqueEqIndex = iuniqueEqIndex;
2146+
/* a = f() or f() = a */
2147+
case (_, _) guard (Expression.isCref(left) or Expression.isCref(right))
2148+
equation
2149+
if Expression.isCref(left) then
2150+
crefExp = left;
2151+
callExp = right;
2152+
else
2153+
crefExp = right;
2154+
callExp = left;
2155+
end if;
21512156

2152-
// tmp = f(x, y)
2153-
etmp = Expression.crefExp(crtmp);
2154-
stms = DAE.STMT_ASSIGN(tp, etmp, e2_1, source);
2155-
eqSystlst = {SimCode.SES_ALGORITHM(uniqueEqIndex, {stms})};
2156-
then (eqSystlst, uniqueEqIndex+1, tempvars);
2157+
//get cref from crefExp before expand
2158+
cr = Expression.getCrefFromCrefOrAsub(crefExp);
21572159

2158-
// f() = a
2159-
case (_, DAE.CREF(componentRef=cr)) equation
2160-
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
2161-
(e1_1, _) = Expression.extendArrExp(inExp, false);
2162-
// ((e2_1, _)) = Expression.extendArrExp((inExp1, false));
2163-
(tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path))) = Expression.typeof(inExp1);
2164-
// tmp
2160+
(crefExp, _) = Expression.extendArrExp(crefExp, false);
2161+
(tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path))) = Expression.typeof(callExp);
2162+
2163+
// create tmp vars
21652164
ident = Absyn.pathStringUnquoteReplaceDot(path, "_");
21662165
crtmp = ComponentReference.makeCrefIdent("$TMP_" + ident + intString(iuniqueEqIndex), tp, {});
21672166
tempvars = createTempVars(varLst, crtmp, itempvars);
2167+
21682168
// 0 = a - tmp
21692169
e1lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, cr);
21702170
e2lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, crtmp);
@@ -2174,66 +2174,47 @@ algorithm
21742174

21752175
// tmp = f(x, y)
21762176
etmp = Expression.crefExp(crtmp);
2177-
stms = DAE.STMT_ASSIGN(tp, etmp, e1_1, source);
2177+
stms = DAE.STMT_ASSIGN(tp, etmp, callExp, source);
21782178
eqSystlst = {SimCode.SES_ALGORITHM(uniqueEqIndex, {stms})};
21792179
then (eqSystlst, uniqueEqIndex+1, tempvars);
21802180

2181-
// Record() = f()
2182-
case (DAE.CALL(path=path, expLst=e2lst, attr=DAE.CALL_ATTR(ty= tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(rpath)))), _) equation
2183-
true = Absyn.pathEqual(path, rpath);
2184-
(e2_1, _) = Expression.extendArrExp(inExp1, false);
2185-
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
2181+
case (_, _) guard (Expression.isRecordCall(left, funcsIn) or Expression.isRecordCall(right, funcsIn))
2182+
equation
2183+
if Expression.isRecordCall(left, funcsIn) then
2184+
recordExp = left;
2185+
callExp = right;
2186+
else
2187+
recordExp = right;
2188+
callExp = left;
2189+
end if;
2190+
2191+
DAE.CALL(expLst=expl, attr=DAE.CALL_ATTR(ty= tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(path)))) = recordExp;
2192+
21862193
// tmp = f()
21872194
ident = Absyn.pathStringUnquoteReplaceDot(path, "_");
2188-
cr = ComponentReference.makeCrefIdent("$TMP_" + ident + intString(iuniqueEqIndex), tp, {});
2189-
e1_1 = Expression.crefToExp(cr);
2190-
stms = DAE.STMT_ASSIGN(tp, e1_1, e2_1, source);
2195+
crtmp = ComponentReference.makeCrefIdent("$TMP_" + ident + intString(iuniqueEqIndex), tp, {});
2196+
tempvars = createTempVars(varLst, crtmp, itempvars);
2197+
etmp = Expression.crefToExp(crtmp);
2198+
stms = DAE.STMT_ASSIGN(tp, etmp, callExp, source);
21912199
simeqn = SimCode.SES_ALGORITHM(iuniqueEqIndex, {stms});
21922200
uniqueEqIndex = iuniqueEqIndex + 1;
2201+
eqSystlst = {simeqn};
21932202

2194-
// Record()-tmp = 0
2195-
// Expand the tmp record and any arrays
2196-
e1lst = Expression.expandExpression(e1_1);
2203+
// 0 = tmp - Record()
2204+
// Expand the record for any arrays
2205+
e1lst = List.mapFlat(expl, Expression.expandExpression);
21972206
// Expand the varLst. Each var might be an array or record.
2198-
e2lst = List.mapFlat(e2lst, Expression.expandExpression);
2207+
e2lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, crtmp);
21992208
// pair each of the expanded expressions to coressponding one
22002209
exptl = List.threadTuple(e1lst, e2lst);
22012210
// Create residual equations for each pair
22022211
outResEqn = List.map2(exptl, BackendEquation.generateRESIDUAL_EQUATION1, source, inEqAttr);
2203-
uniqueEqIndex = iuniqueEqIndex;
2204-
2205-
eqSystlst = {simeqn};
2206-
2207-
tempvars = createTempVars(varLst, cr, itempvars);
2208-
then (eqSystlst, uniqueEqIndex, tempvars);
2209-
2210-
// f() = Record()
2211-
case (_, DAE.CALL(path=path, expLst=e2lst, attr=DAE.CALL_ATTR(ty=tp as DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(rpath))))) equation
2212-
true = Absyn.pathEqual(path, rpath);
2213-
(e1_1, _) = Expression.extendArrExp(inExp1, false);
2214-
// true = ComponentReference.crefEqualNoStringCompare(cr, cr2);
2215-
// tmp = f()
2216-
ident = Absyn.pathStringUnquoteReplaceDot(path, "_");
2217-
cr = ComponentReference.makeCrefIdent("$TMP_" + ident + intString(iuniqueEqIndex), tp, {});
2218-
e2_1 = Expression.crefExp(cr);
2219-
stms = DAE.STMT_ASSIGN(tp, e2_1, e1_1, source);
2220-
simeqn = SimCode.SES_ALGORITHM(iuniqueEqIndex, {stms});
2221-
uniqueEqIndex = iuniqueEqIndex + 1;
2222-
// Record()-tmp = 0
2223-
e1lst = List.map1(varLst, Expression.generateCrefsExpFromExpVar, cr);
2224-
exptl = List.threadTuple(e1lst, e2lst);
2225-
outResEqn = List.map2(exptl, BackendEquation.generateRESIDUAL_EQUATION1, source, inEqAttr);
2226-
uniqueEqIndex = iuniqueEqIndex;
2227-
2228-
eqSystlst = {simeqn};
2229-
2230-
tempvars = createTempVars(varLst, cr, itempvars);
22312212
then (eqSystlst, uniqueEqIndex, tempvars);
22322213

22332214
// failure
22342215
else equation
2235-
s1 = ExpressionDump.printExpStr(inExp);
2236-
s2 = ExpressionDump.printExpStr(inExp1);
2216+
s1 = ExpressionDump.printExpStr(left);
2217+
s2 = ExpressionDump.printExpStr(right);
22372218
s = stringAppendList({"function createDAEResidualComplexEquation failed for: ", s1, " = " , s2 });
22382219
Error.addInternalError(s, sourceInfo());
22392220
then fail();
@@ -9636,9 +9617,9 @@ algorithm
96369617
result := index1 > index2;
96379618
end compareVarIndexGt;
96389619

9639-
public function compareSimVarTupleIndexGt
9640-
input tuple<SimCodeVar.SimVar, BackendDAE.Var> var1;
9641-
input tuple<SimCodeVar.SimVar, BackendDAE.Var> var2;
9620+
public function compareSimVarTupleIndexGt<anyT>
9621+
input tuple<SimCodeVar.SimVar, anyT> var1;
9622+
input tuple<SimCodeVar.SimVar, anyT> var2;
96429623
output Boolean result;
96439624
protected
96449625
Integer index1, index2;

0 commit comments

Comments
 (0)