Skip to content

Commit b34014d

Browse files
authored
Nb event detection revamp (#11463)
* [NB] untested baseline for event detection revamp - remove all tree structures and replace with unorderedmaps/sets - change the mapping to condition -> var (eqns) - make own structure for composite events (not yet handled correctly, works with regular time events) * [NB] some dumping improvements * [NB] update single time events * [testsuite] update for NB event changes
1 parent 7e09cdd commit b34014d

File tree

10 files changed

+482
-427
lines changed

10 files changed

+482
-427
lines changed

OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public
353353

354354
function size
355355
input Iterator iter;
356-
output Integer size = product(i for i in sizes(iter));
356+
output Integer size = product(i for i in 1 :: sizes(iter));
357357
end size;
358358

359359
function createLocationReplacements
@@ -1509,11 +1509,11 @@ public
15091509
end getType;
15101510

15111511
function getForIterator
1512+
"does not work for algorithms"
15121513
input Equation eqn;
15131514
output Iterator iterator;
15141515
algorithm
15151516
iterator := match eqn
1516-
// ToDo: algorithms!
15171517
case FOR_EQUATION() then eqn.iter;
15181518
else Iterator.EMPTY();
15191519
end match;
@@ -2259,7 +2259,7 @@ public
22592259
case Expression.ARRAY() then List.flatten(list(getConditions(elem) for elem in cond.elements));
22602260
case Expression.CALL() guard(Call.isNamed(cond.call, "initial")) then {};
22612261
else algorithm
2262-
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
2262+
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed for condition: " + Expression.toString(cond)});
22632263
then fail();
22642264
end match;
22652265
end getConditions;

OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ public
975975
var := fromCref(var_cref, NFAttributes.IMPL_DISCRETE_ATTR);
976976
// update the variable to be discrete and pass the pointer to the original variable
977977
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, BackendExtension.DISCRETE());
978+
var.backendinfo := BackendExtension.BackendInfo.setHideResult(var.backendinfo, true);
978979
// create the new variable pointer and safe it to the component reference
979980
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
980981
end makeEventVar;

OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBEvents.mo

Lines changed: 419 additions & 400 deletions
Large diffs are not rendered by default.

OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBFunctionAlias.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ protected
362362
if isSome(aux_opt) then
363363
aux := Util.getOption(aux_opt);
364364
new_exp := aux.replacer;
365+
ty := Expression.typeOf(new_exp);
365366
else
366367
// for initial systems create parameters, otherwise use type to determine variable kind
367368
ty := Expression.typeOf(exp);

OMCompiler/Compiler/NFFrontEnd/NFBackendExtension.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,9 @@ public
13201320
then ();
13211321
// do not create a notification for TearingSelect since its parsed in attributes
13221322
case SCode.NAMEMOD(ident = "TearingSelect") then ();
1323+
// do not create a notification for the following since they are handled in the frontend
1324+
case SCode.NAMEMOD(ident = "Placement") then ();
1325+
case SCode.NAMEMOD(ident = "Dialog") then ();
13231326
else algorithm
13241327
Error.addCompilerNotification("Could not parse annotation \"" + submod.ident + "\" in the backend.");
13251328
then ();

OMCompiler/Compiler/NSimCode/NSimCode.mo

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected
6060
import BackendDAE = NBackendDAE;
6161
import BEquation = NBEquation;
6262
import NBEquation.{Equation, EquationPointer, EquationPointers, EqData};
63-
import NBEvents.{EventInfo, StateEvent};
63+
import NBEvents.{EventInfo, Condition};
6464
import NBVariable.{VariablePointers, VarData};
6565
import BVariable = NBVariable;
6666
import System = NBSystem;
@@ -221,7 +221,7 @@ public
221221
algorithm
222222
str := StringUtil.headline_1("SimCode " + str + "(" + simCode.fileNamePrefix + ")");
223223
str := str + ModelInfo.toString(simCode.modelInfo);
224-
str := str + SimStrongComponent.Block.listToString(simCode.init, " ", "INIT") + "\n";
224+
str := str + SimStrongComponent.Block.listToString(simCode.init, " ", "Initial Partition") + "\n";
225225
for blck_lst in simCode.ode loop
226226
str := str + SimStrongComponent.Block.listToString(blck_lst, " ", "ODE Partition " + intString(idx)) + "\n";
227227
idx := idx + 1;
@@ -231,6 +231,7 @@ public
231231
str := str + SimStrongComponent.Block.listToString(blck_lst, " ", "Algebraic Partition " + intString(idx)) + "\n";
232232
idx := idx + 1;
233233
end for;
234+
str := str + SimStrongComponent.Block.listToString(simCode.allSim, " ", "Event Partition") + "\n";
234235
if not listEmpty(simCode.generic_loop_calls) then
235236
str := str + StringUtil.headline_3("Generic Calls");
236237
str := str + List.toString(simCode.generic_loop_calls, SimGenericCall.toString, "", " ", "\n ", "\n\n");
@@ -686,9 +687,9 @@ public
686687
output VarInfo varInfo;
687688
algorithm
688689
varInfo := VAR_INFO(
689-
numZeroCrossings = sum(StateEvent.size(se) for se in eventInfo.stateEvents),
690-
numTimeEvents = listLength(eventInfo.timeEvents),
691-
numRelations = sum(StateEvent.size(se) for se in eventInfo.stateEvents),
690+
numZeroCrossings = sum(Condition.size(cond) for cond in UnorderedMap.keyList(eventInfo.state_map)),
691+
numTimeEvents = listLength(UnorderedSet.toList(eventInfo.time_set)),
692+
numRelations = sum(Condition.size(cond) for cond in UnorderedMap.keyList(eventInfo.state_map)),
692693
numMathEventFunctions = eventInfo.numberMathEvents,
693694
numStateVars = listLength(vars.stateVars),
694695
numAlgVars = listLength(vars.algVars),

OMCompiler/Compiler/NSimCode/NSimStrongComponent.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public
251251
case GENERIC_ASSIGN() then str + "(" + intString(blck.index) + ") " + "single generic call [index " + intString(blck.call_index) + "] " + List.toString(inList = blck.scal_indices, inPrintFunc = intString, maxLength = 10) + "\n";
252252
case ENTWINED_ASSIGN() then str + List.toString(blck.single_calls, function toString(str=""), "### entwined call (" + intString(blck.index) + ") ###", "\n ", " ", "");
253253
case ALIAS() then str + "(" + intString(blck.index) + ") Alias of " + intString(blck.aliasOf) + "\n";
254-
case ALGORITHM() then str + "(" + intString(blck.index) + ") Algorithm\n" + Statement.toStringList(blck.stmts, str) + "\n";
254+
case ALGORITHM() then str + "(" + intString(blck.index) + ") Algorithm\n" + Statement.toStringList(blck.stmts, str);
255255
case INVERSE_ALGORITHM() then str + "(" + intString(blck.index) + ") Inverse Algorithm\n" + Statement.toStringList(blck.stmts, str) + "\n";
256256
case IF() then str + "(" + intString(blck.index) + ") " + List.toString(blck.branches, function ifTplStr(str = str), "", str, str + "else ", str + "end if;\n");
257257
case WHEN() then str + "(" + intString(blck.index) + ") " + whenString(blck.conditions, blck.when_stmts, blck.else_when, str);

testsuite/simulation/modelica/NBackend/basics/implicitEquation.mos

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ simulate(implicitEquation); getErrorString();
3333
// ************************
3434
// (0)[RES-] (1) Real $RES_SIM_0
3535
//
36-
// INIT
37-
// ------
36+
// Initial Partition
37+
// -------------------
3838
// (3) $FUN_1 := sin(time)
3939
// (2) Nonlinear System (size = 1, homotopy = false, mixed = false, torn = true)
4040
// --Iteration Vars:{x}
@@ -45,6 +45,11 @@ simulate(implicitEquation); getErrorString();
4545
// (5) Alias of 3
4646
// (4) Alias of 2
4747
//
48+
// Event Partition
49+
// -----------------
50+
// (5) Alias of 3
51+
// (4) Alias of 2
52+
//
4853
// ======================================================
4954
// [EMPTY] SimCode Jacobian A(idx = 0, partition = 0)
5055
// ======================================================

testsuite/simulation/modelica/NBackend/event_handling/compositeEvent.mos

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ equation
1313
end when;
1414
end CompositeEvent;
1515
"); getErrorString();
16+
setCommandLineOptions("--newBackend"); getErrorString();
1617
simulate(CompositeEvent, simflags="-lv LOG_EVENTS"); getErrorString();
1718
val(n,0.125);
1819
val(n,0.375);
@@ -22,27 +23,22 @@ val(n,0.875);
2223
// Result:
2324
// true
2425
// ""
26+
// true
27+
// ""
2528
// record SimulationResult
2629
// resultFile = "CompositeEvent_res.mat",
27-
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'CompositeEvent', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv LOG_EVENTS'",
30+
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'CompositeEvent', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv LOG_EVENTS'",
2831
// messages = "LOG_EVENTS | info | status of relations at time=0
29-
// | | | | | [1] (pre: true) true = x < 1.0
30-
// | | | | | [2] (pre: false) false = x > 0.5
3132
// LOG_EVENTS | info | status of zero crossings at time=0
32-
// | | | | | [1] (pre: 0) -1 = x < 1.0 and x > 0.5 and sample(1, 0.125, 0.25)
3333
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
3434
// LOG_EVENTS | info | time event at time=0.125
35-
// | | | | | [1] sample(0.125, 0.25)
35+
// | | | | | [0] sample(0.125, 0.25)
3636
// LOG_EVENTS | info | time event at time=0.375
37-
// | | | | | [1] sample(0.125, 0.25)
37+
// | | | | | [0] sample(0.125, 0.25)
3838
// LOG_EVENTS | info | time event at time=0.625
39-
// | | | | | [1] sample(0.125, 0.25)
40-
// LOG_EVENTS | info | state event at time=0.626
41-
// | | | | | [1] x < 1.0 and x > 0.5 and sample(1, 0.125, 0.25)
39+
// | | | | | [0] sample(0.125, 0.25)
4240
// LOG_EVENTS | info | time event at time=0.875
43-
// | | | | | [1] sample(0.125, 0.25)
44-
// LOG_EVENTS | info | state event at time=0.876
45-
// | | | | | [1] x < 1.0 and x > 0.5 and sample(1, 0.125, 0.25)
41+
// | | | | | [0] sample(0.125, 0.25)
4642
// LOG_SUCCESS | info | The simulation finished successfully.
4743
// "
4844
// end SimulationResult;

testsuite/simulation/modelica/NBackend/event_handling/eventSystem.mos

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ model eventSystem
77
Real x1(start = 0.0, fixed = true);
88
Real x2(start = -1.0, fixed = true);
99
Real x3(start = 1.0, fixed = true);
10+
Real y;
1011
Real z;
1112
parameter Real t = 0.2;
1213
equation
1314
assert(z <= 1, \"z is wrong\");
1415

16+
if time > 2*t then
17+
y = cos(time);
18+
else
19+
y = sin(time);
20+
end if;
21+
1522
when t < time then
1623
x1 = pre(x1) + 1;
1724
end when;
@@ -28,7 +35,7 @@ end eventSystem;
2835
"); getErrorString();
2936

3037
setCommandLineOptions("--newBackend"); getErrorString();
31-
simulate(eventSystem); getErrorString();
38+
simulate(eventSystem, simflags="-lv=LOG_EVENTS"); getErrorString();
3239

3340
val(x1, 0.19);
3441
val(x1, 0.21);
@@ -46,14 +53,36 @@ val(x3, 0.53);
4653
// ""
4754
// record SimulationResult
4855
// resultFile = "eventSystem_res.mat",
49-
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'eventSystem', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
50-
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
56+
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'eventSystem', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv=LOG_EVENTS'",
57+
// messages = "LOG_EVENTS | info | status of relations at time=0
58+
// | | | | | [1] (pre: false) false = z > 0.5
59+
// | | | | | [2] (pre: true) true = z <= 1.0
60+
// LOG_EVENTS | info | status of zero crossings at time=0
61+
// | | | | | [1] (pre: 0) -1 = z > 0.5
62+
// | | | | | [2] (pre: 0) 1 = z <= 1.0
63+
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
64+
// LOG_EVENTS | info | time event at time=0.1
65+
// | | | | | [0] sample(0.1, 0.2)
66+
// LOG_EVENTS | info | time event at time=0.2
67+
// | | | | | [1] sample(0.2, 4.61169e+18)
68+
// LOG_EVENTS | info | time event at time=0.3
69+
// | | | | | [0] sample(0.1, 0.2)
70+
// LOG_EVENTS | info | time event at time=0.4
71+
// | | | | | [2] sample(0.4, 4.61169e+18)
72+
// LOG_EVENTS | info | time event at time=0.5
73+
// | | | | | [0] sample(0.1, 0.2)
74+
// LOG_EVENTS | info | state event at time=0.523598775599
75+
// | | | | | [1] z > 0.5
76+
// LOG_EVENTS | info | time event at time=0.7
77+
// | | | | | [0] sample(0.1, 0.2)
78+
// LOG_EVENTS | info | time event at time=0.9
79+
// | | | | | [0] sample(0.1, 0.2)
5180
// LOG_SUCCESS | info | The simulation finished successfully.
5281
// "
5382
// end SimulationResult;
5483
// ""
5584
// 0.0
56-
// 0.0
85+
// 1.0
5786
// -0.5
5887
// 0.0
5988
// 1.0

0 commit comments

Comments
 (0)