Skip to content

Commit

Permalink
[DAEmode] improving array equations support
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Mar 5, 2018
1 parent c5d63e7 commit 04159c5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -9078,8 +9078,8 @@ algorithm
end for;
else
(_, _, name) := System.dladdr(func);
Error.addInternalError("BackendDAEUtil.traverseEqSystemStrongComponents failed
with function:\n" + name + "\n", sourceInfo());
Error.addInternalError("BackendDAEUtil.traverseEqSystemStrongComponents failed " +
"with function:\n" + name + "\n", sourceInfo());
fail();
end try;
end traverseEqSystemStrongComponents;
Expand Down
77 changes: 70 additions & 7 deletions Compiler/BackEnd/DAEMode.mo
Expand Up @@ -265,7 +265,7 @@ algorithm
BackendDAE.Variables systemVars;
BackendDAE.Var dummyVar;
BackendDAE.Equation eq, new_eq, aux_eq;
Integer newnumResVars;
Integer size, newnumResVars;
DAE.Exp exp, exp2;
BackendDAE.BackendDAEModeData globalDAEData;
DAE.ComponentRef cref, newCref;
Expand Down Expand Up @@ -321,6 +321,19 @@ algorithm
then
(traverserArgs);

case ({eq as BackendDAE.ARRAY_EQUATION()}, vars)
guard (not listEmpty(varIdxs) and // not inside of EQNS_SYSTEM possible solveable
not Util.boolOrList(list(BackendVariable.isStateVar(v) for v in vars)))
equation
new_eq = BackendEquation.setEquationAttributes(eq, BackendDAE.EQ_ATTR_DEFAULT_AUX);
traverserArgs.auxVars = listAppend(vars, traverserArgs.auxVars);
traverserArgs.auxEqns = listAppend({new_eq}, traverserArgs.auxEqns);
if debug then print("[DAEmode] Create solved array equations. vars:\n" +
BackendDump.varListString(vars, "") + "eq:\n" +
BackendDump.equationListString({new_eq}, "") + "\n"); end if;
then
(traverserArgs);

case ({eq as BackendDAE.WHEN_EQUATION()}, {var})
guard (not listEmpty(varIdxs)) // not inside of EQNS_SYSTEM possible solveable
equation
Expand Down Expand Up @@ -348,6 +361,54 @@ algorithm
then
(traverserArgs);

// a = f(...) and a is an array
case ({eq as BackendDAE.ARRAY_EQUATION(left=exp)}, vars)
guard (Util.boolOrList(list(BackendVariable.isStateVar(v) for v in vars))
or listEmpty(varIdxs) // inside of EQNS_SYSTEM
and Expression.isCref(exp) )
equation
globalDAEData = traverserArgs.globalDAEData;

cref = Expression.expCref(exp);

// create aux variables for recordCref
newAuxVars = BackendVariable.getVar(cref, traverserArgs.systemVars);
crlst = ComponentReference.expandCref(cref, true);
newAuxVars = list(BackendVariable.copyVarNewName(ComponentReference.crefPrefixAux(cr), v) threaded for cr in crlst, v in newAuxVars);
newAuxVars = list(BackendVariable.setVarKind(v, BackendDAE.DAE_AUX_VAR()) for v in newAuxVars);
traverserArgs.auxVars = listAppend(newAuxVars, traverserArgs.auxVars);

// create aux equation aux = f(...)
newCref = ComponentReference.crefPrefixAux(cref);
eq.left = Expression.crefExp(newCref);
aux_eq = eq;

aux_eq = BackendEquation.setEquationAttributes(aux_eq, BackendDAE.EQ_ATTR_DEFAULT_AUX);
traverserArgs.auxEqns = listAppend({aux_eq}, traverserArgs.auxEqns);

// prepare res equation aux = recordCref
globalDAEData = traverserArgs.globalDAEData;
eq.right = Expression.crefToExp(cref);
newResEqns = BackendEquation.equationToScalarResidualForm(eq, traverserArgs.functionTree);
//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, globalDAEData.numResVars);
globalDAEData.numResVars = newnumResVars;
newResEqns = list(BackendEquation.setEquationAttributes(e, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC) for e in newResEqns);
traverserArgs.resVars = listAppend(newResVars, traverserArgs.resVars);
traverserArgs.resEqns = listAppend(listReverse(newResEqns), traverserArgs.resEqns);
globalDAEData = addVarsGlobalData(globalDAEData, vars);

traverserArgs.globalDAEData = globalDAEData;
if debug then print("[DAEmode] Added residual array equation\n" +
BackendDump.varListString(newResVars, "") + "states:\n" +
BackendDump.varListString(vars, "") + "eqs:\n" +
BackendDump.equationListString(newResEqns, "") + "\n"); end if;
then
(traverserArgs);

case ({eq}, vars)
guard (Util.boolOrList(list(BackendVariable.isStateVar(v) for v in vars))
or listEmpty(varIdxs)) // inside of EQNS_SYSTEM
Expand All @@ -361,7 +422,7 @@ algorithm
globalDAEData.numResVars = newnumResVars;
newResEqns = list(BackendEquation.setEquationAttributes(e, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC) for e in newResEqns);
traverserArgs.resVars = listAppend(newResVars, traverserArgs.resVars);
traverserArgs.resEqns = listAppend(newResEqns, traverserArgs.resEqns);
traverserArgs.resEqns = listAppend(listReverse(newResEqns), traverserArgs.resEqns);
globalDAEData = addVarsGlobalData(globalDAEData, vars);
traverserArgs.globalDAEData = globalDAEData;
if debug then print("[DAEmode] Added strong component or state eqns\n" +
Expand Down Expand Up @@ -391,15 +452,15 @@ algorithm

// create aux equation aux = f(...)
newCref = ComponentReference.crefPrefixAux(cref);
eq.left = Expression.crefExp(newCref);
eq.left = Expression.crefToExp(newCref);
aux_eq = eq;

aux_eq = BackendEquation.setEquationAttributes(aux_eq, BackendDAE.EQ_ATTR_DEFAULT_AUX);
traverserArgs.auxEqns = listAppend({aux_eq}, traverserArgs.auxEqns);

// prepare res equation aux = recordCref
globalDAEData = traverserArgs.globalDAEData;
eq.right = Expression.crefExp(cref);
eq.right = Expression.crefToExp(cref);
newResEqns = BackendEquation.equationToScalarResidualForm(eq, traverserArgs.functionTree);
//if debug then print("case: new eqns:\n" + BackendDump.dumpEqnsStr(newResEqns) + "\n"); end if;
dummyVar = BackendVariable.makeVar(DAE.emptyCref);
Expand All @@ -422,12 +483,14 @@ algorithm
(traverserArgs);

case(_, _)
guard( listLength(inVars) == listLength(inEqns) and not listEmpty(varIdxs))
guard(not listEmpty(varIdxs))
algorithm
vars := inVars;
for e in inEqns loop
var :: vars := vars;
traverserArgs := traverserStrongComponents({e}, {var}, {}, {}, traverserArgs);
size := BackendEquation.equationSize(e);
newAuxVars := List.firstN(vars, size);
traverserArgs := traverserStrongComponents({e}, newAuxVars, {}, {}, traverserArgs);
vars := List.stripN(vars, size);
end for;
then
(traverserArgs);
Expand Down
1 change: 1 addition & 0 deletions Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -1216,6 +1216,7 @@ algorithm
// create auxiliary variables, set index and push them SimCode Hash Table
((_, auxVars)) := BackendVariable.traverseBackendDAEVars(daeVars, BackendVariable.collectVarKindVarinVariables, (BackendVariable.isDAEmodeAuxVar, BackendVariable.emptyVars()));
((auxiliaryVars, _)) := BackendVariable.traverseBackendDAEVars(auxVars, SimCodeUtil.traversingdlowvarToSimvar, ({}, BackendVariable.emptyVars()));
auxiliaryVars := List.sort(auxiliaryVars, SimCodeUtil.simVarCompareByCrefSubsAtEndlLexical);
auxiliaryVars := SimCodeUtil.rewriteIndex(auxiliaryVars, 0);
(auxiliaryVars, _) := SimCodeUtil.setVariableIndexHelper(auxiliaryVars, 0);
crefToSimVarHT:= List.fold(auxiliaryVars,SimCodeUtil.addSimVarToHashTable,crefToSimVarHT);
Expand Down

0 comments on commit 04159c5

Please sign in to comment.