Skip to content

Commit

Permalink
- move symbolic manipulation from tearing to SimCodeUtil
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12865 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Sep 11, 2012
1 parent 122e29f commit db38494
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 32 deletions.
153 changes: 124 additions & 29 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5052,9 +5052,9 @@ algorithm
BackendDAE.DAE({eqSystem},shared) = linearDAE1;
BackendDAE.EQSYSTEM(variables,eqArray,_,_,matching) = eqSystem;
BackendDAE.MATCHING(v1_new,v2_new,_) = matching;
(relaxedEqSystem,relaxedShared,_) = tearingSystemNew1(eqSystem,shared,{comps_Newton},false);
//(relaxedEqSystem,relaxedShared,_) = tearingSystemNew1(eqSystem,shared,{comps_Newton},false);
print("\nrelaxedEqSystem\n");
BackendDump.dumpEqSystem(relaxedEqSystem);
//BackendDump.dumpEqSystem(relaxedEqSystem);
print("\n----end of linear system----\n");

//-------------
Expand Down Expand Up @@ -9558,10 +9558,11 @@ algorithm
Boolean b,b1,b2;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;

case (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)),(shared, b1))
array<Integer> ass1,ass2;
case (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(ass1=ass1,ass2=ass2,comps=comps)),(shared, b1))
equation
(syst,shared,b2) = tearingSystemNew1(syst,shared,comps,false);
(comps,b2) = tearingSystemNew1(syst,shared,comps,false,{});
syst = Debug.bcallret2(b2, BackendDAEUtil.setEqSystemMatching, syst,BackendDAE.MATCHING(ass1,ass2,comps), syst);
b = b1 or b2;
then
(syst,(shared,b));
Expand All @@ -9574,43 +9575,43 @@ protected function tearingSystemNew1 "function tearingSystemNew1
input BackendDAE.Shared ishared;
input BackendDAE.StrongComponents inComps;
input Boolean iRunMatching;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
input BackendDAE.StrongComponents iAcc;
output BackendDAE.StrongComponents oComps;
output Boolean outRunMatching;
algorithm
(osyst,oshared,outRunMatching):=
matchcontinue (isyst,ishared,inComps,iRunMatching)
(oComps,outRunMatching):=
matchcontinue (isyst,ishared,inComps,iRunMatching,iAcc)
local
list<Integer> eindex,vindx;
list<list<Integer>> othercomps;
Boolean b,b1;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.StrongComponents comps;
BackendDAE.StrongComponents comps,acc;
BackendDAE.StrongComponent comp,comp1;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> ojac;
BackendDAE.JacobianType jacType;
case (_,_,{},_)
then (isyst,ishared,iRunMatching);
case (_,shared,
(comp as BackendDAE.EQUATIONSYSTEM(eqns=eindex,vars=vindx,jac=ojac,jacType=jacType))::comps,_)
case (_,_,{},_,_)
then (listReverse(iAcc),iRunMatching);
case (_,_,
(comp as BackendDAE.EQUATIONSYSTEM(eqns=eindex,vars=vindx,jac=ojac,jacType=jacType))::comps,_,_)
equation
(syst,shared,b) = tearingSystemNew1_1(isyst,ishared,eindex,vindx,ojac,jacType);
(syst,shared,b1) = tearingSystemNew1(syst,shared,comps,b or iRunMatching);
(comp1,b) = tearingSystemNew1_1(isyst,ishared,eindex,vindx,ojac,jacType);
acc = List.consOnTrue(b, comp1, iAcc);
(acc,b1) = tearingSystemNew1(isyst,ishared,comps,b or iRunMatching,acc);
then
(syst,shared,b1);
case (_,_,(comp as BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1))::comps,_)
(acc,b1);
case (_,_,(comp as BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1))::comps,_,_)
equation
(eindex,vindx) = BackendDAETransform.getEquationAndSolvedVarIndxes(comp);
(syst,shared,b) = tearingSystemNew1_1(isyst,ishared,eindex,vindx,NONE(),BackendDAE.JAC_NO_ANALYTIC());
(syst,shared,b1) = tearingSystemNew1(syst,shared,comps,b or iRunMatching);
(comp1,b) = tearingSystemNew1_1(isyst,ishared,eindex,vindx,NONE(),BackendDAE.JAC_NO_ANALYTIC());
acc = List.consOnTrue(b, comp1, iAcc);
(acc,b1) = tearingSystemNew1(isyst,ishared,comps,b or iRunMatching,acc);
then
(syst,shared,b1);
case (_,_,comp::comps,_)
(acc,b1);
case (_,_,comp::comps,_,_)
equation
(syst,shared,b) = tearingSystemNew1(isyst,ishared,comps,iRunMatching);
(acc,b) = tearingSystemNew1(isyst,ishared,comps,iRunMatching,comp::iAcc);
then
(syst,shared,b);
(acc,b);
end matchcontinue;
end tearingSystemNew1;

Expand All @@ -9622,8 +9623,7 @@ protected function tearingSystemNew1_1 "function tearingSystemNew1
input list<Integer> vindx;
input Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> ojac;
input BackendDAE.JacobianType jacType;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.StrongComponent ocomp;
output Boolean outRunMatching;
protected
list<Integer> tvars,residual,unsolvables;
Expand Down Expand Up @@ -9706,7 +9706,8 @@ algorithm
// print("OtherEquationsOrder:\n");
// BackendDump.dumpComponentsOLD(othercomps); print("\n");
// handle system in case of liniear and other cases
(osyst,oshared,outRunMatching) := tearingSystemNew4(jacType,isyst,ishared,subsyst,tvars,residual,ass1,ass2,othercomps,eindex,vindx,mapEqnIncRow,mapIncRowEqn);
//(osyst,oshared,outRunMatching) := tearingSystemNew4(jacType,isyst,ishared,subsyst,tvars,residual,ass1,ass2,othercomps,eindex,vindx,mapEqnIncRow,mapIncRowEqn);
(ocomp,outRunMatching) := tearingSystemNew4_1(jacType,isyst,ishared,subsyst,tvars,residual,ass1,ass2,othercomps,eindex,vindx,mapEqnIncRow,mapIncRowEqn);
Debug.fcall(Flags.TEARING_DUMP, print,Util.if_(outRunMatching,"Ok system torn\n","System not torn\n"));
end tearingSystemNew1_1;

Expand Down Expand Up @@ -10640,6 +10641,100 @@ algorithm
end match;
end tearingBFS2;

protected function tearingSystemNew4_1 "function tearingSystemNew4
author: Frenkel TUD 2012-09"
input BackendDAE.JacobianType jacType;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.EqSystem subsyst;
input list<Integer> tvars;
input list<Integer> residual;
input array<Integer> ass1;
input array<Integer> ass2;
input list<list<Integer>> othercomps;
input list<Integer> eindex;
input list<Integer> vindx;
input array<list<Integer>> mapEqnIncRow;
input array<Integer> mapIncRowEqn;
output BackendDAE.StrongComponent ocomp;
output Boolean outRunMatching;
algorithm
(ocomp,outRunMatching):=
matchcontinue (jacType,isyst,ishared,subsyst,tvars,residual,ass1,ass2,othercomps,eindex,vindx,mapEqnIncRow,mapIncRowEqn)
local
list<Integer> ores,residual1,ovars;
list<tuple<Integer,list<Integer>>> eqnvartpllst;
array<Integer> eindxarr,varindxarr;
Boolean linear;
case (_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
Debug.fcall(Flags.TEARING_DUMP, print,"handle torn System\n");
// map indexes back
residual1 = List.map1r(residual,arrayGet,mapIncRowEqn);
residual1 = List.unique(residual1);
eindxarr = listArray(eindex);
ores = List.map1r(residual1,arrayGet,eindxarr);
varindxarr = listArray(vindx);
ovars = List.map1r(tvars,arrayGet,varindxarr);
eqnvartpllst = tearingSystemNew4_2(othercomps,ass2,mapIncRowEqn,eindxarr,varindxarr,{});
linear = getLinearfromJacType(jacType);
then
(BackendDAE.TORNSYSTEM(ovars, ores, eqnvartpllst, linear),true);
case (_,_,_,_,_,_,_,_,_,_,_,_,_)
then
(BackendDAE.TORNSYSTEM({}, {}, {}, false),false);
end matchcontinue;
end tearingSystemNew4_1;

protected function tearingSystemNew4_2
input list<list<Integer>> othercomps;
input array<Integer> ass2;
input array<Integer> mapIncRowEqn;
input array<Integer> eindxarr;
input array<Integer> varindxarr;
input list<tuple<Integer,list<Integer>>> iAcc;
output list<tuple<Integer,list<Integer>>> oEqnVarTplLst;
algorithm
oEqnVarTplLst :=
match (othercomps,ass2,mapIncRowEqn,eindxarr,varindxarr,iAcc)
local
list<list<Integer>> rest;
Integer e,v,c;
list<Integer> vlst,clst,elst;
case ({},_,_,_,_,_) then listReverse(iAcc);
case ({c}::rest,_,_,_,_,_)
equation
e = mapIncRowEqn[c];
e = eindxarr[e];
v = ass2[c];
v = varindxarr[v];
then
tearingSystemNew4_2(rest,ass2,mapIncRowEqn,eindxarr,varindxarr,(e,{v})::iAcc);
case (clst::rest,_,_,_,_,_)
equation
elst = List.map1r(clst,arrayGet,mapIncRowEqn);
c::{} = List.unique(elst);
e = mapIncRowEqn[c];
e = eindxarr[e];
vlst = List.map1r(clst,arrayGet,ass2);
vlst = List.map1r(vlst,arrayGet,varindxarr);
then
tearingSystemNew4_2(rest,ass2,mapIncRowEqn,eindxarr,varindxarr,(e,vlst)::iAcc);
end match;
end tearingSystemNew4_2;

protected function getLinearfromJacType
input BackendDAE.JacobianType jacType;
output Boolean linear;
algorithm
linear := match(jacType)
case (BackendDAE.JAC_CONSTANT()) then true;
case (BackendDAE.JAC_TIME_VARYING()) then true;
case (BackendDAE.JAC_NONLINEAR()) then false;
case (BackendDAE.JAC_NO_ANALYTIC()) then false;
end match;
end getLinearfromJacType;

protected function tearingSystemNew4 "function tearingSystemNew4
author: Frenkel TUD 2012-05"
input BackendDAE.JacobianType jacType;
Expand Down
137 changes: 135 additions & 2 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4477,12 +4477,12 @@ algorithm
(equations_,uniqueEqIndex,tempvars) = createOdeSystem2(false, skipDiscInAlgorithm, vars_1,knvars_1,eqns_1,constrs,clsAttrs, jac, jac_tp, helpVarInfo,funcs,iuniqueEqIndex,itempvars);
then
(equations_,equations_,uniqueEqIndex,tempvars);
/* case (_, skipDiscInAlgorithm, false,BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_,comp as BackendDAE.TORNSYSTEM(tearingvars=tf, residualequations=rf, otherEqnVarTpl=eqnvartpllst, linear=b),helpVarInfo,_,_)
case (_, skipDiscInAlgorithm, false,BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqns),_,comp as BackendDAE.TORNSYSTEM(tearingvars=tf, residualequations=rf, otherEqnVarTpl=eqnvartpllst, linear=b),helpVarInfo,_,_)
equation
(equations_,uniqueEqIndex,tempvars) = createTornSystem(b, skipDiscInAlgorithm, tf,rf,eqnvartpllst, isyst, ishared, helpVarInfo,iuniqueEqIndex,itempvars);
then
(equations_,equations_,uniqueEqIndex,tempvars);
*/ else
else
equation
msg = "createOdeSystem failed for " +& BackendDump.printComponent(inComp);
Error.addMessage(Error.INTERNAL_ERROR, {msg});
Expand Down Expand Up @@ -4522,6 +4522,139 @@ algorithm
end match;
end updateJacobian;

protected function createTornSystem
input Boolean liniear;
input Boolean skipDiscInAlgorithm "if true skip discrete algorithm vars";
input list<Integer> tearingVars;
input list<Integer> residualEqns;
input list<tuple<Integer,list<Integer>>> otherEqns;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input list<SimCode.HelpVarInfo> helpVarInfo;
input Integer iuniqueEqIndex;
input list<SimCode.SimVar> itempvars;
output list<SimCode.SimEqSystem> equations_;
output Integer ouniqueEqIndex;
output list<SimCode.SimVar> otempvars;
algorithm
(equations_,ouniqueEqIndex,otempvars) :=
matchcontinue(liniear,skipDiscInAlgorithm,tearingVars,residualEqns,otherEqns,isyst,ishared,helpVarInfo,iuniqueEqIndex,itempvars)
local
list<BackendDAE.Var> tvars;
list<BackendDAE.Equation> reqns;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendVarTransform.VariableReplacements repl;
list<SimCode.SimVar> tempvars;
list<SimCode.SimEqSystem> simequations,resEqs;
Integer uniqueEqIndex;
list<DAE.ComponentRef> tcrs;
// linear case
/* TODO */
// nonliniear case
case(_,_,_,_,_,BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),_,_,_,_)
equation
//get tearing vars
tvars = List.map1r(tearingVars, BackendVariable.getVarAt, vars);
tvars = List.map(tvars, transformXToXd);
// get residual eqns
reqns = BackendEquation.getEqns(residualEqns, eqns);
reqns = replaceDerOpInEquationList(reqns);
// generate residual replacements
tcrs = List.map(tvars,BackendVariable.varCref);
repl = makeResidualReplacements(tcrs);
// generade other equations
(simequations,uniqueEqIndex,tempvars) = createTornSystemOtherEqns(otherEqns,repl,skipDiscInAlgorithm,isyst,ishared,helpVarInfo,iuniqueEqIndex,itempvars,{});
(resEqs,uniqueEqIndex,tempvars) = createNonlinearResidualEquations(reqns, repl,uniqueEqIndex,tempvars);
simequations = listAppend(simequations,resEqs);
then
({SimCode.SES_NONLINEAR(uniqueEqIndex, simequations, tcrs)},uniqueEqIndex+1,tempvars);
end matchcontinue;
end createTornSystem;

protected function createTornSystemOtherEqns
input list<tuple<Integer,list<Integer>>> otherEqns;
input BackendVarTransform.VariableReplacements repl;
input Boolean skipDiscInAlgorithm "if true skip discrete algorithm vars";
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input list<SimCode.HelpVarInfo> helpVarInfo;
input Integer iuniqueEqIndex;
input list<SimCode.SimVar> itempvars;
input list<SimCode.SimEqSystem> isimequations;
output list<SimCode.SimEqSystem> equations_;
output Integer ouniqueEqIndex;
output list<SimCode.SimVar> otempvars;
algorithm
(equations_,ouniqueEqIndex,otempvars) :=
matchcontinue(otherEqns,repl,skipDiscInAlgorithm,isyst,ishared,helpVarInfo,iuniqueEqIndex,itempvars,isimequations)
local
BackendDAE.EquationArray eqns;
list<SimCode.SimVar> tempvars;
list<SimCode.SimEqSystem> simequations;
Integer uniqueEqIndex,eqnindx;
BackendDAE.Equation eqn;
list<Integer> vars;
list<tuple<Integer,list<Integer>>> rest;
BackendDAE.EqSystem syst;
BackendDAE.StrongComponent comp;
case({},_,_,_,_,_,_,_,_)
then
(isimequations,iuniqueEqIndex,itempvars);
case((eqnindx,vars)::rest,_,_,BackendDAE.EQSYSTEM(orderedEqs=eqns),_,_,_,_,_)
equation
// get Eqn
eqn = BackendDAEUtil.equationNth(eqns, eqnindx-1);
// apply replacements
(eqn::_,_) = BackendVarTransform.replaceEquations({eqn}, repl, SOME(BackendVarTransform.skipPreOperator));
syst = BackendEquation.equationSetnthDAE(eqnindx-1, eqn, isyst);
// geneate comp
comp = createTornSystemOtherEqns1(eqn,eqnindx,vars);
(simequations,_,uniqueEqIndex,tempvars) = createEquations(false, false, true, skipDiscInAlgorithm, false, syst, ishared, {comp}, helpVarInfo, iuniqueEqIndex, itempvars);
simequations = listAppend(isimequations,simequations);
// generade other equations
(simequations,uniqueEqIndex,tempvars) = createTornSystemOtherEqns(rest,repl,skipDiscInAlgorithm,isyst,ishared,helpVarInfo,uniqueEqIndex,tempvars,simequations);
then
(simequations,uniqueEqIndex,tempvars);
end matchcontinue;
end createTornSystemOtherEqns;

protected function createTornSystemOtherEqns1
input BackendDAE.Equation eqn;
input Integer eqnindx;
input list<Integer> varindx;
output BackendDAE.StrongComponent ocomp;
algorithm
ocomp := match(eqn,eqnindx,varindx)
local
Integer v;
case (BackendDAE.EQUATION(exp=_),_,v::{})
then
BackendDAE.SINGLEEQUATION(eqnindx,v);
case (BackendDAE.RESIDUAL_EQUATION(exp=_),_,v::{})
then
BackendDAE.SINGLEEQUATION(eqnindx,v);
case (BackendDAE.SOLVED_EQUATION(source=_),_,v::{})
then
BackendDAE.SINGLEEQUATION(eqnindx,v);
case (BackendDAE.ARRAY_EQUATION(dimSize=_),_,_)
then
BackendDAE.SINGLEARRAY(eqnindx, varindx);
case (BackendDAE.ALGORITHM(size=_),_,_)
then
BackendDAE.SINGLEALGORITHM(eqnindx, varindx);
case (BackendDAE.COMPLEX_EQUATION(size=_),_,_)
then
BackendDAE.SINGLECOMPLEXEQUATION(eqnindx, varindx);
else
equation
print("SimCodeUtil.createTornSystemOtherEqns1 failed for\n");
BackendDump.dumpEqns({eqn});
then
fail();
end match;
end createTornSystemOtherEqns1;

protected function generateTearingSystem "function: generateTearingSystem
author: Frenkel TUD

Expand Down
1 change: 0 additions & 1 deletion Compiler/Util/Flags.mo
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"simplifysemiLinear",
"removeSimpleEquations",
"tearingSystem",
"removeSimpleEquations",
// "countOperations",
"removeUnusedFunctions",
"simplifyTimeIndepFuncCalls",
Expand Down

0 comments on commit db38494

Please sign in to comment.