Skip to content

Commit

Permalink
- Exp.mo
Browse files Browse the repository at this point in the history
  Handle also WHOLEDIM in Exp.replaceSliceSub.
  WHOLEDIM is *also* a special case of SLICE
  that contains the all subscripts, so we need
  to handle that too!
  This will get rid of errors like:
  The generated equations:
   controller.observer.y[:,1] = controller.L.u[:,1];
   controller.observer.y[:,2] = controller.L.u[:,2];
  should be instead;
   controller.observer.y[1] = controller.L.u[1];
   controller.observer.y[2] = controller.L.u[2];

- DAEUtil.mo
  - count internal equations in if equations by removing assert and terminate
  - handle more equation types in DAEUtil.makeEquationToResidualExp
  - todo: handle assert in if equations by translating them to
          assert(NOT condition_from_if OR condition_from_assert, ...)
          or something like this (my boolean logic doesn't work at this hour).



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5025 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Feb 24, 2010
1 parent 65b7bf3 commit ad3d230
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 32 deletions.
109 changes: 99 additions & 10 deletions Compiler/DAEUtil.mo
Expand Up @@ -48,8 +48,7 @@ public constant DAE.DAElist emptyDae = DAE.DAE({},DAE.AVLTREENODE(NONE,0,NONE,NO

public function derivativeOrder "
Function to sort derivatives.
Used for Util.sort
"
Used for Util.sort"
input tuple<Integer,DAE.derivativeCond> e1,e2; //greaterThanFunc
output Boolean b;
Integer i1,i2;
Expand Down Expand Up @@ -100,7 +99,7 @@ matchcontinue (bindExp,attr)
then SOME(DAE.VAR_ATTR_BOOL(e1,e2,e3,SOME(bindExp),ip,fn));
case (bindExp,SOME(DAE.VAR_ATTR_STRING(e1,e2,_,ip,fn)))
then SOME(DAE.VAR_ATTR_STRING(e1,e2,SOME(bindExp),ip,fn));
case(_,_) equation print("-failur DAE.add_Equation_Bound_String\n"); then fail();
case(_,_) equation print("-failure in DAEUtil.addEquationBoundString\n"); then fail();
end matchcontinue;
end addEquationBoundString;

Expand Down Expand Up @@ -5491,7 +5490,7 @@ algorithm
case (elt as DAE.IF_EQUATION(condition1=_),onlyConstantEval) // only display failure on if equation
equation
elt_str = dumpElementsStr({elt});
Debug.fprintln("failtrace", "- DAE.ifEqToExpr failed " +& elt_str);
Debug.fprintln("failtrace", "- DAEUtil.ifEqToExpr failed " +& elt_str);
then fail();
end matchcontinue;
end ifEqToExpr;
Expand All @@ -5503,16 +5502,40 @@ algorithm len := matchcontinue(tbs)
local
list<DAE.Element> tb;
Integer recLen;
case(tb::{}) then listLength(tb);
case(tb::{}) then countEquations(tb);
case(tb::tbs)
equation
recLen = ifEqToExpr2(tbs);
recLen = Util.if_(intEq(recLen,listLength(tb)),recLen,-1);
recLen = Util.if_(intEq(recLen,countEquations(tb)),recLen,-1);
then
recLen;
end matchcontinue;
end ifEqToExpr2;

protected function countEquations
input list<DAE.Element> equations;
output Integer nrOfEquations;
algorithm
nrOfEquations := matchcontinue(equations)
local
list<DAE.Element> rest;
Integer nr;
// empty case
case ({}) then 0;
// ignore assert!
case (DAE.ASSERT(condition = _)::rest)
then countEquations(rest);
// ignore terminate!
case (DAE.TERMINATE(message=_)::rest)
then countEquations(rest);
// any other case, just add 1
case (_::rest)
equation
nr = countEquations(rest);
then nr + 1;
end matchcontinue;
end countEquations;

protected function makeEquationsFromIf
input list<DAE.Exp> inExp1;
input list<list<DAE.Element>> inElementLst2;
Expand All @@ -5523,19 +5546,24 @@ algorithm
outElementLst := matchcontinue (inExp1,inElementLst2,inElementLst3,source)
local
list<list<DAE.Element>> tbs,rest1,tbsRest,tbsFirstL;
list<DAE.Element> tbsFirst,fbs,rest_res;
list<DAE.Element> tbsFirst,fbs,rest_res,tb;
DAE.Element fb,eq;
list<DAE.Exp> conds,tbsexp;
DAE.Exp fbexp,ifexp;
DAE.Exp fbexp,ifexp, cond;
DAE.ElementSource source "the origin of the element";

case (_,tbs,{},_)
equation
Util.listMap0(tbs, Util.assertListEmpty);
then {};

// adrpo: not all equations can be transformed using makeEquationToResidualExp
// for example, assert, terminate, etc. TODO! FIXME!
// if cond then assert(cnd, ...); endif; can be translated to:
// assert(cond AND cnd, ...

case (conds,tbs,fb::fbs,source)
equation
equation
tbsRest = Util.listMap(tbs,Util.listRest);
rest_res = makeEquationsFromIf(conds, tbsRest, fbs, source);

Expand All @@ -5557,11 +5585,72 @@ algorithm
oExp := matchcontinue(eq)
local
DAE.Exp e1,e2;
DAE.ComponentRef cr1,cr2;
DAE.ExpType ty,ty1,ty2;
// normal equation
case(DAE.EQUATION(e1,e2,_))
equation
oExp = DAE.BINARY(e1,DAE.SUB(DAE.ET_REAL()),e2);
ty = Exp.typeof(e1);
oExp = DAE.BINARY(e1,DAE.SUB(ty),e2);
then
oExp;
// initial equation
case(DAE.INITIALEQUATION(e1,e2,_))
equation
ty = Exp.typeof(e1);
oExp = DAE.BINARY(e1,DAE.SUB(ty),e2);
then
oExp;
// complex equation
case(DAE.COMPLEX_EQUATION(lhs = e1, rhs = e2))
equation
ty = Exp.typeof(e1);
oExp = DAE.BINARY(e1,DAE.SUB(ty),e2);
then
oExp;
// complex initial equation
case(DAE.INITIAL_COMPLEX_EQUATION(lhs = e1, rhs = e2))
equation
ty = Exp.typeof(e1);
oExp = DAE.BINARY(e1,DAE.SUB(ty),e2);
then
oExp;
// equation from connect
case(DAE.EQUEQUATION(cr1, cr2, _))
equation
ty1 = Exp.crefType(cr1);
ty2 = Exp.crefType(cr2);
oExp = DAE.BINARY(DAE.CREF(cr1,ty1),DAE.SUB(ty1),DAE.CREF(cr2,ty2));
then
oExp;
// equation from define
case(DAE.DEFINE(cr1, e2, _))
equation
ty1 = Exp.crefType(cr1);
oExp = DAE.BINARY(DAE.CREF(cr1,ty1),DAE.SUB(ty1),e2);
then
oExp;
// equation from initial define
case(DAE.INITIALDEFINE(cr1, e2, _))
equation
ty1 = Exp.crefType(cr1);
oExp = DAE.BINARY(DAE.CREF(cr1,ty1),DAE.SUB(ty1),e2);
then
oExp;
// equation from array TODO! check if this works!
case(DAE.ARRAY_EQUATION(_, e1, e2, _))
equation
ty = Exp.typeof(e1);
oExp = DAE.BINARY(e1,DAE.SUB(ty),e2);
then
oExp;
// failure
case(eq)
equation
true = RTOpts.debugFlag("failtrace");
Debug.fprintln("failtrace", "- DAEUtil.makeEquationToResidualExp failed to transform equation: " +&
dumpEquationStr(eq) +& " to residual form!");
then fail();
end matchcontinue;
end makeEquationToResidualExp;

Expand Down
64 changes: 42 additions & 22 deletions Compiler/Exp.mo
Expand Up @@ -10076,7 +10076,17 @@ algorithm outCr := matchcontinue(inCr,newSub)
Type t2,identType;
ComponentRef child;
list<Subscript> subs;
String name;
String name, str1, str2, str;

// debugging case, uncomment for enabling
// case(child,newSub)
// equation
// str1 = printComponentRefStr(child);
// str2 = Util.stringDelimitList(Util.listMap(newSub, printSubscriptStr), ", ");
// str = "Exp.replaceCrefSliceSub(" +& str1 +& " subs: [" +& str2 +& "]\n";
// print(str);
// then
// fail();

// Case where we try to find a Exp.DAE.SLICE()
case(DAE.CREF_IDENT(name,identType,subs),newSub)
Expand All @@ -10092,14 +10102,14 @@ algorithm outCr := matchcontinue(inCr,newSub)
child = subscriptCref(child,newSub);
then
child;

case( child as DAE.CREF_IDENT(identType = t2, subscriptLst = subs),newSub)
equation
false = (listLength(arrayTypeDimensions(t2)) >= (listLength(subs)+listLength(newSub)));
child = subscriptCref(child,newSub);
Debug.fprint("failtrace", "WARNING - Exp.replaceCref_SliceSub setting subscript last, not containing dimension\n ");
Debug.fprintln("failtrace", "WARNING - Exp.replaceCref_SliceSub setting subscript last, not containing dimension");
then
child;
child;

// Try DAE.CREF_QUAL with DAE.SLICE subscript
case(DAE.CREF_QUAL(name,identType,subs,child),newSub)
Expand All @@ -10124,39 +10134,49 @@ algorithm outCr := matchcontinue(inCr,newSub)
DAE.CREF_QUAL(name,identType,subs,child);

case(_,_)
equation
equation
Debug.fprint("failtrace", "- Exp.replaceCref_SliceSub failed\n ");
then
fail();
end matchcontinue;
end replaceCrefSliceSub;

protected function replaceSliceSub "
A function for replacing any occurance of DAE.SLICE with new sub.
"
A function for replacing any occurance of DAE.SLICE or DAE.WHOLEDIM with new sub."
input list<Subscript> inSubs;
input list<Subscript> inSub;
output list<Subscript> osubs;
algorithm osubs := matchcontinue(inSubs,inSub)
local
list<Subscript> subs;
Subscript sub;
case((sub as DAE.SLICE(_))::subs,inSub)
equation
subs = listAppend(inSub,subs);
then
subs;
case((sub)::subs,inSub)
equation
subs = replaceSliceSub(subs,inSub);
algorithm
osubs := matchcontinue(inSubs,inSub)
local
list<Subscript> subs;
Subscript sub;
case((sub as DAE.SLICE(_))::subs,inSub)
equation
subs = listAppend(inSub,subs);
then
subs;
// adrpo, 2010-02-23:
// WHOLEDIM is *also* a special case of SLICE
// that contains the all subscripts, so we need
// to handle that too here!
case((sub as DAE.WHOLEDIM())::subs,inSub)
equation
subs = listAppend(inSub,subs);
then
subs;
case((sub)::subs,inSub)
equation
subs = replaceSliceSub(subs,inSub);
then
(sub::subs);
end matchcontinue;
end matchcontinue;
end replaceSliceSub;

protected function dumpSimplifiedExp
input Exp inExp;
input Exp outExp;
"a function to dump simplified expressions"
input Exp inExp;
input Exp outExp;
algorithm
_ := matchcontinue(inExp,outExp)
case(inExp,outExp) equation
Expand Down

0 comments on commit ad3d230

Please sign in to comment.