4.2. stochsim
submodule¶
- class mod.stochsim.Simulator(*, labelSettings: mod.LabelSettings = mod.LabelSettings(mod.LabelType.String, mod.LabelRelation.Isomorphism), graphDatabase: List[mod.Graph], expandNetwork: Callable[[mod.DG.Builder, List[mod.Graph], List[mod.Graph]], None], initialState: Dict[mod.Graph, int], draw: Callable[[mod.DG], DrawFunction] = DrawMassAction(), drawTime: Callable[[float], float] = DrawTimeExponential(), withSetCompare: bool = True)¶
The main class for performing stochasim simulations.
- Parameters:
labelSettings (LabelSettings) – a settings object that will be passed to
mod.DG.__init__()
.graphDatabase (list[mod.Graph]) – a list of graphs that will be passed to
mod.DG.__init__()
.expandNetwork (Callable[ [DG.Builder, list[mod.Graph], list[mod.Graph]], None]) –
a callback which will be called when the simulator needs new hyperedge/reaction information. The callback will be given:
the DG builder object for the underlying derivation graph.
a list of graphs that were newly discovered in the last iteration.
a list of all graphs in the current state.
For example, if expansion is done via some
mod.DGStrat
,strat
, a reasonable callback could bedef expandNetwork(b, subset, universe): b.execute(addSubset(subset) >> addUniverse(universe) >> strat, verbosity=0)
The
ExpandByStrategy
class is a shorthand for this type of callback.initialState (dict[mod.Graph, int]) – the initial simulation state in terms of the number of copies of each graph/molecule. Graphs/molecules not mentioned are set to 0.
draw (Callable[[DG], DrawFunction]) – The simulator will initially create a
mod.DG
which is given to this function. It must then return a callable that is used in each simulation step to draw the next hyperedge/reaction. Defaults to a default constructed instance ofDrawMassAction
.drawTime (Callable[[float], float]) – The function to use for drawing the time increment in each simulation step. The function will be given the reactivity, and must return the time increment. Defaults to an instance of
DrawTimeExponential
.withSetCompare (bool) – Whether to skip network expansion if a larger subset of graphs has been used for expansion before. Defaults to
True
.
- onDeadlock¶
- onIterationBegin¶
- onNewState¶
- onRecompute¶
- onRecomputeAvoided¶
- simulate(*, time: float | None = None, advanceToEndTime: bool = False, iterations: int | None = None) mod.causality.EventTrace ¶
Start/continue the simulation.
Simulate an additional amount of time or number of iterations, whichever is reached first.
- Parameters:
time (Optional[float]) – the additional amount of time to simulate, or
None
for unbounded.advanceToEndTime (bool) – if a time bound is given and the simulation stops due to this bound, advance the current time to the time bound, instead of staying at the time of the last event.
iterations (Optional[int]) – the additional number of iterations to simulate, or
None
for unbounded.
- Returns:
a references to the event trace for the entire simulation. It should not be modified.
- Return type:
- class mod.stochsim.ExpandByStrategy(strat: mod.DGStrat)¶
A shorthand for an expansion callback that executes a strategy.
- Parameters:
strat (DGStrat) – a strategy.
- strat¶
- class mod.stochsim.DrawFunction¶
An abstract base class for draw functions.
When constructing a simulator, a draw function creator must be given. When that is invoked, it must return an object that conforms to the interface of this class.
- draw(marking: mod.causality.Marking) Tuple[mod.causality.Action, float] ¶
Called in order to draw the next edge event.
- class mod.stochsim.DrawMassAction(*, inputRate: Callable[[mod.DG.Vertex], Tuple[float, bool]] | Tuple[float, bool] = (0.0, True), reactionRate: Callable[[mod.DG.HyperEdge], Tuple[float, bool]] | Tuple[float, bool] = (1.0, True), outputRate: Callable[[mod.DG.Vertex], Tuple[float, bool]] | Tuple[float, bool] = (0.0, True))¶
A creator for a drawing function implementing the law of mass action.
- Parameters:
inputRate (Callable[[DG.Vertex], Tuple[float, bool]] or Tuple[float, bool]) – the rate used for pseudo-reactions for creating molecules. It may be a callback or a constant rate. Though, the return value (or constant value) must be a pair with the first entry being the rate, and the second entry a boolean indicating whether rate should be cached.
reactionRate (Callable[[DG.Vertex], Tuple[float, bool]] or Tuple[float, bool]) – a callback for reaction rates, Though, the return value (or constant value) must be a pair with the first entry being the rate, and the second entry a boolean indicating whether rate should be cached.
outputRate (Callable[[DG.Vertex], Tuple[float, bool]] or Tuple[float, bool]) – the rate used for pseudo-reactions for destroying molecules. It may be a callback or a constant rate. Though, the return value (or constant value) must be a pair with the first entry being the rate, and the second entry a boolean indicating whether rate should be cached.
- __call__(dg: mod.DG) mod.causality.MassActionKinetics ¶
- Parameters:
dg (DG) – the derivation graph underlying the simulation.
- Returns:
a drawing function implementing the law of mass action.
- Return type:
- inputRate = (0.0, True)¶
- outputRate = (0.0, True)¶
- reactionRate = (1.0, True)¶