Skip to content

Commit

Permalink
Do partitioning for removed equations
Browse files Browse the repository at this point in the history
  • Loading branch information
gossen authored and OpenModelica-Hudson committed Jul 10, 2015
1 parent 7065083 commit 7c81416
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 178 deletions.
15 changes: 8 additions & 7 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -1636,26 +1636,27 @@ public function partitionIndependentBlocksHelper
algorithm
(systs,oshared) := matchcontinue (isyst,ishared,numErrorMessages,throwNoError)
local
BackendDAE.IncidenceMatrix m,mT;
array<Integer> ixs;
BackendDAE.IncidenceMatrix m, mT, rm, rmT;
array<Integer> ixs, rixs;
Boolean b;
Integer i;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
DAE.FunctionTree funcs;
case (syst,shared,_,_)
equation
// print("partitionIndependentBlocks: TODO: Implement me\n");
funcs = BackendDAEUtil.getFunctions(ishared);
(syst,m,mT) = BackendDAEUtil.getIncidenceMatrixfromOption(syst,BackendDAE.NORMAL(),SOME(funcs));
ixs = arrayCreate(arrayLength(m),0);
(syst, m, mT) = BackendDAEUtil.getIncidenceMatrixfromOption(syst, BackendDAE.NORMAL(), SOME(funcs));
(rm, rmT) = BackendDAEUtil.removedIncidenceMatrix(syst, BackendDAE.NORMAL(), SOME(funcs));
ixs = arrayCreate(arrayLength(m), 0);
rixs = arrayCreate(arrayLength(m), 0);
// ixsT = arrayCreate(arrayLength(mT),0);
i = SynchronousFeatures.partitionIndependentBlocks0(m,mT,ixs);
i = SynchronousFeatures.partitionIndependentBlocks0(m, mT, rm, rmT, ixs, rixs);
// i2 = SynchronousFeatures.partitionIndependentBlocks0(mT,m,ixsT);
b = i > 1;
// bcall2(b,BackendDump.dumpBackendDAE,BackendDAE.DAE({syst},shared), "partitionIndependentBlocksHelper");
// printPartition(b,ixs);
systs = if b then SynchronousFeatures.partitionIndependentBlocksSplitBlocks(i,syst,ixs,mT,throwNoError) else {syst};
systs = if b then SynchronousFeatures.partitionIndependentBlocksSplitBlocks(i, syst, ixs, rixs, mT, throwNoError) else {syst};
// print("Number of partitioned systems: " + intString(listLength(systs)) + "\n");
// List.map1_0(systs, BackendDump.dumpEqSystem, "System");
then (systs,shared);
Expand Down
80 changes: 49 additions & 31 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -109,6 +109,7 @@ protected import Types;
protected import UnitCheck;
protected import Values;
protected import XMLDump;
protected import MetaModelica.Dangerous.listReverseInPlace;

protected
type Var = BackendDAE.Var;
Expand Down Expand Up @@ -3613,6 +3614,15 @@ algorithm
osyst := BackendDAEUtil.setEqSystMatrices(syst, SOME(outM), SOME(outMT));
end getIncidenceMatrixScalar;

public function removedIncidenceMatrix
input BackendDAE.EqSystem inSyst;
input BackendDAE.IndexType inIndxType;
input Option<DAE.FunctionTree> inFunctionTree;
output BackendDAE.IncidenceMatrix outM;
output BackendDAE.IncidenceMatrix outMT;
algorithm
(outM, outMT) := incidenceMatrixDispatch(inSyst.orderedVars, inSyst.removedEqs, inIndxType, inFunctionTree);
end removedIncidenceMatrix;

protected function traverseStmts "Author: Frenkel TUD 2012-06
traverese DAE.Statement without change possibility."
Expand Down Expand Up @@ -6664,7 +6674,7 @@ algorithm

case (_, (optModule, moduleStr, _)::rest) equation
BackendDAE.DAE(systs, shared) = optModule(inDAE);
systs = filterEmptySystems(systs);
(systs, shared) = filterEmptySystems(systs, shared);
dae = BackendDAE.DAE(systs, shared);
SimCodeFunctionUtil.execStat("preOpt " + moduleStr);
if Flags.isSet(Flags.OPT_DAE_DUMP) then
Expand Down Expand Up @@ -6949,7 +6959,7 @@ algorithm
case (_, (optModule, moduleStr, _)::rest, _, _)
equation
BackendDAE.DAE(systs, shared) = optModule(inDAE);
systs = filterEmptySystems(systs);
(systs, shared) = filterEmptySystems(systs, shared);
dae = BackendDAE.DAE(systs, shared);
SimCodeFunctionUtil.execStat("postOpt " + moduleStr);
if Flags.isSet(Flags.OPT_DAE_DUMP) then
Expand Down Expand Up @@ -7510,11 +7520,11 @@ protected
list<BackendDAE.EqSystem> systs;
BackendDAE.Shared shared;
algorithm
BackendDAE.DAE(systs,shared) := dae;
(systs,shared) := List.map1Fold(systs,func,a,shared);
BackendDAE.DAE(systs, shared) := dae;
(systs, shared) := List.map1Fold(systs, func, a, shared);
// Filter out empty systems
systs := filterEmptySystems(systs);
odae := BackendDAE.DAE(systs,shared);
(systs, shared) := filterEmptySystems(systs, shared);
odae := BackendDAE.DAE(systs, shared);
end mapEqSystem1;

public function mapEqSystemAndFold<B>
Expand All @@ -7540,7 +7550,7 @@ algorithm
BackendDAE.DAE(systs, shared) := inDAE;
(systs, shared, outExtra) := List.mapFold2(systs, inFunc, shared, initialExtra);
// Filter out empty systems
systs := filterEmptySystems(systs);
(systs, shared) := filterEmptySystems(systs, shared);
outDAE := BackendDAE.DAE(systs, shared);
end mapEqSystemAndFold;

Expand All @@ -7561,10 +7571,10 @@ protected
list<BackendDAE.EqSystem> systs;
BackendDAE.Shared shared;
algorithm
BackendDAE.DAE(systs,shared) := dae;
BackendDAE.DAE(systs, shared) := dae;
extra := List.fold1(systs,func,shared,initialExtra);
// Filter out empty systems
systs := filterEmptySystems(systs);
(systs, shared) := filterEmptySystems(systs, shared);
end foldEqSystem;

public function mapEqSystem
Expand All @@ -7585,43 +7595,51 @@ algorithm
BackendDAE.DAE(systs, shared) := inDAE;
(systs, shared) := List.mapFold(systs, inFunc, shared);
// Filter out empty systems
systs := filterEmptySystems(systs);
(systs, shared) := filterEmptySystems(systs, shared);
outDAE := BackendDAE.DAE(systs, shared);
end mapEqSystem;

public function nonEmptySystem
input BackendDAE.EqSystem syst;
output Boolean nonEmpty;
protected
Integer num;
BackendDAE.Variables vars;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars) := syst;
num := BackendVariable.varsSize(vars);
nonEmpty := num <> 0;
nonEmpty := BackendVariable.varsSize(syst.orderedVars) <> 0 or BackendDAEUtil.equationArraySize(syst.removedEqs) <> 0;
end nonEmptySystem;

public function filterEmptySystems
"Filter out equation systems leaving at least one behind"
input BackendDAE.EqSystems systs;
output BackendDAE.EqSystems osysts;
algorithm
osysts := filterEmptySystems2(List.select(systs,nonEmptySystem),systs);
end filterEmptySystems;

protected function filterEmptySystems2
"Filter out equation systems leaving at least one behind"
input BackendDAE.EqSystems systs;
input BackendDAE.EqSystems full;
output BackendDAE.EqSystems olst;
input BackendDAE.EqSystems inSysts;
input BackendDAE.Shared inShared;
output BackendDAE.EqSystems outSysts;
output BackendDAE.Shared outShared = inShared;
protected
list<BackendDAE.Equation> reqns;
BackendDAE.Equation eq;
algorithm
olst := match (systs,full)
(reqns, outSysts) := List.fold(inSysts, filterEmptySystem, ({}, {}));
outSysts := match outSysts
local
BackendDAE.EqSystem syst;
case ({},syst::_) then {syst};
else systs;
case {}
then {BackendDAEUtil.createEqSystem(BackendVariable.emptyVars(), BackendEquation.emptyEqns())};
else listReverseInPlace(outSysts);
end match;
end filterEmptySystems2;
outShared.removedEqs := BackendEquation.addEquations(reqns, outShared.removedEqs);
end filterEmptySystems;

protected function filterEmptySystem
input BackendDAE.EqSystem inSyst;
input tuple<list<BackendDAE.Equation>, BackendDAE.EqSystems> inTpl;
output tuple<list<BackendDAE.Equation>, BackendDAE.EqSystems> outTpl;
protected
list<BackendDAE.Equation> reqs;
BackendDAE.EqSystems systs;
algorithm
(reqs, systs) := inTpl;
outTpl := if BackendVariable.varsSize(inSyst.orderedVars) == 0
then (listAppend(BackendEquation.equationList(inSyst.removedEqs), reqs), systs)
else (reqs, inSyst::systs);
end filterEmptySystem;

public function getAllVarLst "retrieve all variables of the dae by collecting them from each equation system and combining with known vars"
input BackendDAE.BackendDAE dae;
Expand Down
80 changes: 26 additions & 54 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -104,11 +104,11 @@ algorithm
BackendDAE.DAE(eqs, shared) := inBackendDAE;
List.map_0(eqs, printEqSystem);
print("\n");
printShared(eqs, shared);
printShared(shared);
end printBackendDAE;

public function printEqSystem "This function prints the BackendDAE.EqSystem representation to stdout."
input BackendDAE.EqSystem inEqSystem;
input BackendDAE.EqSystem inSyst;
protected
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
Expand All @@ -118,23 +118,16 @@ protected
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
algorithm
BackendDAE.EQSYSTEM(orderedVars=orderedVars,
orderedEqs=orderedEqs,
m=m,
mT=mT,
matching=matching,
stateSets=stateSets,
partitionKind=partitionKind) := inEqSystem;

print("\n" + partitionKindString(partitionKind) + "\n" + UNDERLINE + "\n");
dumpVariables(orderedVars, "Variables");
dumpEquationArray(orderedEqs, "Equations");
dumpStateSets(stateSets, "State Sets");
dumpOption(m, dumpIncidenceMatrix);
dumpOption(mT, dumpIncidenceMatrixT);
print("\n" + partitionKindString(inSyst.partitionKind) + "\n" + UNDERLINE + "\n");
dumpVariables(inSyst.orderedVars, "Variables");
dumpEquationArray(inSyst.orderedEqs, "Equations");
dumpEquationArray(inSyst.removedEqs, "Simple Equations");
dumpStateSets(inSyst.stateSets, "State Sets");
dumpOption(inSyst.m, dumpIncidenceMatrix);
dumpOption(inSyst.mT, dumpIncidenceMatrixT);

print("\n");
dumpFullMatching(matching);
dumpFullMatching(inSyst.matching);
print("\n");
end printEqSystem;

Expand Down Expand Up @@ -256,49 +249,28 @@ public function printClassAttributes "This unction print the Optimica ClassAttr
end printClassAttributes;

public function printShared "This function dumps the BackendDAE.Shared representation to stdout."
input BackendDAE.EqSystems inSysts "for backward compatibility";
input BackendDAE.Shared inShared;
protected
BackendDAE.Variables knownVars, externalObjects, aliasVars;
BackendDAE.EquationArray initialEqs, removedEqs;
list<DAE.Constraint> constraints;
list<BackendDAE.ZeroCrossing> zeroCrossingLst, sampleLst, relationsLst;
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.TimeEvent> timeEvents;
BackendDAE.ExternalObjectClasses extObjClasses;
BackendDAE.BackendDAEType backendDAEType;
BackendDAE.SymbolicJacobians symjacs;
algorithm
BackendDAE.SHARED(knownVars=knownVars,
externalObjects=externalObjects,
aliasVars=aliasVars,
initialEqs=initialEqs,
constraints=constraints,
eventInfo=BackendDAE.EVENT_INFO( timeEvents=timeEvents, relationsLst=relationsLst, zeroCrossingLst=zeroCrossingLst,
sampleLst=sampleLst, whenClauseLst=whenClauseLst ),
extObjClasses=extObjClasses,
backendDAEType=backendDAEType,
symjacs=symjacs) := inShared;
removedEqs := BackendDAEUtil.collapseRemovedEqs(BackendDAE.DAE(inSysts, inShared));
algorithm

print("\nBackendDAEType: ");
printBackendDAEType(backendDAEType);
printBackendDAEType(inShared.backendDAEType);
print("\n\n");

dumpVariables(knownVars, "Known Variables (constants)");
dumpVariables(externalObjects, "External Objects");
dumpExternalObjectClasses(extObjClasses, "Classes of External Objects");
dumpVariables(aliasVars, "Alias Variables");
dumpEquationArray(removedEqs, "Simple Equations");
dumpEquationArray(initialEqs, "Initial Equations");
dumpZeroCrossingList(zeroCrossingLst, "Zero Crossings");
dumpZeroCrossingList(relationsLst, "Relations");
dumpVariables(inShared.knownVars, "Known Variables (constants)");
dumpVariables(inShared.externalObjects, "External Objects");
dumpExternalObjectClasses(inShared.extObjClasses, "Classes of External Objects");
dumpVariables(inShared.aliasVars, "Alias Variables");
dumpEquationArray(inShared.removedEqs, "Simple Shared Equations");
dumpEquationArray(inShared.initialEqs, "Initial Equations");
dumpZeroCrossingList(inShared.eventInfo.zeroCrossingLst, "Zero Crossings");
dumpZeroCrossingList(inShared.eventInfo.relationsLst, "Relations");
if stringEqual(Config.simCodeTarget(), "Cpp") then
dumpZeroCrossingList(sampleLst, "Samples");
dumpZeroCrossingList(inShared.eventInfo.sampleLst, "Samples");
else
dumpTimeEvents(timeEvents, "Time Events");
dumpTimeEvents(inShared.eventInfo.timeEvents, "Time Events");
end if;
dumpWhenClauseList(whenClauseLst, "When Clauses");
dumpConstraintList(constraints, "Constraints");
dumpWhenClauseList(inShared.eventInfo.whenClauseLst, "When Clauses");
dumpConstraintList(inShared.constraints, "Constraints");
end printShared;

public function printClocks
Expand Down Expand Up @@ -3267,7 +3239,7 @@ algorithm
print(headerline + ":\n");
List.map_0(eqs, printEqSystem);
print("\n");
printShared(eqs, shared);
printShared(shared);
then ();
end matchcontinue;
end bltdump;
Expand Down
10 changes: 10 additions & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -350,6 +350,16 @@ algorithm
end matchcontinue;
end traversingStateRefFinder;

public function assertWithCondTrue "author: Frenkel TUD 2012-12"
input BackendDAE.Equation inEqn;
output Boolean b;
algorithm
b := match inEqn
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=DAE.BCONST(true))})) then false;
else true;
end match;
end assertWithCondTrue;

public function equationsParams "author: marcusw
From a list of equations return all occurring parameter variables. Duplicates are removed."
input list<BackendDAE.Equation> inEquationLst;
Expand Down
22 changes: 6 additions & 16 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -3571,9 +3571,9 @@ algorithm
((_, eqnslst, b1)) = BackendEquation.traverseEquationArray(shared.initialEqs, replaceEquationTraverser, (repl, {}, false));
shared.initialEqs = if b1 then BackendEquation.listEquation(eqnslst) else shared.initialEqs;

((_, eqnslst, b1)) = BackendEquation.traverseEquationArray(shared.removedEqs, replaceEquationTraverser, (repl, {}, false));
eqnslst = List.select(eqnslst, assertWithCondTrue);
shared.removedEqs = if b1 then BackendEquation.listEquation(eqnslst) else shared.removedEqs;
((_, eqnslst, _)) = BackendEquation.traverseEquationArray(shared.removedEqs, replaceEquationTraverser, (repl, {}, false));
eqnslst = List.select(eqnslst, BackendEquation.assertWithCondTrue);
shared.removedEqs = BackendEquation.listEquation(eqnslst);

(eventInfo.whenClauseLst, _) =
BackendVarTransform.replaceWhenClauses(eventInfo.whenClauseLst, repl, SOME(BackendVarTransform.skipPreChangeEdgeOperator));
Expand Down Expand Up @@ -3672,16 +3672,6 @@ algorithm
end matchcontinue;
end replaceVarTraverser;

protected function assertWithCondTrue "author: Frenkel TUD 2012-12"
input BackendDAE.Equation inEqn;
output Boolean b;
algorithm
b := match inEqn
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=DAE.BCONST(true))})) then false;
else true;
end match;
end assertWithCondTrue;

protected function removeSimpleEquationsShared1 "author: Frenkel TUD 2012-12"
input BackendDAE.EqSystems inSysts;
input BackendDAE.EqSystems inSysts1;
Expand Down Expand Up @@ -3715,7 +3705,7 @@ algorithm

((_, eqnslst, _)) := BackendEquation.traverseEquationArray(syst.removedEqs, replaceEquationTraverser, (repl, {}, false));
// remove asserts with condition=true from removed equations
eqnslst := List.select(eqnslst, assertWithCondTrue);
eqnslst := List.select(eqnslst, BackendEquation.assertWithCondTrue);
syst.removedEqs := BackendEquation.listEquation(eqnslst);
then
removeSimpleEquationsShared1(rest, syst::inSysts1, repl, statesetrepl1, aliasVars);
Expand Down Expand Up @@ -4421,12 +4411,12 @@ algorithm
remEqList = BackendEquation.equationList(syst.removedEqs);
(remEqList,_) = BackendEquation.traverseExpsOfEquationList(remEqList, traverseExpTopDown, HTCrToExp);
//remove asserts with condition=true from removed equations
syst.removedEqs = BackendEquation.listEquation(List.select(remEqList, assertWithCondTrue));
syst.removedEqs = BackendEquation.listEquation(List.select(remEqList, BackendEquation.assertWithCondTrue));

remEqList = BackendEquation.equationList(shared.removedEqs);
(remEqList,_) = BackendEquation.traverseExpsOfEquationList(remEqList, traverseExpTopDown, HTCrToExp);
//remove asserts with condition=true from removed equations
shared.removedEqs = BackendEquation.listEquation(List.select(remEqList, assertWithCondTrue));
shared.removedEqs = BackendEquation.listEquation(List.select(remEqList, BackendEquation.assertWithCondTrue));


// BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
Expand Down

0 comments on commit 7c81416

Please sign in to comment.