From 88fda20a17a14f7bc123410d6e25a3eefdfd2ade Mon Sep 17 00:00:00 2001 From: Willi Braun Date: Mon, 10 Sep 2018 16:44:14 +0200 Subject: [PATCH] [Backend] Skip all discrete equations in the ODE block --- Compiler/BackEnd/HpcOmTaskGraph.mo | 22 +++++++--- Compiler/SimCode/SimCodeUtil.mo | 66 ++++++++++++++---------------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Compiler/BackEnd/HpcOmTaskGraph.mo b/Compiler/BackEnd/HpcOmTaskGraph.mo index 536fd3e187..fc1b81bb56 100644 --- a/Compiler/BackEnd/HpcOmTaskGraph.mo +++ b/Compiler/BackEnd/HpcOmTaskGraph.mo @@ -802,14 +802,15 @@ protected BackendDAE.StrongComponents comps; BackendDAE.Matching matching; BackendDAE.EquationArray orderedEqs; + BackendDAE.Variables systVars; list eventEqs; list eventEqsIn; Integer offset; algorithm - BackendDAE.EQSYSTEM(orderedEqs=orderedEqs,matching=matching) := systIn; + BackendDAE.EQSYSTEM(orderedEqs=orderedEqs,orderedVars=systVars,matching=matching) := systIn; comps := BackendDAEUtil.getCompsOfMatching(matching); (eventEqsIn,offset) := eventInfoIn; - eventEqs := getEventNodeEqs1(comps,offset,{}); + eventEqs := getEventNodeEqs1(comps,orderedEqs,systVars,offset,{}); offset := offset+ExpandableArray.getNumberOfElements(orderedEqs); eventEqs := listAppend(eventEqs,eventEqsIn); eventInfoOut := (eventEqs,offset); @@ -818,6 +819,8 @@ end getEventNodeEqs; protected function getEventNodeEqs1 "author: Waurich TUD 2013-06 Fold-function for getEventNodeEqs to compute the when equation in an eqSystem." input BackendDAE.StrongComponents comps; + input BackendDAE.EquationArray orderedEqs; + input BackendDAE.Variables systVars; input Integer offset; input list eventEqsIn; output list eventEqsOut; @@ -827,7 +830,7 @@ algorithm Integer eqn; Integer sysCount; list eventEqs; - list condVars; + list eqnVars; BackendDAE.StrongComponents rest; BackendDAE.StrongComponent head; case((head::rest),_,_) @@ -835,13 +838,22 @@ algorithm true = isWhenEquation(head); BackendDAE.SINGLEWHENEQUATION(eqn = eqn) = head; eqn = eqn+offset; - eventEqs = getEventNodeEqs1(rest,offset,eqn::eventEqsIn); + eventEqs = getEventNodeEqs1(rest,orderedEqs,systVars,offset,eqn::eventEqsIn); + then + eventEqs; + // discrete variables + case((head::rest),_,_) + equation + (eqnVars,_,_,eventEqs) = BackendDAEUtil.getStrongComponentVarsAndEquations(head, systVars, orderedEqs); + true = List.mapBoolAnd(eqnVars, BackendVariable.isVarDiscrete); + eventEqs = list(i+offset for i in eventEqs); + eventEqs = getEventNodeEqs1(rest,orderedEqs,systVars,offset,listAppend(eventEqs,eventEqsIn)); then eventEqs; case((head::rest),_,_) equation false = isWhenEquation(head); - eventEqs = getEventNodeEqs1(rest,offset,eventEqsIn); + eventEqs = getEventNodeEqs1(rest,orderedEqs,systVars,offset,eventEqsIn); then eventEqs; case({},_,_) diff --git a/Compiler/SimCode/SimCodeUtil.mo b/Compiler/SimCode/SimCodeUtil.mo index 6db516952c..4a69d9c20b 100644 --- a/Compiler/SimCode/SimCodeUtil.mo +++ b/Compiler/SimCode/SimCodeUtil.mo @@ -1485,9 +1485,9 @@ end createEquationsForSystem; protected function addEquationsToLists input list inEq; - input array stateeqnsmark; - input array zceqnsmark; - input list eqsIdx; + input Boolean bdynamic; + input Boolean bzceqns; + input Boolean skipDiscrete; input list> inOdeEquations; input list> inAlgebraicEquations; input list> inAllEquations; @@ -1496,16 +1496,11 @@ protected function addEquationsToLists output list> outAlgebraicEquations; output list> outAllEquations; output list> outEquationsforZeroCrossings; -protected - Boolean bdynamic "block is dynamic, belongs to dynamic section"; - Boolean bzceqns "block needs to evaluate zeroCrossings"; algorithm - bdynamic := BackendDAEUtil.blockIsDynamic(eqsIdx, stateeqnsmark); - bzceqns := BackendDAEUtil.blockIsDynamic(eqsIdx, zceqnsmark); - outOdeEquations := if bdynamic then inEq::inOdeEquations else inOdeEquations; - outAlgebraicEquations := if not bdynamic then inEq::inAlgebraicEquations else inAlgebraicEquations; + outOdeEquations := if bdynamic and not skipDiscrete then inEq::inOdeEquations else inOdeEquations; + outAlgebraicEquations := if not bdynamic and not skipDiscrete then inEq::inAlgebraicEquations else inAlgebraicEquations; outAllEquations := inEq::inAllEquations; - outEquationsforZeroCrossings := if bzceqns then inEq::inEquationsforZeroCrossings else inEquationsforZeroCrossings; + outEquationsforZeroCrossings := if bzceqns and not skipDiscrete then inEq::inEquationsforZeroCrossings else inEquationsforZeroCrossings; end addEquationsToLists; protected function createEquationsForSystem1 @@ -1526,26 +1521,32 @@ protected list eqsIdx,varIdx; list varlst; list eqnlst; - Boolean createAlgebraicEquations, bdynamic, skip; + Boolean createAlgebraicEquations, bdynamic, bzceqns, skip; Boolean debug = false; algorithm (stateeqnsmark, zceqnsmark, syst, shared, createAlgebraicEquations) := inArg; (uniqueEqIndex, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars, eqSccMapping, eqBackendSimCodeMapping, backendMapping, sccIndex) := inFold; (varlst,varIdx,eqnlst,eqsIdx) := BackendDAEUtil.getStrongComponentVarsAndEquations(comp, syst.orderedVars, syst.orderedEqs); - bdynamic := BackendDAEUtil.blockIsDynamic(eqsIdx, stateeqnsmark); skip := false; + // skip is when equations + skip := List.mapBoolAnd(eqnlst, BackendEquation.isWhenEquation); + // skip is discrete + skip := skip or List.mapBoolAnd(varlst, BackendVariable.isVarDiscrete); + + // Do we need this equation in the ode block? + bdynamic := BackendDAEUtil.blockIsDynamic(eqsIdx, stateeqnsmark); + // Do we need this equation to detect zerocrossings? + bzceqns := BackendDAEUtil.blockIsDynamic(eqsIdx, zceqnsmark); + if debug then print("Proceed component: " + BackendDump.strongComponentString(comp) + "\n"); BackendDump.dumpEquationList(eqnlst,"Equations:"); BackendDump.dumpVarList(varlst,"Variables:"); + print("Discrete equation: "+boolString(skip)+" \n"); end if; - // skip is when equations - skip := List.mapBoolAnd(eqnlst, BackendEquation.isWhenEquation); - // skip is discrete - skip := skip or List.mapBoolAnd(varlst, BackendVariable.isVarDiscrete); outFold := match comp local @@ -1558,6 +1559,7 @@ algorithm list equations1, noDiscEquations1; String message; + // case used for then inline solver, if "not createAlgebraicEquations = true" case _ guard not (createAlgebraicEquations or bdynamic) or skip and not createAlgebraicEquations then (uniqueEqIndex, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars, eqSccMapping, eqBackendSimCodeMapping, backendMapping, sccIndex); @@ -1572,13 +1574,9 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(firstEqIndex, uniqueEqIndex1 - 1, index, eqBackendSimCodeMapping); backendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex1 - 1), {index}, backendMapping); end if; - if BackendEquation.isWhenEquation(BackendEquation.get(syst.orderedEqs, index)) then - allEquations = equations1::allEquations; - else - (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {index}, odeEquations, - algebraicEquations, allEquations, equationsforZeroCrossings); - end if; + (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, + algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars, eqSccMapping, eqBackendSimCodeMapping, backendMapping, sccIndex + 1); @@ -1594,7 +1592,7 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, e, eqBackendSimCodeMapping); (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {e}, odeEquations, + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, @@ -1611,7 +1609,7 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, e, eqBackendSimCodeMapping); (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {e}, odeEquations, + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, @@ -1629,7 +1627,7 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, e, eqBackendSimCodeMapping); (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {e}, odeEquations, + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, @@ -1664,7 +1662,7 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, index, eqBackendSimCodeMapping); (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {e}, odeEquations, + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, @@ -1681,13 +1679,9 @@ algorithm eqBackendSimCodeMapping = appendSccIdxRange(firstEqIndex, uniqueEqIndex1 - 1, index, eqBackendSimCodeMapping); backendMapping = setEqMapping(List.intRange2(firstEqIndex, uniqueEqIndex1 - 1),{index}, backendMapping); end if; - if BackendEquation.isWhenEquation(BackendEquation.get(syst.orderedEqs, index)) then - allEquations = equations1::allEquations; - else - (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(equations1, stateeqnsmark, zceqnsmark, {index}, odeEquations, - algebraicEquations, allEquations, equationsforZeroCrossings); - end if; + (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = + addEquationsToLists(equations1, bdynamic, bzceqns, skip, odeEquations, + algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars, eqSccMapping, eqBackendSimCodeMapping,backendMapping, sccIndex + 1); @@ -1703,7 +1697,7 @@ algorithm //eqSccMapping = appendSccIdxRange(uniqueEqIndex, uniqueEqIndex1 - 1, sccIndex, eqSccMapping); (odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings) = - addEquationsToLists(noDiscEquations1, stateeqnsmark, zceqnsmark, eqnslst, odeEquations, + addEquationsToLists(noDiscEquations1, bdynamic, bzceqns, skip, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings); then (uniqueEqIndex1, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars,