Skip to content

Commit

Permalink
- bugfix mergStartValues, now use the fixed one, also if it has not a…
Browse files Browse the repository at this point in the history
…n explicit start value

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13633 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Oct 25, 2012
1 parent 5eab378 commit 05ece79
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 76 deletions.
219 changes: 143 additions & 76 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,67 @@ algorithm
end match;
end setVarStartValue;

public function setVarStartValueOption
"function: setVarStartValueOption
author: Frenkel TUD
Sets the start value attribute of a variable."
input BackendDAE.Var inVar;
input Option<DAE.Exp> inExp;
output BackendDAE.Var outVar;
algorithm
outVar := match (inVar,inExp)
local
DAE.ComponentRef a;
BackendDAE.VarKind b;
DAE.VarDirection c;
DAE.VarParallelism prl;
BackendDAE.Type d;
Option<DAE.Exp> e;
Option<Values.Value> f;
list<DAE.Subscript> g;
DAE.ElementSource source;
DAE.VariableAttributes attr;
Option<DAE.VariableAttributes> oattr1;
Option<SCode.Comment> s;
DAE.ConnectorType ct;

case (BackendDAE.VAR(varName = a,
varKind = b,
varDirection = c,
varParallelism = prl,
varType = d,
bindExp = e,
bindValue = f,
arryDim = g,
source = source,
values = SOME(attr),
comment = s,
connectorType = ct),_)
equation
oattr1 = DAEUtil.setStartAttrOption(SOME(attr),inExp);
then BackendDAE.VAR(a,b,c,prl,d,e,f,g,source,oattr1,s,ct);

case (BackendDAE.VAR(values = NONE()),NONE()) then inVar;

case (BackendDAE.VAR(varName = a,
varKind = b,
varDirection = c,
varParallelism = prl,
varType = d,
bindExp = e,
bindValue = f,
arryDim = g,
source = source,
values = NONE(),
comment = s,
connectorType = ct),_)
equation
attr = getVariableAttributefromType(d);
oattr1 = DAEUtil.setStartAttrOption(SOME(attr),inExp);
then BackendDAE.VAR(a,b,c,prl,d,e,f,g,source,oattr1,s,ct);
end match;
end setVarStartValueOption;

public function setVarStartOrigin
"function: setVarStartOrigin
author: Frenkel TUD
Expand Down Expand Up @@ -3545,69 +3606,42 @@ algorithm
DAE.Exp sa,sb,e;
Integer i,ia;
Option<DAE.Exp> origin;
case (v as BackendDAE.VAR(varName=cr),true,SOME(sa),_,va as BackendDAE.VAR(varName=cra),true,SOME(sb),_,_,_)
DAE.Type ty,tya;
Option<DAE.VariableAttributes> attr,attra;
// legal cases one fixed the other one not fixed, use the fixed one
case (v,true,_,_,_,false,_,_,_,_)
then v;
case (v,false,_,_,va,true,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
(e,_) = getNonZeroStart(sa,NONE(),e,NONE(),knVars);
v1 = setVarStartValue(v,e);
then v1;
case (v as BackendDAE.VAR(varName=cr),true,SOME(sa),_,va as BackendDAE.VAR(varName=cra),true,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
// according to MSL
// use the value from the variable that is closer to the top of the
// hierarchy i.e. A.B value has priority over X.Y.Z value!
i = ComponentReference.crefDepth(cr);
ia = ComponentReference.crefDepth(cra);
then
mergeStartFixed1(intLt(ia,i),v,cr,sa,cra,e,soa,negate," both fixed and have start values ");
case (v,true,SOME(sa),_,va,true,NONE(),_,_,_)
then v;
case (v,true,SOME(sa),_,va,false,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
(e,_) = getNonZeroStart(sa,NONE(),e,NONE(),knVars);
v1 = setVarStartValue(v,e);
then v1;
case (v,true,SOME(sa),_,va,false,SOME(sb),_,_,_)
then
v;
case (v,true,SOME(sa),_,va,false,NONE(),_,_,_)
then v;
case (v,true,NONE(),_,va,true,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
v1 = setVarStartValue(v,e);
then v1;
case (v,true,NONE(),_,va,true,NONE(),_,_,_)
then v;
case (v,true,NONE(),_,va,false,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
v1 = setVarStartValue(v,e);
then v1;
case (v,true,NONE(),_,va,false,NONE(),_,_,_)
then v;
case (v,false,SOME(sa),_,va,true,SOME(sb),_,_,_)
equation
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
(e,_) = getNonZeroStart(sa,NONE(),e,NONE(),knVars);
v1 = setVarStartValue(v,e);
v2 = setVarFixed(v1,true);
then v2;
case (v,false,NONE(),_,va,true,NONE(),_,_,_)
equation
v1 = setVarFixed(v,true);
then v1;
case (v,false,SOME(sa),_,va,true,NONE(),_,_,_)
equation
v1 = setVarStartValueOption(v,NONE());
v1 = setVarFixed(v,true);
then v1;
case (v,false,SOME(sa),_,va,false,SOME(sb),_,_,_)
// legal case both fixed=false
case (v,false,NONE(),_,va,false,NONE(),_,_,_)
then v;
case (v as BackendDAE.VAR(varName=cr,varType=ty,values = attr),false,_,_,va as BackendDAE.VAR(varName=cra,varType=tya,values = attra),false,_,_,_,_)
equation
sa = startValueType(sv,ty);
sb = startValueType(sva,tya);
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
(e,origin) = getNonZeroStart(sa,so,e,soa,knVars);
(e,origin) = getNonZeroStart(false,sa,so,e,soa,knVars);
v1 = setVarStartValue(v,e);
v1 = setVarStartOrigin(v,origin);
then v1;
case (v as BackendDAE.VAR(varName=cr),false,SOME(sa),_,va as BackendDAE.VAR(varName=cra),false,SOME(sb),_,_,_)
then v1;
case (v as BackendDAE.VAR(varName=cr,varType=ty),false,_,_,va as BackendDAE.VAR(varName=cra,varType=tya),false,_,_,_,_)
equation
sa = startValueType(sv,ty);
sb = startValueType(sva,tya);
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
// according to MSL
// use the value from the variable that is closer to the top of the
Expand All @@ -3616,29 +3650,69 @@ algorithm
ia = ComponentReference.crefDepth(cra);
then
mergeStartFixed1(intLt(ia,i),v,cr,sa,cra,e,soa,negate," have start values ");
case (v,false,SOME(sa),_,va,false,NONE(),_,_,_)
then v;
case (v,false,NONE(),_,va,true,SOME(sb),_,_,_)
// legal case both fixed = true and start exp equal
case (v,true,NONE(),_,va,true,NONE(),_,_,_)
then v;
case (v as BackendDAE.VAR(varName=cr,varType=ty,values = attr),true,_,_,va as BackendDAE.VAR(varName=cra,varType=tya,values = attra),true,_,_,_,_)
equation
sa = startValueType(sv,ty);
sb = startValueType(sva,tya);
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
(e,origin) = getNonZeroStart(true,sa,so,e,soa,knVars);
v1 = setVarStartValue(v,e);
v2 = setVarFixed(v1,true);
then v2;
case (v,false,NONE(),_,va,true,NONE(),_,_,_)
equation
v1 = setVarFixed(v,true);
then v1;
case (v,false,NONE(),_,va,false,SOME(sb),_,_,_)
v1 = setVarStartOrigin(v,origin);
then v1;
// not legal case both fixed with unequal start values
case (v as BackendDAE.VAR(varName=cr,varType=ty,values = attr),true,_,_,va as BackendDAE.VAR(varName=cra,varType=tya,values = attra),true,_,_,_,_)
equation
sa = startValueType(sv,ty);
sb = startValueType(sva,tya);
e = Debug.bcallret1(negate,Expression.negate,sb,sb);
v1 = setVarStartValue(v,e);
v1 = setVarStartOrigin(v,soa);
then v1;
case (v,false,NONE(),_,va,false,NONE(),_,_,_)
then v;
// overconstrained system report warning/error
i = ComponentReference.crefDepth(cr);
ia = ComponentReference.crefDepth(cra);
then
mergeStartFixed1(intLt(ia,i),v,cr,sa,cra,e,soa,negate," both fixed and have start values ");
end matchcontinue;
end mergeStartFixed;

protected function startValueType
"autor: Frenkel TUD 2012-10
return the start value or the default value in case of NONE()"
input Option<DAE.Exp> iExp;
input DAE.Type iTy;
output DAE.Exp oExp;
algorithm
oExp := matchcontinue(iExp,iTy)
local
DAE.Exp e;
case(SOME(e),_) then e;
case(NONE(),_)
equation
true = Types.isRealOrSubTypeReal(iTy);
then
DAE.RCONST(0.0);
case(NONE(),_)
equation
true = Types.isIntegerOrSubTypeInteger(iTy);
then
DAE.ICONST(0);
case(NONE(),_)
equation
true = Types.isBooleanOrSubTypeBoolean(iTy);
then
DAE.BCONST(false);
case(NONE(),_)
equation
true = Types.isStringOrSubTypeString(iTy);
then
DAE.SCONST("");
else
then
DAE.RCONST(0.0);
end matchcontinue;
end startValueType;

protected function mergeStartFixed1
"autor: Frenkel TUD 2011-04"
input Boolean b "true if Alias Var have less dots in the name";
Expand Down Expand Up @@ -3717,6 +3791,7 @@ end replaceCrefWithBindExp;

protected function getNonZeroStart
"autor: Frenkel TUD 2011-04"
input Boolean mustBeEqual;
input DAE.Exp exp1;
input Option<DAE.Exp> so "StartOrigin";
input DAE.Exp exp2;
Expand All @@ -3726,29 +3801,21 @@ protected function getNonZeroStart
output Option<DAE.Exp> outStartOrigin;
algorithm
(outExp,outStartOrigin) :=
matchcontinue (exp1,so,exp2,sao,knVars)
matchcontinue (mustBeEqual,exp1,so,exp2,sao,knVars)
local
DAE.Exp exp2_1,exp1_1;
Integer i,ia;
Boolean b1,b2;
Option<DAE.Exp> origin;
/* case (_,_,_,_,_)
equation
true = Expression.isZero(exp2);
then (exp1,so);
case (_,_,_,_,_)
equation
true = Expression.isZero(exp1);
then (exp2,sao);
*/ case (_,_,_,_,_)
case (_,_,_,_,_,_)
equation
true = Expression.expEqual(exp1,exp2);
// use highest origin
i = startOriginToValue(so);
ia = startOriginToValue(sao);
origin = Util.if_(intGt(ia,i),sao,so);
then (exp1,origin);
case (_,_,_,_,_)
case (false,_,_,_,_,_)
equation
// if one is bound and the other not use the bound one
i = startOriginToValue(so);
Expand All @@ -3757,7 +3824,7 @@ algorithm
((exp1_1,origin)) = Util.if_(intGt(ia,i),(exp2,sao),(exp1,so));
then
(exp1_1,origin);
case (_,_,_,_,_)
case (_,_,_,_,_,_)
equation
// simple evaluation, by replace crefs with bind expressions recursivly
((exp1_1, (_,b1,_))) = Expression.traverseExp(exp1, replaceCrefWithBindExp, (knVars,false,HashSet.emptyHashSet()));
Expand Down
33 changes: 33 additions & 0 deletions Compiler/FrontEnd/DAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,39 @@ algorithm
end match;
end setStartAttr;

public function setStartAttrOption "
sets the start attribute. If NONE(), assumes Real attributes."
input Option<DAE.VariableAttributes> attr;
input Option<DAE.Exp> start;
output Option<DAE.VariableAttributes> outAttr;
algorithm
outAttr:=
match (attr,start)
local
Option<DAE.Exp> q,u,du,f,n,so;
tuple<Option<DAE.Exp>, Option<DAE.Exp>> minMax;
Option<DAE.StateSelect> ss;
Option<DAE.Uncertainty> unc;
Option<DAE.Distribution> distOpt;
Option<DAE.Exp> eb;
Option<Boolean> ip,fn;

case (SOME(DAE.VAR_ATTR_REAL(q,u,du,minMax,_,f,n,ss,unc,distOpt,eb,ip,fn,so)),_)
then SOME(DAE.VAR_ATTR_REAL(q,u,du,minMax,start,f,n,ss,unc,distOpt,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_INT(q,minMax,_,f,unc,distOpt,eb,ip,fn,so)),_)
then SOME(DAE.VAR_ATTR_INT(q,minMax,start,f,unc,distOpt,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_BOOL(q,_,f,eb,ip,fn,so)),_)
then SOME(DAE.VAR_ATTR_BOOL(q,start,f,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_STRING(q,_,eb,ip,fn,so)),_)
then SOME(DAE.VAR_ATTR_STRING(q,start,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_ENUMERATION(q,minMax,u,du,eb,ip,fn,so)),_)
then SOME(DAE.VAR_ATTR_ENUMERATION(q,minMax,start,du,eb,ip,fn,so));
case (NONE(),NONE()) then NONE();
case (NONE(),_)
then SOME(DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),(NONE(),NONE()),start,NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE()));
end match;
end setStartAttrOption;

public function setStartOrigin "
sets the startOrigin attribute. If NONE(), assumes Real attributes."
input Option<DAE.VariableAttributes> attr;
Expand Down
19 changes: 19 additions & 0 deletions Compiler/FrontEnd/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,25 @@ algorithm
end matchcontinue;
end isBooleanOrSubTypeBoolean;

public function isStringOrSubTypeString
"@author: adrpo
This function verifies if it is some kind of a String type we are working with."
input Type inType;
output Boolean b;
algorithm
b := matchcontinue(inType)
local Type ty; Boolean lb1,lb2,lb3;
case(ty)
equation
lb1 = isString(ty);
lb2 = subtype(ty, DAE.T_STRING_DEFAULT);
lb3 = subtype(DAE.T_STRING_DEFAULT, ty);
lb1 = boolOr(lb1,boolAnd(lb2,lb3));
then lb1;
case(_) then false;
end matchcontinue;
end isStringOrSubTypeString;

public function isIntegerOrRealOrSubTypeOfEither
"Checks if a type is either some Integer or Real type."
input Type t;
Expand Down

0 comments on commit 05ece79

Please sign in to comment.