Anton Kukoba
Anton Kukoba

Reputation: 293

How to stop simulation from the code?

I want my simulation to stop as soon as a specific condition in the code is met. By 'stop' I mean it should pause the simulation as if I pressed stop button in the simulation window. So that I could continue the simulation with a slower speed from this moment.

Is there a way to do it from the module code?

Upvotes: 1

Views: 1384

Answers (2)

Jerzy D.
Jerzy D.

Reputation: 7012

The alert() method is very convenient to implement @Rudi trick.
In GUI (Run, Fast, and Express) alert() pauses the simulation, shows a message box with own text, waits for user reaction, and then continues the simulation in the same mode. In Cmdenv it shows a text in console without pausing a simulation.
Here is a sample code that prints current event number using this method:

char text[128];
sprintf(text,"Event number: %lld", getSimulation()->getEventNumber());
getSimulation()->getActiveEnvir()->alert(text);

After learning this event number one should do what @Rudi has proposed in the last bullet.

Upvotes: 1

Rudi
Rudi

Reputation: 6681

There is no API for managing the GUI's run state. The reason is, that you can run the simulation also in Cmdenv without recompiling the code and it's not possible/makes no sense to pause a simulation in Cmdenv.

You can however do a trick, because the simulation is repeatable:

  • Add some code that prints out the event number when the specific condition occurred.
  • Run the simulation and note the event number it prints out when the simulation is run.
  • Now start the simulation again (with the same parameters) but use the "Run until... / Event number" and specify the event number where the GUI should stop.

Upvotes: 2

Related Questions