question

alan.zhang avatar image
2 Likes"
alan.zhang asked alan.zhang commented

OnRunStop when empty event list

Is there any way to fire some code if the simulation stops when the event list is empty?

I found that I could not achieve this by writing the code in OnRunStop.

In the attached very simple model, the simulation will stop at time 10 due to no events. I put code in the OnRunStop to display a message box. If I click Stop button to stop the simulation, OnRunStop will be called. But if I let simulation run to time 10 to stop because of no events, OnRunStop will not be called.

FlexSim 17.0.2
Choose One
event list
· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image Phil BoBo ♦♦ commented ·

The model is still running, but it has no events to process. If you were to generate an event, the simulation engine would process it. It isn't stopped; it is still checking for events to process and finding none.

What are you actually trying to do? What do you want your code to do?

0 Likes 0 ·
alan.zhang avatar image alan.zhang Phil BoBo ♦♦ commented ·

We are trying to see if there is a generic way to fire some code at the end of simulation, for example collecting and outputting some statistic information.

You are saying that the simulation engine is still running and waiting for any events. That makes sense to me. Thanks.

0 Likes 0 ·
Joerg Vogel avatar image Joerg Vogel Phil BoBo ♦♦ commented ·

If you build endless running process or sub model like one product is processed by a constant time. This is released to a queue and processed again. Then you get the opportunity to evaluate the event list. If the list contains only the events of the sub process or the sub model you call the command stop().

0 Likes 0 ·
Phil BoBo avatar image
2 Likes"
Phil BoBo answered alan.zhang commented

If you are going to poll to see if the event list is empty, then you can do it with a single User Event with the following code:

if(eventqty() == 0) {
	msg("Model Stopped","Ran Out Of Events");
	stop();
} else {
	delayednodefunction(c, eventget(eventqty(), 2) - time());
}

See the attached model emptyeventlist-pb1.fsm

As Mischa mentioned, if you have repeating events, then this method of checking the event list's size won't ever stop the model, because you will constantly have more events.


· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

alan.zhang avatar image alan.zhang commented ·

This is a good solution. I see it should not impact the performance since only one event is added into the event list to check if the event is empty, using the last event's time in the event list. Thanks.

0 Likes 0 ·
Mischa Spelt avatar image
3 Likes"
Mischa Spelt answered alan.zhang commented

Though I understand FlexSim's reasoning of why the Run Stop event should not fire, as per @phil.bobo's answer, I also see where @alan.zhang's question is coming from. A typical model that would have this behavior is where you have for example a finite arrival schedule of orders and you just want to run the model until all orders have been processed. Depending on what you want to do, you can, for example

  • Put an On Entry trigger on the final Sink in the model, and call the stop(0) command from there (I think there is even a "stop the model" option in the dropdown) once you detect that the sink's input is equal to the number of items to be processed in the model.
  • Do what you wanted to do in the Run Stop somewhere else. For example, if you want to use the Run Stop to log the total time that your model has run to a global variable, you can also consider logging it every time an item enters the Sink. By overwriting the variable each time, at the end of the replication, you will have the last entry time in your global variable.

It would sometimes be nice to have some way to make the model stop as soon as it "naturally" runs out of events and you are always free to post an Idea on this site about it. For example, I sometimes build a stop(0) command into the model somewhere, then when I run the Experimenter I log "time()" as a PFM.

Note that such a solution would still not cover other common cases. A common issue is that some "old" graphs or repeating user events keep on generating periodic events even when nothing interesting is happening in the model anymore, and for these models you would still have to build in a stop condition manually.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

alan.zhang avatar image alan.zhang commented ·

Mischa, Thank you for answering this. We are trying to find a generic way to execute code for whatever reason when simulation stops. Your suggestions are certainly valid for specific cases. Viewing Phil's explanation, it appears creating stopping conditions case by case is the only way.

0 Likes 0 ·
Jeff Nordgren avatar image
0 Likes"
Jeff Nordgren answered alan.zhang commented

@Alan Zhang

Attached is a sample model of the way I would handle something like you're describing.

I create a User Event that fires just once. And set the time to be something far beyond what the model will ever run. Then I create a User Command that checks for the number of events in the event list. In the Source, in the OnReset trigger, I set a message back to the Source which calls the User Command in the OnMessage trigger.

So, when the Source calls the User Command, it will check for the number of events in the list. If there are <= 1, the model will display a message and stop. If not, it will call the OnMessage trigger of the Source again. You just need to specify the duration between calls to the Source's OnMessage trigger.

Would something like this work for you?

emptyeventlist-jnfsm.fsm


· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

alan.zhang avatar image alan.zhang commented ·

Thank you, Jeff. This is a good attempt but requires additional source object to make it work.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.