Skip to content

Commit 890fa49

Browse files
committed
- fixes for additional equations generated from a conditional component
which has condition false and contains a connector which has a flow component. - now the MultiBody elementary models DoublePendulum and Pendulum are balanced! git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@4759 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent a53758e commit 890fa49

File tree

1 file changed

+116
-81
lines changed

1 file changed

+116
-81
lines changed

Compiler/Connect.mo

Lines changed: 116 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -532,32 +532,35 @@ algorithm oIndexies := matchcontinue(curr,Indexies)
532532
end mergeCurrentWithRestIndexies;
533533

534534
public function equations "
535-
- Equation generation
536-
537-
From a number of connection sets, this function generates a list
538-
of equations.
539-
"
535+
Equation generation
536+
From a number of connection sets, this function generates a list of equations."
540537
input Sets sets;
541538
input Prefix.Prefix pre "prefix required for checking deleted components";
542539
output list<DAE.Element> eqns;
543-
algorithm
544-
eqns := matchcontinue(sets,pre)
545-
local list<Set> s;
540+
algorithm
541+
eqns := matchcontinue(sets,pre)
542+
local
543+
list<Set> s;
546544
list<DAE.ComponentRef> crs,deletedComps;
547545
DAE.ComponentRef cr,deletedComp;
548546
list<OuterConnect> outerConn;
549-
550-
case(sets as SETS(s,crs,{},outerConn),pre) equation
547+
// no deleted components
548+
case(sets as SETS(s,crs,{},outerConn),pre)
549+
equation
550+
//print(printSetsStr(sets));
551551
then equations2(sets);
552-
553-
case(SETS(s,crs,deletedComp::deletedComps,outerConn),pre) equation
554-
cr= deletedComp;
555-
s = removeComponentInSets(cr,s);
556-
then equations(SETS(s,crs,deletedComps,outerConn),pre);
557-
case(_,_) equation
558-
Debug.fprint("failtrace","Connect.equations failed\n");
559-
then fail();
560-
end matchcontinue;
552+
// handle deleted components
553+
case(SETS(s,crs,deletedComp::deletedComps,outerConn),pre)
554+
equation
555+
cr = deletedComp;
556+
s = removeComponentInSets(cr,s);
557+
then
558+
equations(SETS(s,crs,deletedComps,outerConn),pre);
559+
// failure
560+
case(_,_) equation
561+
Debug.fprint("failtrace","Connect.equations failed\n");
562+
then fail();
563+
end matchcontinue;
561564
end equations;
562565

563566
protected function removeComponentInSets "Removes all connections to component from the set"
@@ -566,19 +569,31 @@ protected function removeComponentInSets "Removes all connections to component f
566569
output list<Set> outS;
567570
algorithm
568571
outS := matchcontinue(compName,s)
569-
local list<DAE.ComponentRef> crs;
570-
list<tuple<DAE.ComponentRef, Face>> fcrs;
572+
local
573+
list<DAE.ComponentRef> crs;
574+
list<tuple<DAE.ComponentRef, Face>> fcrs;
575+
// handle the empty case
571576
case(compName,{}) then {};
572-
case(compName, EQU(crs)::s) equation
573-
crs = Util.listSelect1R(crs,compName,Exp.crefNotPrefixOf);
574-
s = removeComponentInSets(compName,s);
575-
then EQU(crs)::s;
576-
case(compName, FLOW(fcrs)::s) equation
577-
fcrs = Util.listSelect1(fcrs,compName,flowTupleNotPrefixOf);
578-
s = removeComponentInSets(compName,s);
579-
then FLOW(fcrs)::s;
580-
case(_,_) equation
581-
Debug.fprint("failtrace","-removeComponentInSets failed\n");
577+
// we have an equation
578+
case(compName, EQU(crs)::s)
579+
equation
580+
//print("Deleting: " +& Exp.printComponentRefStr(compName) +& "\n");
581+
crs = Util.listSelect1R(crs,compName,Exp.crefNotPrefixOf);
582+
//print("Result EQU after remove: " +& Util.stringDelimitList(Util.listMap(crs, Exp.printComponentRefStr), ", ") +& "\n");
583+
s = removeComponentInSets(compName,s);
584+
then EQU(crs)::s;
585+
// we have a flow component
586+
case(compName, FLOW(fcrs)::s)
587+
equation
588+
//print("Deleting: " +& Exp.printComponentRefStr(compName) +& "\n");
589+
fcrs = Util.listSelect1(fcrs,compName,flowTupleNotPrefixOf);
590+
//print("Result FLOW after remove: " +& Util.stringDelimitList(Util.listMap(Util.listMap(fcrs, Util.tuple21), Exp.printComponentRefStr), ", ") +& "\n");
591+
s = removeComponentInSets(compName,s);
592+
then FLOW(fcrs)::s;
593+
// failure
594+
case(compName,_) equation
595+
// print("Failed to remove component:" +& Exp.printComponentRefStr(compName) +& "\n");
596+
Debug.fprintln("failtrace","- Connect.removeComponentInSets failed");
582597
then fail();
583598
end matchcontinue;
584599
end removeComponentInSets;
@@ -597,14 +612,12 @@ algorithm
597612
end flowTupleNotPrefixOf;
598613

599614
protected function equations2 "
600-
Helper function to equations. Once deleted components has been removed from connection sets,
601-
this function generates the equations.
602-
"
615+
Helper function to equations. Once deleted components has been
616+
removed from connection sets, this function generates the equations."
603617
input Sets inSets;
604618
output list<DAE.Element> outDAEElementLst;
605619
algorithm
606-
outDAEElementLst:=
607-
matchcontinue (inSets)
620+
outDAEElementLst := matchcontinue (inSets)
608621
local
609622
list<DAE.Element> dae1,dae2,dae;
610623
list<DAE.ComponentRef> cs,crs,dc;
@@ -625,7 +638,7 @@ algorithm
625638
equation
626639
dae = equations2(SETS(ss,crs,dc,outerConn));
627640
then
628-
dae;
641+
dae;
629642

630643
case (SETS((EQU(expComponentRefLst = cs) :: ss),crs,dc,outerConn))
631644
equation
@@ -644,7 +657,7 @@ algorithm
644657
dae;
645658
case (sets)
646659
equation
647-
Debug.fprint("failtrace","-equations2 failed\n");
660+
Debug.fprint("failtrace","- Connect.equations2 failed\n");
648661
then
649662
fail();
650663
end matchcontinue;
@@ -679,16 +692,13 @@ algorithm
679692
end equEquations;
680693

681694
protected function flowEquations "function: flowEquations
682-
683695
Generating equations from a flow connection set is a little
684696
trickier that from a non-flow set. Only one equation is
685697
generated, but it has to consider whether the comoponents were
686698
inner or outer connectors.
687-
688699
This function uses `flow_sum\' to create the sum of all components
689700
(some of which will be negated), and the returns the equation
690-
where this sum is equal to 0.0.
691-
"
701+
where this sum is equal to 0.0."
692702
input list<tuple<DAE.ComponentRef, Face>> cs;
693703
output list<DAE.Element> outDAEElementLst;
694704
DAE.Exp sum;
@@ -698,25 +708,23 @@ algorithm
698708
end flowEquations;
699709

700710
protected function flowSum "function: flowSum
701-
702711
This function creates an exression expressing the sum of all
703712
components in the given list. Before adding the component to the
704713
sum, it is passed to `sign_flow\' which will negate all outer
705-
connectors.
706-
"
714+
connectors."
707715
input list<tuple<DAE.ComponentRef, Face>> inTplExpComponentRefFaceLst;
708716
output DAE.Exp outExp;
709717
algorithm
710-
outExp:=
711-
matchcontinue (inTplExpComponentRefFaceLst)
718+
outExp := matchcontinue (inTplExpComponentRefFaceLst)
712719
local
713720
DAE.Exp exp,exp1,exp2;
714721
DAE.ComponentRef c;
715722
Face f;
716723
list<tuple<DAE.ComponentRef, Face>> cs;
717724
case {(c,f)}
718-
equation
725+
equation
719726
exp = signFlow(c, f);
727+
// print("Generating flow expresion: " +& Exp.printExpStr(exp) +& "\n");
720728
then
721729
exp;
722730
case (((c,f) :: cs))
@@ -1063,8 +1071,7 @@ public function unconnectedFlowEquations "Unconnected flow variables.
10631071
output Env.Cache outCache;
10641072
output list<DAE.Element> outDAEElementLst;
10651073
algorithm
1066-
(outCache,outDAEElementLst) :=
1067-
matchcontinue (inCache,inSets,inDAEElementLst,inEnv,prefix,inBoolean,ocl)
1074+
(outCache,outDAEElementLst) := matchcontinue (inCache,inSets,inDAEElementLst,inEnv,prefix,inBoolean,ocl)
10681075
local
10691076
list<DAE.ComponentRef> v1,v2,v3,vSpecial,vars,vars2,vars3,unconnectedvars,deletedComponents;
10701077
list<DAE.Element> dae_1,dae;
@@ -1089,7 +1096,7 @@ algorithm
10891096
//print(" Inside connectors, v2: " +& Util.stringDelimitList(Util.listMap(v2,Exp.printComponentRefStr),", ") +& "\n");
10901097

10911098
vars = listAppend(v1, v2);
1092-
vars2 = getInsideFlowVariables(csets);
1099+
vars2 = getInsideFlowVariables(csets);
10931100
vars3 = getOuterConnectFlowVariables(csets,vars,prefix);
10941101
vars2 = listAppend(vars3,vars2);
10951102

@@ -1099,7 +1106,7 @@ algorithm
10991106
// last array subscripts are not present in vars, therefor removed from vars2 too.
11001107
vars2 = Util.listMap(vars2,Exp.crefStripLastSubs);
11011108
unconnectedvars = removeVariables(vars, vars2);
1102-
unconnectedvars = removeUnconnectedDeletedComponents(unconnectedvars,csets);
1109+
unconnectedvars = removeUnconnectedDeletedComponents(unconnectedvars,csets,prefix);
11031110

11041111
// no prefix for top level
11051112
/* SE COMMENT ABOVE
@@ -1119,7 +1126,7 @@ algorithm
11191126
// last array subscripts are not present in vars, therefor removed from vars2 too.
11201127
vars2 = Util.listMap(vars2,Exp.crefStripLastSubs);
11211128
unconnectedvars = removeVariables(vars, vars2);
1122-
unconnectedvars = removeUnconnectedDeletedComponents(unconnectedvars,csets);
1129+
unconnectedvars = removeUnconnectedDeletedComponents(unconnectedvars,csets,prefix);
11231130

11241131
// Add prefix that was "removed" above
11251132
(cache,dae_1) = generateZeroflowEquations(cache,unconnectedvars,env,prefix,deletedComponents);
@@ -1247,31 +1254,59 @@ end extractFlowCrefs;
12471254
protected function removeUnconnectedDeletedComponents "Removes deleted components,
12481255
i.e. with conditional declaration = false, from
12491256
the list of unconnected variables"
1250-
input list<DAE.ComponentRef> vars;
1251-
input Sets sets;
1252-
output list<DAE.ComponentRef> outVars;
1257+
input list<DAE.ComponentRef> vars;
1258+
input Sets sets;
1259+
input Prefix.Prefix prefix;
1260+
output list<DAE.ComponentRef> outVars;
12531261
algorithm
1254-
outVars := matchcontinue(vars,sets)
1255-
local
1256-
DAE.ComponentRef deletedComp;
1257-
list<Set> s;
1258-
list<DAE.ComponentRef> crs,deletedComps;
1259-
list<OuterConnect> outerConn;
1262+
outVars := matchcontinue(vars,sets,prefix)
1263+
local
1264+
DAE.ComponentRef deletedComp;
1265+
list<Set> s;
1266+
list<DAE.ComponentRef> crs,deletedComps;
1267+
list<OuterConnect> outerConn;
12601268

1261-
case(vars,SETS(s,crs,{},_)) then vars;
1269+
case(vars,SETS(s,crs,{},_),prefix) then vars;
12621270

1263-
case(vars,SETS(s,crs,deletedComp::deletedComps,outerConn))
1271+
case(vars,SETS(s,crs,deletedComp::deletedComps,outerConn),prefix)
12641272
equation
1265-
vars = Util.listSelect1R(vars,deletedComp,Exp.crefNotPrefixOf);
1266-
vars = removeUnconnectedDeletedComponents(vars,SETS(s,crs,deletedComps,outerConn));
1267-
then vars;
1273+
vars = Util.listSelect2(vars, deletedComp, prefix, crefNotPrefixOf);
1274+
// print("Deleting: " +& Exp.printComponentRefStr(deletedComp) +& "\n");
1275+
// print("Result unconnected vars after remove -> prefix: " +& Prefix.printPrefixStr(prefix) +& "/" +& Util.stringDelimitList(Util.listMap(vars, Exp.printComponentRefStr), ", ") +& "\n");
1276+
vars = removeUnconnectedDeletedComponents(vars,SETS(s,crs,deletedComps,outerConn),prefix);
1277+
then vars;
12681278
end matchcontinue;
12691279
end removeUnconnectedDeletedComponents;
12701280

1281+
protected function crefNotPrefixOf
1282+
input DAE.ComponentRef crSubPrefix;
1283+
input DAE.ComponentRef cr;
1284+
input Prefix.Prefix prefix;
1285+
output Boolean selected;
1286+
algorithm
1287+
selected := matchcontinue (crSubPrefix, cr, prefix)
1288+
local DAE.ComponentRef prefixCref; Boolean select;
1289+
// deal with NO prefix!
1290+
case (crSubPrefix, cr, Prefix.NOPRE())
1291+
equation
1292+
select = not Exp.crefPrefixOf(cr, crSubPrefix);
1293+
then
1294+
select;
1295+
case (crSubPrefix, cr, prefix)
1296+
equation
1297+
// adrpo: we need to ADD the prefix otherwise it won't find components!
1298+
// Example of the problem: Deleting: rev.constantTorque
1299+
// Result unconnected vars after remove: constantTorque.support.tau <- add it here
1300+
prefixCref = Prefix.prefixToCref(prefix);
1301+
crSubPrefix = Exp.joinCrefs(prefixCref, crSubPrefix);
1302+
select = not Exp.crefPrefixOf(cr, crSubPrefix);
1303+
then
1304+
select;
1305+
end matchcontinue;
1306+
end crefNotPrefixOf;
1307+
12711308
protected function removeVariables "function: removeVariables
1272-
1273-
Removes all the variables in the second list from the first list.
1274-
"
1309+
Removes all the variables in the second list from the first list."
12751310
input list<DAE.ComponentRef> inExpComponentRefLst1;
12761311
input list<DAE.ComponentRef> inExpComponentRefLst2;
12771312
output list<DAE.ComponentRef> outExpComponentRefLst;
@@ -1721,30 +1756,31 @@ algorithm
17211756
end printFlowRef;
17221757

17231758
public function printSetsStr "function: printSetsStr
1724-
1725-
Prints a description of a number of connection sets to a string
1726-
"
1759+
Prints a description of a number of connection sets to a string"
17271760
input Sets inSets;
17281761
output String outString;
17291762
algorithm
1730-
outString:=
1731-
matchcontinue (inSets)
1763+
outString := matchcontinue (inSets)
17321764
local
17331765
list<String> s1;
17341766
String s1_1,s2,res,s3,s4;
17351767
list<Set> sets;
17361768
list<DAE.ComponentRef> crs;
17371769
list<DAE.ComponentRef> dc;
17381770
list<OuterConnect> outerConn;
1771+
case SETS(setLst = {},connection = {},deletedComponents = {},outerConnects = {})
1772+
equation
1773+
res = "SETS( EMPTY )\n";
1774+
then
1775+
res;
17391776
case SETS(setLst = sets,connection = crs,deletedComponents=dc,outerConnects=outerConn)
17401777
equation
17411778
s1 = Util.listMap(sets, printSetStr);
17421779
s1_1 = Util.stringDelimitList(s1, ", ");
17431780
s2 = printSetCrsStr(crs);
17441781
s3 = Util.stringDelimitList(Util.listMap(dc,Exp.printComponentRefStr),",");
17451782
s4 = printOuterConnectsStr(outerConn);
1746-
res = Util.stringAppendList({"SETS(\n ",s1_1,",\n ",s2,", \n deleted comps:",s3,
1747-
"\n outer connections:",s4,"\n)\n"});
1783+
res = Util.stringAppendList({"SETS( ",s1_1,", ",s2,", deleted comps: ",s3,", outer connections:",s4,")\n"});
17481784
then
17491785
res;
17501786
end matchcontinue;
@@ -1772,8 +1808,7 @@ protected function printSetStr
17721808
input Set inSet;
17731809
output String outString;
17741810
algorithm
1775-
outString:=
1776-
matchcontinue (inSet)
1811+
outString := matchcontinue (inSet)
17771812
local
17781813
list<String> strs;
17791814
String s1,res;
@@ -1782,15 +1817,15 @@ algorithm
17821817
equation
17831818
strs = Util.listMap(cs, Exp.printComponentRefStr);
17841819
s1 = Util.stringDelimitList(strs, ", ");
1785-
res = Util.stringAppendList({" non-flow set: { ",s1,"}"});
1820+
res = Util.stringAppendList({" non-flow set: {",s1,"}"});
17861821
then
17871822
res;
17881823
case FLOW(tplExpComponentRefFaceLst = cs)
17891824
local list<tuple<DAE.ComponentRef, Face>> cs;
17901825
equation
17911826
strs = Util.listMap(cs, printFlowRefStr);
17921827
s1 = Util.stringDelimitList(strs, ", ");
1793-
res = Util.stringAppendList({" flow set: { ",s1,"}"});
1828+
res = Util.stringAppendList({" flow set: {",s1,"}"});
17941829
then
17951830
res;
17961831
end matchcontinue;

0 commit comments

Comments
 (0)