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<Integer> eventEqs; list<Integer> 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<Integer> eventEqsIn; output list<Integer> eventEqsOut; @@ -827,7 +830,7 @@ algorithm Integer eqn; Integer sysCount; list<Integer> eventEqs; - list<Integer> condVars; + list<BackendDAE.Var> 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..f372739fe8 100644 --- a/Compiler/SimCode/SimCodeUtil.mo +++ b/Compiler/SimCode/SimCodeUtil.mo @@ -1485,9 +1485,9 @@ end createEquationsForSystem; protected function addEquationsToLists input list<SimCode.SimEqSystem> inEq; - input array<Integer> stateeqnsmark; - input array<Integer> zceqnsmark; - input list<Integer> eqsIdx; + input Boolean bdynamic; + input Boolean bzceqns; + input Boolean skipDiscrete; input list<list<SimCode.SimEqSystem>> inOdeEquations; input list<list<SimCode.SimEqSystem>> inAlgebraicEquations; input list<list<SimCode.SimEqSystem>> inAllEquations; @@ -1496,16 +1496,12 @@ protected function addEquationsToLists output list<list<SimCode.SimEqSystem>> outAlgebraicEquations; output list<list<SimCode.SimEqSystem>> outAllEquations; output list<list<SimCode.SimEqSystem>> 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; outAllEquations := inEq::inAllEquations; - outEquationsforZeroCrossings := if bzceqns then inEq::inEquationsforZeroCrossings else inEquationsforZeroCrossings; + + outOdeEquations := if bdynamic and not skipDiscrete then inEq::inOdeEquations else inOdeEquations; + outAlgebraicEquations := if not bdynamic and not skipDiscrete then inEq::inAlgebraicEquations else inAlgebraicEquations; + outEquationsforZeroCrossings := if bzceqns and not skipDiscrete then inEq::inEquationsforZeroCrossings else inEquationsforZeroCrossings; end addEquationsToLists; protected function createEquationsForSystem1 @@ -1526,26 +1522,32 @@ protected list<Integer> eqsIdx,varIdx; list<BackendDAE.Var> varlst; list<BackendDAE.Equation> 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,7 +1560,8 @@ algorithm list<SimCode.SimEqSystem> equations1, noDiscEquations1; String message; - case _ guard not (createAlgebraicEquations or bdynamic) or skip and not createAlgebraicEquations + // case used for then inline solver, if "not createAlgebraicEquations = true" + case _ guard ((not bdynamic) or skip) and not createAlgebraicEquations then (uniqueEqIndex, odeEquations, algebraicEquations, allEquations, equationsforZeroCrossings, tempvars, eqSccMapping, eqBackendSimCodeMapping, backendMapping, sccIndex); @@ -1572,13 +1575,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 +1593,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 +1610,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 +1628,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 +1663,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 +1680,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 +1698,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, diff --git a/SimulationRuntime/c/simulation/solver/events.c b/SimulationRuntime/c/simulation/solver/events.c index 36e04edf6e..63acc7dc7b 100644 --- a/SimulationRuntime/c/simulation/solver/events.c +++ b/SimulationRuntime/c/simulation/solver/events.c @@ -278,6 +278,8 @@ void handleEvents(DATA* data, threadData_t *threadData, LIST* eventLst, double * debugStreamPrint(LOG_EVENTS, 0, "next sample-event at t = %g", data->simulationInfo->nextSampleEvent); solverInfo->sampleEvents++; + /* update the whole system */ + updateDiscreteSystem(data, threadData); } TRACE_POP