Skip to content

Commit

Permalink
- add new functions to BackendVariable.mo
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15565 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Mar 14, 2013
1 parent f0929b5 commit 324be10
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 37 deletions.
194 changes: 167 additions & 27 deletions Compiler/BackEnd/BackendVariable.mo
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ protected import Types;

/* =======================================================
*
* Section for functions that deals with Var
* Section for type definitions
*
* =======================================================
*/

protected constant Integer HASHVECFACTOR = 1.4;

/* =======================================================
*
* Section for functions that deals with Var
*
* =======================================================
*/
Expand Down Expand Up @@ -2074,18 +2083,18 @@ algorithm
tp = BackendDAEUtil.makeExpType(vartype);
cond = getMinMaxAsserts1(ominmax,e,tp);
(cond,_) = ExpressionSimplify.simplify(cond);
// do not add if const true
false = Expression.isConstTrue(cond);
str = str +& ExpressionDump.printExpStr(cond) +& " has value: ";
// if is real use %g otherwise use %d (ints and enums)
format = Util.if_(Types.isRealOrSubTypeReal(tp), "g", "d");
msg = DAE.BINARY(
DAE.SCONST(str),
DAE.ADD(DAE.T_STRING_DEFAULT),
DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString)
DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString)
);
// do not add if const true
false = Expression.isConstTrue(cond);
BackendDAEUtil.checkAssertCondition(cond,msg,DAE.ASSERTIONLEVEL_WARNING,DAEUtil.getElementSourceFileInfo(source));
then
then
DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond,msg,DAE.ASSERTIONLEVEL_WARNING,source)})::iMinmax;
else then iMinmax;
end matchcontinue;
Expand Down Expand Up @@ -2465,7 +2474,7 @@ protected
Integer bucketSize, arrSize;
algorithm
arrSize := intMax(BaseHashTable.lowBucketSize,size);
bucketSize := realInt(realMul(intReal(arrSize), 1.4));
bucketSize := realInt(realMul(intReal(arrSize), HASHVECFACTOR));
arr := arrayCreate(bucketSize, {});
emptyarr := arrayCreate(arrSize, NONE());
outVariables := BackendDAE.VARIABLES(arr,BackendDAE.VARIABLE_ARRAY(0, arrSize, emptyarr), bucketSize, 0);
Expand Down Expand Up @@ -2494,25 +2503,12 @@ public function listVar
Takes Var list and creates a BackendDAE.Variables structure, see also var_list."
input list<BackendDAE.Var> inVarLst;
output BackendDAE.Variables outVariables;
protected
Integer size;
algorithm
outVariables := match (inVarLst)
local
BackendDAE.Variables res,vars;
BackendDAE.Var v;
list<BackendDAE.Var> vs;

case ({})
equation
res = emptyVars();
then
res;

case ((v :: vs))
equation
vars = listVar(vs);
then
addVar(v, vars);
end match;
size := listLength(inVarLst);
outVariables := emptyVarsSized(size);
outVariables := List.fold(listReverse(inVarLst),addVar,outVariables);
end listVar;

public function listVarSized "function listVarSized
Expand Down Expand Up @@ -2595,7 +2591,93 @@ algorithm
end match;
end varsSize;

public function resizeVars "function: resizeVars
author: Frenkel TUD

check the number of vars and the bucketSize and expand the bucketSize if neccessary.
(Shure the hashentries also updated)
"
input BackendDAE.Variables inVariables;
output BackendDAE.Variables outVariables;
protected
Integer numberOfVars,bucketSize,size;
BackendDAE.VariableArray varArr;
algorithm
BackendDAE.VARIABLES(numberOfVars = numberOfVars,bucketSize = bucketSize,varArr=varArr) := inVariables;
size := realInt(realMul(intReal(numberOfVars), HASHVECFACTOR));
outVariables := Debug.bcallret2(intGt(numberOfVars,bucketSize),resizeVars1,varArr,numberOfVars,inVariables);
end resizeVars;

protected function resizeVars1 "function: resizeVars
author: Frenkel TUD

check the number of vars and the bucketSize and expand the bucketSize if neccessary.
(Shure the hashentries also updated)
"
input BackendDAE.VariableArray inVariables;
input Integer numberOfVars;
output BackendDAE.Variables outVariables;
protected
Integer arrSize,bucketSize;
array<list<BackendDAE.CrefIndex>> arr;
array<Option<BackendDAE.Var>> varOptArr;
algorithm
BackendDAE.VARIABLE_ARRAY(varOptArr=varOptArr) := inVariables;
arrSize:=intMax(BaseHashTable.lowBucketSize, numberOfVars);
bucketSize:=realInt(realMul(intReal(arrSize), HASHVECFACTOR));
arr:=arrayCreate(bucketSize, {});
arr := resizeVars2(numberOfVars,varOptArr,bucketSize,arr);
outVariables := BackendDAE.VARIABLES(arr,inVariables, bucketSize, numberOfVars);
end resizeVars1;

protected function resizeVars2
"function: resizeVars2
author: Frenkel TUD"
input Integer index;
input array<Option<BackendDAE.Var>> varOptArr;
input Integer bucketSize;
input array<list<BackendDAE.CrefIndex>> iArr;
output array<list<BackendDAE.CrefIndex>> oArr;
algorithm
oArr := match (index,varOptArr,bucketSize,iArr)
local
array<list<BackendDAE.CrefIndex>> arr;
case (0,_,_,_) then iArr;
case (_,_,_,_)
equation
arr = resizeVars3(varOptArr[index],index,bucketSize,iArr);
then
resizeVars2(index-1,varOptArr,bucketSize,arr);
end match;
end resizeVars2;

protected function resizeVars3
"function: resizeVars3
author: Frenkel TUD"
input Option<BackendDAE.Var> inVar;
input Integer pos;
input Integer bucketSize;
input array<list<BackendDAE.CrefIndex>> iArr;
output array<list<BackendDAE.CrefIndex>> oArr;
algorithm
oArr := match (inVar,pos,bucketSize,iArr)
local
Integer indx,indx_1,pos_1;
list<BackendDAE.CrefIndex> indexes;
array<list<BackendDAE.CrefIndex>> hashvec;
DAE.ComponentRef cr;
case (NONE(),_,_,_) then iArr;
case (SOME(BackendDAE.VAR(varName = cr)),_,_,_)
equation
indx = ComponentReference.hashComponentRefMod(cr, bucketSize);
indx_1 = indx + 1;
indexes = iArr[indx_1];
pos_1 = pos - 1;
hashvec = arrayUpdate(iArr, indx_1, (BackendDAE.CREFINDEX(cr,pos_1) :: indexes));
then
hashvec;
end match;
end resizeVars3;

public function isVariable
"function: isVariable
Expand Down Expand Up @@ -3094,6 +3176,35 @@ algorithm
end match;
end addKnVarDAE;

public function addNewKnVarDAE
"function: addNewKnVarDAE
author: Frenkel TUD 2011-04
Add a variable to Variables of a BackendDAE.
No Check if variable already exist. Use only for new variables"
input BackendDAE.Var inVar;
input BackendDAE.Shared shared;
output BackendDAE.Shared oshared;
algorithm
oshared := match (inVar,shared)
local
BackendDAE.Variables knvars,exobj,knvars1,aliasVars;
BackendDAE.EquationArray remeqns,inieqns;
array<DAE.Constraint> constrs;
array<DAE.ClassAttributes> clsAttrs;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.BackendDAEType btp;
case (_,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs))
equation
knvars1 = addNewVar(inVar,knvars);
then BackendDAE.SHARED(knvars1,exobj,aliasVars,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs);
end match;
end addNewKnVarDAE;

public function addAliasVarDAE
"function: addAliasVarDAE
author: Frenkel TUD 2012-09
Expand Down Expand Up @@ -3123,6 +3234,35 @@ algorithm
end match;
end addAliasVarDAE;

public function addNewAliasVarDAE
"function: addNewAliasVarDAE
author: Frenkel TUD 2012-09
Add a alias variable to Variables of a BackendDAE.Shared
No Check if variable already exist. Use only for new variables"
input BackendDAE.Var inVar;
input BackendDAE.Shared shared;
output BackendDAE.Shared oshared;
algorithm
oshared := match (inVar,shared)
local
BackendDAE.Variables knvars,exobj,aliasVars;
BackendDAE.EquationArray remeqns,inieqns;
array<DAE.Constraint> constrs;
array<DAE.ClassAttributes> clsAttrs;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.BackendDAEType btp;
case (_,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs))
equation
aliasVars = addNewVar(inVar,aliasVars);
then BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,clsAttrs,cache,env,funcs,einfo,eoc,btp,symjacs);
end match;
end addNewAliasVarDAE;

public function addVars "function: addVars
author: PA
Adds a list of BackendDAE.Var to BackendDAE.Variables"
Expand Down Expand Up @@ -3439,14 +3579,14 @@ algorithm
crlst = ComponentReference.expandCref(cr1,true);
(vLst as _::_,indxs) = getVarLst(crlst,inVariables,{},{});
then
(vLst,indxs);
(vLst,indxs);
/* failure
case (cr,vars)
case (_,_)
equation
Debug.fprintln(Flags.DAE_LOW, "- getVar failed on component reference: " +& ComponentReference.printComponentRefStr(cr));
then
fail();
*/
*/
end matchcontinue;
end getVar;

Expand Down
82 changes: 72 additions & 10 deletions Compiler/Util/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1163,29 +1163,54 @@ public function uniqueIntNArr
algorithm
oAcc := matchcontinue(inList,markarr,iAcc)
local
Integer i,len,mark;
list<Integer> ilst,acc;
Integer len,mark;
list<Integer> acc;
case ({},_,_)
equation
len = arrayLength(markarr);
_=arrayUpdate(markarr,len,markarr[len]+1);
then iAcc;
case (i::ilst,_,_)
case (_,_,_)
equation
len = arrayLength(markarr);
mark = markarr[len];
_=arrayUpdate(markarr,len,mark+1);
acc = uniqueIntNArr1(inList,len,mark+1,markarr,iAcc);
then
acc;
else
equation
print("List.uniqueIntNArr failed entry to large\n");
then
fail();
end matchcontinue;
end uniqueIntNArr;

protected function uniqueIntNArr1
"helpfer for uniqueIntNArr1"
input list<Integer> inList;
input Integer len;
input Integer mark;
input array<Integer> markarr;
input list<Integer> iAcc;
output list<Integer> oAcc;
algorithm
oAcc := matchcontinue(inList,len,mark,markarr,iAcc)
local
Integer i;
list<Integer> ilst,acc;
case ({},_,_,_,_) then iAcc;
case (i::ilst,_,_,_,_)
equation
len = arrayLength(markarr);
true = intLt(i,len);
mark = markarr[len];
acc = consOnTrue(intNe(markarr[i],mark),i,iAcc);
_=arrayUpdate(markarr,i,mark);
then
uniqueIntNArr(ilst,markarr,acc);
uniqueIntNArr1(ilst,len,mark,markarr,acc);
else
equation
print("List.uniqueIntNArr failed entrie to large\n");
then
fail();
end matchcontinue;
end uniqueIntNArr;
end uniqueIntNArr1;

public function uniqueOnTrue
"Takes a list of elements and a comparison function over two elements of the
Expand Down Expand Up @@ -4719,6 +4744,43 @@ algorithm
end match;
end fold2;

public function foldList2
"Takes a list and a function operating on list elements having an extra
argument that is 'updated', thus returned from the function, and two constant
arguments that is not updated. fold will call the function for each element in
a sequence, updating the start value."
input list<list<ElementType>> inList;
input FoldFunc inFoldFunc;
input ArgType1 inExtraArg1;
input ArgType2 inExtraArg2;
input FoldType inStartValue;
output FoldType outResult;

partial function FoldFunc
input ElementType inElement;
input ArgType1 inConstantArg1;
input ArgType2 inConstantArg2;
input FoldType inFoldArg;
output FoldType outFoldArg;
end FoldFunc;
algorithm
outResult := match(inList, inFoldFunc, inExtraArg1, inExtraArg2, inStartValue)
local
list<ElementType> e;
list<list<ElementType>> rest;
FoldType arg;

case ({}, _, _, _, _) then inStartValue;

case (e :: rest, _, _, _, _)
equation
arg = fold2(e, inFoldFunc, inExtraArg1, inExtraArg2, inStartValue);
then
foldList2(rest, inFoldFunc, inExtraArg1, inExtraArg2, arg);

end match;
end foldList2;

public function fold2r
"Same as fold2, but with reversed order on the fold function arguments."
input list<ElementType> inList;
Expand Down

0 comments on commit 324be10

Please sign in to comment.