Skip to content

Commit fe850cb

Browse files
author
Björn Zachrisson
committed
* Fixed initial array equations
* Activated back patching of initial if equations with Connection.isRoot() as statements. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5117 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 5b6f8ef commit fe850cb

File tree

3 files changed

+180
-11
lines changed

3 files changed

+180
-11
lines changed

Compiler/DAEUtil.mo

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,14 @@ algorithm
840840
then
841841
();
842842
case (DAE.DAE(elementLst = {})) then ();
843-
843+
844+
//BZ Could be nice to know when this failes (when new elements are introduced)
845+
case(DAE.DAE((_ :: xs),funcs))
846+
equation
847+
Print.printBuf("\n\ndump2 failed to print element\n");
848+
dump2(DAE.DAE(xs,funcs));
849+
then
850+
();
844851
case (_)
845852
equation
846853
Print.printBuf("dump2 failed\n");
@@ -1078,6 +1085,18 @@ algorithm
10781085
Util.listMap0(l, dumpAlgorithm);
10791086
end dumpFunctionElements;
10801087

1088+
public function dumpDAEElementsStr "
1089+
Author BZ
1090+
print a DAE.DAEList to a string
1091+
"
1092+
input DAE.DAElist d;
1093+
output String str;
1094+
algorithm str := matchcontinue(d)
1095+
local list<DAE.Element> l;
1096+
case(DAE.DAE(elementLst=l)) then dumpElementsStr(l);
1097+
end matchcontinue;
1098+
end dumpDAEElementsStr;
1099+
10811100
public function dumpElementsStr "function: dumpElementsStr
10821101
Dump elements to a string"
10831102
input list<DAE.Element> l;
@@ -1366,7 +1385,7 @@ algorithm
13661385
s4 = Exp.printExpStr(e2);
13671386
s4_1 = stringAppend(s3, s4);
13681387
s5 = stringAppend(s4_1, ";\n");
1369-
s6 = dumpEquationsStr(xs);
1388+
s6 = dumpInitialequationsStr(xs);
13701389
str = stringAppend(s5, s6);
13711390
then
13721391
str;
@@ -2401,12 +2420,6 @@ algorithm
24012420
str = s1 +& " = " +& s2;
24022421
then str;
24032422

2404-
case(DAE.INITIAL_ARRAY_EQUATION(exp=e1,array=e2)) equation
2405-
s1 = Exp.printExpStr(e1);
2406-
s2 = Exp.printExpStr(e2);
2407-
str = s1 +& " = " +& s2;
2408-
then str;
2409-
24102423
case(DAE.COMPLEX_EQUATION(lhs=e1,rhs=e2)) equation
24112424
s1 = Exp.printExpStr(e1);
24122425
s2 = Exp.printExpStr(e2);

Compiler/Inst.mo

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,127 @@ algorithm
394394
end matchcontinue;
395395
end instantiateClass;
396396

397-
public function instantiatePartialClass
397+
protected function reEvaluateInitialIfEqns "
398+
Author BZ
399+
This is a backpatch to fix the case of 'connection.isRoot' in initial if equations.
400+
After the class is instantiated a second sweep is done to check the initial if equations conditions.
401+
If all conditions are constand, we return only the 'correct' branch equations.
402+
"
403+
input Env.Cache cache;
404+
input Env.Env env;
405+
input DAE.DAElist dae;
406+
input Boolean isTopCall;
407+
output DAE.DAElist odae;
408+
algorithm odae := matchcontinue(cache,env,dae,isTopCall)
409+
local
410+
DAE.FunctionTree funcs;
411+
list<DAE.Element> elems;
412+
case(cache,env,DAE.DAE(elementLst = elems,functions = funcs),true)
413+
equation
414+
elems = reEvaluateInitialIfEqns2(cache,env,elems);
415+
then
416+
DAE.DAE(elems,funcs);
417+
case(_,_,dae,false) then dae;
418+
end matchcontinue;
419+
end reEvaluateInitialIfEqns;
420+
421+
protected function reEvaluateInitialIfEqns2 ""
422+
input Env.Cache cache;
423+
input Env.Env env;
424+
input list<DAE.Element> elems;
425+
output list<DAE.Element> oelems;
426+
algorithm oelems := matchcontinue(cache,env,elems)
427+
local
428+
list<DAE.Exp> conds;
429+
list<Values.Value> valList;
430+
list<list<DAE.Element>> tbs;
431+
list<DAE.Element> fb,selectedBranch;
432+
DAE.Element elem;
433+
DAE.ElementSource source;
434+
list<Boolean> blist;
435+
case(_,_,{}) then {};
436+
case(cache,env,(elem as DAE.INITIAL_IF_EQUATION(condition1 = conds, equations2=tbs, equations3=fb, source=source))::elems)
437+
equation
438+
//print(" (Initial if)To ceval: " +& Util.stringDelimitList(Util.listMap(conds,Exp.printExpStr),", ") +& "\n");
439+
(cache,valList) = Ceval.cevalList(cache,env, conds, true, NONE, Ceval.NO_MSG());
440+
//print(" Ceval res: ("+&Util.stringDelimitList(Util.listMap(valList,ValuesUtil.printValStr),",")+&")\n");
441+
442+
blist = Util.listMap(valList,ValuesUtil.valueBool);
443+
selectedBranch = selectList(blist, tbs, fb);
444+
selectedBranch = makeDAEElementInitial(selectedBranch);
445+
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
446+
oelems = listAppend(selectedBranch,oelems);
447+
448+
//print("RETURN _INITIAL_ DAE: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE(selectedBranch,DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
449+
//print(" INSTEAD OF: " +& DAEUtil.dumpDAEElementsStr(DAE.DAE({elem},DAE.AVLTREENODE(NONE,0,NONE,NONE))) +& "\n");
450+
then
451+
oelems;
452+
case(cache,env,elem::elems)
453+
equation
454+
oelems = reEvaluateInitialIfEqns2(cache,env,elems);
455+
then
456+
elem::oelems;
457+
end matchcontinue;
458+
end reEvaluateInitialIfEqns2;
459+
460+
protected function makeDAEElementInitial "
461+
Author BZ
462+
Helper function for reEvaluateInitialIfEqns, makes the contenst of an initial if equation initial.
463+
"
464+
input list<DAE.Element> inElems;
465+
output list<DAE.Element> outElems;
466+
algorithm
467+
outElems := matchcontinue(inElems)
468+
local
469+
DAE.Element elem;
470+
DAE.ComponentRef cr;
471+
DAE.Exp e1,e2,e3;
472+
DAE.ElementSource s;
473+
list<DAE.Exp> expl;
474+
list<list<DAE.Element>> tbs ;
475+
list<DAE.Element> fb;
476+
DAE.Algorithm al;
477+
list<Integer> dims;
478+
case({}) then {};
479+
case(DAE.DEFINE(cr,e1,s)::inElems)
480+
equation
481+
outElems = makeDAEElementInitial(inElems);
482+
then
483+
DAE.INITIALDEFINE(cr,e1,s)::outElems;
484+
case(DAE.ARRAY_EQUATION(dims,e1,e2,s)::_)
485+
equation
486+
outElems = makeDAEElementInitial(inElems);
487+
then
488+
DAE.INITIAL_ARRAY_EQUATION(dims,e1,e2,s)::outElems;
489+
case(DAE.EQUATION(e1,e2,s)::inElems)
490+
equation
491+
outElems = makeDAEElementInitial(inElems);
492+
then
493+
DAE.INITIALEQUATION(e1,e2,s)::outElems;
494+
case(DAE.IF_EQUATION(expl,tbs,fb,s)::inElems)
495+
equation
496+
outElems = makeDAEElementInitial(inElems);
497+
then
498+
DAE.INITIAL_IF_EQUATION(expl,tbs,fb,s)::outElems;
499+
case(DAE.ALGORITHM(al,s)::inElems)
500+
equation
501+
outElems = makeDAEElementInitial(inElems);
502+
then
503+
DAE.INITIALALGORITHM(al,s)::outElems;
504+
case(DAE.COMPLEX_EQUATION(e1,e2,s)::inElems)
505+
equation
506+
outElems = makeDAEElementInitial(inElems);
507+
then
508+
DAE.INITIAL_COMPLEX_EQUATION(e1,e2,s)::outElems;
509+
case(elem::inElems) // safe "last case" since we can not fail in cases above.
510+
equation
511+
outElems = makeDAEElementInitial(inElems);
512+
then
513+
elem::outElems;
514+
end matchcontinue;
515+
end makeDAEElementInitial;
516+
517+
public function instantiatePartialClass
398518
"Author: BZ, 2009-07
399519
This is a function for instantiating partial 'top' classes.
400520
It does so by converting the partial class into a non partial class.
@@ -626,6 +746,10 @@ algorithm
626746
(cache,env_1,ih,_,dae,_,_,_,_,graph) = instClass(cache,env, ih, UnitAbsynBuilder.emptyInstStore(), DAE.NOMOD(), Prefix.NOPRE(), Connect.emptySet, c, {}, false, TOP_CALL(), ConnectionGraph.EMPTY) "impl" ;
627747
// deal with Overconstrained connections
628748
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);
749+
750+
//print(" ********************** backpatch 2 **********************\n");
751+
dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);
752+
629753
// check the models for balancing
630754
//Debug.fcall2("checkModel",checkModelBalancing,SOME(inPath),dae);
631755
then
@@ -911,6 +1035,9 @@ algorithm
9111035
Debug.fcall("execstat",print, "*** Inst -> instClass finished at time: " +& realString(clock()) +& "\n" );
9121036
// deal with Overconstrained connections
9131037
dae = ConnectionGraph.handleOverconstrainedConnections(graph, dae);
1038+
//print(" ********************** backpatch 3 **********************\n");
1039+
dae = reEvaluateInitialIfEqns(cache,env_1,dae,true);
1040+
9141041
// check the models for balancing
9151042
//Debug.fcall2("checkModel",checkModelBalancing,containedInOpt,dae);
9161043

@@ -920,7 +1047,8 @@ algorithm
9201047
// finish with the execution statistics
9211048
Debug.fcall("execstat",print, "*** Inst -> exit at time: " +& realString(clock()) +& "\n" );
9221049

923-
daeElts = DAEUtil.daeElements(dae); funcs = DAEUtil.daeFunctionTree(dae);
1050+
daeElts = DAEUtil.daeElements(dae);
1051+
funcs = DAEUtil.daeFunctionTree(dae);
9241052
dae = DAE.DAE({DAE.COMP(n,daeElts,source)},funcs);
9251053
then
9261054
(cache,ih,dae);

Compiler/Util.mo

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,35 @@ algorithm
20732073
end matchcontinue;
20742074
end listListMap2;
20752075

2076-
public function listFold "function: listFold
2076+
public function listFoldList "
2077+
Author BZ
2078+
apply a function on the heads of two equally length list of generic type.
2079+
"
2080+
input list<Type_a> lst1;
2081+
input list<Type_a> lst2;
2082+
input listAddFunc func;
2083+
output list<Type_a> mergedList;
2084+
partial function listAddFunc
2085+
input Type_a ia1;
2086+
input Type_a ia2;
2087+
output Type_a oa1;
2088+
end listAddFunc;
2089+
replaceable type Type_a subtypeof Any;
2090+
algorithm
2091+
mergedList := matchcontinue(lst1,lst2,func)
2092+
local
2093+
Type_a a1,a2,aRes;
2094+
case({},{},_) then {};
2095+
case(a1::lst1,a2::lst2,func)
2096+
equation
2097+
aRes = func(a1,a2);
2098+
mergedList = listFoldList(lst1,lst2,func);
2099+
then
2100+
aRes::mergedList;
2101+
end matchcontinue;
2102+
end listFoldList;
2103+
2104+
public function listFold "function: listFold
20772105
Takes a list and a function operating on list elements having an extra argument that is \'updated\'
20782106
thus returned from the function. The third argument is the startvalue for the updated value.
20792107
listFold will call the function for each element in a sequence, updating the startvalue

0 commit comments

Comments
 (0)