Skip to content

Commit

Permalink
added first heuristic for sorting terms (optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
vruge authored and OpenModelica-Hudson committed Jan 26, 2016
1 parent 072f8e9 commit de5898c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
146 changes: 146 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4340,6 +4340,152 @@ algorithm

end simplifyComplexFunction2;

// =============================================================================
// section for hets
//
// (h)euristic (e)quation (t)erms (s)ort
// heuristic sorting of terms for better numeric in equations(res, torn,...)
//
// author: Vitalij Ruge
// =============================================================================

public function hets
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if Flags.getConfigString(Flags.HETS)<> "none" then
outDAE := hetsWork(inDAE);
else
outDAE := inDAE;
end if;
end hets;

protected function hetsWork
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE = inDAE;
protected
BackendDAE.EquationArray eqns;
BackendDAE.Variables vars;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl;
tuple<Integer,list<Integer>> tpl;
Integer i,j;
BackendDAE.Equation eqn;
list<Integer> tvars "be careful with states, this are solved for der(x)";
list<Integer> teqns;
DAE.ComponentRef cr;
BackendDAE.StrongComponents comps;
BackendDAE.Shared shared;
algorithm
shared := outDAE.shared;
for syst in outDAE.eqs loop
BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,matching=matching as BackendDAE.MATCHING(comps=comps),stateSets=stateSets,partitionKind=partitionKind) := syst;

for comp in comps loop
if BackendEquation.isTornSystem(comp) then

BackendDAE.TORNSYSTEM(strictTearingSet = BackendDAE.TEARINGSET(tearingvars=tvars, residualequations=teqns, otherEqnVarTpl= otherEqnVarTpl)) := comp;
for tpl in otherEqnVarTpl loop
try
(i,{j}) := tpl;
eqn := BackendEquation.equationNth1(eqns, i);
BackendDAE.VAR(varName = cr) := BackendVariable.getVarAt(vars, j);
eqn := BackendEquation.solveEquation(eqn, Expression.crefExp(cr), SOME(shared.functionTree));
eqn := hetsSplitRhs(eqn);
eqns := BackendEquation.setAtIndex(eqns, i, eqn);
else
end try;
end for;

for i in teqns loop
eqn := BackendEquation.equationNth1(eqns, i);
eqn := hetsSplitRes(eqn);
eqns := BackendEquation.setAtIndex(eqns, i, eqn);
end for;
elseif BackendEquation.isEquationsSystem(comp) then
BackendDAE.EQUATIONSYSTEM(eqns=teqns) := comp;
for i in teqns loop
eqn := BackendEquation.equationNth1(eqns, i);
eqn := hetsSplitRes(eqn);
eqns := BackendEquation.setAtIndex(eqns, i, eqn);
end for;
end if;
end for;
end for;
end hetsWork;

function hetsSplitRes
input BackendDAE.Equation iEqn;
output BackendDAE.Equation oEqn;

algorithm
oEqn := match iEqn
local DAE.Exp e1,e2, e;
DAE.ElementSource source;
BackendDAE.EquationAttributes attr;

case BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, attr=attr)
equation
e = Expression.createResidualExp(e1, e2);
e = hetsSplitExp(e);
then BackendDAE.RESIDUAL_EQUATION(e, source, attr);

case BackendDAE.RESIDUAL_EQUATION(e, source, attr)
equation
e = hetsSplitExp(e);
then BackendDAE.RESIDUAL_EQUATION(e, source, attr);

else iEqn;
end match;
end hetsSplitRes;

function hetsSplitRhs
input BackendDAE.Equation iEqn;
output BackendDAE.Equation oEqn;

algorithm
oEqn := match iEqn
local DAE.Exp e1,e2, e;
DAE.ElementSource source;
BackendDAE.EquationAttributes attr;

case BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, attr=attr)
equation
e2 = hetsSplitExp(e2);
then BackendDAE.EQUATION(e1, e2, source, attr);

else iEqn;
end match;
end hetsSplitRhs;

function hetsSplitExp
input DAE.Exp iExp;
output DAE.Exp oExp;
algorithm
oExp := match iExp
local DAE.Exp e,e1,e2; DAE.Operator op;
list<DAE.Exp> terms, termsDer;

case DAE.BINARY(e1, op, e2)
guard Expression.isMulOrDiv(op)
algorithm
e1 := hetsSplitExp(e1);
e2 := hetsSplitExp(e2);
then DAE.BINARY(e1, op, e2);

case e as DAE.BINARY(e1, op, e2)
guard Expression.isAddOrSub(op)
algorithm
terms := Expression.terms(e);
terms := list(hetsSplitExp(t) for t in terms);
(terms, termsDer) := List.splitOnTrue(terms, Expression.expHasDer);
then Expression.expAdd(Expression.makeSum1(terms), Expression.makeSum1(termsDer));
else iExp;
end match;
end hetsSplitExp;

// =============================================================================
// section for simplifyLoops
//
Expand Down
6 changes: 6 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7127,6 +7127,7 @@ protected function allPostOptimizationModules
(ExpressionSolve.solveSimpleEquations, "solveSimpleEquations"),
(BackendDAEOptimize.simplifyTimeIndepFuncCalls, "simplifyTimeIndepFuncCalls"),
(BackendDAEOptimize.simplifyAllExpressions, "simplifyAllExpressions"),
(BackendDAEOptimize.hets, "hets"),
// TODO: move the following modules to the correct position
(BackendDump.dumpComponentsGraphStr, "dumpComponentsGraphStr"),
(BackendDump.dumpDAE, "dumpDAE"),
Expand Down Expand Up @@ -7281,6 +7282,11 @@ algorithm
enabledModules := "simplifyLoops"::enabledModules;
end if;

if Flags.getConfigString(Flags.HETS) <> "none" then
enabledModules := "hets"::enabledModules;
end if;


if Flags.isSet(Flags.COUNT_OPERATIONS) then
enabledModules := "countOperations"::enabledModules;
end if;
Expand Down
31 changes: 31 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -6395,6 +6395,37 @@ algorithm
end matchcontinue;
end traversingexpHasDerCref;

public function expHasDer "
returns true if the expression contains the function der"
input DAE.Exp inExp;
output Boolean hasCref;
algorithm
(_, hasCref) := traverseExpTopDown(inExp, traversingexpHasDer, false);
end expHasDer;

public function traversingexpHasDer "
Returns a true if the exp contains in der"
input DAE.Exp inExp;
input Boolean inTpl;
output DAE.Exp outExp;
output Boolean cont;
output Boolean outTpl;
algorithm
(outExp,cont,outTpl) := matchcontinue(inExp,inTpl)
local
Boolean b;

case (DAE.CALL(path= Absyn.IDENT("der")), (false))
then (inExp,false,true);

//isAlias

case (_,b) then (inExp, not b, inTpl);

end matchcontinue;
end traversingexpHasDer;


public function expHasCrefNoPreorDer "
@author: Frenkel TUD 2011-04
returns true if the expression contains the cref, but not in pre,change,edge"
Expand Down
10 changes: 9 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -1197,6 +1197,13 @@ constant ConfigFlag INIT_OPT_MODULES_SUB = CONFIG_FLAG(86, "initOptModules-",
constant ConfigFlag PERMISSIVE = CONFIG_FLAG(87, "permissive",
NONE(), INTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Disables some error checks to allow erroneous models to compile."));
constant ConfigFlag HETS = CONFIG_FLAG(88, "hets",
NONE(), INTERNAL(), STRING_FLAG("none"),SOME(
STRING_DESC_OPTION({
("none", Util.gettext("do nothing")),
("derCalls", Util.gettext("sort terms based on der-calls"))
})),
Util.gettext("heuristic euqtion terms sort"));

protected
// This is a list of all configuration flags. A flag can not be used unless it's
Expand Down Expand Up @@ -1289,7 +1296,8 @@ constant list<ConfigFlag> allConfigFlags = {
POST_OPT_MODULES_SUB,
INIT_OPT_MODULES_ADD,
INIT_OPT_MODULES_SUB,
PERMISSIVE
PERMISSIVE,
HETS
};

public function new
Expand Down

0 comments on commit de5898c

Please sign in to comment.