gillespy2.solvers.cpp.build package

Submodules

gillespy2.solvers.cpp.build.build_engine module

class gillespy2.solvers.cpp.build.build_engine.BuildEngine(debug: bool = False, output_dir: Optional[str] = None)[source]

Bases: object

build_cache(cache_dir: str, force_rebuild: bool = False)[source]

Build object dependencies and cache into directory for later use.

Parameters:
  • cache_dir (str) – The directory to use as a cache.

  • force_rebuild (bool) – Delete and rebuild the cache directory.

build_simulation(simulation_name: str, definitions: dict[str, str] = None) str[source]

Build the solver to the temp directory.

Parameters:
  • simulation_name (str) – The name of the simulation to build. For example, ODESimulation.

  • definitions – Dictionary of environment variables to be overriden when the Makefile is invoked.

Intended for use when running through a debugger environment or profiler. :type definitions: dict[str, str]

Returns:

The path of the newly build solver executable.

clean()[source]

Delete the output directory and all other associated build artifacts.

get_executable_path() str[source]

Resolves the filepath of the simulation executable. Only valid after the simulation has been built.

Returns:

String containing path to executable. None if no executable exists.

classmethod get_missing_dependencies() list[str][source]

Determine which dependencies are missing on the system, if any.

Returns:

A list of missing dependencies.

prepare(model: Union[Model, SanitizedModel], variable=False) str[source]

Prepare the template directory for compilation. The following operations will be performed:

  1. If no cached object files are found, prebuild them.

  2. Copy the C++ template directory into a new temp directory.

  3. Remove the sample template_definitions.h file.

  4. Generate and write a template_definitions.h file from the model.

Parameters:
  • model (gillespy2.Model) – A GillesPy2 model who’s template definitions will be generated.

  • variable (bool) – A template_gen argument requirement which enables support for non-constant param values.

Returns:

The path of the output directory.

template_definitions_name = 'template_definitions.h'
template_options_name = 'template_opts.h'

gillespy2.solvers.cpp.build.expression module

class gillespy2.solvers.cpp.build.expression.CppConverter(tree: AST)[source]

Bases: ExpressionConverter

class CppExpressionTransformer[source]

Bases: NodeTransformer

visit_BinOp(node: BinOp)[source]
get_str() str[source]
visit_And(node: And)[source]
visit_Or(node: Or)[source]
class gillespy2.solvers.cpp.build.expression.Expression(blacklist: Union[list[str], dict[ast.operator, str]] = None, namespace: dict[str, any] = None, sanitize=False)[source]

Bases: object

Accepts an expression string to validate and convert. Allows for pre-flight syntax and namespace validations, as well as converting between Python and C++ expressions.

class ValidationVisitor(namespace: dict[str, str] = None, blacklist: dict[ast.operator, str] = None, sanitize=False)[source]

Bases: NodeTransformer

check_blacklist(operator)[source]
visit_Assign(node: Assign)[source]
visit_BinOp(node: BinOp)[source]
visit_BoolOp(node: BoolOp)[source]
visit_Call(node: Call)[source]
visit_Compare(node: Compare)[source]
visit_Name(node: Name)[source]
visit_UnaryOp(node: UnaryOp)[source]
getexpr_cpp(statement: str) Optional[str][source]

Converts the expression object into a C++ expression string. Raises a SyntaxError if conversion to a C++ string is impossible.

Raises:

SyntaxError

Returns:

C++ expression string

getexpr_python(statement: str) Optional[str][source]

Converts the expression object into a Python expression string. Raises a SyntaxError if conversion to a Python string is impossible.

Raises:

SyntaxError

Returns:

Python expression string, if valid. Returns None if validation fails.

classmethod map_operator(operator: Union[str, list[str]])[source]
operator_map = {'!': <class '_ast.Not'>, '!=': <class '_ast.NotEq'>, '%': <class '_ast.Mod'>, '&': <class '_ast.BitAnd'>, '*': <class '_ast.Mult'>, '**': <class '_ast.Pow'>, '+': <class '_ast.Add'>, '-': <class '_ast.Sub'>, '/': <class '_ast.Div'>, '//': <class '_ast.FloorDiv'>, ':=': <class '_ast.Assign'>, '<': <class '_ast.Lt'>, '<<': <class '_ast.LShift'>, '<=': <class '_ast.LtE'>, '=': <class '_ast.Assign'>, '==': <class '_ast.Eq'>, '>': <class '_ast.Gt'>, '>=': <class '_ast.GtE'>, '>>': <class '_ast.RShift'>, '@': <class '_ast.MatMult'>, '^': <class '_ast.BitXor'>, 'and': <class '_ast.And'>, 'or': <class '_ast.Or'>, '|': <class '_ast.BitOr'>}
validate(statement: str) ExpressionResults[source]

Using the information provided so far, ensure that the given Python expression is valid. The Python expression is parsed, raising a SyntaxError if it is an invalid Python expression. The expression is then checked against the given properties, such as namespace and operator blacklist. Additionally, the expression is rejected if it is not a single rvalue expression.

Raises:

SyntaxError – The statement is not a valid Python expression.

Returns:

True if the statement is valid, otherwise returns false.

with_blacklist(blacklist: list[str] = None) Expression[source]

Create a new duplicate of the current expression, with a different operator blacklist. Overrides operator handling behavior when converting or validating the expression.

Parameters:

blacklist (list[ast.operator]) – List of operators which are not allowed.

Returns:

List of operators which, based on the given parameters, are not valid.

An empty list indicates that no invalid operators were found.

with_namespace(namespace: dict[str, any] = None) Expression[source]

Create a new duplicate of the current expression, with a different namespace. Any identifiers present in the expression which are not listed in the namespace will cause the expression to be flagged as an invalid namespace during validation.

Parameters:

namespace – A dictionary containing the namespace mappings for the expression.

The keys of the dict are expected to be the “only” valid identifiers. The values of the namespace are what the keys map to during sanitization, if used. :type namespace: dict[str, str]

Returns:

New expression containing the given namespace.

The returned expression is a copy of the current expression. :rtype: Expression

class gillespy2.solvers.cpp.build.expression.ExpressionConverter(tree: AST)[source]

Bases: NodeVisitor

classmethod convert_str(expression: str)[source]
get_str() str[source]
parse_comparison(comparator: str)[source]
parse_logical(operator: str)[source]
parse_operator(operator: str)[source]
visit_Add(node: Add)[source]
visit_BinOp(node: BinOp)[source]
visit_BoolOp(node: BoolOp)[source]
visit_Bytes(node: Bytes)[source]
visit_Call(node: Call)[source]
visit_Compare(node: Compare)[source]
visit_Constant(node: Constant)[source]
visit_Div(node: Div)[source]
visit_Ellipsis(node: Ellipsis)[source]
visit_Eq(node: Eq)[source]
visit_Gt(node: Gt)[source]
visit_GtE(node: GtE)[source]
visit_Lt(node: Lt)[source]
visit_LtE(node: LtE)[source]
visit_Mult(node: Mult)[source]
visit_Name(node: Name)[source]
visit_NameConstant(node: NameConstant)[source]
visit_NotEq(node: NotEq)[source]
visit_Num(node: Num)[source]
visit_Pow(node: Pow)[source]
visit_Str(node: Str)[source]
visit_Sub(node: Sub)[source]
visit_USub(node: USub)[source]
visit_UnaryOp(node: UnaryOp)[source]
class gillespy2.solvers.cpp.build.expression.ExpressionResults(invalid_names: set[str] = None, invalid_operators: set[str] = None, is_valid=True)[source]

Bases: object

Container struct for returning the results of expression validation. Any expression items which indicate an invalid expression are listed on an ExpressionResults instance. Empty lists indicate that the expression is valid.

class gillespy2.solvers.cpp.build.expression.PythonConverter(tree: AST)[source]

Bases: ExpressionConverter

visit_And(node: And)[source]
visit_Or(node: Or)[source]

gillespy2.solvers.cpp.build.make module

class gillespy2.solvers.cpp.build.make.Make(makefile: str, output_dir: str, obj_dir: Optional[str] = None, template_dir: Optional[str] = None)[source]

Bases: object

build_simulation(simulation_name: str, **kwargs)[source]
prebuild()[source]

gillespy2.solvers.cpp.build.template_gen module

class gillespy2.solvers.cpp.build.template_gen.SanitizedModel(model: Model, variable=False)[source]

Bases: object

Utility class for preparing a model to be passed to a C++ solver. Ensures that any sanitized mappings are consistent between different mappings. Wraps around an existing GillesPy2 model to provide sanitized mappings.

Parameters:

model (gillespy2.Model) – GillesPy2 model to produce sanitized mappings of.

function_map = {'abs': 'abs', 'round': 'round'}
get_options() Optional[dict[str, str]][source]

Creates a dictionary of C++ macro definitions for optional parameters of the model. The keys of the dictionary contain the name of the macro definition. The values of the dictionary are the values their corresponding macro should be defined to.

Returns:

Dictionary of fully-formatted macro definitions.

Return type:

dict[str, str]

get_template() dict[str, str][source]

Creates a dictionary of C++ macro definitions from the given model. The keys of the dictionary contain the name of the macro definition. The values of the dictionary are the values their corresponding macro should be defined to.

Parameters:

variable (bool) – Set to true to allow for non-constant parameter values.

Returns:

Dictionary of fully-formatted macro definitions.

Return type:

dict[str, str]

reserved_names = {'t': 't'}
use_propensity(reaction: Reaction, ode=False)[source]

Populates the given reaction’s propensities into the sanitized model. Expression conversion and sanitization are automatically applied.

Parameters:
  • reaction (gillespy2.Reaction) – Reaction to generate propensities from.

  • ode – Determines whether the stochastic propensity or ODE mass-action rate is used.

If set to true, the mass-action rate is used instead of the propensity.

use_rate_rule(rate_rule: RateRule) SanitizedModel[source]

Attach the given rate rule to the sanitized model. The rate rule will automatically be validated and sanitized before being applied.

Parameters:

rate_rule (gillespy2.RateRule) – GillesPy2 RateRule object to attach to the sanitized model.

Returns:

Pass-through of sanitized model object.

Return type:

SanitizedModel

use_reaction(reaction: Reaction) SanitizedModel[source]

Adds the given reaction to the sanitized model. Populates the name and stoichiometry matrix into the model.

Parameters:

reaction (gillespy2.Reaction) – Reaction to add to the sanitized model.

gillespy2.solvers.cpp.build.template_gen.get_model_defines(model: Model, variable=False) dict[str, str][source]

Creates a dictionary of C++ macro definitions from the given model. The keys of the dictionary contain the name of the macro definition. The values of the dictionary are the values their corresponding macro should be defined to.

Parameters:
  • model (gillespy2.Model) – Model to get the macro definitions of.

  • variable (bool) – Set to true to allow for non-constant parameter values.

Returns:

Dictionary of fully-formatted macro definitions.

gillespy2.solvers.cpp.build.template_gen.template_def_propensities(model: SanitizedModel, ode=False) dict[str, str][source]

Formats the given list of pre-sorted, pre-sanitized propensities.

Parameters:

model (SanitizedModel) – Sanitized model containing runtime definitions.

Returns:

Dictionary containing propensity macro definitions.

gillespy2.solvers.cpp.build.template_gen.template_def_rate_rules(model: SanitizedModel) dict[str, str][source]

Generates template definitions for SBML rate rules.

gillespy2.solvers.cpp.build.template_gen.template_def_reactions(model: SanitizedModel, ode=False) dict[str, str][source]

Passed dictionaries/lists are assumed to be sanitized and sorted. Formats the relevant reactions and propensities to be passed to a C++ simulation template.

Parameters:

model (SanitizedModel) – Sanitized model containing runtime definitions.

Returns:

Dictionary of macro definitions for reactions.

gillespy2.solvers.cpp.build.template_gen.template_def_species(model: SanitizedModel) dict[str, str][source]

Passed dictionaries/lists are assumed to be sanitized and sorted. Formats the relevant species data to be passed to a C++ simulation template.

Parameters:

model (SanitizedModel) – Sanitized model containing runtime definitions.

Returns:

Dictionary of macro definitions for species and data related to species.

gillespy2.solvers.cpp.build.template_gen.template_def_variables(model: SanitizedModel, variable=False) dict[str, str][source]

Formats the relevant parameters to be passed to a C++ simulation template. Passed dictionaries/lists are assumed to be sanitized and sorted.

Parameters:
  • model (SanitizedModel) – Sanitized model containing runtime definitions.

  • variable (bool) – Indicates whether the model’s variables should be modifiable at runtime.

Returns:

The result as a list of tuples containing key-value pairs to be templated.

gillespy2.solvers.cpp.build.template_gen.update_model_options(model: SanitizedModel, definitions: dict[str, str] = None) dict[str, str][source]

Generate the solver-specific options of the SanitizedModel with the given definitions. This includes both solver-defined custom definitions, as well as SBML features.

Parameters:

model – Sanitized model to populate the options into.

The model itself is not modified. :type model: SanitizedModel

Parameters:

definitions (dict[str, str]) – Dictionary of macro definition key-value pairs to pass to the model.

Returns:

Updated dictionary of macro definitions.

Includes both solver-specific definitions provided and SBML features on the model. :rtype: dict[str, str]

gillespy2.solvers.cpp.build.template_gen.write_definitions(path: str, defines: dict[str, str])[source]

Write the given key-value pairs as a C/C++ template file. Contents of the given defines dict are written as #define {key} {value} format.

Parameters:
  • path (str) – Absolute filepath of the header file to create (including the .h extension.

  • defines (dict[str, str]) – Dictionary of key-value pairs representing macro definitions to create.

gillespy2.solvers.cpp.build.template_gen.write_template(path: str, model: Model, variable=False)[source]

Write template definitions to a specified path. If the file does not exist, one is created. Definitions are written in #define KEY VALUES format.

Parameters:
  • model (gillespy2.Model) – Model to get the macro definitions of.

  • variable (bool) – Set to true to allow for non-constant parameter values.

Module contents