Running long simulations

Simulations can take a long time to run. In these cases, it’s desirable to have some form of communication between the simulation back-end (running in C or C++) and the python caller. This can be used to provide user feedback about the task’s progess and prevents the application from appearing frozen.

To this end, simulations (and other long running tasks) in Myokit can implement the ProgressReporter interface. In the GUI, this allows a progress bar to be displayed. Running from the console, a ProgressPrinter can be used to periodically provide status updates.

A special reporter Timeout is provided that doesn’t provide feedback, but will cancel a simulation after a given time limit is reached.

class myokit.ProgressReporter

Interface for progress updates in Simulations. Also allows some job types to be cancelled by the user.

Many simulation types take an argument progress that can be used to pass in an object implementing this interface. The simulation will use this object to report on its progress.

Note that progress reporters should be re-usable, but the behavior when making calls to a reporter from two different processes (either through multi-threading/multi-processing or jobs nested within jobs) is undefined.

An optional description of the job to run can be passed in at construction time as msg.

enter(msg=None)

This method will be called when the job that provides progress updates is started.

An optional description of the job to run can be passed in at construction time as msg.

exit()

Called when a job is finished and the progress reports should stop.

job(msg=None)

Returns a context manager that will enter and exit this ProgressReporter using the with statement.

update(progress)

This method will be called to provides updates about the current progress. This is indicated using the floating point value progress, which will have a value in the range [0, 1].

The return value of this update can be used to signal a job should be cancelled: Return True to keep going, False to cancel.

class myokit.ProgressPrinter(digits=1)

Writes progress information to stdout, can be used during a simulation.

For example:

m, p, x = myokit.load('example')
s = myokit.Simulation(m, p)
w = myokit.ProgressPrinter(digits=1)
d = s.run(10000, progress=w)

This will print strings like:

[8.9 minutes] 71.7 % done, estimated 4.2 minutes remaining

To stdout during the simulation.

Output is only written if the new percentage done differs from the old one, in a string format specified by the number of digits to display. The digits parameter accepts the special value -1 to only print out a status every ten percent.

enter(msg=None)

This method will be called when the job that provides progress updates is started.

An optional description of the job to run can be passed in at construction time as msg.

update(f)

See: ProgressReporter.update().

class myokit.Timeout(timeout)

Progress reporter that cancels a simulation after timeout seconds.

enter(msg=None)

This method will be called when the job that provides progress updates is started.

An optional description of the job to run can be passed in at construction time as msg.

update(progress)

This method will be called to provides updates about the current progress. This is indicated using the floating point value progress, which will have a value in the range [0, 1].

The return value of this update can be used to signal a job should be cancelled: Return True to keep going, False to cancel.