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
templatewith the givenvariablesinto a module calledname, then imports it and returns a reference to the imported module.Arguments:
nameThe name of the generated module (used when importing)
templateA template to evaluate
variablesVariables to pass in to the template
libsA list of C libraries to link to, e.g.
libs=['sundials_cvodes'].libdA list of directories to search for shared library objects, or
None.incdA list of directories to search for header files, or
None.cargA list of extra compiler arguments (e.g.
carg=['-Wall']), or ``None.largA list of extra linker arguments (e.g.
larg=['-framework', 'OpenCL']), orNone.store_buildIf set to
False(the default), the method will delete the temporary directory that the module was built in.continue_in_debug_modeIf
myokit.DEBUG_SGormyokit.DEBUG_WGare set, the generated code will be printed to screen and/or written to disk, andsys.exit(1)will be called. Setcontinue_in_debug_modetoTrueto skip the exitting and keep going instead.
Returns a tuple
(module, build_path), wheremoduleis the compiled and imported module, andbuild_pathis eitherNone(the default) or the path to a temporary directory that the build files are stored in.
- _debug_show(template, variables)¶
Processes
templateand prints the output to screen.
- _debug_write(template, variables)¶
Processes
templateand writes the output to file.
- _export(template, variables, target=None, continue_in_debug_mode=False)¶
Exports a source
templatewith the givenvariablesand returns the result as a string or writes it to the file given bytarget.If
myokit.DEBUG_SGormyokit.DEBUG_WGare 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 unlesscontinue_in_debug_modeis changed toTrue.
- _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
CModuleclass 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
templatewith the givenvariablesinto a module calledname, then imports it and returns a reference to the imported module.Arguments:
nameThe name of the generated module (used when importing)
templateA template to evaluate
variablesVariables to pass in to the template
libsA list of C libraries to link to, e.g.
libs=['sundials_cvodes'].libdA list of directories to search for shared library objects, or
None.incdA list of directories to search for header files, or
None.cargA list of extra compiler arguments (e.g.
carg=['-Wall']), or ``None.largA list of extra linker arguments (e.g.
larg=['-framework', 'OpenCL']), orNone.store_buildIf set to
False(the default), the method will delete the temporary directory that the module was built in.continue_in_debug_modeIf
myokit.DEBUG_SGormyokit.DEBUG_WGare set, the generated code will be printed to screen and/or written to disk, andsys.exit(1)will be called. Setcontinue_in_debug_modetoTrueto skip the exitting and keep going instead.
Returns a tuple
(module, build_path), wheremoduleis the compiled and imported module, andbuild_pathis eitherNone(the default) or the path to a temporary directory that the build files are stored in.
- _debug_show(template, variables)¶
Processes
templateand prints the output to screen.
- _debug_write(template, variables)¶
Processes
templateand writes the output to file.
- _export(template, variables, target=None, continue_in_debug_mode=False)¶
Exports a source
templatewith the givenvariablesand returns the result as a string or writes it to the file given bytarget.If
myokit.DEBUG_SGormyokit.DEBUG_WGare 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 unlesscontinue_in_debug_modeis changed toTrue.
- _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
Noneif no compiler could be found.If
debugis set toTrue, 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 dictvariables.
- 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
TemplateEngineExtends: Exception