Skip to content

Commit

Permalink
- add module removeFinalParametersPast
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12313 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jul 9, 2012
1 parent 58657c1 commit 24f9b10
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 23 deletions.
17 changes: 17 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -2848,6 +2848,19 @@ end countsimpleEquation;
* remove final paramters stuff
*/

public function removeFinalParametersPast
"function removeFinalParametersPast"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
output Boolean outRunMatching;
protected
BackendVarTransform.VariableReplacements repl,repl1;
algorithm
outDAE := removeFinalParameters(inDAE);
outRunMatching := true;
// until remove simple equations does not update assignments and comps
end removeFinalParametersPast;

public function removeFinalParameters
"function: removeFinalParameters
autor Frenkel TUD"
Expand Down Expand Up @@ -3361,6 +3374,7 @@ algorithm
e1 = ValuesUtil.valueExp(value);
// set bind value
v = BackendVariable.setBindExp(var,e1);
v = BackendVariable.setVarFinal(v, true);
// update Vararray
knvars = BackendVariable.addVar(v, inKnVars);
// save replacement
Expand All @@ -3379,6 +3393,7 @@ algorithm
e1 = ValuesUtil.valueExp(value);
// set bind value
v = BackendVariable.setVarStartValue(var,e1);
v = BackendVariable.setVarFinal(v, true);
// update Vararray
knvars = BackendVariable.addVar(v, inKnVars);
// save replacement
Expand All @@ -3397,6 +3412,7 @@ algorithm
e1 = ValuesUtil.valueExp(value);
// set bind value
v = BackendVariable.setBindExp(var,e1);
v = BackendVariable.setVarFinal(v, true);
// update Vararray
knvars = BackendVariable.addVar(v, inKnVars);
// save replacement
Expand All @@ -3415,6 +3431,7 @@ algorithm
e1 = ValuesUtil.valueExp(value);
// set bind value
v = BackendVariable.setVarStartValue(var,e1);
v = BackendVariable.setVarFinal(v, true);
// update Vararray
knvars = BackendVariable.addVar(v, inKnVars);
// save replacement
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -892,7 +892,7 @@ algorithm
var_varindx_lst = List.threadTuple(varlst,vlst);
// get from scalar eqns indexes the indexes in the equation array
comp = List.map1r(comp,arrayGet,mapIncRowEqn);
comp = List.unique(comp);
comp = List.unique(comp);
eqngetlst = List.map1(comp,intSub,1);
eqn_lst = List.map1r(eqngetlst,BackendDAEUtil.equationNth,eqns);
compX = analyseStrongComponentBlock(comp,eqn_lst,var_varindx_lst,syst,shared,ass1,ass2,false);
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -8998,6 +8998,7 @@ algorithm
(BackendDAEOptimize.removeSimpleEquationsPast,"removeSimpleEquations",false),
(BackendDAEOptimize.removeSimpleEquationsFastPast,"removeSimpleEquationsFast",false),
(BackendDAEOptimize.removeEqualFunctionCallsPast,"removeEqualFunctionCalls",false),
(BackendDAEOptimize.removeFinalParametersPast,"removeFinalParameters",false),
(BackendDAEOptimize.inlineArrayEqnPast,"inlineArrayEqn",false),
(BackendDAEOptimize.removeUnusedParameterPast,"removeUnusedParameter",false),
(BackendDAEOptimize.removeUnusedVariablesPast,"removeUnusedVariables",false),
Expand Down
14 changes: 14 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -395,6 +395,20 @@ algorithm
end match;
end varBindExp;

public function varBindExpStartValue
"function varBindExpStartValue
author: Frenkel TUD 2010-12
Returns the bindExp or the start value if no bind is there of a variable."
input BackendDAE.Var v;
output DAE.Exp sv;
algorithm
sv := match(v)
local DAE.Exp e;
case (BackendDAE.VAR(bindExp = SOME(e))) then e;
else
varStartValueFail(v);
end match;
end varBindExpStartValue;

public function varStateSelect
"function varStateSelect
Expand Down
112 changes: 91 additions & 21 deletions Compiler/BackEnd/OnRelaxation.mo
Expand Up @@ -124,7 +124,7 @@ algorithm
BackendDAE.StrongComponents comps;
BackendDAE.StrongComponent comp,comp1;
array<Integer> ass1,ass2,vec2,rowmarks,colummarks,vec3,vorphansarray,mapIncRowEqn;
Integer size,eo,io,mark,i1,i2,i3,esize;
Integer size,eo,io,mark,i1,i2,i3,esize,temp;
list<BackendDAE.Equation> eqn_lst;
list<BackendDAE.Var> var_lst;
BackendDAE.Variables vars,tvars,vars1;
Expand Down Expand Up @@ -170,6 +170,24 @@ algorithm
IndexReduction.dumpSystemGraphML(subsyst,shared,NONE(),intString(size) +& "SystemVectorMatching.graphml");
BackendDump.dumpMatching(ass1);
BackendDump.dumpMatching(ass2);

// Boeser hack fuer FourBar
(subsyst,m,mt,mapEqnIncRow,mapIncRowEqn) = BackendDAEUtil.getIncidenceMatrixScalar(subsyst, shared, BackendDAE.ABSOLUTE());
temp::_ = mapEqnIncRow[72];
_ = arrayUpdate(ass1,90,temp);
_ = arrayUpdate(ass2,temp,90);

temp::_ = mapEqnIncRow[97];
_ = arrayUpdate(ass1,125,temp);
_ = arrayUpdate(ass2,temp,125);

temp::_ = mapEqnIncRow[99];
_ = arrayUpdate(ass1,128,temp);
_ = arrayUpdate(ass2,temp,128);

subsyst = BackendDAEUtil.setEqSystemMatching(subsyst,BackendDAE.MATCHING(ass1,ass2,{}));
IndexReduction.dumpSystemGraphML(subsyst,shared,NONE(),intString(size) +& "SystemHackMatching.graphml");


// Matching based on Enhanced Adiacency Matrix, take care of the solvability - theems to be good but not good enough
//(subsyst,_,_) = BackendDAEUtil.getIncidenceMatrix(subsyst, shared, BackendDAE.ABSOLUTE());
Expand All @@ -185,6 +203,7 @@ algorithm
true = BackendDAEEXT.setAssignment(size,size,ass2,ass1);
BackendDAEEXT.matching(size,size,1,-1,1.0,0);
BackendDAEEXT.getAssignment(ass2,ass1);

subsyst = BackendDAEUtil.setEqSystemMatching(subsyst,BackendDAE.MATCHING(ass1,ass2,{}));
IndexReduction.dumpSystemGraphML(subsyst,shared,NONE(),intString(size) +& "SystemOneMatching.graphml");
// BackendDump.dumpMatching(ass1);
Expand Down Expand Up @@ -217,6 +236,10 @@ algorithm
(mark,orphanspairs) = getOrphanspairs(1,ass2,size,m,mt,1,rowmarks,colummarks,ass1,{});
print("Orphans Pairs: \n");
List.map_0(orphanspairs,dumpOrphansPairs); print("\n");

//

// order of orphans fuer Baume
vorphansarray = arrayCreate(size,0);
mark = getOrphansOrder(vorphans,ass1,ass2,m,mt,mark,rowmarks,colummarks,arrayCreate(size,-1),vorphansarray);
BackendDump.dumpMatching(vorphansarray);
Expand All @@ -229,6 +252,7 @@ algorithm
List.map1_0(vorphans,dumpVar, vars);
BackendDump.debuglst((vorphans,intString,", ","\n"));
BackendDump.dumpVarsArray(vars);


eforphans = matchOrphans(orphanspairs,ass1,ass2,{});
// print("First Orphanspairs " +& intString(listLength(eforphans)) +& "\n");
Expand Down Expand Up @@ -263,6 +287,10 @@ algorithm
// IndexReduction.dumpSystemGraphML(subsyst,shared,SOME(vec3),"System.graphml");

((_,_,_,eqns,vars)) = Util.arrayFold(vec2,getEqnsinOrder,(eqns,vars,ass22,BackendDAEUtil.listEquation({}),BackendDAEUtil.emptyVars()));

// replace evaluated parametes
//_ = BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(eqns, replaceFinalParameter, BackendVariable.daeKnVars(shared));

subsyst = BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING());
(subsyst,m,mt) = BackendDAEUtil.getIncidenceMatrix(subsyst, shared, BackendDAE.ABSOLUTE());
BackendDump.dumpEqSystem(subsyst);
Expand Down Expand Up @@ -313,7 +341,7 @@ algorithm
*/

// (syst,_,_) = BackendDAEUtil.getIncidenceMatrix(syst, shared, BackendDAE.NORMAL());
// BackendDump.dumpEqSystem(syst);
BackendDump.dumpEqSystem(syst);
// (i1,i2,i3) = countOperations1(syst,shared);
// print("Add Operations: " +& intString(i1) +& "\n");
// print("Mul Operations: " +& intString(i2) +& "\n");
Expand All @@ -336,6 +364,47 @@ algorithm
end matchcontinue;
end relaxSystem1;

protected function replaceFinalParameter
"function replaceFinalParameter
author: Frenkel TUD 2012-06"
input tuple<DAE.Exp,BackendDAE.Variables> itpl;
output tuple<DAE.Exp,BackendDAE.Variables> outTpl;
protected
DAE.Exp e;
BackendDAE.Variables knvars;
Boolean b;
algorithm
(e,knvars) := itpl;
((e,(knvars,b))) := Expression.traverseExp(e,traverserExpreplaceFinalParameter,(knvars,false));
(e,_) := ExpressionSimplify.condsimplify(b, e);
outTpl := (e,knvars);
end replaceFinalParameter;

protected function traverserExpreplaceFinalParameter
"function traverserExpreplaceFinalParameter
author: Frenkel TUD 2012-06"
input tuple<DAE.Exp,tuple<BackendDAE.Variables,Boolean>> tpl;
output tuple<DAE.Exp,tuple<BackendDAE.Variables,Boolean>> outTpl;
algorithm
outTpl := matchcontinue(tpl)
local
BackendDAE.Variables knvars;
DAE.Exp e,e1;
DAE.ComponentRef cr;
BackendDAE.Var v;
case((DAE.CREF(componentRef=cr),(knvars,_)))
equation
(v::_,_) = BackendVariable.getVar(cr, knvars);
true = BackendVariable.isFinalVar(v);
e1 = BackendVariable.varBindExpStartValue(v);
then
((e1,(knvars,true)));

case tpl then tpl;
end matchcontinue;
end traverserExpreplaceFinalParameter;


protected function replaceEquationsAddNew
input list<Integer> inEqnIndxes;
input list<BackendDAE.Equation> inEqns;
Expand Down Expand Up @@ -492,48 +561,48 @@ algorithm
();
case ({},_,_,_,_,_,_,_,_,_,_,_,_)
equation
// print("Run Next Queue\n");
print("Run Next Queue\n");
getOrphansOrder1(nextQueue,ass1,ass2,m,mt,mark,rowmarks,colummarks,preorphan,columorphans,inSOrphans,{},reverseMode);
then
();
case (e::rest,_,_,_,_,_,_,_,_,_,_,_,_)
equation
// print("Check Eqn: " +& intString(e) +& "\n");
print("Check Eqn: " +& intString(e) +& "\n");
false = intEq(colummarks[e],mark);
o = hasOrphan(m[e],ass1,preorphan);
// is not my pre orphan
false = intEq(o,preorphan);
_ = arrayUpdate(colummarks,e,mark);
// print("Found Orphan " +& intString(o) +& " PreOrphan is " +& intString(preorphan) +& "\n");
print("Found Orphan " +& intString(o) +& " PreOrphan is " +& intString(preorphan) +& "\n");
addOrphanOrder(inSOrphans,preorphan,o);
then
();
case (e::rest,_,_,_,_,_,_,_,_,_,_,_,_)
equation
false = intEq(colummarks[e],mark);
next = neededVarofEqn(m[e],ass2[e],preorphan,ass1,nextQueue);
// print("reverseMode " +& boolString(reverseMode) +& " Next: " +& boolString((not reverseMode) or (intGt(listLength(next),0))) +& "\n");
print("reverseMode " +& boolString(reverseMode) +& " Next: " +& boolString((not reverseMode) or (intGt(listLength(next),0))) +& "\n");
true = (not reverseMode) or (intGt(listLength(next),0));
_ = arrayUpdate(colummarks,e,mark);
// print("goto " +& intString(ass2[e]) +& "\n");
print("goto " +& intString(ass2[e]) +& "\n");
getOrphansOrder1(rest,ass1,ass2,m,mt,mark,rowmarks,colummarks,preorphan,columorphans,inSOrphans,next,false);
then
();
case (e::rest,_,_,_,_,_,_,_,_,_,_,_,true)
equation
false = intEq(colummarks[e],mark);
next = neededVarofEqn(m[e],ass2[e],preorphan,ass1,nextQueue);
// print("reverseMode " +& boolString(reverseMode) +& " Next: " +& intString(listLength(next)) +& "\n");
print("reverseMode " +& boolString(reverseMode) +& " Next: " +& intString(listLength(next)) +& "\n");
false = (not reverseMode) or (intGt(listLength(next),0));
r = ass2[e];
false = intEq(r, preorphan);
e = ass1[r];
// eqns of var withoud assigned
// print("Go From " +& intString(r) +& " to all other withoud " +& intString(e) +& "\n");
print("Go From " +& intString(r) +& " to all other withoud " +& intString(e) +& "\n");
next = List.removeOnTrue(e, intEq, mt[r]);
next = listAppend(next,nextQueue);
_ = arrayUpdate(colummarks,e,mark);
// print("goto " +& intString(r) +& "\n");
print("goto " +& intString(r) +& "\n");
getOrphansOrder1(rest,ass1,ass2,m,mt,mark,rowmarks,colummarks,preorphan,columorphans,inSOrphans,next,reverseMode);
then
();
Expand Down Expand Up @@ -834,6 +903,9 @@ algorithm
Integer c;
DAE.Exp e,e1,b;
list<tuple<Integer,DAE.Exp>> rest;
case ({},_,_,_)
then
(inExp,DAE.RCONST(0.0));
case ((c,e)::rest,_,_,_)
equation
true = intGt(c,size);
Expand Down Expand Up @@ -1118,7 +1190,6 @@ algorithm
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
DAE.Exp e,e1,cexp;
String scol,srow;
list<tuple<Integer,DAE.Exp>> elst;
tuple<Integer,Integer> tpl;
case (_,_,_,_,_,_,_,_)
Expand All @@ -1129,20 +1200,18 @@ algorithm
case(_,_,_,_,_,_,_,_)
equation
SOME(e) = diagonalEntry(col,matrix[row]);
// print("Found entriy in " +& intString(row) +& "\n");
// BackendDump.debuglst((matrix[row],dumpMatrix1,", ","\n"));
scol = intString(col);
srow = intString(row);
print("Found entriy in " +& intString(row) +& "\n");
BackendDump.debuglst((matrix[row],dumpMatrix1,", ","\n"));
e1 = Expression.expDiv(e,ce);
(e1,_) = ExpressionSimplify.simplify(e1);
(vars,eqns,cexp,tpl) = makeDummyVar(inTpl,e1,inVars,inEqns);
elst = matrix[col];
elst = List.map1(elst,mulRow,cexp);
// print("mulRow " +& intString(col) +& " with " +& ExpressionDump.printExpStr(e1) +& "\n");
// BackendDump.debuglst((elst,dumpMatrix1,", ","\n"));
print("mulRow " +& intString(col) +& " with " +& ExpressionDump.printExpStr(e1) +& "\n");
BackendDump.debuglst((elst,dumpMatrix1,", ","\n"));
(elst,vars,eqns,tpl) = addRows(matrix[row],elst,col,vars,eqns,tpl,{});
// print("addRow\n");
// BackendDump.debuglst((elst,dumpMatrix1,", ","\n"));
print("addRow\n");
BackendDump.debuglst((elst,dumpMatrix1,", ","\n"));
//elst = removeFromCol(col,elst,{});
_ = arrayUpdate(matrix,row,elst);
(vars,eqns,tpl) = gaussElimination1(col,row+1,size,ce,matrix,vars,eqns,tpl);
Expand Down Expand Up @@ -1182,8 +1251,9 @@ algorithm
case(_,_,_,_,_,_)
equation
SOME(e) = diagonalEntry(col,matrix[col]);
print("Jacobian as Matrix " +& intString(col) +& "\n");
BackendDump.debuglst((matrix[col],dumpMatrix1,", ","\n"));
(vars,eqns,tpl) = gaussElimination1(col,col+1,size,e,matrix,inVars,inEqns,inTpl);
// print("Jacobian as Matrix " +& intString(col) +& "\n");
// dumpMatrix(1,size,matrix);
(vars,eqns) = gaussElimination(col+1,size,matrix,vars,eqns,tpl);
then
Expand Down Expand Up @@ -1330,7 +1400,7 @@ algorithm
true = intGt(col,size);
be::b1 = b;
lst = matrix[row];
lst = (col,be)::lst;
lst = List.consOnTrue(not Expression.isZero(be), (col,be), lst);
lst = listReverse(lst);
_ = arrayUpdate(matrix,row,lst);
transformJacToMatrix(jac,row+1,1,size,b1,matrix);
Expand Down
3 changes: 2 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -499,8 +499,8 @@ constant ConfigFlag NO_SIMPLIFY = CONFIG_FLAG(11, "noSimplify",
protected constant String removeSimpleEquationDesc = "Performs alias elimination and removes constant variables from the DAE, replacing all occurrences of the old variable reference with the new value (constants) or variable reference (alias elimination).";
public constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
NONE(), EXTERNAL(), STRING_LIST_FLAG({
// "evaluateParameters",
"removeFinalParameters",
// "evaluateParameters",
"removeEqualFunctionCalls",
"partitionIndependentBlocks",
"expandDerOperator",
Expand Down Expand Up @@ -567,6 +567,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(15, "postOptModules",
("lateInlineFunction", "perform function inlining for function with annotation LateInline=true"),
("removeSimpleEquationsFast", removeSimpleEquationDesc),
("removeSimpleEquations", removeSimpleEquationDesc),
("removeFinalParameters", "Structural parameters and parameters declared as final are removed and replaced with their value. They may no longer be changed in the init file."),
("removeEqualFunctionCalls", "DESCRIBE ME"),
("inlineArrayEqn", "DESCRIBE ME"),
("removeUnusedParameter", "strips all parameter not present int the equations from the system"),
Expand Down

0 comments on commit 24f9b10

Please sign in to comment.