Skip to content

Commit

Permalink
- bugfixes index reduction and matching
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12322 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jul 12, 2012
1 parent 1be7d96 commit 8e7c9cf
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 86 deletions.
21 changes: 9 additions & 12 deletions Compiler/BackEnd/BackendDAETransform.mo
Expand Up @@ -2508,41 +2508,38 @@ public function makeDummyState "function: makeDummyState
input Integer stateindx;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input array<Integer> mapIncRowEqn;
output DAE.ComponentRef outDerDummyState;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output list<Integer> ochangedeqns;
algorithm
(outDerDummyState,osyst,oshared) :=
matchcontinue (dummystate,stateindx,isyst,ishared)
(outDerDummyState,osyst,oshared,ochangedeqns) :=
matchcontinue (dummystate,stateindx,isyst,ishared,mapIncRowEqn)
local
list<DAE.ComponentRef> dummystates;
list<tuple<DAE.ComponentRef,Integer>> states;
list<Integer> changedeqns;
DAE.ComponentRef dummy_der;
DAE.Exp stateexp,stateexpcall,dummyderexp;
DAE.Type tp;
BackendDAE.BackendDAE dae,dae1,dae2;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;

case (dummystate,stateindx,syst as BackendDAE.EQSYSTEM(mT=SOME(mt)),shared)
case (dummystate,stateindx,syst as BackendDAE.EQSYSTEM(mT=SOME(mt)),shared,_)
equation
(dummy_der,syst) = newDummyVar(dummystate, syst, DAE.NEW_DUMMY_DER(dummystate,{}));
Debug.fcall(Flags.BLT_DUMP, BackendDump.debugStrCrefStr, ("Chosen dummy: ",dummy_der," as dummy state\n"));
changedeqns = BackendDAEUtil.eqnsForVarWithStates(mt, stateindx);
stateexp = Expression.crefExp(dummystate);
stateexpcall = DAE.CALL(Absyn.IDENT("der"),{stateexp},DAE.callAttrBuiltinReal);
dummyderexp = Expression.crefExp(dummy_der);
changedeqns = List.unique(List.map1r(changedeqns,arrayGet,mapIncRowEqn));
(syst,shared) = replaceDummyDer(stateexpcall, dummyderexp, syst, shared, changedeqns)
"We need to change variables in the differentiated equations and in the equations having the dummy derivative" ;
syst = makeAlgebraic(syst, dummystate);
Debug.fcall(Flags.BLT_DUMP, print ,"Update Incidence Matrix: ");
Debug.fcall(Flags.BLT_DUMP, BackendDump.debuglst, (changedeqns,intString," ","\n"));
syst = BackendDAEUtil.updateIncidenceMatrix(syst, shared, changedeqns);
then
(dummy_der,syst,shared);
(dummy_der,syst,shared,changedeqns);
else
equation
print("BackendDAETransform.makeDummyState failed\n");
Expand Down Expand Up @@ -2747,7 +2744,7 @@ algorithm
(syst,shared) = replaceDummyDer(stateexpcall, dummyderexp, syst, shared, changedeqns)
"We need to change variables in the differentiated equations and in the equations having the dummy derivative" ;
syst = makeAlgebraic(syst, state);
(syst,mapEqnIncRow,mapIncRowEqn) = BackendDAEUtil.updateIncidenceMatrixScalar(syst, shared, changedeqns,mapEqnIncRow,mapIncRowEqn);
(syst,mapEqnIncRow,mapIncRowEqn) = BackendDAEUtil.updateIncidenceMatrixScalar(syst, shared,BackendDAE.SOLVABLE(), changedeqns,mapEqnIncRow,mapIncRowEqn);
// print("new DAE:");
// BackendDump.dumpEqSystem(syst);
// BackendDump.dump(BackendDAE.DAE({syst},shared));
Expand Down Expand Up @@ -3604,7 +3601,7 @@ algorithm
dummyvar = BackendDAE.VAR(dummyvar_cr,BackendDAE.DUMMY_DER(),dir,prl,tp,NONE(),NONE(),dim,0,source,NONE(),comment,flowPrefix,streamPrefix);
/* Dummy variables are algebraic variables, hence fixed = false */
dummyvar = BackendVariable.setVarFixed(dummyvar,false);
vars_1 = BackendVariable.addVar(dummyvar, vars);
vars_1 = BackendVariable.addNewVar(dummyvar, vars);
then
(dummyvar_cr,BackendDAE.EQSYSTEM(vars_1,eqns,om,omT,matching));

Expand Down
42 changes: 24 additions & 18 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2362,10 +2362,10 @@ public function systemSize
author: Frenkel TUD
Returns the size of the dae system, the size of the equations in an EquationArray,
which not corresponds to the number of equations in a system."
input BackendDAE.EqSystem dae;
input BackendDAE.EqSystem syst;
output Integer n;
algorithm
n := match(dae)
n := match(syst)
local
EquationArray eqns;
case BackendDAE.EQSYSTEM(orderedEqs = eqns)
Expand Down Expand Up @@ -5054,14 +5054,15 @@ public function updateIncidenceMatrixScalar
outputs: (IncidenceMatrix, IncidenceMatrixT)"
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input BackendDAE.IndexType inIndxType;
input list<Integer> inIntegerLst;
input array<list<Integer>> iMapEqnIncRow;
input array<Integer> iMapIncRowEqn;
output BackendDAE.EqSystem osyst;
output array<list<Integer>> oMapEqnIncRow;
output array<Integer> oMapIncRowEqn;
algorithm
(osyst,oMapEqnIncRow,oMapIncRowEqn) := matchcontinue (syst,shared,inIntegerLst,iMapEqnIncRow,iMapIncRowEqn)
(osyst,oMapEqnIncRow,oMapIncRowEqn) := matchcontinue (syst,shared,inIndxType,inIntegerLst,iMapEqnIncRow,iMapIncRowEqn)
local
BackendDAE.IncidenceMatrix m,m_1,m_2;
BackendDAE.IncidenceMatrixT mt,mt_1,mt_2,mt_3;
Expand All @@ -5076,7 +5077,7 @@ algorithm
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn;

case (BackendDAE.EQSYSTEM(vars,daeeqns,SOME(m),SOME(mt),matching),BackendDAE.SHARED(eventInfo = BackendDAE.EVENT_INFO(whenClauseLst = wc)),eqns,_,_)
case (BackendDAE.EQSYSTEM(vars,daeeqns,SOME(m),SOME(mt),matching),BackendDAE.SHARED(eventInfo = BackendDAE.EVENT_INFO(whenClauseLst = wc)),_,eqns,_,_)
equation
// extend the mapping arrays
oldsize = arrayLength(iMapEqnIncRow);
Expand All @@ -5090,10 +5091,10 @@ algorithm
m = Util.arrayExpand(deltasize,m,{});
mt = Util.arrayExpand(deltasize,mt,{});
// fill the extended parts first
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar2(oldsize+1,newsize,oldsize1,vars,daeeqns,wc,m,mt,mapEqnIncRow,mapIncRowEqn);
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar2(oldsize+1,newsize,oldsize1,vars,daeeqns,wc,m,mt,mapEqnIncRow,mapIncRowEqn,inIndxType);
// update the old
eqns = List.removeOnTrue(oldsize, intLt, eqns);
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar1(vars,daeeqns,wc,m,mt,eqns,mapEqnIncRow,mapIncRowEqn);
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar1(vars,daeeqns,wc,m,mt,eqns,mapEqnIncRow,mapIncRowEqn,inIndxType);
then
(BackendDAE.EQSYSTEM(vars,daeeqns,SOME(m),SOME(mt),matching),mapEqnIncRow,mapIncRowEqn);

Expand All @@ -5115,14 +5116,15 @@ protected function updateIncidenceMatrixScalar1
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT;
input list<Integer> inIntegerLst;
input array<list<Integer>> iMapEqnIncRow;
input array<Integer> iMapIncRowEqn;
input array<Integer> iMapIncRowEqn;
input BackendDAE.IndexType inIndxType;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
output array<list<Integer>> oMapEqnIncRow;
output array<Integer> oMapIncRowEqn;
algorithm
(outIncidenceMatrix,outIncidenceMatrixT,oMapEqnIncRow,oMapIncRowEqn):=
matchcontinue (vars,daeeqns,wc,inIncidenceMatrix,inIncidenceMatrixT,inIntegerLst,iMapEqnIncRow,iMapIncRowEqn)
matchcontinue (vars,daeeqns,wc,inIncidenceMatrix,inIncidenceMatrixT,inIntegerLst,iMapEqnIncRow,iMapIncRowEqn,inIndxType)
local
BackendDAE.IncidenceMatrix m,m_1,m_2;
BackendDAE.IncidenceMatrixT mt,mt_1,mt_2,mt_3;
Expand All @@ -5133,23 +5135,23 @@ algorithm
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn;

case (_,_,_,m,mt,{},_,_) then (m,mt,iMapEqnIncRow,iMapIncRowEqn);
case (_,_,_,m,mt,{},_,_,_) then (m,mt,iMapEqnIncRow,iMapIncRowEqn);

case (vars,daeeqns,wc,m,mt,e::eqns,_,_)
case (vars,daeeqns,wc,m,mt,e::eqns,_,_,_)
equation
abse = intAbs(e);
e_1 = abse - 1;
eqn = equationNth(daeeqns, e_1);
size = BackendEquation.equationSize(eqn);
(row,_) = incidenceRow(eqn,vars,wc,BackendDAE.NORMAL(),{});
(row,_) = incidenceRow(eqn,vars,wc,inIndxType,{});
scalarindxs = iMapEqnIncRow[abse];
oldvars = getOldVars(m,listGet(scalarindxs,1));
(_,outvars,invars) = List.intersection1OnTrue(oldvars,row,intEq);
// do the same for each scalar indxs
m_1 = List.fold1r(scalarindxs,arrayUpdate,row,m);
mt_1 = List.fold1(scalarindxs,removeValuefromMatrix,outvars,mt);
mt_2 = List.fold1(scalarindxs,addValuetoMatrix,invars,mt_1);
(m_2,mt_3,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar1(vars,daeeqns,wc,m_1,mt_2,eqns,iMapEqnIncRow,iMapIncRowEqn);
(m_2,mt_3,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar1(vars,daeeqns,wc,m_1,mt_2,eqns,iMapEqnIncRow,iMapIncRowEqn,inIndxType);
then (m_2,mt_3,mapEqnIncRow,mapIncRowEqn);

end matchcontinue;
Expand All @@ -5166,14 +5168,15 @@ protected function updateIncidenceMatrixScalar2
input BackendDAE.IncidenceMatrix inIncidenceMatrix;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT;
input array<list<Integer>> iMapEqnIncRow;
input array<Integer> iMapIncRowEqn;
input array<Integer> iMapIncRowEqn;
input BackendDAE.IndexType inIndxType;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
output array<list<Integer>> oMapEqnIncRow;
output array<Integer> oMapIncRowEqn;
algorithm
(outIncidenceMatrix,outIncidenceMatrixT,oMapEqnIncRow,oMapIncRowEqn):=
matchcontinue (index,n,size,vars,daeeqns,wc,inIncidenceMatrix,inIncidenceMatrixT,iMapEqnIncRow,iMapIncRowEqn)
matchcontinue (index,n,size,vars,daeeqns,wc,inIncidenceMatrix,inIncidenceMatrixT,iMapEqnIncRow,iMapIncRowEqn,inIndxType)
local
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
Expand All @@ -5183,24 +5186,24 @@ algorithm
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn;

case (_,_,_,_,_,wc,m,mt,_,_)
case (_,_,_,_,_,wc,m,mt,_,_,_)
equation
false = intGt(index,n);
abse = intAbs(index);
e_1 = abse - 1;
eqn = equationNth(daeeqns, e_1);
rowsize = BackendEquation.equationSize(eqn);
(row,_) = incidenceRow(eqn,vars,wc,BackendDAE.NORMAL(),{});
(row,_) = incidenceRow(eqn,vars,wc,inIndxType,{});
new_size = size+rowsize;
scalarindxs = List.intRange2(size+1,new_size);
mapEqnIncRow = arrayUpdate(iMapEqnIncRow,abse,scalarindxs);
mapIncRowEqn = List.fold1r(scalarindxs,arrayUpdate,abse,iMapIncRowEqn);
m = List.fold1r(scalarindxs,arrayUpdate,row,m);
mt = fillincidenceMatrixT(row,scalarindxs,mt);
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar2(index+1,n,new_size,vars,daeeqns,wc,m,mt,mapEqnIncRow,mapIncRowEqn);
(m,mt,mapEqnIncRow,mapIncRowEqn) = updateIncidenceMatrixScalar2(index+1,n,new_size,vars,daeeqns,wc,m,mt,mapEqnIncRow,mapIncRowEqn,inIndxType);
then
(m,mt,mapEqnIncRow,mapIncRowEqn);
case (_,_,_,_,_,_,m,mt,_,_)
case (_,_,_,_,_,_,m,mt,_,_,_)
then
(m,mt,iMapEqnIncRow,iMapIncRowEqn);
end matchcontinue;
Expand Down Expand Up @@ -8537,10 +8540,13 @@ algorithm

case (_,_,_,(matchingAlgorithmfunc,mAmethodstr),(sssHandler,str1,_,_))
equation
print("SystemSize: " +& intString(systemSize(isyst)) +& "\n");
(syst,_,_,mapEqnIncRow,mapIncRowEqn) = getIncidenceMatrixScalar(isyst,ishared,BackendDAE.SOLVABLE());
match_opts = Util.getOptionOrDefault(inMatchingOptions,(BackendDAE.INDEX_REDUCTION(), BackendDAE.EXACT()));
arg = IndexReduction.getStructurallySingularSystemHandlerArg(syst,ishared,mapEqnIncRow,mapIncRowEqn);
profilerinit();
(syst,shared,arg) = matchingAlgorithmfunc(syst,ishared, match_opts, sssHandler, arg);
profilerresults();
Debug.execStat("transformDAE -> matchingAlgorithm " +& mAmethodstr +& " index Reduction Method " +& str1,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
then (syst,shared,arg);
else
Expand Down
116 changes: 107 additions & 9 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -3497,12 +3497,8 @@ algorithm
osyst := match (inVar,syst)
local
BackendDAE.Var var;
BackendDAE.Variables ordvars,knvars,exobj,ordvars1;
BackendDAE.AliasVariables aliasVars;
BackendDAE.EquationArray eqns,remeqns,inieqns;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.Shared shared;
BackendDAE.Variables ordvars,ordvars1;
BackendDAE.EquationArray eqns;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Matching matching;
case (var,BackendDAE.EQSYSTEM(ordvars,eqns,m,mT,matching))
Expand All @@ -3524,17 +3520,16 @@ algorithm
oshared := match (inVar,shared)
local
BackendDAE.Var var;
BackendDAE.Variables ordvars,knvars,exobj,knvars1;
BackendDAE.Variables knvars,exobj,knvars1;
BackendDAE.AliasVariables aliasVars;
BackendDAE.EquationArray eqns,remeqns,inieqns;
BackendDAE.EquationArray remeqns,inieqns;
array<DAE.Constraint> constrs;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;
case (var,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,cache,env,funcs,einfo,eoc,btp,symjacs))
equation
Expand Down Expand Up @@ -3611,6 +3606,109 @@ algorithm
end matchcontinue;
end addVar;

public function addNewVar
"function: addNewVar
author: Frenkel TUD - 2012-07
Add a variable to Variables.
Did not check if the variable is already there. Use it only for
new variables."
input BackendDAE.Var inVar;
input BackendDAE.Variables inVariables;
output BackendDAE.Variables outVariables;
algorithm
outVariables := matchcontinue (inVar,inVariables)
local
BackendDAE.Value hval,indx,newpos,n_1,hvalold,indxold,bsize,n,indx_1;
BackendDAE.VariableArray varr_1,varr;
list<BackendDAE.CrefIndex> indexes;
array<list<BackendDAE.CrefIndex>> hashvec_1,hashvec;
String name_str;
BackendDAE.Var v,newv;
DAE.ComponentRef cr;
DAE.Flow flowPrefix;
BackendDAE.Variables vars;
HashTableCrILst.HashTable fastht;
case ((v as BackendDAE.VAR(varName = cr,flowPrefix = flowPrefix)),(vars as BackendDAE.VARIABLES(crefIdxLstArr = hashvec,varArr = varr,bucketSize = bsize,numberOfVars = n, fastht = fastht)))
equation
indx = HashTable2.hashFunc(cr, bsize);
newpos = vararrayLength(varr);
varr_1 = vararrayAdd(varr, v);
indexes = hashvec[indx + 1];
hashvec_1 = arrayUpdate(hashvec, indx + 1, (BackendDAE.CREFINDEX(cr,newpos) :: indexes));
n_1 = vararrayLength(varr_1);
//fastht = BaseHashTable.add((cr,{newpos}),fastht);
then
BackendDAE.VARIABLES(hashvec_1,varr_1,bsize,n_1,fastht);

case (_,_)
equation
print("- BackendVariable.addNewVar failed\n");
then
fail();
end matchcontinue;
end addNewVar;

public function expandVarsDAE
"function: expandVars
author: Frenkel TUD 2011-04
Expand the Variable array."
input Integer needed;
input BackendDAE.EqSystem syst;
output BackendDAE.EqSystem osyst;
algorithm
osyst := match (needed,syst)
local
BackendDAE.Var var;
BackendDAE.Variables ordvars,ordvars1;
BackendDAE.EquationArray eqns;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Matching matching;
case (_,BackendDAE.EQSYSTEM(ordvars,eqns,m,mT,matching))
equation
ordvars1 = expandVars(needed,ordvars);
then BackendDAE.EQSYSTEM(ordvars1,eqns,m,mT,matching);
end match;
end expandVarsDAE;

public function expandVars
"function: expandVars
author: Frenkel TUD - 2012-07
Expand the variable array"
input Integer needed;
input BackendDAE.Variables inVariables;
output BackendDAE.Variables outVariables;
algorithm
outVariables := matchcontinue (needed,inVariables)
local
BackendDAE.Value size,noe,bsize,n,size1,expandsize;
BackendDAE.VariableArray varr_1,varr;
list<BackendDAE.CrefIndex> indexes;
array<list<BackendDAE.CrefIndex>> hashvec;
BackendDAE.Variables vars;
HashTableCrILst.HashTable fastht;
array<Option<BackendDAE.Var>> arr,arr_1;
case (_,(vars as BackendDAE.VARIABLES(crefIdxLstArr = hashvec,varArr = BackendDAE.VARIABLE_ARRAY(numberOfElements=noe,arrSize=size,varOptArr=arr),bucketSize = bsize,numberOfVars = n, fastht = fastht)))
equation
size1 = noe + needed;
true = intGt(size1,size);
expandsize = size1-size1;
arr_1 = Util.arrayExpand(expandsize, arr, NONE());
then
BackendDAE.VARIABLES(hashvec,BackendDAE.VARIABLE_ARRAY(noe,size1,arr_1),bsize,n,fastht);

case (_,(vars as BackendDAE.VARIABLES(crefIdxLstArr = hashvec,varArr = BackendDAE.VARIABLE_ARRAY(numberOfElements=noe,arrSize=size,varOptArr=arr),bucketSize = bsize,numberOfVars = n, fastht = fastht)))
then
inVariables;

case (_,_)
equation
print("- BackendVariable.expandVars failed\n");
then
fail();
end matchcontinue;
end expandVars;


public function getVarAt
"function: getVarAt
author: PA
Expand Down

0 comments on commit 8e7c9cf

Please sign in to comment.