Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
niklwors committed Jul 8, 2015
2 parents 21906db + 2839510 commit 6a82052
Show file tree
Hide file tree
Showing 42 changed files with 1,507 additions and 1,077 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -106,6 +106,7 @@ Compiler/Script/OpenModelicaScriptingAPIQt.h
Compiler/Template/AbsynDumpTpl.mo
Compiler/Template/CodegenAdevs.mo
Compiler/Template/CodegenC.mo
Compiler/Template/CodegenCFunctions.mo
Compiler/Template/CodegenC.mo.log
Compiler/Template/CodegenCSharp.mo
Compiler/Template/CodegenCpp.mo
Expand All @@ -116,6 +117,7 @@ Compiler/Template/CodegenFMU1.mo
Compiler/Template/CodegenFMU2.mo
Compiler/Template/CodegenFMUCommon.mo
Compiler/Template/CodegenFMUCpp.mo
Compiler/Template/CodegenFMUCppHpcom.mo
Compiler/Template/CodegenJS.mo
Compiler/Template/CodegenJava.mo
Compiler/Template/CodegenModelica.mo
Expand Down
64 changes: 64 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -8232,5 +8232,69 @@ algorithm
clocks := arrayCreate(0, DAE.INFERRED_CLOCK());
end emptyClocks;

public function componentsEqual"outputs true if 1 strongly connected components are equal"
input BackendDAE.StrongComponent comp1;
input BackendDAE.StrongComponent comp2;
output Boolean isEqual;
algorithm
isEqual := matchcontinue(comp1,comp2)
local
Integer i1,i2, j1,j2;
list<Integer> l1,l2,k1,k2;
list<tuple<Integer,list<Integer>>> l3,k3;
case(BackendDAE.SINGLEEQUATION(eqn=i1,var=i2),BackendDAE.SINGLEEQUATION(eqn=j1,var=j2))
equation
then intEq(i1,j1) and intEq(i2,j2);

case(BackendDAE.EQUATIONSYSTEM(eqns=l1,vars=l2),BackendDAE.EQUATIONSYSTEM(eqns=k1,vars=k2))
equation
then List.isEqualOnTrue(l1,k1,intEq) and List.isEqualOnTrue(l2,k2,intEq);

case(BackendDAE.SINGLEARRAY(eqn=i1,vars=l1),BackendDAE.SINGLEARRAY(eqn=j1,vars=k1))
equation
then intEq(i1,j1) and List.isEqualOnTrue(l1,k1,intEq);

case(BackendDAE.SINGLEALGORITHM(eqn=i1,vars=l1),BackendDAE.SINGLEALGORITHM(eqn=j1,vars=k1))
equation
then intEq(i1,j1) and List.isEqualOnTrue(l1,k1,intEq);

case(BackendDAE.SINGLECOMPLEXEQUATION(eqn=i1,vars=l1),BackendDAE.SINGLECOMPLEXEQUATION(eqn=j1,vars=k1))
equation
then intEq(i1,j1) and List.isEqualOnTrue(l1,k1,intEq);

case(BackendDAE.SINGLEWHENEQUATION(eqn=i1,vars=l1),BackendDAE.SINGLEWHENEQUATION(eqn=j1,vars=k1))
equation
then intEq(i1,j1) and List.isEqualOnTrue(l1,k1,intEq);

case(BackendDAE.SINGLEIFEQUATION(eqn=i1,vars=l1),BackendDAE.SINGLEIFEQUATION(eqn=j1,vars=k1))
equation
then intEq(i1,j1) and List.isEqualOnTrue(l1,k1,intEq);

case(BackendDAE.TORNSYSTEM(strictTearingSet=BackendDAE.TEARINGSET(tearingvars=l1,residualequations=l2,otherEqnVarTpl=l3)),BackendDAE.TORNSYSTEM(strictTearingSet=BackendDAE.TEARINGSET(tearingvars=k1,residualequations=k2,otherEqnVarTpl=k3)))
equation
then List.isEqualOnTrue(l1,k1,intEq) and List.isEqualOnTrue(l2,k2,intEq) and List.isEqualOnTrue(l3,k3,otherEqnVarTplEqual);
else
then false;
end matchcontinue;
end componentsEqual;

protected function otherEqnVarTplEqual"compares 2 tpls from otherEqnVarTpl in TearingSets"
input tuple<Integer,list<Integer>> tpl1;
input tuple<Integer,list<Integer>> tpl2;
output Boolean isEqual;
algorithm
isEqual := matchcontinue(tpl1,tpl2)
local
Integer i1,i2;
list<Integer> l1,l2;
case((i1,l1),(i2,l2))
equation
then intEq(i1,i2) and List.isEqualOnTrue(l1,l2,intEq);

else
then false;
end matchcontinue;
end otherEqnVarTplEqual;

annotation(__OpenModelica_Interface="backend");
end BackendDAEUtil;
19 changes: 9 additions & 10 deletions Compiler/BackEnd/Causalize.mo
Expand Up @@ -134,28 +134,27 @@ protected function singularSystemCheck1
input tuple<BackendDAEFunc.matchingAlgorithmFunc,String> matchingAlgorithm;
input BackendDAE.StructurallySingularSystemHandlerArg arg;
input BackendDAE.Shared iShared;
output BackendDAE.EqSystem outSyst;
output BackendDAE.EqSystem outSyst = iSyst;
protected
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mT;
BackendDAE.StateSets stateSets;
list<list<Integer>> comps;
array<Integer> ass1,ass2;
BackendDAEFunc.matchingAlgorithmFunc matchingFunc;
BackendDAE.BaseClockPartitionKind partitionKind;
BackendDAE.EqSystem syst;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,m=SOME(m),mT=SOME(mT),stateSets=stateSets,partitionKind=partitionKind) := iSyst;
BackendDAE.EQSYSTEM(m=SOME(m), mT=SOME(mT)) := iSyst;
(matchingFunc,_) := matchingAlgorithm;
// get absolute Incidence Matrix
m := BackendDAEUtil.absIncidenceMatrix(m);
mT := BackendDAEUtil.absIncidenceMatrix(mT);
// try to match
outSyst := BackendDAE.EQSYSTEM(vars,eqns,SOME(m),SOME(mT),BackendDAE.NO_MATCHING(),stateSets,partitionKind);
syst := BackendDAEUtil.setEqSystMatrices(iSyst, SOME(m), SOME(mT));
syst.matching := BackendDAE.NO_MATCHING();
// do matching
(outSyst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(ass1=ass1,ass2=ass2)),_,_) := matchingFunc(outSyst,iShared,true,(BackendDAE.INDEX_REDUCTION(),eqnConstr),foundSingularSystem,arg);
outSyst := BackendDAEUtil.setEqSystMatching(iSyst,BackendDAE.MATCHING(ass1,ass2,{}));
(syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(ass1=ass1, ass2=ass2)), _, _) :=
matchingFunc(syst, iShared, true, (BackendDAE.INDEX_REDUCTION(), eqnConstr), foundSingularSystem, arg);
outSyst.matching := BackendDAE.MATCHING(ass1, ass2, {});
/*
print("singularSystemCheck:\n");
BackendDump.printEqSystem(outSyst);
Expand All @@ -164,7 +163,7 @@ algorithm
DumpGraphML.dumpSystem(outSyst,iShared,NONE(),"SingularSystemCheck" + intString(nVars) + ".graphml",false);
*/
// free states matching information because there it is unkown if the state or the state derivative was matched
((_,ass1,ass2)) := BackendVariable.traverseBackendDAEVars(vars, freeStateAssignments, (1,ass1,ass2));
((_,ass1,ass2)) := BackendVariable.traverseBackendDAEVars(outSyst.orderedVars, freeStateAssignments, (1,ass1,ass2));
end singularSystemCheck1;

protected function freeStateAssignments "unset assignments of statevariables."
Expand Down
13 changes: 2 additions & 11 deletions Compiler/BackEnd/ExpressionSolve.mo
Expand Up @@ -98,23 +98,14 @@ protected
BackendDAE.StrongComponent c;
Integer i = 1;
list<BackendDAE.Var> newVars = {};

BackendDAE.Variables vars;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
BackendDAE.EquationArray eqns;

algorithm
for comp in inComps loop
(osyst,oshared, newVars) := findSimpleEquationWork(osyst,oshared,comp, i, newVars);
i := i + 1;
end for;

if not listEmpty(newVars) then
BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,matching=matching,stateSets=stateSets,partitionKind=partitionKind) := osyst;
vars := BackendVariable.addVars(newVars, vars);
osyst := BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),matching,stateSets,partitionKind);
osyst.orderedVars := BackendVariable.addVars(newVars, osyst.orderedVars);
osyst := BackendDAEUtil.setEqSystMatrices(osyst);
//BackendDump.printEqSystem(osyst);
end if;

Expand Down
56 changes: 27 additions & 29 deletions Compiler/BackEnd/HpcOmEqSystems.mo
Expand Up @@ -169,7 +169,8 @@ algorithm
list<Integer> tvarIdcs, resEqIdcs, eqIdcs, varIdcs;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl;
BackendDAE.BaseClockPartitionKind partitionKind;
BackendDAE.EqSystem systTmp;
BackendDAE.EqSystem syst;
EqSys hpcSyst;
BackendDAE.EquationArray eqs;
BackendDAE.Jacobian jac;
BackendDAE.JacobianType jacType;
Expand All @@ -182,14 +183,13 @@ algorithm
BackendVarTransform.VariableReplacements derRepl;
list<BackendDAE.Equation> eqLst, eqsNew, eqsOld, resEqs, addEqs;
list<BackendDAE.Var> varLst,varLstRepl, varsNew, varsOld, tvars, addVars;
EqSys syst;
case(_,_,_,_,_,_,_)
equation
// completed
true = listLength(compsIn) < compIdx;
then
(systIn,tornSysIdxIn);
case(_,_,_,_,_,_,_)
case(_,_,_,_,syst,_,_)
equation
// strongComponent is a linear tornSystem
true = listLength(compsIn) >= compIdx;
Expand All @@ -204,16 +204,14 @@ algorithm

// add the new vars and equations to the original EqSystem
BackendDAE.MATCHING(ass1=ass1New, ass2=ass2New, comps=compsNew) = matchingNew;
BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqs, stateSets = stateSets, partitionKind=partitionKind) = systIn;
varsOld = BackendVariable.varList(vars);
eqsOld = BackendEquation.equationList(eqs);
varsOld = BackendVariable.varList(syst.orderedVars);
eqsOld = BackendEquation.equationList(syst.orderedEqs);

varLst = listAppend(varsOld,varsNew);
eqLst = listAppend(eqsOld, eqsNew);
eqLst = List.fold2(List.intRange(listLength(resEqIdcs)),replaceAtPositionFromList,resEqs,resEqIdcs,eqLst); // replaces the old residualEquations with the new ones
vars = BackendVariable.listVar1(varLst); // !!! BackendVariable.listVar outputs the reversed order therefore listVar1
eqs = BackendEquation.listEquation(eqLst);

syst.orderedVars = BackendVariable.listVar1(varLst); // !!! BackendVariable.listVar outputs the reversed order therefore listVar1
syst.orderedEqs = BackendEquation.listEquation(eqLst);

// build the matching
ass1All = arrayCreate(listLength(varLst),-1);
Expand All @@ -231,15 +229,15 @@ algorithm
compsNew = listAppend(compsNew, otherComps);
compsTmp = List.replaceAtWithList(compsNew,compIdx-1,compsIn);
((ass1All,ass2All)) = List.fold2(List.intRange(arrayLength(ass1New)),updateMatching,(listLength(eqsOld),listLength(varsOld)),(ass1New,ass2New),(ass1All,ass2All));
matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);
syst.matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);

//build new DAE-EqSystem
systTmp = BackendDAE.EQSYSTEM(vars,eqs,NONE(),NONE(),matching,stateSets,partitionKind);
(systTmp,_,_) = BackendDAEUtil.getIncidenceMatrix(systTmp, BackendDAE.NORMAL(),NONE());
(systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1+numNewSingleEqs,compsTmp,ass1All,ass2All,systTmp,sharedIn,tornSysIdxIn+1);
syst = BackendDAEUtil.setEqSystMatrices(syst);
(syst,_,_) = BackendDAEUtil.getIncidenceMatrix(syst, BackendDAE.NORMAL(),NONE());
(syst, tornSysIdx) = reduceLinearTornSystem1(compIdx+1+numNewSingleEqs,compsTmp,ass1All,ass2All,syst,sharedIn,tornSysIdxIn+1);
then
(systTmp,tornSysIdx);
case(_,_,_,_,BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs = eqs, stateSets=stateSets, partitionKind=partitionKind),_,_)
(syst, tornSysIdx);
case(_,_,_,_,syst as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs = eqs),_,_)
equation
// strongComponent is a system of equations
true = listLength(compsIn) >= compIdx;
Expand All @@ -263,9 +261,9 @@ algorithm
//BackendDump.dumpEquationList(eqLst,"eqLst");

// build linear system
syst = getEqSystem(eqLst,varLstRepl);
//dumpEqSys(syst);
(eqsNew,addEqs,addVars) = CramerRule(syst);
hpcSyst = getEqSystem(eqLst, varLstRepl);
//dumpEqSys(hpcSyst);
(eqsNew,addEqs,addVars) = CramerRule(hpcSyst);
(eqsNew,_) = BackendVarTransform.replaceEquations(eqsNew,derRepl,NONE());//introduce der(.) for $DER.

//BackendDump.dumpEquationList(eqsNew,"eqsNew");
Expand All @@ -288,31 +286,31 @@ algorithm
eqLst = listAppend(eqsOld,addEqs);
varLst = listAppend(varsOld,addVars);
eqLst = List.fold2(List.intRange(listLength(eqsNew)),replaceAtPositionFromList,eqsNew,eqIdcs,eqLst); // replaces the old residualEquations with the new ones
eqs = BackendEquation.listEquation(eqLst);
vars = BackendVariable.listVar1(varLst);
syst.orderedEqs = BackendEquation.listEquation(eqLst);
syst.orderedVars = BackendVariable.listVar1(varLst);

// update assignments
ass1All = arrayCreate(listLength(varLst),-1);
ass2All = arrayCreate(listLength(varLst),-1); // actually has to be listLength(eqLst), but there is still the problem that ass1 and ass2 have the same size
ass1All = Array.copy(ass1,ass1All); // the comps before and after the tornsystem
ass2All = Array.copy(ass2,ass2All);
List.map2_0(compsNew,updateAssignmentsByComp,ass1All,ass2All);
matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);
syst.matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);
//BackendDump.dumpFullMatching(matching);

//build new DAE-EqSystem
systTmp = BackendDAE.EQSYSTEM(vars,eqs,NONE(),NONE(),matching,stateSets,partitionKind);
syst = BackendDAEUtil.setEqSystMatrices(syst);
//(systTmp,_,_) = BackendDAEUtil.getIncidenceMatrix(systTmp, BackendDAE.NORMAL(),NONE());

(systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsTmp,ass1All,ass2All,systTmp,sharedIn,tornSysIdxIn+1);
(syst,tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsTmp,ass1All,ass2All,syst,sharedIn,tornSysIdxIn+1);
then
(systTmp,tornSysIdx);
(syst,tornSysIdx);
else
// go to next StrongComponent
equation
(systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn);
(syst, tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn);
then
(systTmp,tornSysIdx);
(syst, tornSysIdx);
end matchcontinue;
end reduceLinearTornSystem1;

Expand Down Expand Up @@ -579,7 +577,7 @@ protected
algorithm
eqArr := BackendEquation.listEquation(eqsIn);
varArr := BackendVariable.listVar1(varsIn);
eqSys := BackendDAE.EQSYSTEM(varArr,eqArr,NONE(),NONE(),BackendDAE.NO_MATCHING(),{},BackendDAE.UNKNOWN_PARTITION());
eqSys := BackendDAEUtil.createEqSystem(varArr, eqArr);
(m,mT) := BackendDAEUtil.incidenceMatrix(eqSys,BackendDAE.ABSOLUTE(),NONE());
size := listLength(eqsIn);
(eqIdcs,varIdcs,resEqsOut) := List.fold(List.intRange(size),function simplifyNewEquations1(eqArr=eqArr,varArr=varArr,m=m,mt=mT,numAuxiliaryVars=numAuxiliaryVars),({},{},resEqsIn));
Expand Down Expand Up @@ -1044,7 +1042,7 @@ algorithm
// get the EQSYSTEM, the incidenceMatrix and a matching
vars = BackendVariable.listVar1(inVars);
eqArr = BackendEquation.listEquation(inEqs);
sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,NONE(),NONE(),BackendDAE.NO_MATCHING(),{},BackendDAE.UNKNOWN_PARTITION());
sysTmp = BackendDAEUtil.createEqSystem(vars, eqArr);
(sysTmp,m,mt) = BackendDAEUtil.getIncidenceMatrix(sysTmp,BackendDAE.NORMAL(),NONE());
nVars = listLength(inVars);
nEqs = listLength(inEqs);
Expand All @@ -1054,7 +1052,7 @@ algorithm
BackendDAEEXT.matching(nVars, nEqs, 5, -1, 0.0, 1);
BackendDAEEXT.getAssignment(ass2, ass1);
matching = BackendDAE.MATCHING(ass1, ass2, {});
sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,SOME(m),SOME(mt),matching,{},BackendDAE.UNKNOWN_PARTITION());
sysTmp = BackendDAEUtil.createEqSystem(vars, eqArr);
// perform BLT to order the StrongComponents
mapIncRowEqn = listArray(List.intRange(nEqs));
mapEqnIncRow = Array.map(mapIncRowEqn,List.create);
Expand Down
27 changes: 22 additions & 5 deletions Compiler/BackEnd/HpcOmTaskGraph.mo
Expand Up @@ -4989,18 +4989,35 @@ protected function validateComponents "author: marcusw
input list<tuple<BackendDAE.StrongComponent,Integer>> systComps;
output Boolean res;
protected
Boolean isEqual;
Integer i1,i2;
BackendDAE.StrongComponent comp1,comp2;
tuple<BackendDAE.StrongComponent,Integer> tpl1,tpl2;
list<tuple<BackendDAE.StrongComponent,Integer>> sortedGraphComps, sortedSystComps;
algorithm
res := matchcontinue(graphComps,systComps)
case(_,_)
equation
sortedGraphComps = List.sort(graphComps,compareComponents);
sortedSystComps = List.sort(systComps,compareComponents);
true = List.isEqual(sortedGraphComps, sortedSystComps, true);
algorithm
sortedGraphComps := List.sort(graphComps,compareComponents);
sortedSystComps := List.sort(systComps,compareComponents);
//true := List.isEqual(sortedGraphComps, sortedSystComps, true);
if intNe(listLength(sortedSystComps),listLength(sortedGraphComps)) then print("the graph and the system have a difference number of components.\n"); end if;
isEqual := true;
while isEqual and not listEmpty(sortedGraphComps) loop
tpl1::sortedGraphComps := sortedGraphComps;
tpl2::sortedSystComps := sortedSystComps;
(comp1,i1) := tpl1;
(comp2,i2) := tpl2;
if BackendDAEUtil.componentsEqual(comp1,comp2) and intEq(i1,i2) then isEqual:= true;
else
isEqual := false;
print("comp"+intString(i1)+BackendDump.printComponent(comp1)+" is not equal to "+"comp"+intString(i2)+BackendDump.printComponent(comp2)+"\n");
end if;
end while;
then true;
else
equation
print("Different components in graph and system");
print("Different components in graph and system\n");
then false;
end matchcontinue;
end validateComponents;
Expand Down

0 comments on commit 6a82052

Please sign in to comment.