Simulation back-ends

Simulations are run in C or C++ using custom back-ends built for each model on the fly. To do this, Myokit utilises a number of modules detailed here.

Back-end classes

class myokit.CModule

Abstract base class for classes that dynamically create and compile a back-end C-module.

_code(tpl, tpl_vars, line_numbers=False)

Returns the code that would be created by the equivalent call to _compile().

_compile(name, tpl, tpl_vars, libs, libd=None, incd=None, flags=None)

Compiles a source template into a module and returns it.

The module’s name is specified by name.

The template to compile is given by tpl, while any variables required to process the template should be given as the dict tpl_vars.

Any C libraries needed for compilation should be given in the sequence type libs. Library dirs and include dirs can be passed in using libd and incd. Extra compiler arguments can be given in the list flags.

_export(source, varmap, target=None)

Exports the given source to the file target using the variable mapping varmap. If no target is given, the result is returned as a string.

_source_file()

Returns a name for the source file created and compiled for this module.

class myokit.CppModule

Extends the CModule class and adds C++ support.

_code(tpl, tpl_vars, line_numbers=False)

Returns the code that would be created by the equivalent call to _compile().

_compile(name, tpl, tpl_vars, libs, libd=None, incd=None, flags=None)

Compiles a source template into a module and returns it.

The module’s name is specified by name.

The template to compile is given by tpl, while any variables required to process the template should be given as the dict tpl_vars.

Any C libraries needed for compilation should be given in the sequence type libs. Library dirs and include dirs can be passed in using libd and incd. Extra compiler arguments can be given in the list flags.

_export(source, varmap, target=None)

Exports the given source to the file target using the variable mapping varmap. If no target is given, the result is returned as a string.

class myokit.PyCapture(enabled=True)

A context manager that redirects and captures the standard and error output of the python interpreter, using pure python techniques.

_start_capturing()

Starts capturing output to stdout and stderr.

_stop_capturing()

Stops capturing output. If capturing was already halted, this does nothing.

disable()

Disables the silencing. Any capturing currently taking place is halted.

enable()

Enables the context manager and starts capturing output.

text()

Returns the captured text.

class myokit.SubCapture(enabled=True)

A context manager that redirects and captures the standard and error output of the current process, using low-level file descriptor duplication.

_start_capturing()

Starts capturing output to stdout and stderr.

_stop_capturing()

Stops capturing output. If capturing was already halted, this does nothing.

bypass()

Returns a link to stdout, allowing you to bypass capture (for example for debug output).

disable()

Disables the silencing. Any capturing currently taking place is halted.

enable()

Enables the context manager and starts capturing output.

text()

Returns the captured text.

Templating engine

Myokit comes with a tiny templating engine called “Pype” that it uses to export models to various languages and to create source files for on-the-fly compilation.

It works in a quick-and-dirty way: each file read by Pype is scanned for php-style tags <? and ?> as well as the <?= value ?> operator. Anything between these tags is left untouched, while everything around it is turned into a triple quoted python string. The result is an ugly piece of python code which, when run through the python interpreter, will print the “processed” version of the template. The final step is then to redirect the output buffer (stdout), run the script and return the caught output.

Note: It should be clear from the preceding that Pype is completely unsuitable for use in a web-based or other insecure environment. Much better packages exist for such purposes.

class myokit.pype.TemplateEngine

A tiny templating engine using a php style syntax.

Not intended for use in websites or with untrusted templates.

Basic syntax:

Hello <? print("world") ?>

Fast syntax to write expressions:

Hello <?= "world" ?>

All processed template data is printed to the standard output stream or a stream specified by the user.

error_details()

After a PypeError has been thrown, calling this will method will return a detailed (multi-line) error message.

If no PypeError occurred the return value will be None.

process(filename, variables={})

Processes the file stored at filename. Any variables required by this template should be passed in through the dict variables.

set_output_stream(stream)

When handling a template, all output will be directed into this stream. If no stream is specified, the standard output stream stdout is used.

class myokit.pype.PypeError(msg)

Extends: Exception

An error thrown by the TemplateEngine

myokit.pype.from_shell()

Parses command lines arguments to allow Pype templating from the shell.