Skip to content

Commit

Permalink
added DAEMode module
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#2142
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Feb 1, 2018
1 parent bcc4086 commit ef25927
Show file tree
Hide file tree
Showing 24 changed files with 1,425 additions and 98 deletions.
24 changes: 24 additions & 0 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -131,6 +131,7 @@ uniontype Shared "Data shared for all equation-systems"
SymbolicJacobians symjacs "Symbolic Jacobians";
ExtraInfo info "contains extra info that we send around like the model name";
PartitionsInfo partitionsInfo;
BackendDAEModeData daeModeData "DAEMode Data";
end SHARED;
end Shared;

Expand Down Expand Up @@ -180,6 +181,7 @@ uniontype BackendDAEType "BackendDAEType to indicate different types of BackendD
record PARAMETERSYSTEM "Type for parameter system BackendDAE.DAE" end PARAMETERSYSTEM;
record INITIALSYSTEM "Type for initial system BackendDAE.DAE" end INITIALSYSTEM;
record INLINESYSTEM "Type for inline system BackendDAE.DAE" end INLINESYSTEM;
record DAEMODESYSTEM "Type for DAEmode system BackendDAE.DAE" end DAEMODESYSTEM;
end BackendDAEType;

//
Expand Down Expand Up @@ -270,6 +272,7 @@ uniontype VarKind "variable kind"
record ALG_STATE end ALG_STATE; // algebraic state used by inline solver
record ALG_STATE_OLD end ALG_STATE_OLD; // algebraic state old value used by inline solver
record DAE_RESIDUAL_VAR end DAE_RESIDUAL_VAR; // variable kind used for DAEmode
record DAE_AUX_VAR end DAE_AUX_VAR; // auxiliary variable used for DAEmode
end VarKind;

public uniontype TearingSelect
Expand All @@ -291,6 +294,10 @@ public uniontype EquationKind "equation kind"
record CLOCKED_EQUATION
Integer clk;
end CLOCKED_EQUATION;
record DISCRETE_EQUATION
end DISCRETE_EQUATION;
record AUX_EQUATION
end AUX_EQUATION;
record UNKNOWN_EQUATION_KIND
end UNKNOWN_EQUATION_KIND;
end EquationKind;
Expand Down Expand Up @@ -835,6 +842,23 @@ public uniontype CompInfo"types to count operations for the components"

end CompInfo;

public
uniontype BackendDAEModeData
record BDAE_MODE_DATA
Integer numAuxVars;
list<Var> auxVars;

Integer numResVars;
list<Var> resVars;

Integer numAuxEqns;
list<Equation> auxEqns;

Integer numResEqns;
list<Equation> resEqns;
end BDAE_MODE_DATA;
end BackendDAEModeData;
constant BackendDAEModeData emptyDAEModeData = BDAE_MODE_DATA(0,{},0,{},0,{},0,{});

annotation(__OpenModelica_Interface="backend");
end BackendDAE;
3 changes: 2 additions & 1 deletion Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -166,7 +166,8 @@ algorithm
extObjCls,
BackendDAE.SIMULATION(),
symjacs,inExtraInfo,
BackendDAEUtil.emptyPartitionsInfo()
BackendDAEUtil.emptyPartitionsInfo(),
BackendDAE.emptyDAEModeData
));
BackendDAEUtil.checkBackendDAEWithErrorMsg(outBackendDAE);
BackendDAEUtil.checkIncidenceMatrixSolvability(syst, functionTree);
Expand Down
146 changes: 146 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5169,6 +5169,152 @@ algorithm
end matchcontinue;
end introDerAlias;


// =============================================================================
// section for replaceDerCall
//
// =============================================================================

public function replaceDerCalls
" This module replaces all der(cref)-calls by $DER.cref crefs expression."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := BackendDAEUtil.mapEqSystem(inDAE, replaceDerCallWork);
end replaceDerCalls;

protected function replaceDerCallWork
input BackendDAE.EqSystem inSyst;
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared = shared;
protected
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Equation> eqnsList;
algorithm
osyst := match inSyst
local
BackendDAE.EqSystem syst;
BackendDAE.Variables localKnowns;
case syst as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns)
algorithm
(eqns, vars) :=
BackendEquation.traverseEquationArray_WithUpdate(eqns, traverserreplaceDerCall, vars);
(localKnowns, vars) := BackendVariable.traverseBackendDAEVars(vars,
moveStatesVariables, (oshared.localKnownVars, vars));
oshared.localKnownVars := localKnowns;
syst.orderedEqs := eqns; syst.orderedVars := vars;
then syst;
end match;
end replaceDerCallWork;

protected function traverserreplaceDerCall "
Help function to e.g. replaceDerCall"
input BackendDAE.Equation inEq;
input BackendDAE.Variables inVars;
output BackendDAE.Equation outEq;
output BackendDAE.Variables outVars = inVars;
protected
BackendDAE.Equation e;
BackendDAE.Variables vars;
list<DAE.SymbolicOperation> ops;
algorithm
(e, ops) := BackendEquation.traverseExpsOfEquation(inEq, traverserreplaceDerCallExp, {});
outEq := List.foldr(ops, BackendEquation.addOperation, e);
end traverserreplaceDerCall;

protected function traverserreplaceDerCallExp "
Help function to e.g. traverserreplaceDerCall"
input DAE.Exp inExp;
input list<DAE.SymbolicOperation> tpl;
output DAE.Exp outExp;
output list<DAE.SymbolicOperation> outTpl;
protected
DAE.Exp e, e1;
tuple<BackendDAE.Variables, Boolean> ext_arg;
BackendDAE.Variables vars;
list<DAE.SymbolicOperation> ops;
DAE.FunctionTree funcs;
Boolean b, addVars;
BackendDAE.Shared shared;
list<BackendDAE.Equation> eqnLst;
algorithm
e := inExp;
ops := tpl;
(e1, b) := Expression.traverseExpBottomUp(e, replaceDerCall, false);
ops := List.consOnTrue(b, DAE.SUBSTITUTION({e1}, e), ops);
outExp := e1;
outTpl := ops;
end traverserreplaceDerCallExp;

protected function replaceDerCall "
Help function to e.g. traverserreplaceDerCallExp"
input DAE.Exp inExp;
input Boolean itpl;
output DAE.Exp outExp;
output Boolean tpl;
algorithm
(outExp,tpl) := matchcontinue (inExp, itpl)
local
BackendDAE.Variables vars;
DAE.ComponentRef cr,cref;
DAE.Type ty;
String str;
BackendDAE.Shared shared;
list<BackendDAE.Var> varlst;
BackendDAE.Var v, v1;
Boolean b, addVar;
DAE.FunctionTree funcs;
list<BackendDAE.Equation> eqnLst;
Integer numVars;
list<DAE.Exp> expLst;
String str;

case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr, ty=ty)}), _) equation
cref = ComponentReference.crefPrefixDer(cr);
outExp = DAE.CREF(cref,ty);
then (outExp, true);

case (DAE.CALL(path=Absyn.IDENT(name="der")), _) equation
str = "BackendDAEOptimize.replaceDerCall failed for: " + ExpressionDump.printExpStr(inExp) + "\n";
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();

else (inExp, itpl);
end matchcontinue;
end replaceDerCall;

protected function moveStatesVariables
input BackendDAE.Var inVar;
input tuple<BackendDAE.Variables, BackendDAE.Variables> inTpl;
output BackendDAE.Var outVar = inVar;
output tuple<BackendDAE.Variables, BackendDAE.Variables> outTpl = inTpl;
algorithm
_ := match(inVar)
local
DAE.ComponentRef cref;
BackendDAE.Var newVar;
BackendDAE.Variables localKnowns, newVars;
case(BackendDAE.VAR(varKind = BackendDAE.STATE(), varName=cref)) algorithm
(localKnowns, newVars) := inTpl;
// remove the state from variables
newVars := BackendVariable.deleteVar(cref, newVars);
// push it to the local knows
localKnowns := BackendVariable.addVar(inVar, localKnowns);

cref := ComponentReference.crefPrefixDer(cref);
newVar := BackendVariable.copyVarNewName(cref, inVar);
newVar := BackendVariable.setVarKind(newVar, BackendDAE.STATE_DER());

newVars := BackendVariable.addVar(newVar, newVars);
outTpl := (localKnowns, newVars);
then ();
else then();
end match;
end moveStatesVariables;


// =============================================================================
// replace expression with rewritten expression
//
Expand Down

0 comments on commit ef25927

Please sign in to comment.