Skip to content

Commit

Permalink
[DAEmode] Varios fixes including complex equations
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Feb 2, 2018
1 parent 70d5639 commit 17ce08d
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 57 deletions.
6 changes: 6 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -3070,6 +3070,12 @@ algorithm
list<Integer> irest;
Integer i;
case ({},{},_,_) then vars;
case (BackendDAE.VAR(varKind = BackendDAE.DAE_AUX_VAR())::rest,i::irest,_,_)
guard not AvlSetInt.hasKey(vars, i)
then incidenceRowExp1(rest,irest,AvlSetInt.add(vars, i),diffindex);
case (BackendDAE.VAR(varKind = BackendDAE.DAE_RESIDUAL_VAR())::rest,i::irest,_,_)
guard not AvlSetInt.hasKey(vars, i)
then incidenceRowExp1(rest,irest,AvlSetInt.add(vars, i),diffindex);
case (BackendDAE.VAR(varKind = BackendDAE.JAC_DIFF_VAR())::rest,i::irest,_,_)
guard not AvlSetInt.hasKey(vars, i)
then incidenceRowExp1(rest,irest,AvlSetInt.add(vars, i),diffindex);
Expand Down
37 changes: 24 additions & 13 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -1319,6 +1319,7 @@ public function equationToScalarResidualForm "author: Frenkel TUD 2012-06
This function transforms an equation to its scalar residual form.
For instance, a=b is transformed to a-b=0, and the instance {a[1], a[2]}=b to a[1]=b[1] and a[2]=b[2]"
input BackendDAE.Equation inEquation;
input DAE.FunctionTree funcTree;
output list<BackendDAE.Equation> outEquations;
algorithm

Expand All @@ -1335,7 +1336,7 @@ algorithm
list<list<DAE.Subscript>> subslst;
Real r;
BackendDAE.EquationAttributes attr;
list<DAE.ComponentRef> crlst;
list<DAE.ComponentRef> crlst, crlst2;

case (BackendDAE.EQUATION(exp=DAE.TUPLE(explst), scalar=e2, source=source, attr=attr)) equation
((_, eqns)) = List.fold3(explst,equationTupleToScalarResidualForm, e2, source, attr, (1, {}));
Expand Down Expand Up @@ -1371,28 +1372,37 @@ algorithm
eqns = List.map2(explst, generateRESIDUAL_EQUATION, source, attr);
then eqns;

case (BackendDAE.COMPLEX_EQUATION(left=DAE.CALL(expLst=explst,
attr=DAE.CALL_ATTR(DAE.T_COMPLEX(complexClassType=ClassInf.RECORD()))),
case (BackendDAE.COMPLEX_EQUATION(left=e1 as DAE.CALL(expLst=explst),
right=e2, source=source, attr=attr))
guard(Expression.isCref(e2))
guard(Expression.isRecordCall(e1, funcTree) and Expression.isCref(e2))
equation
crlst = ComponentReference.expandCref(Expression.expCref(e2),true);
explst2 = list(Expression.crefExp(c) for c in crlst);
explst = List.threadMap(explst, explst2, Expression.createResidualExp);
eqns = List.map2(explst, generateRESIDUAL_EQUATION, source, attr);
then eqns;

case (BackendDAE.COMPLEX_EQUATION(right=DAE.CALL(expLst=explst,
attr=DAE.CALL_ATTR(DAE.T_COMPLEX(complexClassType=ClassInf.RECORD()))),
case (BackendDAE.COMPLEX_EQUATION(right=e1 as DAE.CALL(expLst=explst),
left=e2, source=source, attr=attr))
guard(Expression.isCref(e2))
guard(Expression.isRecordCall(e1, funcTree) and Expression.isCref(e2))
equation
crlst = ComponentReference.expandCref(Expression.expCref(e2),true);
explst2 = list(Expression.crefExp(c) for c in crlst);
explst = List.threadMap(explst, explst2, Expression.createResidualExp);
eqns = List.map2(explst, generateRESIDUAL_EQUATION, source, attr);
then eqns;

case (BackendDAE.COMPLEX_EQUATION(left=e1, right=e2, source=source, attr=attr))
guard(Expression.isCref(e1) and Expression.isCref(e2))
equation
crlst = ComponentReference.expandCref(Expression.expCref(e1),true);
crlst2 = ComponentReference.expandCref(Expression.expCref(e2),true);
explst = list(Expression.crefExp(c) for c in crlst);
explst2 = list(Expression.crefExp(c) for c in crlst2);
explst = List.threadMap(explst, explst2, Expression.createResidualExp);
eqns = List.map2(explst, generateRESIDUAL_EQUATION, source, attr);
then eqns;

case (BackendDAE.COMPLEX_EQUATION(left=e1, right=e2, source=source, attr=attr)) equation
exp = Expression.createResidualExp(e1, e2);
(e, _) = ExpressionSimplify.simplify(exp);
Expand Down Expand Up @@ -1515,20 +1525,21 @@ end equationToResidualForm;
public function traverseEquationToScalarResidualForm
"author: Frenkel TUD 2010-11"
input BackendDAE.Equation inEq;
input list<BackendDAE.Equation> inEqs;
input tuple<DAE.FunctionTree, list<BackendDAE.Equation>> inEqs;
output BackendDAE.Equation outEq;
output list<BackendDAE.Equation> outEqs;
output tuple<DAE.FunctionTree, list<BackendDAE.Equation>> outEqs;
algorithm
(outEq,outEqs) := matchcontinue(inEq,inEqs)
local
list<BackendDAE.Equation> eqns,reqn;
BackendDAE.Equation eqn;
DAE.FunctionTree funcs;

case (eqn, eqns)
case (eqn, (funcs, eqns))
equation
reqn = equationToScalarResidualForm(eqn);
reqn = equationToScalarResidualForm(eqn, funcs);
eqns = listAppend(reqn,eqns);
then (eqn, eqns);
then (eqn, (funcs, eqns));

else (inEq,inEqs);
end matchcontinue;
Expand Down Expand Up @@ -2234,7 +2245,7 @@ algorithm
end matchcontinue;
end solveEquation;

protected function generateRESIDUAL_EQUATION "author: Frenkel TUD 2010-05"
public function generateRESIDUAL_EQUATION "author: Frenkel TUD 2010-05"
input DAE.Exp inExp;
input DAE.ElementSource inSource;
input BackendDAE.EquationAttributes inEqAttr;
Expand Down
123 changes: 96 additions & 27 deletions Compiler/BackEnd/DAEMode.mo
Expand Up @@ -59,8 +59,11 @@ import DAE;
import ErrorExt;
import ExecStat.execStat;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Global;
import Initialization;
import List;
import StackOverflow;
import Util;

Expand Down Expand Up @@ -155,7 +158,7 @@ end getEqSystemDAEmode;
// =============================================================================

protected
type TraverseEqnAryFold = tuple <BackendDAE.BackendDAEModeData, BackendDAE.Variables>;
type TraverseEqnAryFold = tuple <BackendDAE.BackendDAEModeData, BackendDAE.Variables, DAE.FunctionTree>;

public function createDAEmodeBDAE
" This modules creates a BDAE with residual Variables."
Expand All @@ -180,12 +183,12 @@ protected
constant Boolean debug = false;
algorithm
extraArgs := BackendDAE.BDAE_MODE_DATA(shared.daeModeData.numAuxVars,{},shared.daeModeData.numResVars,{},shared.daeModeData.numAuxEqns,{},shared.daeModeData.numResEqns,{});
travArgs := (extraArgs, syst.orderedVars);
travArgs := (extraArgs, syst.orderedVars, shared.functionTree);
if debug then BackendDump.printEqSystem(syst); end if;
// for every equation create corresponding residiual variable(s)
travArgs := BackendEquation.traverseEquationArray(syst.orderedEqs,
traverserEqnAryAuxRes, travArgs);
(extraArgs, vars) := travArgs;
(extraArgs, vars, _) := travArgs;
syst.orderedVars := vars;

// push ordered variables to known vars
Expand Down Expand Up @@ -224,87 +227,153 @@ algorithm
BackendDAE.Var dummyVar;
BackendDAE.Equation eq;
Integer newnumResVars;
DAE.Exp exp;
DAE.Exp exp, exp2;
BackendDAE.BackendDAEModeData travArgs;
DAE.ComponentRef cref;
String str;
DAE.ComponentRef cref, newCref;
DAE.FunctionTree funcsTree;
list<DAE.ComponentRef> crlst;

case (eq as BackendDAE.EQUATION(exp=exp)) guard( CommonSubExpression.isCSEExp(exp))
constant Boolean debug = false;

case (eq as BackendDAE.EQUATION(exp=exp)) guard(CommonSubExpression.isCSEExp(exp))
equation
(travArgs, orderedVars, funcsTree) = extraArgs;
cref = Expression.expCref(exp);
(newResVars,_) = BackendVariable.getVar(cref, orderedVars);
newResVars = list(BackendVariable.setVarKind(v, BackendDAE.DAE_AUX_VAR()) for v in newResVars);
travArgs.auxVars = listAppend(newResVars, travArgs.auxVars);
travArgs.auxEqns = listAppend({eq}, travArgs.auxEqns);
orderedVars = BackendVariable.removeCref(cref, orderedVars);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

case (eq as BackendDAE.COMPLEX_EQUATION(left=exp)) guard(CommonSubExpression.isCSEExp(exp))
equation
(travArgs, orderedVars) = extraArgs;
if debug then print("case: Complex Left: " + BackendDump.equationString(orgEquation) + "\n"); end if;
(travArgs, orderedVars, funcsTree) = extraArgs;
cref = Expression.expCref(exp);
if debug then print("Got cref from exp: " + ExpressionDump.printExpStr(exp) + "\n"); end if;
(newResVars,_) = BackendVariable.getVar(cref, orderedVars);
if debug then print("Got vars: " + BackendDump.varListString(newResVars, "newVars") + "\n"); end if;
newResVars = list(BackendVariable.setVarKind(v, BackendDAE.DAE_AUX_VAR()) for v in newResVars);
if debug then print("Created new vars: " + BackendDump.varListString(newResVars, "newVars") + "\n"); end if;
travArgs.auxVars = listAppend(newResVars, travArgs.auxVars);
travArgs.auxEqns = listAppend({eq}, travArgs.auxEqns);
orderedVars = BackendVariable.removeCref(cref, orderedVars);
extraArgs = (travArgs, orderedVars);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

case (eq as BackendDAE.EQUATION())
equation
(travArgs, orderedVars) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq);
(travArgs, orderedVars, funcsTree) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq, funcsTree);
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
dummyVar = BackendVariable.setVarKind(dummyVar, BackendDAE.DAE_RESIDUAL_VAR());
(newResEqns, newResVars, newnumResVars) = BackendEquation.convertResidualsIntoSolvedEquations(newResEqns,
"$DAEres", dummyVar, travArgs.numResVars);
travArgs.numResVars = newnumResVars;
travArgs.resVars = listAppend(newResVars, travArgs.resVars);
travArgs.resEqns = listAppend(newResEqns, travArgs.resEqns);
extraArgs = (travArgs, orderedVars);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

case (eq as BackendDAE.SOLVED_EQUATION())
equation
(travArgs, orderedVars) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq);
(travArgs, orderedVars, funcsTree) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq, funcsTree);
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
dummyVar = BackendVariable.setVarKind(dummyVar, BackendDAE.DAE_RESIDUAL_VAR());
(newResEqns, newResVars, newnumResVars) = BackendEquation.convertResidualsIntoSolvedEquations(newResEqns,
"$DAEres", dummyVar, travArgs.numResVars);
travArgs.numResVars = newnumResVars;
travArgs.resVars = listAppend(newResVars, travArgs.resVars);
travArgs.resEqns = listAppend(newResEqns, travArgs.resEqns);
extraArgs = (travArgs, orderedVars);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

case (eq as BackendDAE.ARRAY_EQUATION())
equation
(travArgs, orderedVars) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq);
(travArgs, orderedVars, funcsTree) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq, funcsTree);
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
dummyVar = BackendVariable.setVarKind(dummyVar, BackendDAE.DAE_RESIDUAL_VAR());
(newResEqns, newResVars, newnumResVars) = BackendEquation.convertResidualsIntoSolvedEquations(newResEqns,
"$DAEres", dummyVar, travArgs.numResVars);
travArgs.numResVars = newnumResVars;
travArgs.resVars = listAppend(listReverse(newResVars), travArgs.resVars);
travArgs.resVars = listAppend(newResVars, travArgs.resVars);
travArgs.resEqns = listAppend(newResEqns, travArgs.resEqns);
extraArgs = (travArgs, orderedVars);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

case (eq as BackendDAE.COMPLEX_EQUATION())
case (eq as BackendDAE.COMPLEX_EQUATION(left=exp, right=exp2))
equation
(travArgs, orderedVars) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq);
if debug then print("case: Complex: " + BackendDump.equationString(orgEquation) + "\n"); end if;
/*
if debug then print("case: left: " + ExpressionDump.dumpExpStr(exp ,0) + "\n"); end if;
if debug then print("case: right: " + ExpressionDump.dumpExpStr(exp2,0) + "\n"); end if;
*/
(travArgs, orderedVars, funcsTree) = extraArgs;
newResEqns = BackendEquation.equationToScalarResidualForm(eq, funcsTree);
if debug then print("case: new eqns:\n" + BackendDump.dumpEqnsStr(newResEqns) + "\n"); end if;
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
dummyVar = BackendVariable.setVarKind(dummyVar, BackendDAE.DAE_RESIDUAL_VAR());
(newResEqns, newResVars, newnumResVars) = BackendEquation.convertResidualsIntoSolvedEquations(newResEqns,
"$DAEres", dummyVar, travArgs.numResVars);
travArgs.numResVars = newnumResVars;
travArgs.resVars = listAppend(newResVars, travArgs.resVars);
travArgs.resEqns = listAppend(newResEqns, travArgs.resEqns);
extraArgs = (travArgs, orderedVars);
travArgs.resVars = listAppend(listReverse(newResVars), travArgs.resVars);
travArgs.resEqns = listAppend(listReverse(newResEqns), travArgs.resEqns);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);

// recordCref = f(...)
case (eq as BackendDAE.COMPLEX_EQUATION(left=exp))
guard(Expression.isCref(exp))
equation
if debug then print("case: Complex: " + BackendDump.equationString(orgEquation) + "\n"); end if;
/*
if debug then print("case: left: " + ExpressionDump.dumpExpStr(exp ,0) + "\n"); end if;
if debug then print("case: right: " + ExpressionDump.dumpExpStr(exp2,0) + "\n"); end if;
*/
(travArgs, orderedVars, funcsTree) = extraArgs;
cref = Expression.expCref(exp);

// create aux variables for recordCref
(newResVars,_) = BackendVariable.getVar(cref, orderedVars);
crlst = list(BackendVariable.varCref(v) for v in newResVars);
newResVars = list(BackendVariable.copyVarNewName(ComponentReference.crefPrefixAux(cr), v) threaded for cr in crlst, v in newResVars);
newResVars = list(BackendVariable.setVarKind(v, BackendDAE.DAE_AUX_VAR()) for v in newResVars);
travArgs.auxVars = listAppend(newResVars, travArgs.auxVars);

// create aux equation aux = f(...)
newCref = ComponentReference.crefPrefixAux(cref);
eq.left = Expression.crefExp(newCref);
travArgs.auxEqns = listAppend({eq}, travArgs.auxEqns);

// prepare res equation aux = recordCref
eq.right = Expression.crefExp(cref);
newResEqns = BackendEquation.equationToScalarResidualForm(eq, funcsTree);
if debug then print("case: new eqns:\n" + BackendDump.dumpEqnsStr(newResEqns) + "\n"); end if;
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
dummyVar = BackendVariable.setVarKind(dummyVar, BackendDAE.DAE_RESIDUAL_VAR());
(newResEqns, newResVars, newnumResVars) = BackendEquation.convertResidualsIntoSolvedEquations(newResEqns,
"$DAEres", dummyVar, travArgs.numResVars);
travArgs.numResVars = newnumResVars;
travArgs.resVars = listAppend(listReverse(newResVars), travArgs.resVars);
travArgs.resEqns = listAppend(listReverse(newResEqns), travArgs.resEqns);
extraArgs = (travArgs, orderedVars, funcsTree);
then
(extraArgs);


else equation
str = BackendDump.equationString(orgEquation);
Error.addInternalError("DAEMode.traverserEqnAryAuxRes failed on equation:\n" + str + "\n", sourceInfo());
Error.addInternalError("DAEMode.traverserEqnAryAuxRes failed on equation:\n" +
BackendDump.equationString(orgEquation) + "\n", sourceInfo());
then fail();
end matchcontinue;
end traverserEqnAryAuxRes;
Expand Down
7 changes: 4 additions & 3 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -2370,6 +2370,7 @@ protected function prepareTornStrongComponentData
input list<Integer> inIterationvarsInts;
input list<Integer> inResidualequations;
input BackendDAE.InnerEquations innerEquations;
input DAE.FunctionTree funcTree;
output BackendDAE.Variables outDiffVars;
output BackendDAE.Variables outResidualVars;
output BackendDAE.Variables outOtherVars;
Expand Down Expand Up @@ -2397,7 +2398,7 @@ try
reqns := BackendEquation.replaceDerOpInEquationList(reqns);
outResidualEqns := BackendEquation.listEquation(reqns);
// create residual equations
reqns := BackendEquation.traverseEquationArray(outResidualEqns, BackendEquation.traverseEquationToScalarResidualForm, {});
(_, reqns) := BackendEquation.traverseEquationArray(outResidualEqns, BackendEquation.traverseEquationToScalarResidualForm, (funcTree, {}));
reqns := listReverse(reqns);
(reqns, resVarsLst) := BackendEquation.convertResidualsIntoSolvedEquations(reqns, "$res", BackendVariable.makeVar(DAE.emptyCref), 1);
outResidualVars := BackendVariable.listVar1(resVarsLst);
Expand Down Expand Up @@ -2494,7 +2495,7 @@ algorithm
print("*** "+ prename + "-JAC *** start creating Jacobian for a torn system " + name + " of size " + intString(listLength(inTearingSet.tearingvars)) + " time: " + realString(clock()) + "\n");
end if;

(diffVars, resVars, oVars, resEqns, oEqns) := prepareTornStrongComponentData(inVars, inEqns, inTearingSet.tearingvars, inTearingSet.residualequations, inTearingSet.innerEquations);
(diffVars, resVars, oVars, resEqns, oEqns) := prepareTornStrongComponentData(inVars, inEqns, inTearingSet.tearingvars, inTearingSet.residualequations, inTearingSet.innerEquations, inShared.functionTree);

if debug then
print("*** "+ prename + "-JAC *** prepared all data for differentiation at time: " + realString(clock()) + "\n");
Expand Down Expand Up @@ -2589,7 +2590,7 @@ algorithm

eqns = BackendEquation.listEquation(reqns);
// create residual equations
reqns = BackendEquation.traverseEquationArray(eqns, BackendEquation.traverseEquationToScalarResidualForm, {});
(_, reqns) = BackendEquation.traverseEquationArray(eqns, BackendEquation.traverseEquationToScalarResidualForm, (inShared.functionTree, {}));
reqns = listReverse(reqns);
(reqns, resVarsLst) = BackendEquation.convertResidualsIntoSolvedEquations(reqns, "$res", BackendVariable.makeVar(DAE.emptyCref), 1);
resVars = BackendVariable.listVar1(resVarsLst);
Expand Down
8 changes: 8 additions & 0 deletions Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -2078,6 +2078,14 @@ algorithm
outCref := makeCrefQual(DAE.previousNamePrefix, DAE.T_UNKNOWN_DEFAULT, {}, inCref);
end crefPrefixPrevious;

public function crefPrefixAux "public function crefPrefixAux
Appends $AUX to a cref, so a => $AUX.a"
input DAE.ComponentRef inCref;
output DAE.ComponentRef outCref;
algorithm
outCref := makeCrefQual(DAE.auxNamePrefix, DAE.T_REAL_DEFAULT, {}, inCref);
end crefPrefixAux;

public function crefRemovePrePrefix
input output DAE.ComponentRef cref;
algorithm
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -64,6 +64,7 @@ public constant String derivativeNamePrefix = "$DER";
public constant String preNamePrefix = "$PRE";
public constant String previousNamePrefix = "$CLKPRE";
public constant String startNamePrefix = "$START";
public constant String auxNamePrefix = "$AUX";


public uniontype VarKind
Expand Down

0 comments on commit 17ce08d

Please sign in to comment.