Skip to content

Commit

Permalink
- do not replace PreVariables with information from time-equation-sys…
Browse files Browse the repository at this point in the history
…tems

  - move simplifyTimeIndepFuncCalls behind solveinitialsystem to replace all the pre variables for code generation

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13918 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 16, 2012
1 parent 0757b9d commit 945b989
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 79 deletions.
203 changes: 139 additions & 64 deletions Compiler/BackEnd/BackendDAEOptimize.mo

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9140,7 +9140,7 @@ public function solveInitialSystem "public function solveInitialSystem
output BackendDAE.BackendDAE outDAE;
output BackendDAE.BackendDAE outInitDAE;
algorithm
(outDAE, outInitDAE) := match(inDAE)
(outDAE, outInitDAE) := matchcontinue(inDAE)
local
BackendDAE.EqSystems systs;
BackendDAE.Variables knvars, vars, fixvars, evars, eavars;
Expand All @@ -9154,6 +9154,12 @@ algorithm
array<DAE.Constraint> constraints;
array<DAE.ClassAttributes> classAttrs;

case (_)
equation
false = Flags.isSet(Flags.SOLVE_INITIAL_SYSTEM);
then
(inDAE,inDAE);

case(BackendDAE.DAE(systs, BackendDAE.SHARED(knownVars=knvars,
initialEqs=inieqns,
constraints=constraints,
Expand Down Expand Up @@ -9202,7 +9208,7 @@ algorithm
// now let's solve the system!
initdae = solveInitialSystem1(vars, eqns, inDAE, initdae);
then(inDAE, initdae);
end match;
end matchcontinue;
end solveInitialSystem;

protected function solveInitialSystem1 "protected function solveInitialSystem1
Expand Down
52 changes: 52 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,51 @@ algorithm
greaterThan := ComponentReference.crefSortFunc(varCref(v1), varCref(v2));
end varSortFunc;


public function getAlias
" function getAlias
autor: Frenkel TUD 2012-11
returns the original Varname of an AliasVar"
input BackendDAE.Var inVar;
output DAE.ComponentRef outCr;
output Boolean negated;
protected
DAE.Exp e;
algorithm
e := varBindExp(inVar);
(outCr,negated) := getAlias1(e);
end getAlias;

protected function getAlias1
input DAE.Exp inExp;
output DAE.ComponentRef outCr;
output Boolean negated;
algorithm
(outCr,negated) :=
match (inExp)
local
DAE.ComponentRef name;
Absyn.Path fname;

case DAE.CREF(componentRef=name) then (name,false);
case DAE.UNARY(operator=DAE.UMINUS(_),exp=DAE.CREF(componentRef=name)) then (name,true);
case DAE.UNARY(operator=DAE.UMINUS_ARR(_),exp=DAE.CREF(componentRef=name)) then (name,true);
case DAE.LUNARY(operator=DAE.NOT(_),exp=DAE.CREF(componentRef=name)) then (name,true);
case DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)})
equation
name = ComponentReference.crefPrefixDer(name);
then (name,false);
case DAE.UNARY(operator=DAE.UMINUS(_),exp=DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)}))
equation
name = ComponentReference.crefPrefixDer(name);
then (name,true);
case DAE.UNARY(operator=DAE.UMINUS_ARR(_),exp=DAE.CALL(path=Absyn.IDENT(name = "der"), expLst={DAE.CREF(componentRef=name)}))
equation
name = ComponentReference.crefPrefixDer(name);
then (name,true);
end match;
end getAlias1;

/* =======================================================
*
* Section for functions that deals with VariablesArray
Expand Down Expand Up @@ -2228,6 +2273,13 @@ algorithm
BackendDAE.SHARED(knownVars = vars) := shared;
end daeKnVars;

public function daeAliasVars
input BackendDAE.Shared shared;
output BackendDAE.Variables vars;
algorithm
BackendDAE.SHARED(aliasVars = vars) := shared;
end daeAliasVars;

public function varsSize "function: varsSize
author: PA

Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/ExpressionSolve.mo
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ algorithm
then
(rhs,asserts);

// a^b = f(..) -> a = sign(pre(a))*(f(...)^(1/b))
// a^b = f(..) -> a = sign(pre(a))*(f(...)^(1/b))
case (_,_,DAE.CREF(componentRef = cr),_)
equation
e = Expression.makeDiff(inExp1,inExp2);
Expand Down
26 changes: 16 additions & 10 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ algorithm
String cname, fileDir;
Integer n_h,maxDelayedExpIndex, uniqueEqIndex, numberofNonLinearSys, numberofEqns, numberOfInitialEquations, numberOfInitialAlgorithms;
list<SimCode.HelpVarInfo> helpVarInfo;
BackendDAE.BackendDAE dlow,dlow2;
BackendDAE.BackendDAE dlow,dlow2,initDAE;
DAE.FunctionTree functionTree;
BackendDAE.SymbolicJacobians symJacs;
Absyn.Path class_;
Expand Down Expand Up @@ -1862,6 +1862,12 @@ algorithm
ifcpp = stringEqual(Config.simCodeTarget(),"Cpp");
//Debug.fcall(Flags.CPP_VAR,print, "is that Cpp? : " +& Dump.printBoolStr(ifcpp) +& "\n");
cname = Absyn.pathStringNoQual(class_);

// generate initalsystem
(_, initDAE) = BackendDAEUtil.solveInitialSystem(dlow);

// replace pre(alias) in time-equations
dlow = BackendDAEOptimize.simplifyTimeIndepFuncCalls(dlow);

// check if the Sytems has states
dlow = BackendDAEUtil.addDummyStateIfNeeded(dlow);
Expand All @@ -1886,7 +1892,7 @@ algorithm
n_h = listLength(helpVarInfo);

// initialization stuff
(residuals, initialEquations, numberOfInitialEquations, numberOfInitialAlgorithms, uniqueEqIndex, tempvars, useSymbolicInitialization) = createInitialResiduals(dlow2, uniqueEqIndex, {}, helpVarInfo);
(residuals, initialEquations, numberOfInitialEquations, numberOfInitialAlgorithms, uniqueEqIndex, tempvars, useSymbolicInitialization) = createInitialResiduals(dlow2, initDAE, uniqueEqIndex, {}, helpVarInfo);
(jacG, uniqueEqIndex) = createInitialMatrices(dlow2, uniqueEqIndex);

// expandAlgorithmsbyInitStmts
Expand Down Expand Up @@ -6404,6 +6410,7 @@ protected function createInitialResiduals "function createInitialResiduals
author: lochel
This function generates all initial_residuals."
input BackendDAE.BackendDAE inDAE;
input BackendDAE.BackendDAE inInitDAE;
input Integer iuniqueEqIndex;
input list<SimCode.SimVar> itempvars;
input list<SimCode.HelpVarInfo> helpVarInfo;
Expand All @@ -6415,7 +6422,8 @@ protected function createInitialResiduals "function createInitialResiduals
output list<SimCode.SimVar> otempvars;
output Boolean useSymbolicInitialization;
algorithm
(outResiduals, outInitialEqns, outNumberOfInitialEquations, outNumberOfInitialAlgorithms, ouniqueEqIndex, otempvars, useSymbolicInitialization) := matchcontinue(inDAE, iuniqueEqIndex, itempvars, helpVarInfo)
(outResiduals, outInitialEqns, outNumberOfInitialEquations, outNumberOfInitialAlgorithms, ouniqueEqIndex, otempvars, useSymbolicInitialization) :=
matchcontinue(inDAE, inInitDAE, iuniqueEqIndex, itempvars, helpVarInfo)
local
BackendDAE.EqSystems eqs;
BackendDAE.EquationArray initialEqs,removedEqs;
Expand All @@ -6431,14 +6439,12 @@ algorithm
BackendDAE.Variables knvars,aliasVars;

// try to solve the inital system symbolical.
case (_, _, _, _) equation
true = Flags.isSet(Flags.SOLVE_INITIAL_SYSTEM);

// generate initalsystem
(_, BackendDAE.DAE(systs,
case (_, BackendDAE.DAE(systs,
shared as BackendDAE.SHARED(knownVars=knvars,
aliasVars=aliasVars,
removedEqs=removedEqs))) = BackendDAEUtil.solveInitialSystem(inDAE);
removedEqs=removedEqs)), _, _, _) equation
true = Flags.isSet(Flags.SOLVE_INITIAL_SYSTEM);

// generate equations from the solved systems
(uniqueEqIndex, _, _, allEquations, tempvars) = createEquationsForSystems(systs, shared, helpVarInfo, iuniqueEqIndex, {}, {}, {}, itempvars);
// generate equations from the removed equations
Expand All @@ -6456,7 +6462,7 @@ algorithm
(residual_equations, uniqueEqIndex, tempvars) = createNonlinearResidualEquations(initialEqs_lst, uniqueEqIndex, tempvars);
then (residual_equations, allEquations, numberOfInitialEquations, numberOfInitialAlgorithms, uniqueEqIndex, tempvars, true);

case (_, _, _, _) equation
case (_, _, _, _, _) equation
(initialEqs_lst, numberOfInitialEquations, numberOfInitialAlgorithms) = BackendDAEOptimize.collectInitialEquations(inDAE);
(residual_equations, uniqueEqIndex, tempvars) = createNonlinearResidualEquations(initialEqs_lst, iuniqueEqIndex, itempvars);
Debug.fcall(Flags.PEDANTIC, Error.addCompilerWarning, "No system for the symbolic initialization was generated. A method using numerical algorithms will be used instead.\n");
Expand Down
38 changes: 38 additions & 0 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ algorithm
((e,_)) = Expression.traverseExp(e,preCref,false);
then
((e,(true,options)));
case ((DAE.CALL(path=Absyn.IDENT("change"), expLst={e}), (b, options)))
equation
((e,_)) = Expression.traverseExp(e,changeCref,false);
then
((e,(true,options)));
case ((DAE.CALL(path=Absyn.IDENT("edge"), expLst={e}), (b, options)))
equation
((e,_)) = Expression.traverseExp(e,edgeCref,false);
then
((e,(true,options)));

// normal call
case ((e as DAE.CALL(expLst=expl),(_,options)))
Expand Down Expand Up @@ -412,6 +422,34 @@ algorithm
end match;
end preCref;

protected function changeCref
input tuple<DAE.Exp,Boolean> iExp;
output tuple<DAE.Exp,Boolean> oExp;
algorithm
oExp := match(iExp)
local
DAE.Exp e;
Boolean b;
DAE.Type ty;
case ((e as DAE.CREF(ty=ty),_)) then ((DAE.CALL(Absyn.IDENT("change"),{e},DAE.CALL_ATTR(ty,false,true,DAE.NO_INLINE(),DAE.NO_TAIL())),true));
else then iExp;
end match;
end changeCref;

protected function edgeCref
input tuple<DAE.Exp,Boolean> iExp;
output tuple<DAE.Exp,Boolean> oExp;
algorithm
oExp := match(iExp)
local
DAE.Exp e;
Boolean b;
DAE.Type ty;
case ((e as DAE.CREF(ty=ty),_)) then ((DAE.CALL(Absyn.IDENT("edge"),{e},DAE.CALL_ATTR(ty,false,true,DAE.NO_INLINE(),DAE.NO_TAIL())),true));
else then iExp;
end match;
end edgeCref;

public function simplify1
"function: simplify1
This function does some very basic simplification
Expand Down
3 changes: 1 addition & 2 deletions Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,6 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"tearingSystem",
// "countOperations",
"removeUnusedFunctions",
"simplifyTimeIndepFuncCalls",
"inputDerivativesUsed",
"detectJacobianSparsePattern",
// "generateSymbolicJacobian",
Expand All @@ -640,7 +639,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("generateSymbolicLinearization", Util.gettext("Generates symbolic Linearization Matrixes A,B,C,D for Linear Model:\n\t\t\\dot x = Ax + Bu\n\t\ty = Cx +Du")),
("collapseIndependentBlocks", Util.gettext("Collapses all equation systems back into one big system again (undo partitionIndependentBlocks)")),
("removeUnusedFunctions", Util.gettext("removed all unused functions from functionTree")),
("simplifyTimeIndepFuncCalls", Util.gettext("simplifies time independent built in function calls like pre(param) -> param, der(param) -> 0.0, change(param) -> false, edge(param) -> false")),
// ("simplifyTimeIndepFuncCalls", Util.gettext("simplifies time independent built in function calls like pre(param) -> param, der(param) -> 0.0, change(param) -> false, edge(param) -> false")),
("inputDerivativesUsed", Util.gettext("checks if derivatives of inputs are need to calculate the model.")),
("simplifysemiLinear", Util.gettext("simplifies calls to semiLinear")),
("removeConstants", Util.gettext("remove all constants in the system")),
Expand Down

0 comments on commit 945b989

Please sign in to comment.