Skip to content

Commit

Permalink
- Some fixes for indep.eq.syst
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9878 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 20, 2011
1 parent 26a92ba commit f465197
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 71 deletions.
50 changes: 30 additions & 20 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -886,19 +886,10 @@ public function translateDae "function: translateDae
input Option<String> dummy;
output BackendDAE.BackendDAE outBackendDAE;
algorithm
outBackendDAE := mapEqSystem1(inBackendDAE,translateDae1,dummy);
end translateDae;

protected function translateDae1
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input Option<String> dummy;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
algorithm
(osyst,oshared) := match (syst,shared,dummy)
outBackendDAE := match (inBackendDAE,dummy)
local
list<BackendDAE.Var> varlst,knvarlst,extvarlst;
list<list<BackendDAE.Var>> varlst;
list<BackendDAE.Var> knvarlst,extvarlst;
array<BackendDAE.MultiDimEquation> ae;
array<DAE.Algorithm> al;
list<BackendDAE.WhenClause> wc;
Expand All @@ -911,22 +902,41 @@ algorithm
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
case (BackendDAE.EQSYSTEM(vars,eqns,m,mT,matching),BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(whenClauseLst = wc,zeroCrossingLst = zc),extObjCls,btp),_)
BackendDAE.EqSystems systs;
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(whenClauseLst = wc,zeroCrossingLst = zc),extObjCls,btp)),_)
equation
varlst = varList(vars);
varlst = Util.listMapMap(systs,BackendVariable.daeVars,varList);
knvarlst = varList(knvars);
extvarlst = varList(extVars);
varlst = listReverse(varlst);
knvarlst = listReverse(knvarlst);
extvarlst = listReverse(extvarlst);
// varlst = Util.listMap(varlst,listReverse);
// knvarlst = listReverse(knvarlst);
// extvarlst = listReverse(extvarlst);
(varlst,knvarlst,extvarlst) = BackendVariable.calculateIndexes(varlst, knvarlst,extvarlst);
vars = BackendVariable.addVars(varlst, vars);
systs = Util.listThreadMap(systs,varlst,addVarsToEqSystem);
knvars = BackendVariable.addVars(knvarlst, knvars);
extVars = BackendVariable.addVars(extvarlst, extVars);
then
(BackendDAE.EQSYSTEM(vars,eqns,m,mT,matching),BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(wc,zc),extObjCls,btp));
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(wc,zc),extObjCls,btp));
end match;
end translateDae;

protected function addVarsToEqSystem
input BackendDAE.EqSystem syst;
input list<BackendDAE.Var> varlst;
output BackendDAE.EqSystem osyst;
algorithm
osyst := match (syst,varlst)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqs;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Matching matching;
case (BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching),varlst)
equation
vars = BackendVariable.addVars(varlst, vars);
then BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching);
end match;
end translateDae1;
end addVarsToEqSystem;

public function calculateSizes "function: calculateSizes
author: PA
Expand Down
74 changes: 55 additions & 19 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1883,26 +1883,28 @@ public function calculateIndexes "function: calculateIndexes
To seperate them after that they are stored in a list with
the information about the type(vars=0,knvars=1,extvars=2) and the place at the
original list."
input list<BackendDAE.Var> inVarLst1;
input list<list<BackendDAE.Var>> inVarLst1;
input list<BackendDAE.Var> inVarLst2;
input list<BackendDAE.Var> inVarLst3;

output list<BackendDAE.Var> outVarLst1;
output list<list<BackendDAE.Var>> outVarLst1;
output list<BackendDAE.Var> outVarLst2;
output list<BackendDAE.Var> outVarLst3;
algorithm
(outVarLst1,outVarLst2,outVarLst3) := matchcontinue (inVarLst1,inVarLst2,inVarLst3)
local
list<BackendDAE.Var> vars_2,knvars_2,extvars_2,extvars,vars,knvars;
list< tuple<BackendDAE.Var,Integer> > vars_1,knvars_1,extvars_1;
list<list<BackendDAE.Var>> varsLst,vars_2;
list<BackendDAE.Var> knvars_2,extvars_2,extvars,vars,knvars;
list< tuple<BackendDAE.Var,Integer> > knvars_1,extvars_1;
list<list< tuple<BackendDAE.Var,Integer> >> vars_1;
list< tuple<BackendDAE.Var,Integer,Integer> > vars_map,knvars_map,extvars_map,all_map,all_map1,noScalar_map,noScalar_map1,scalar_map,all_map2,mergedvar_map,sort_map;
BackendDAE.Value x,xd,y,p,dummy,ext,x_strType,xd_strType,y_strType,p_strType,dummy_strType;
case (vars,knvars,extvars)
case (varsLst,knvars,extvars)
equation
// store vars,knvars,extvars in the list
vars_map = fillListConst(vars,0,0);
knvars_map = fillListConst(knvars,1,0);
extvars_map = fillListConst(extvars,2,0);
vars_map = fillListListConst(varsLst,0,{});
knvars_map = fillListConst(knvars,-1,0,{});
extvars_map = fillListConst(extvars,-2,0,{});
// connect the lists
all_map = listAppend(vars_map,knvars_map);
all_map1 = listAppend(all_map,extvars_map);
Expand All @@ -1918,11 +1920,11 @@ algorithm
// calculate indexes
(all_map2,x,xd,y,p,dummy,ext,x_strType,xd_strType,y_strType,p_strType,dummy_strType) = calculateIndexes2(mergedvar_map,0,0,0,0,0,0,0,0,0,0,0,{});
// seperate vars,knvars,extvas
vars_1 = getListConst(all_map2,0);
knvars_1 = getListConst(all_map2,1);
extvars_1 = getListConst(all_map2,2);
vars_1 = Util.listMap1r(Util.listIntRange2(0,listLength(varsLst)-1),getListConst,all_map2);
knvars_1 = getListConst(all_map2,-1);
extvars_1 = getListConst(all_map2,-2);
// arrange lists in original order
vars_2 = sortList(vars_1,0);
vars_2 = Util.listMap1(vars_1,sortList,0);
knvars_2 = sortList(knvars_1,0);
extvars_2 = sortList(extvars_1,0);
then
Expand All @@ -1944,27 +1946,53 @@ author: Frenkel TUD
input list<Type_a> inTypeALst;
input Integer inType;
input Integer inPlace;
input list< tuple<Type_a,Integer,Integer> > acc;
output list< tuple<Type_a,Integer,Integer> > outlist;
replaceable type Type_a subtypeof Any;
algorithm
outlist := match (inTypeALst,inType,inPlace)
outlist := match (inTypeALst,inType,inPlace,acc)
local
list<Type_a> rest;
Type_a item;
Integer value,place;
list< tuple<Type_a,Integer,Integer> > out_lst,val_lst;
case ({},value,place) then {};
case (item::rest,value,place)
case ({},value,place,acc) then acc;
case (item::rest,value,place,acc)
equation
/* recursive */
val_lst = fillListConst(rest,value,place+1);
/* fill */
out_lst = listAppend({(item,value,place)},val_lst);
acc = fillListConst(rest,value,place+1,(item,value,place)::acc);
then
out_lst;
acc;
end match;
end fillListConst;

protected function fillListListConst
"function: fillListConst
author: Frenkel TUD
Helper function for calculateIndexes.
Get a list, a type value an a start place and store all elements
of the list in a list of tuples (element,type,place)"
input list<list<Type_a>> inTypeALst;
input Integer inType;
input list< tuple<Type_a,Integer,Integer> > acc;
output list< tuple<Type_a,Integer,Integer> > outlist;
replaceable type Type_a subtypeof Any;
algorithm
outlist := match (inTypeALst,inType,acc)
local
list<list<Type_a>> rest;
list<Type_a> item;
Integer value,place;
list< tuple<Type_a,Integer,Integer> > out_lst,val_lst;
case ({},value,acc) then acc;
case (item::rest,value,acc)
equation
/* recursive */
acc = fillListConst(item,value,0,acc);
then fillListListConst(rest,value+1,acc);
end match;
end fillListListConst;

protected function getListConst
"function: getListConst
author: Frenkel TUD
Expand Down Expand Up @@ -3887,4 +3915,12 @@ algorithm
end match;
end mergeVariableOperations;

public function greater
input BackendDAE.Var lhs;
input BackendDAE.Var rhs;
output Boolean greater;
algorithm
greater := stringCompare(ComponentReference.printComponentRefStr(varCref(lhs)),ComponentReference.printComponentRefStr(varCref(rhs))) > 0;
end greater;

end BackendVariable;
74 changes: 42 additions & 32 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -6487,7 +6487,7 @@ algorithm
externalObjects = extvars,
aliasVars = aliasVars as BackendDAE.ALIASVARS(aliasVars = removedvars))))
equation
/* Extract from variable list */
/* Extract from variable list */
((varsOut,_,_)) = Util.listFold1(Util.listMap(systs,BackendVariable.daeVars),BackendVariable.traverseBackendDAEVars,extractVarsFromList,(SIMVARS({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {},{},{},{},{},{},{},{},{},{},{}),aliasVars,knvars));
/* Extract from known variable list */
((varsOut,_,_)) = BackendVariable.traverseBackendDAEVars(knvars,extractVarsFromList,(varsOut,aliasVars,knvars));
Expand All @@ -6496,7 +6496,7 @@ algorithm
/* Extract from external object list */
((varsOut,_,_)) = BackendVariable.traverseBackendDAEVars(extvars,extractVarsFromList,(varsOut,aliasVars,knvars));
/* sort variables on index */
varsOut = sortSimvarsOnIndex(varsOut);
varsOut = sortSimvars(varsOut,varIndexComparer);
/* Index of algebraic and parameters need
to fix due to separation of int Vars*/
varsOut = fixIndex(varsOut);
Expand Down Expand Up @@ -6802,12 +6802,18 @@ algorithm
end matchcontinue;
end mergeVars;

protected function sortSimvarsOnIndex
protected function sortSimvars
input SimVars unsortedSimvars;
input Comparer comp;
output SimVars sortedSimvars;
partial function Comparer
input SimVar a;
input SimVar b;
output Boolean res;
end Comparer;
algorithm
sortedSimvars :=
match (unsortedSimvars)
match (unsortedSimvars,comp)
local
list<SimVar> stateVars;
list<SimVar> derivativeVars;
Expand All @@ -6833,36 +6839,36 @@ algorithm
list<SimVar> stringConstVars;
case (SIMVARS(stateVars, derivativeVars, algVars, intAlgVars, boolAlgVars, inputVars,
outputVars, aliasVars, intAliasVars, boolAliasVars, paramVars, intParamVars, boolParamVars,
stringAlgVars, stringParamVars, stringAliasVars, extObjVars,jacVars,constVars,intConstVars,boolConstVars,stringConstVars))
equation
stateVars = Util.sort(stateVars, varIndexComparer);
derivativeVars = Util.sort(derivativeVars, varIndexComparer);
algVars = Util.sort(algVars, varIndexComparer);
intAlgVars = Util.sort(intAlgVars, varIndexComparer);
boolAlgVars = Util.sort(boolAlgVars, varIndexComparer);
inputVars = Util.sort(inputVars, varIndexComparer);
outputVars = Util.sort(outputVars, varIndexComparer);
aliasVars = Util.sort(aliasVars, varIndexComparer);
intAliasVars = Util.sort(intAliasVars, varIndexComparer);
boolAliasVars = Util.sort(boolAliasVars, varIndexComparer);
paramVars = Util.sort(paramVars, varIndexComparer);
intParamVars = Util.sort(intParamVars, varIndexComparer);
boolParamVars = Util.sort(boolParamVars, varIndexComparer);
stringAlgVars = Util.sort(stringAlgVars, varIndexComparer);
stringParamVars = Util.sort(stringParamVars, varIndexComparer);
stringAliasVars = Util.sort(stringAliasVars, varIndexComparer);
extObjVars = Util.sort(extObjVars, varIndexComparer);
jacVars = Util.sort(jacVars, varIndexComparer);
constVars = Util.sort(constVars, varIndexComparer);
intConstVars = Util.sort(intConstVars, varIndexComparer);
boolConstVars = Util.sort(boolConstVars, varIndexComparer);
stringConstVars = Util.sort(stringConstVars, varIndexComparer);
stringAlgVars, stringParamVars, stringAliasVars, extObjVars,jacVars,constVars,intConstVars,boolConstVars,stringConstVars),comp)
equation
stateVars = Util.sort(stateVars, comp);
derivativeVars = Util.sort(derivativeVars, comp);
algVars = Util.sort(algVars, comp);
intAlgVars = Util.sort(intAlgVars, comp);
boolAlgVars = Util.sort(boolAlgVars, comp);
inputVars = Util.sort(inputVars, comp);
outputVars = Util.sort(outputVars, comp);
aliasVars = Util.sort(aliasVars, comp);
intAliasVars = Util.sort(intAliasVars, comp);
boolAliasVars = Util.sort(boolAliasVars, comp);
paramVars = Util.sort(paramVars, comp);
intParamVars = Util.sort(intParamVars, comp);
boolParamVars = Util.sort(boolParamVars, comp);
stringAlgVars = Util.sort(stringAlgVars, comp);
stringParamVars = Util.sort(stringParamVars, comp);
stringAliasVars = Util.sort(stringAliasVars, comp);
extObjVars = Util.sort(extObjVars, comp);
jacVars = Util.sort(jacVars, comp);
constVars = Util.sort(constVars, comp);
intConstVars = Util.sort(intConstVars, comp);
boolConstVars = Util.sort(boolConstVars, comp);
stringConstVars = Util.sort(stringConstVars, comp);

then SIMVARS(stateVars, derivativeVars, algVars, intAlgVars, boolAlgVars, inputVars,
outputVars, aliasVars, intAliasVars, boolAliasVars, paramVars, intParamVars, boolParamVars,
stringAlgVars, stringParamVars, stringAliasVars, extObjVars,jacVars,constVars,intConstVars,boolConstVars,stringConstVars);
end match;
end sortSimvarsOnIndex;
end sortSimvars;

protected function fixIndex
input SimVars unfixedSimvars;
Expand Down Expand Up @@ -6962,8 +6968,11 @@ algorithm
local
Integer lhsIndex;
Integer rhsIndex;
case (SIMVAR(index=lhsIndex), SIMVAR(index=rhsIndex))
then rhsIndex < lhsIndex;
DAE.ComponentRef cr1,cr2;
case (SIMVAR(name=cr1,index=lhsIndex), SIMVAR(name=cr2,index=rhsIndex))
equation
res = rhsIndex < lhsIndex;
then res;
end match;
end varIndexComparer;

Expand Down Expand Up @@ -8931,7 +8940,8 @@ algorithm
arrayCref = getArrayCref(dlowVar);
aliasvar = getAliasVar(dlowVar,optAliasVars);
caus = getCausality(dlowVar,vars);
numArrayElement=arraydim1(inst_dims);
numArrayElement=arraydim1(inst_dims);
// print("name: " +& ComponentReference.printComponentRefStr(cr) +& "indx: " +& intString(indx) +& "\n");
then
SIMVAR(cr, kind, commentStr, unit, displayUnit, indx, initVal, nomVal, isFixed, type_, isDiscrete, arrayCref, aliasvar, source, caus,NONE(),numArrayElement);
end match;
Expand Down

0 comments on commit f465197

Please sign in to comment.