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

Commit

Permalink
[DAEmode] improving array equations support
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun committed Mar 5, 2018
1 parent b54a3c9 commit ba6e3e0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -9075,8 +9075,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
65 changes: 63 additions & 2 deletions Compiler/BackEnd/DAEMode.mo
Expand Up @@ -316,6 +316,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 @@ -351,6 +364,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(listReverse(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);

// recordCref = f(...)
case ({eq as BackendDAE.COMPLEX_EQUATION(left=exp)}, vars)
guard(Expression.isCref(exp))
Expand All @@ -371,15 +432,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 Down

0 comments on commit ba6e3e0

Please sign in to comment.