Skip to content

Commit 45e92f3

Browse files
committed
- handle mixed systems using tearing (new approach)
- remove poor handling of mixed systems from the back end (old approach) - remove unused record BackendDAE.MIXEDEQUATIONSYSTEM - remove flag +d=MixedTearing - update two result files - break Modelica.Fluid.Examples.HeatingSystem (msl32) due to non-linear solver issues - fix #1728, #2432 (and probably more) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22641 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 0f55ac7 commit 45e92f3

14 files changed

+47
-520
lines changed

Compiler/BackEnd/BackendDAE.mo

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,6 @@ uniontype StrongComponent
392392
JacobianType jacType;
393393
end EQUATIONSYSTEM;
394394

395-
record MIXEDEQUATIONSYSTEM
396-
StrongComponent condSystem;
397-
list<Integer> disc_eqns;
398-
list<Integer> disc_vars;
399-
end MIXEDEQUATIONSYSTEM;
400-
401395
record SINGLEARRAY
402396
Integer eqn;
403397
list<Integer> vars "be careful with states, this are solved for der(x)";

Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,10 +1766,6 @@ algorithm
17661766
var_lst = List.map1r(vindx, BackendVariable.getVarAt, vars);
17671767
(syst,shared) = solveLinearSystem(syst,shared,eqn_lst,eindex,var_lst,vindx,jac);
17681768
then (syst,shared,true);
1769-
case (syst,shared,(BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1)))
1770-
equation
1771-
(syst,shared,b) = constantLinearSystemWork(syst,shared,comp1);
1772-
then (syst,shared,b);
17731769
else (isyst,ishared,false);
17741770
end matchcontinue;
17751771
end constantLinearSystemWork;
@@ -2276,36 +2272,6 @@ algorithm
22762272

22772273
getSparsePattern2(inputVars, solvedVars, eqns, ineqnSparse, invarSparse, inMark, inUsed, inmarkValue);
22782274

2279-
result = getSparsePattern(rest, result, invarSparse, inMark, inUsed, inmarkValue+1, inMatrix, inMatrixT);
2280-
then result;
2281-
case(BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=BackendDAE.EQUATIONSYSTEM(eqns=eqns1,vars=vars1), disc_eqns=eqns,disc_vars=vars)::rest,result,_,_,_,_,_,_)
2282-
equation
2283-
eqns = listAppend(eqns, eqns1);
2284-
solvedVars = listAppend(vars, vars1);
2285-
inputVarsLst = List.map1(eqns, Util.arrayGetIndexFirst, inMatrix);
2286-
inputVars = List.flatten(inputVarsLst);
2287-
inputVars = List.fold1(solvedVars, List.removeOnTrue, intEq, inputVars);
2288-
2289-
getSparsePattern2(inputVars, solvedVars, eqns, ineqnSparse, invarSparse, inMark, inUsed, inmarkValue);
2290-
2291-
result = getSparsePattern(rest, result, invarSparse, inMark, inUsed, inmarkValue+1, inMatrix, inMatrixT);
2292-
then result;
2293-
case(BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=BackendDAE.TORNSYSTEM(residualequations=eqns1,tearingvars=vars1,otherEqnVarTpl=otherEqnVarTpl), disc_eqns=eqns,disc_vars=vars)::rest,result,_,_,_,_,_,_)
2294-
equation
2295-
inputVarsLst = List.map(otherEqnVarTpl,Util.tuple22);
2296-
vars2 = List.flatten(inputVarsLst);
2297-
eqns2 = List.map(otherEqnVarTpl,Util.tuple21);
2298-
eqns1 = listAppend(eqns1, eqns2);
2299-
vars1 = listAppend(vars1, vars2);
2300-
eqns = listAppend(eqns, eqns1);
2301-
solvedVars = listAppend(vars, vars1);
2302-
2303-
inputVarsLst = List.map1(eqns, Util.arrayGetIndexFirst, inMatrix);
2304-
inputVars = List.flatten(inputVarsLst);
2305-
inputVars = List.fold1(solvedVars, List.removeOnTrue, intEq, inputVars);
2306-
2307-
getSparsePattern2(inputVars, solvedVars, eqns, ineqnSparse, invarSparse, inMark, inUsed, inmarkValue);
2308-
23092275
result = getSparsePattern(rest, result, invarSparse, inMark, inUsed, inmarkValue+1, inMatrix, inMatrixT);
23102276
then result;
23112277
else
@@ -4676,13 +4642,6 @@ algorithm
46764642
(_,tpl) = BackendEquation.traverseBackendDAEExpsEqn(eqn,countOperationsExp,inTpl);
46774643
then
46784644
countOperationstraverseComps(rest,isyst,ishared,tpl);
4679-
case ((comp as BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp1))::rest,_,_,_)
4680-
equation
4681-
tpl = countOperationstraverseComps({comp1},isyst,ishared,inTpl);
4682-
(eqnlst,_,_) = BackendDAETransform.getEquationAndSolvedVar(comp, BackendEquation.getEqnsFromEqSystem(isyst), BackendVariable.daeVars(isyst));
4683-
tpl = BackendDAEUtil.traverseBackendDAEExpsEqns(BackendEquation.listEquation(eqnlst),countOperationsExp,tpl);
4684-
then
4685-
countOperationstraverseComps(rest,isyst,ishared,tpl);
46864645
case ((comp as BackendDAE.EQUATIONSYSTEM(jac=jac,jacType=BackendDAE.JAC_LINEAR()))::rest,_,BackendDAE.SHARED(functionTree=funcs),_)
46874646
equation
46884647
(eqnlst,varlst,_) = BackendDAETransform.getEquationAndSolvedVar(comp, BackendEquation.getEqnsFromEqSystem(isyst), BackendVariable.daeVars(isyst));
@@ -7485,14 +7444,6 @@ algorithm
74857444
case ({}, _)
74867445
then {};
74877446

7488-
case (BackendDAE.MIXEDEQUATIONSYSTEM(disc_vars=vlst)::rest, _) equation
7489-
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
7490-
false = List.isEmpty(varlst);
7491-
7492-
warning = "Iteration variables of mixed equation system:\n" +& warnAboutVars(varlst);
7493-
warningList = listAllIterationVariables2(rest, inVars);
7494-
then warning::warningList;
7495-
74967447
case (BackendDAE.EQUATIONSYSTEM(vars=vlst, jacType=BackendDAE.JAC_NONLINEAR())::rest, _) equation
74977448
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
74987449
false = List.isEmpty(varlst);

Compiler/BackEnd/BackendDAETransform.mo

Lines changed: 2 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,11 @@ algorithm
447447
case (compelem::{},_,(_,v)::{},_,_,_,_,false)
448448
then BackendDAE.SINGLEEQUATION(compelem,v);
449449

450-
case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(orderedVars=_,orderedEqs=_),shared,ass1,ass2,false)
451-
equation
452-
var_lst = List.map(var_varindx_lst,Util.tuple21);
453-
true = BackendVariable.hasDiscreteVar(var_lst);
454-
true = BackendVariable.hasContinousVar(var_lst);
455-
varindxs = List.map(var_varindx_lst,Util.tuple22);
456-
(cont_eqn,cont_var,_,_,indxcont_eqn,indxcont_var,indxdisc_eqn,indxdisc_var) = splitMixedEquations(eqn_lst, comp, var_lst, varindxs);
457-
var_varindx_lst_cond = List.threadTuple(cont_var,indxcont_var);
458-
sc = analyseStrongComponentBlock(indxcont_eqn,cont_eqn,var_varindx_lst_cond,syst,shared,ass1,ass2,true);
459-
then
460-
BackendDAE.MIXEDEQUATIONSYSTEM(sc,indxdisc_eqn,indxdisc_var);
461-
462450
case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(orderedVars=_,orderedEqs=_),shared,_,_,_)
463451
equation
464452
var_lst = List.map(var_varindx_lst,Util.tuple21);
465-
false = BackendVariable.hasDiscreteVar(var_lst);
453+
//false = BackendVariable.hasDiscreteVar(var_lst); //lochel: mixed systems and non-linear systems are treated the same
454+
true = BackendVariable.hasContinousVar(var_lst); //lochel: pure discrete equation systems are not supported
466455
varindxs = List.map(var_varindx_lst,Util.tuple22);
467456
eqn_lst1 = replaceDerOpInEquationList(eqn_lst);
468457
// States are solved for der(x) not x.
@@ -667,16 +656,6 @@ algorithm
667656
var = BackendVariable.getVarAt(vars, v);
668657
then
669658
({eqn}, {var}, e);
670-
case (BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp, disc_eqns=elst, disc_vars=vlst), eqns, vars)
671-
equation
672-
eqnlst1 = BackendEquation.getEqns(elst, eqns);
673-
varlst1 = List.map1r(vlst, BackendVariable.getVarAt, vars);
674-
e = List.first(elst);
675-
(eqnlst, varlst, _) = getEquationAndSolvedVar(comp, eqns, vars);
676-
eqnlst = listAppend(eqnlst, eqnlst1);
677-
varlst = listAppend(varlst, varlst1);
678-
then
679-
(eqnlst, varlst, e);
680659
case (BackendDAE.EQUATIONSYSTEM(eqns=elst, vars=vlst), eqns, vars)
681660
equation
682661
eqnlst = BackendEquation.getEqns(elst, eqns);
@@ -788,13 +767,6 @@ algorithm
788767
case (BackendDAE.SINGLEEQUATION(eqn=e,var=v))
789768
then
790769
({e},{v});
791-
case BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp,disc_eqns=elst,disc_vars=vlst)
792-
equation
793-
(elst1,vlst1) = getEquationAndSolvedVarIndxes(comp);
794-
elst = listAppend(elst1,elst);
795-
vlst = listAppend(vlst1,vlst);
796-
then
797-
(elst,vlst);
798770
case BackendDAE.EQUATIONSYSTEM(eqns=elst,vars=vlst)
799771
then
800772
(elst,vlst);
@@ -830,183 +802,6 @@ algorithm
830802
end matchcontinue;
831803
end getEquationAndSolvedVarIndxes;
832804

833-
protected function splitMixedEquations "author: PA
834-
835-
Splits the equation of a mixed equation system into its continuous and
836-
discrete parts.
837-
838-
Even though the matching algorithm might say that a discrete variable is solved in a specific equation
839-
(when part of a mixed system) this is not always correct. It might be impossible to solve the discrete
840-
variable from that equation, for instance solving v from equation x = v < 0; This happens for e.g. the Gear model.
841-
Instead, to split the equations and variables the following scheme is used:
842-
843-
1. Split the variables into continuous and discrete.
844-
2. For each discrete variable v, select among the equations where it is present
845-
for an equation v = expr. (This could be done
846-
by looking at incidence matrix but for now we look through all equations. This is sufficiently
847-
efficient for small systems of mixed equations < 100)
848-
3. The equations not selected in step 2 are continuous equations.
849-
"
850-
input list<BackendDAE.Equation> eqnLst;
851-
input list<Integer> indxEqnLst;
852-
input list<BackendDAE.Var> varLst;
853-
input list<Integer> indxVarLst;
854-
output list<BackendDAE.Equation> contEqnLst;
855-
output list<BackendDAE.Var> contVarLst;
856-
output list<BackendDAE.Equation> discEqnLst;
857-
output list<BackendDAE.Var> discVarLst;
858-
output list<Integer> indxcontEqnLst;
859-
output list<Integer> indxcontVarLst;
860-
output list<Integer> indxdiscEqnLst;
861-
output list<Integer> indxdiscVarLst;
862-
algorithm
863-
(contEqnLst, contVarLst, discEqnLst, discVarLst, indxcontEqnLst, indxcontVarLst, indxdiscEqnLst, indxdiscVarLst) := matchcontinue (eqnLst, indxEqnLst, varLst, indxVarLst)
864-
local
865-
list<tuple<BackendDAE.Equation, Integer>> eqnindxlst;
866-
867-
case (_, _, _, _) equation
868-
(discVarLst, contVarLst, indxdiscVarLst, indxcontVarLst) = splitVars(varLst, indxVarLst, BackendVariable.isVarDiscrete, {}, {}, {}, {});
869-
eqnindxlst = List.map1(discVarLst, findDiscreteEquation, (eqnLst, indxEqnLst));
870-
discEqnLst = List.map(eqnindxlst, Util.tuple21);
871-
indxdiscEqnLst = List.map(eqnindxlst, Util.tuple22);
872-
contEqnLst = List.setDifferenceOnTrue(eqnLst, discEqnLst, BackendEquation.equationEqual);
873-
indxcontEqnLst = List.setDifferenceOnTrue(indxEqnLst, indxdiscEqnLst, intEq);
874-
then (contEqnLst, contVarLst, discEqnLst, discVarLst, indxcontEqnLst, indxcontVarLst, indxdiscEqnLst, indxdiscVarLst);
875-
876-
case (_, _, _, _) equation
877-
Error.addInternalError(BackendDump.varListString(varLst, "involved variables"));
878-
Error.addInternalError(BackendDump.equationListString(eqnLst, "involved equations"));
879-
then fail();
880-
end matchcontinue;
881-
end splitMixedEquations;
882-
883-
protected function splitVars "
884-
Helper function to splitMixedEquations."
885-
input list<Type_a> inList;
886-
input list<Type_b> inListb;
887-
input PredicateFunc inFunc;
888-
input list<Type_a> inTrueList;
889-
input list<Type_a> inFalseList;
890-
input list<Type_b> inTrueListb;
891-
input list<Type_b> inFalseListb;
892-
output list<Type_a> outTrueList;
893-
output list<Type_a> outFalseList;
894-
output list<Type_b> outTrueListb;
895-
output list<Type_b> outFalseListb;
896-
897-
replaceable type Type_a subtypeof Any;
898-
replaceable type Type_b subtypeof Any;
899-
900-
partial function PredicateFunc
901-
input Type_a inElement;
902-
output Boolean outResult;
903-
end PredicateFunc;
904-
algorithm
905-
(outTrueList, outFalseList,outTrueListb, outFalseListb) := match(inList, inListb, inFunc, inTrueList, inFalseList, inTrueListb, inFalseListb)
906-
local
907-
Type_a e;
908-
Type_b eb;
909-
list<Type_a> rest_e, tl, fl;
910-
list<Type_b> rest_eb, tlb, flb;
911-
Boolean pred;
912-
913-
case ({}, {}, _, tl, fl, tlb, flb)
914-
then (listReverse(tl), listReverse(fl),listReverse(tlb), listReverse(flb));
915-
916-
case (e :: rest_e,eb :: rest_eb, _, tl, fl, tlb, flb) equation
917-
pred = inFunc(e);
918-
(tl, fl,tlb, flb) = splitVars1(e, rest_e,eb, rest_eb, pred, inFunc, tl, fl, tlb, flb);
919-
then (tl, fl,tlb, flb);
920-
end match;
921-
end splitVars;
922-
923-
protected function splitVars1 "
924-
Helper function to splitVars."
925-
input Type_a inHead;
926-
input list<Type_a> inRest;
927-
input Type_b inHeadb;
928-
input list<Type_b> inRestb;
929-
input Boolean inPred;
930-
input PredicateFunc inFunc;
931-
input list<Type_a> inTrueList;
932-
input list<Type_a> inFalseList;
933-
input list<Type_b> inTrueListb;
934-
input list<Type_b> inFalseListb;
935-
output list<Type_a> outTrueList;
936-
output list<Type_a> outFalseList;
937-
output list<Type_b> outTrueListb;
938-
output list<Type_b> outFalseListb;
939-
940-
replaceable type Type_a subtypeof Any;
941-
replaceable type Type_b subtypeof Any;
942-
943-
partial function PredicateFunc
944-
input Type_a inElement;
945-
output Boolean outResult;
946-
end PredicateFunc;
947-
algorithm
948-
(outTrueList, outFalseList,outTrueListb, outFalseListb) := match(inHead, inRest,inHeadb, inRestb, inPred, inFunc, inTrueList, inFalseList,inTrueListb, inFalseListb)
949-
local
950-
list<Type_a> tl, fl;
951-
list<Type_b> tlb, flb;
952-
953-
case (_, _, _, _, true, _, tl, fl, tlb, flb) equation
954-
tl = inHead :: tl;
955-
tlb = inHeadb :: tlb;
956-
(tl, fl, tlb, flb) = splitVars(inRest, inRestb, inFunc, tl, fl, tlb, flb);
957-
then (tl, fl, tlb, flb);
958-
959-
case (_, _, _, _, false, _, tl, fl, tlb, flb) equation
960-
fl = inHead :: fl;
961-
flb = inHeadb :: flb;
962-
(tl, fl, tlb, flb) = splitVars(inRest, inRestb, inFunc, tl, fl, tlb, flb);
963-
then (tl, fl, tlb, flb);
964-
end match;
965-
end splitVars1;
966-
967-
protected function findDiscreteEquation "help function to splitMixedEquations, finds the discrete equation
968-
on the form v = expr for solving variable v"
969-
input BackendDAE.Var v;
970-
input tuple<list<BackendDAE.Equation>, list<Integer>> eqnIndxLst;
971-
output tuple<BackendDAE.Equation, Integer> eqnindx;
972-
algorithm
973-
eqnindx := matchcontinue(v, eqnIndxLst)
974-
local
975-
DAE.ComponentRef cr1, cr;
976-
DAE.Exp e2;
977-
Integer i;
978-
BackendDAE.Equation eqn;
979-
list<Integer> ilst;
980-
list<BackendDAE.Equation> eqnLst;
981-
String errstr;
982-
983-
case (_, (((eqn as BackendDAE.EQUATION(exp=DAE.CREF(componentRef=cr), scalar=_))::_), i::_)) equation
984-
cr1=BackendVariable.varCref(v);
985-
true = ComponentReference.crefEqualNoStringCompare(cr1, cr);
986-
then ((eqn, i));
987-
988-
case(_, (((eqn as BackendDAE.EQUATION(exp=_, scalar=DAE.CREF(componentRef=cr)))::_), i::_)) equation
989-
cr1=BackendVariable.varCref(v);
990-
true = ComponentReference.crefEqualNoStringCompare(cr1, cr);
991-
then ((eqn, i));
992-
993-
case(_, (_::eqnLst, _::ilst)) equation
994-
((eqn, i)) = findDiscreteEquation(v, (eqnLst, ilst));
995-
then ((eqn, i));
996-
997-
else equation
998-
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAETransform.mo: function findDiscreteEquation failed
999-
Your model contains a mixed system involving algorithms or other complex-equations.
1000-
Sorry. Currently are supported only mixed system involving simple equations and boolean variables.
1001-
Try to break the loop by using the pre operator."});
1002-
true = Flags.isSet(Flags.FAILTRACE);
1003-
Debug.trace("findDiscreteEquation failed, searching for variables: ");
1004-
errstr = ComponentReference.printComponentRefStr(BackendVariable.varCref(v));
1005-
Debug.traceln(errstr);
1006-
then fail();
1007-
end matchcontinue;
1008-
end findDiscreteEquation;
1009-
1010805
public function tarjanAlgorithm "author: PA
1011806

1012807
This is the second part of the BLT sorting. It takes the variable

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7413,11 +7413,6 @@ algorithm
74137413
arg = traverseBackendDAEExps(bdae, inFunc, inTypeA);
74147414
then
74157415
traverseStrongComponentsJacobiansExp(rest, inFunc, arg);
7416-
case (BackendDAE.MIXEDEQUATIONSYSTEM(condSystem=comp)::rest, _, _)
7417-
equation
7418-
arg = traverseStrongComponentsJacobiansExp({comp}, inFunc, inTypeA);
7419-
then
7420-
traverseStrongComponentsJacobiansExp(rest, inFunc, arg);
74217416
case (_::rest, _, _) then
74227417
traverseStrongComponentsJacobiansExp(rest, inFunc, inTypeA);
74237418
end matchcontinue;

0 commit comments

Comments
 (0)