Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

[Backend] Skip all discrete equations in the ODE block #2639

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Compiler/BackEnd/HpcOmTaskGraph.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -827,21 +830,30 @@ algorithm
Integer eqn;
Integer sysCount;
list<Integer> eventEqs;
list<Integer> condVars;
list<BackendDAE.Var> eqnVars;
BackendDAE.StrongComponents rest;
BackendDAE.StrongComponent head;
case((head::rest),_,_)
equation
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({},_,_)
Expand Down
69 changes: 32 additions & 37 deletions Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/c/simulation/solver/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down