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.

_compile(name, template, variables, libs, libd=None, incd=None, carg=None, larg=None, store_build=False, continue_in_debug_mode=False)

Compiles a source template with the given variables into a module called name, then imports it and returns a reference to the imported module.

Arguments:

name

The name of the generated module (used when importing)

template

A template to evaluate

variables

Variables to pass in to the template

libs

A list of C libraries to link to, e.g. libs=['sundials_cvodes'].

libd

A list of directories to search for shared library objects, or None.

incd

A list of directories to search for header files, or None.

carg

A list of extra compiler arguments (e.g. carg=['-Wall']), or ``None.

larg

A list of extra linker arguments (e.g. larg=['-framework', 'OpenCL']), or None.

store_build

If set to False (the default), the method will delete the temporary directory that the module was built in.

continue_in_debug_mode

If myokit.DEBUG_SG or myokit.DEBUG_WG are set, the generated code will be printed to screen and/or written to disk, and sys.exit(1) will be called. Set continue_in_debug_mode to True to skip the exitting and keep going instead.

Returns a tuple (module, build_path), where module is the compiled and imported module, and build_path is either None (the default) or the path to a temporary directory that the build files are stored in.

_debug_show(template, variables)

Processes template and prints the output to screen.

_debug_write(template, variables)

Processes template and writes the output to file.

_export(template, variables, target=None, continue_in_debug_mode=False)

Exports a source template with the given variables and returns the result as a string or writes it to the file given by target.

If myokit.DEBUG_SG or myokit.DEBUG_WG are set, the method will print the generated code to screen and/or write it to disk. Following this, it will terminate with exit code 1 unless continue_in_debug_mode is changed to True.

_export_inner(template, variables, target=None, continue_in_debug_mode=False)

Internal version of _export().

_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.

_compile(name, template, variables, libs, libd=None, incd=None, carg=None, larg=None, store_build=False, continue_in_debug_mode=False)

Compiles a source template with the given variables into a module called name, then imports it and returns a reference to the imported module.

Arguments:

name

The name of the generated module (used when importing)

template

A template to evaluate

variables

Variables to pass in to the template

libs

A list of C libraries to link to, e.g. libs=['sundials_cvodes'].

libd

A list of directories to search for shared library objects, or None.

incd

A list of directories to search for header files, or None.

carg

A list of extra compiler arguments (e.g. carg=['-Wall']), or ``None.

larg

A list of extra linker arguments (e.g. larg=['-framework', 'OpenCL']), or None.

store_build

If set to False (the default), the method will delete the temporary directory that the module was built in.

continue_in_debug_mode

If myokit.DEBUG_SG or myokit.DEBUG_WG are set, the generated code will be printed to screen and/or written to disk, and sys.exit(1) will be called. Set continue_in_debug_mode to True to skip the exitting and keep going instead.

Returns a tuple (module, build_path), where module is the compiled and imported module, and build_path is either None (the default) or the path to a temporary directory that the build files are stored in.

_debug_show(template, variables)

Processes template and prints the output to screen.

_debug_write(template, variables)

Processes template and writes the output to file.

_export(template, variables, target=None, continue_in_debug_mode=False)

Exports a source template with the given variables and returns the result as a string or writes it to the file given by target.

If myokit.DEBUG_SG or myokit.DEBUG_WG are set, the method will print the generated code to screen and/or write it to disk. Following this, it will terminate with exit code 1 unless continue_in_debug_mode is changed to True.

_export_inner(template, variables, target=None, continue_in_debug_mode=False)

Internal version of _export().

_source_file()

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

class myokit.Compiler

Tests for distutils C-compilation support.

static info(debug=False)

Returns a string with information about the compiler found on this system, or None if no compiler could be found.

If debug is set to True, compilation errors will be printed to stdout.

myokit.pid_hash()

Returns a positive integer hash that depends on the current time as well as the process and thread id, so that it’s likely to return a different number when called twice.

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(message)

An error thrown by the TemplateEngine

Extends: Exception