Skip to content

Commit

Permalink
- included Jacobian in TearingSet because it differs for different sets
Browse files Browse the repository at this point in the history
- adapt some modules to new tearing structure


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25782 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
ptaeuber committed Apr 28, 2015
1 parent fb2ca72 commit 2da1e33
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -465,7 +465,6 @@ uniontype StrongComponent
TearingSet strictTearingSet;
Option<TearingSet> casualTearingSet;
Boolean linear;
Jacobian jac;
Boolean mixedSystem "true for system that discrete dependecies to the iteration variables";
end TORNSYSTEM;
end StrongComponent;
Expand All @@ -476,6 +475,7 @@ uniontype TearingSet
list<Integer> tearingvars;
list<Integer> residualequations;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl "list of tuples of indexes for Equation and Variable solved in the equation, in the order they have to be solved";
Jacobian jac;
end TEARINGSET;
end TearingSet;

Expand Down
14 changes: 12 additions & 2 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4559,7 +4559,7 @@ algorithm
local
BackendDAE.StrongComponents rest;
list<BackendDAE.Var> varlst;
list<Integer> vlst;
list<Integer> vlst,vlst2;
Boolean linear;
String str;
String warning;
Expand Down Expand Up @@ -4592,7 +4592,17 @@ algorithm
warningList = listAllIterationVariables2(rest, inVars);
then warning::warningList;

case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst), linear=linear)::rest, _) equation
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst), NONE(), linear=linear)::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
false = listEmpty(varlst);

str = if linear then "linear" else "nonlinear";
warning = "Iteration variables of torn " + str + " equation system:\n" + warnAboutVars(varlst);
warningList = listAllIterationVariables2(rest, inVars);
then warning::warningList;

case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst), SOME(BackendDAE.TEARINGSET(tearingvars=vlst2)), linear=linear)::rest, _) equation
vlst = List.unique(listAppend(vlst,vlst2));
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
false = listEmpty(varlst);

Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6170,7 +6170,7 @@ algorithm
arg = traverseBackendDAEExps(bdae, inFunc, inTypeA);
then
traverseStrongComponentsJacobiansExp(rest, inFunc, arg);
case (BackendDAE.TORNSYSTEM(jac=BackendDAE.GENERIC_JACOBIAN(jacobian = (bdae,_,_,_,_)))::rest, _, _)
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(jac=BackendDAE.GENERIC_JACOBIAN(jacobian = (bdae,_,_,_,_))))::rest, _, _)
equation
arg = traverseBackendDAEExps(bdae, inFunc, inTypeA);
then
Expand Down
70 changes: 62 additions & 8 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -919,15 +919,15 @@ algorithm
matchcontinue (inComps,eqns,vars)
local
Integer e,v;
list<Integer> elst,vlst,vlst1,elst1;
list<Integer> elst,vlst,vlst1,elst1,vlst2,elst2;
BackendDAE.StrongComponent comp;
BackendDAE.StrongComponents rest;
BackendDAE.Var var;
BackendDAE.Equation eqn;
list<BackendDAE.Var> varlst;
list<BackendDAE.Equation> eqnlst;
BackendDAE.JacobianType jacType;
list<tuple<Integer,list<Integer>>> eqnsvartpllst;
list<tuple<Integer,list<Integer>>> eqnsvartpllst,eqnsvartpllst2;
Boolean b;
String s;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;
Expand Down Expand Up @@ -1011,7 +1011,7 @@ algorithm
dumpEqnsSolved2(rest,eqns,vars);
then
();
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst,residualequations=elst,otherEqnVarTpl=eqnsvartpllst),linear=b)::rest,_,_)
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst,residualequations=elst,otherEqnVarTpl=eqnsvartpllst),NONE(),linear=b)::rest,_,_)
equation
s = if b then "linear" else "nonlinear";
print("torn " + s + " Equationsystem:\n");
Expand All @@ -1031,6 +1031,41 @@ algorithm
dumpEqnsSolved2(rest,eqns,vars);
then
();
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=vlst,residualequations=elst,otherEqnVarTpl=eqnsvartpllst),SOME(BackendDAE.TEARINGSET(tearingvars=vlst2,residualequations=elst2,otherEqnVarTpl=eqnsvartpllst2)),linear=b)::rest,_,_)
equation
s = if b then "linear" else "nonlinear";
print("Strict torn " + s + " Equationsystem:\n");
vlst1 = List.flatten(List.map(eqnsvartpllst,Util.tuple22));
elst1 = List.map(eqnsvartpllst,Util.tuple21);
varlst = List.map1r(vlst1, BackendVariable.getVarAt, vars);
printVarList(varlst);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
printVarList(varlst);
print("\n");
eqnlst = BackendEquation.getEqns(elst1,eqns);
printEquationList(eqnlst);
print("\n");
eqnlst = BackendEquation.getEqns(elst,eqns);
printEquationList(eqnlst);
print("\n");
dumpEqnsSolved2(rest,eqns,vars);
print("Casual torn " + s + " Equationsystem:\n");
vlst1 = List.flatten(List.map(eqnsvartpllst2,Util.tuple22));
elst1 = List.map(eqnsvartpllst2,Util.tuple21);
varlst = List.map1r(vlst1, BackendVariable.getVarAt, vars);
printVarList(varlst);
varlst = List.map1r(vlst2, BackendVariable.getVarAt, vars);
printVarList(varlst);
print("\n");
eqnlst = BackendEquation.getEqns(elst1,eqns);
printEquationList(eqnlst);
print("\n");
eqnlst = BackendEquation.getEqns(elst2,eqns);
printEquationList(eqnlst);
print("\n");
dumpEqnsSolved2(rest,eqns,vars);
then
();
case (_::rest,_,_)
equation
true = Flags.isSet(Flags.FAILTRACE);
Expand Down Expand Up @@ -1159,18 +1194,18 @@ public function printComponent
output String oString;

protected
String tmpStr;
String tmpStr,tmpStr2;

algorithm
oString := match (inComp)
local
Integer i,v;
list<Integer> ilst,vlst;
list<Integer> ilst,vlst,ilst2,vlst2;
list<String> ls;
String s,s2,s3,s4;
BackendDAE.JacobianType jacType;
BackendDAE.StrongComponent comp;
list<tuple<Integer,list<Integer>>> eqnvartpllst;
list<tuple<Integer,list<Integer>>> eqnvartpllst,eqnvartpllst2;
Boolean b;
case BackendDAE.SINGLEEQUATION(eqn=i,var=v)
equation
Expand Down Expand Up @@ -1215,7 +1250,7 @@ algorithm
s = stringDelimitList(ls, ", ");
tmpStr = "WhenEquation " + " {" + intString(i) + ":" + s + "}\n";
then tmpStr;
case BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(residualequations=ilst,tearingvars=vlst,otherEqnVarTpl=eqnvartpllst),linear=b)
case BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(residualequations=ilst,tearingvars=vlst,otherEqnVarTpl=eqnvartpllst),NONE(),linear=b)
equation
ls = List.map(eqnvartpllst, tupleString);
s = stringDelimitList(ls, ", ");
Expand All @@ -1224,8 +1259,27 @@ algorithm
ls = List.map(vlst, intString);
s3 = stringDelimitList(ls, ", ");
s4 = if b then "linear" else "nonlinear";
tmpStr = "{{" + s + "}\n,{" + s2 + ":" + s3 + "} Size: " + intString(listLength(vlst)) + " " + s4 + "\n";
tmpStr = "{{" + s + "}\n,{" + s2 + ":" + s3 + "}} Size: " + intString(listLength(vlst)) + " " + s4 + "\n";
then tmpStr;
case BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(residualequations=ilst,tearingvars=vlst,otherEqnVarTpl=eqnvartpllst),SOME(BackendDAE.TEARINGSET(residualequations=ilst2,tearingvars=vlst2,otherEqnVarTpl=eqnvartpllst2)),linear=b)
equation
ls = List.map(eqnvartpllst, tupleString);
s = stringDelimitList(ls, ", ");
ls = List.map(ilst, intString);
s2 = stringDelimitList(ls, ", ");
ls = List.map(vlst, intString);
s3 = stringDelimitList(ls, ", ");
s4 = if b then "linear" else "nonlinear";
tmpStr = "{{" + s + "}\n,{" + s2 + ":" + s3 + "}} Size: " + intString(listLength(vlst)) + " " + s4 + " (strict tearing set)\n";
ls = List.map(eqnvartpllst2, tupleString);
s = stringDelimitList(ls, ", ");
ls = List.map(ilst2, intString);
s2 = stringDelimitList(ls, ", ");
ls = List.map(vlst2, intString);
s3 = stringDelimitList(ls, ", ");
s4 = if b then "linear" else "nonlinear";
tmpStr2 = "{{" + s + "}\n,{" + s2 + ":" + s3 + "}} Size: " + intString(listLength(vlst2)) + " " + s4 + " (casual tearing set)\n";
then tmpStr + tmpStr2;
end match;
end printComponent;

Expand Down
91 changes: 85 additions & 6 deletions Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -2305,9 +2305,9 @@ algorithm
local
BackendDAE.StrongComponent comp;
BackendDAE.Shared shared;
list<Integer> iterationvarsInts;
list<Integer> residualequations;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl;
list<Integer> iterationvarsInts,iterationvarsInts2;
list<Integer> residualequations,residualequations2;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl,otherEqnVarTpl2;
Boolean b;

list<list<Integer>> otherVarsIntsLst;
Expand All @@ -2318,14 +2318,56 @@ algorithm
list<BackendDAE.Equation> reqns, otherEqnsLst;
BackendDAE.EquationArray eqns, oeqns;

BackendDAE.Jacobian jacobian;
BackendDAE.Jacobian jacobian,jacobian2;

String name;
Boolean mixedSystem;

case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=iterationvarsInts, residualequations=residualequations, otherEqnVarTpl=otherEqnVarTpl), linear=b, mixedSystem=mixedSystem), _, _, _)
case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=iterationvarsInts, residualequations=residualequations, otherEqnVarTpl=otherEqnVarTpl), NONE(), linear=b, mixedSystem=mixedSystem), _, _, _)
equation
true = (Flags.isSet(Flags.NLS_ANALYTIC_JACOBIAN) and not b) or b;
// get iteration vars
iterationvars = List.map1r(iterationvarsInts, BackendVariable.getVarAt, inVars);
iterationvars = List.map(iterationvars, BackendVariable.transformXToXd);
diffVars = BackendVariable.listVar1(iterationvars);

// get residual eqns
reqns = BackendEquation.getEqns(residualequations, inEqns);
reqns = BackendEquation.replaceDerOpInEquationList(reqns);
eqns = BackendEquation.listEquation(reqns);
// create residual equations
reqns = BackendEquation.traverseEquationArray(eqns, BackendEquation.traverseEquationToScalarResidualForm, {});
reqns = listReverse(reqns);
(reqns, resVarsLst) = convertResidualsIntoSolvedEquations(reqns);
resVars = BackendVariable.listVar1(resVarsLst);
eqns = BackendEquation.listEquation(reqns);

// get other eqns
otherEqnsInts = List.map(otherEqnVarTpl, Util.tuple21);
otherEqnsLst = BackendEquation.getEqns(otherEqnsInts, inEqns);
otherEqnsLst = BackendEquation.replaceDerOpInEquationList(otherEqnsLst);
oeqns = BackendEquation.listEquation(otherEqnsLst);

// get other vars
otherVarsIntsLst = List.map(otherEqnVarTpl, Util.tuple22);
otherVarsInts = List.unionList(otherVarsIntsLst);
ovarsLst = List.map1r(otherVarsInts, BackendVariable.getVarAt, inVars);
ovarsLst = List.map(ovarsLst, BackendVariable.transformXToXd);
ovars = BackendVariable.listVar1(ovarsLst);

//generate jacobian name
name = "NLSJac" + intString(System.tmpTickIndex(Global.backendDAE_jacobianSeq));

// generate generic jacobian backend dae
(jacobian, shared) = getSymbolicJacobian(diffVars, eqns, resVars, oeqns, ovars, inShared, inVars, name);

then (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(iterationvarsInts, residualequations, otherEqnVarTpl, jacobian), NONE(), b, mixedSystem), shared);

case (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=iterationvarsInts, residualequations=residualequations, otherEqnVarTpl=otherEqnVarTpl), SOME(BackendDAE.TEARINGSET(tearingvars=iterationvarsInts2, residualequations=residualequations2, otherEqnVarTpl=otherEqnVarTpl2)), linear=b, mixedSystem=mixedSystem), _, _, _)
equation
true = (Flags.isSet(Flags.NLS_ANALYTIC_JACOBIAN) and not b) or b;

// Get Jacobian for strict tearing set
// get iteration vars
iterationvars = List.map1r(iterationvarsInts, BackendVariable.getVarAt, inVars);
iterationvars = List.map(iterationvars, BackendVariable.transformXToXd);
Expand Down Expand Up @@ -2361,8 +2403,45 @@ algorithm
// generate generic jacobian backend dae
(jacobian, shared) = getSymbolicJacobian(diffVars, eqns, resVars, oeqns, ovars, inShared, inVars, name);

then (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(iterationvarsInts, residualequations, otherEqnVarTpl), NONE(), b, jacobian, mixedSystem), shared);

// Get Jacobian for casual tearing set
// get iteration vars
iterationvars = List.map1r(iterationvarsInts2, BackendVariable.getVarAt, inVars);
iterationvars = List.map(iterationvars, BackendVariable.transformXToXd);
diffVars = BackendVariable.listVar1(iterationvars);

// get residual eqns
reqns = BackendEquation.getEqns(residualequations2, inEqns);
reqns = BackendEquation.replaceDerOpInEquationList(reqns);
eqns = BackendEquation.listEquation(reqns);
// create residual equations
reqns = BackendEquation.traverseEquationArray(eqns, BackendEquation.traverseEquationToScalarResidualForm, {});
reqns = listReverse(reqns);
(reqns, resVarsLst) = convertResidualsIntoSolvedEquations(reqns);
resVars = BackendVariable.listVar1(resVarsLst);
eqns = BackendEquation.listEquation(reqns);

// get other eqns
otherEqnsInts = List.map(otherEqnVarTpl2, Util.tuple21);
otherEqnsLst = BackendEquation.getEqns(otherEqnsInts, inEqns);
otherEqnsLst = BackendEquation.replaceDerOpInEquationList(otherEqnsLst);
oeqns = BackendEquation.listEquation(otherEqnsLst);

// get other vars
otherVarsIntsLst = List.map(otherEqnVarTpl2, Util.tuple22);
otherVarsInts = List.unionList(otherVarsIntsLst);
ovarsLst = List.map1r(otherVarsInts, BackendVariable.getVarAt, inVars);
ovarsLst = List.map(ovarsLst, BackendVariable.transformXToXd);
ovars = BackendVariable.listVar1(ovarsLst);

//generate jacobian name
name = "NLSJac" + intString(System.tmpTickIndex(Global.backendDAE_jacobianSeq));

// generate generic jacobian backend dae
(jacobian2, shared) = getSymbolicJacobian(diffVars, eqns, resVars, oeqns, ovars, inShared, inVars, name);

then (BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(iterationvarsInts, residualequations, otherEqnVarTpl, jacobian), SOME(BackendDAE.TEARINGSET(iterationvarsInts2, residualequations2, otherEqnVarTpl2, jacobian2)), b, mixedSystem), shared);

// do not touch linear and constand systems for now
case (comp as BackendDAE.EQUATIONSYSTEM(jacType=BackendDAE.JAC_CONSTANT()), _, _, _) then (comp, inShared);
case (comp as BackendDAE.EQUATIONSYSTEM(jacType=BackendDAE.JAC_LINEAR()), _, _, _) then (comp, inShared);
Expand Down
18 changes: 9 additions & 9 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -101,7 +101,6 @@ algorithm

// get method function and traveres systems
case(_) equation
//fcall2(Flags.TEARING_DUMPVERBOSE, BackendDump.dumpBackendDAE, inDAE, "DAE");
methodString = Config.getTearingMethod();
BackendDAE.DAE(shared=shared) = inDAE;
BackendDAE.SHARED(backendDAEType=DAEtype) = shared;
Expand Down Expand Up @@ -1610,10 +1609,10 @@ algorithm
eqnvartpllst = omcTearing4_1(othercomps,ass2,mapIncRowEqn,eindxarr,varindxarr,columark,mark,{});
linear = getLinearfromJacType(jacType);
then
(BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(ovars, ores, eqnvartpllst), NONE(), linear,BackendDAE.EMPTY_JACOBIAN(),mixedSystem),true);
(BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(ovars, ores, eqnvartpllst, BackendDAE.EMPTY_JACOBIAN()), NONE(), linear,mixedSystem),true);
case (_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)
then
(BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET({}, {}, {}), NONE(), false, BackendDAE.EMPTY_JACOBIAN(),mixedSystem),false);
(BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET({}, {}, {}, BackendDAE.EMPTY_JACOBIAN()), NONE(), false,mixedSystem),false);
end matchcontinue;
end omcTearing4;

Expand Down Expand Up @@ -1713,7 +1712,7 @@ protected
Option<BackendDAE.TearingSet> casualTearingSet;
list<BackendDAE.Equation> eqn_lst;
list<BackendDAE.Var> var_lst;
Boolean linear,simulation;
Boolean linear,simulation,b;
algorithm
BackendDAE.SHARED(backendDAEType=DAEtype) := ishared;
simulation := stringEq(BackendDump.printBackendDAEType2String(DAEtype), "simulation");
Expand Down Expand Up @@ -1799,7 +1798,7 @@ algorithm

// check if tearing makes sense
tornsize := listLength(OutTVars);
true := intLt(tornsize, size);
b := intLt(tornsize, size);

// Unassigned equations are residual equations
((_,residual)) := Array.fold(ass2,getUnassigned,(1,{}));
Expand All @@ -1824,7 +1823,7 @@ algorithm
otherEqnVarTpl := assignOtherEqnVarTpl(order,eindex,vindx,ass2,mapEqnIncRow,{});

// Create BackendDAE.TearingSet for strict set
strictTearingSet := BackendDAE.TEARINGSET(OutTVars,residual,otherEqnVarTpl);
strictTearingSet := BackendDAE.TEARINGSET(OutTVars,residual,otherEqnVarTpl,BackendDAE.EMPTY_JACOBIAN());


// Determine casual tearing set if dynamic tearing is enabled
Expand Down Expand Up @@ -1911,7 +1910,7 @@ algorithm
otherEqnVarTpl := assignOtherEqnVarTpl(order,eindex,vindx,ass2,mapEqnIncRow,{});

// Create BackendDAE.TearingSet for casual set
casualTearingSet := SOME(BackendDAE.TEARINGSET(OutTVars,residual,otherEqnVarTpl));
casualTearingSet := SOME(BackendDAE.TEARINGSET(OutTVars,residual,otherEqnVarTpl,BackendDAE.EMPTY_JACOBIAN()));
else
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\n" + BORDER + "\n* TEARING RESULTS (CASUAL SET):\n*\n* No of equations in strong Component: "+intString(size)+"\n");
Expand All @@ -1920,19 +1919,20 @@ algorithm
print("*\n* The casual tearing set is not smaller\n* than the strict tearing set and there-\n* fore it is discarded.\n*" + BORDER + "\n");
end if;

true := b;
casualTearingSet := NONE();
end if;

else

true := b;
casualTearingSet := NONE();
end if;

// Determine the rest of the information needed for BackendDAE.TORNSYSTEM
// ***************************************************************************

linear := getLinearfromJacType(jacType);
ocomp := BackendDAE.TORNSYSTEM(strictTearingSet,casualTearingSet,linear,BackendDAE.EMPTY_JACOBIAN(),mixedSystem);
ocomp := BackendDAE.TORNSYSTEM(strictTearingSet,casualTearingSet,linear,mixedSystem);
outRunMatching := true;
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nEND of CellierTearing\n" + BORDER + "\n\n");
Expand Down
2 changes: 1 addition & 1 deletion Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -3965,7 +3965,7 @@ algorithm
then (equations_, equations_, uniqueEqIndex, tempvars, tmpEqSccMapping, tmpBackendMapping);

// TORNSYSTEM
case (_, _, BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=tf, residualequations=rf, otherEqnVarTpl=eqnvartpllst), jac = jacobian, linear=b, mixedSystem=mixedSystem))
case (_, _, BackendDAE.TORNSYSTEM(BackendDAE.TEARINGSET(tearingvars=tf, residualequations=rf, otherEqnVarTpl=eqnvartpllst, jac = jacobian), linear=b, mixedSystem=mixedSystem))
equation
(equations_, uniqueEqIndex, tempvars) = createTornSystem(b, skipDiscInAlgorithm, tf, rf, eqnvartpllst, jacobian, isyst, ishared, iuniqueEqIndex, mixedSystem, itempvars);
tmpEqSccMapping = appendSccIdx(uniqueEqIndex-1, isccIndex, ieqSccMapping);
Expand Down

0 comments on commit 2da1e33

Please sign in to comment.