Compile¶
compile
¶
op_system.compile.
Compile normalized RHS specifications into efficient, runnable callables.
This module is domain-agnostic and intentionally does not import flepimop2.
Contract¶
- Accepts
NormalizedRhs(fromop_system.specs) and produces aCompiledRhsobject containing aneval_fnsuitable for numerical backends. - Raises built-in exceptions with standardized messages.
Security note¶
This module uses eval() on code objects compiled from user-provided strings.
To reduce risk, expressions are parsed and validated with a conservative AST
whitelist, and evaluation runs with empty builtins.
BodyEvalFn
¶
Bases: Protocol
Callable that evaluates history signal bodies at (t, y, **params).
Returns a mapping from signal_id to the evaluated body array/value.
CompiledReactant(state_base, state_axes, full_axes, pinned, order)
dataclass
¶
Molecular reactant metadata aligned to an expanded reaction channel.
The record is numerical-array neutral. Providers combine state_axes
with the enclosing reaction's channel coordinates and pinned indices
to locate a state cell, then place order in their reactant-order
matrix. Catalysts appear here even when their net stoichiometric change is
zero.
CompiledReaction(name, from_base, from_axes, full_axes, to_base, to_axes, sum_axes, pinned, from_pinned, propensity_fn, reactants=(), reactants_complete=False)
dataclass
¶
Compiled propensity + axis bookkeeping for one named transition.
Produced from :class:op_system._reactions.ReactionArtifactIR at
compile time. A firing event at from-cell index i (in the natural
N-D shape given by from_axes) depletes 1 from from_base at that
cell and adds 1 to to_base at the cell obtained by: keeping every
axis in to_axes at i's value for that axis, and using the fixed
coordinate index from pinned for every axis in pinned (this
includes both axes that are wildcard on from_axes and pinned on
to -- a "collapse to a fixed target" transition, where multiple
source cells share one destination and the axis also appears in
sum_axes -- and axes pinned on BOTH from and to, a
"point-to-point" shift between two specific coordinates on the same
axis, e.g. a vaccination-dose-progression transition, which do NOT
appear in sum_axes since there's only ever one source value on
that axis). Consumers that need a single combined scatter/reduce
target (e.g. an engine applying many simultaneous firings) sum over
sum_axes when depositing into to_base -- op_system does not do
that summation itself, since whether/how to combine simultaneous
firings across a summed axis is an execution-semantics decision for
the consumer, not a compile-time one.
from_base is None for a SOURCE-ONLY (from: null) reaction
-- an exogenous hazard with no compartment to deplete (e.g.
cross-district case importation). A consumer must special-case this:
skip every from-side depletion/clamp step and apply only the
to_base scatter, using from_axes/full_axes (which are the
to-side template's own axes in this case, not a nonexistent
from-side one -- see ReactionArtifactIR).
CompiledRhs(state_names, param_names, eval_fn, meta=(lambda: MappingProxyType({}))(), operators=tuple(), factorize_axes=tuple(), block_axes=tuple(), pytree_eval_fn=None, template_shapes=None, block_pytree_eval_fn=None, block_template_shapes=None, history_requirements=tuple(), history_eval_fn=None, body_eval_fn=None, block_history_eval_fn=None, block_body_eval_fn=None, reactions=tuple(), _rhs=None)
dataclass
¶
Container for a compiled RHS evaluation function.
Instances produced by :func:compile_rhs retain a private reference to
their source :class:NormalizedRhs so the container can be pickled and
re-hydrated by re-running the compile pipeline on load. eval_fn
itself is a closure (and on the vectorized path captures compiled code
objects), so it is dropped from the pickle and rebuilt by
:func:compile_rhs in :meth:__setstate__. Round-tripping a
CompiledRhs therefore costs one compile on load and yields a
functionally equivalent instance whose eval_fn produces identical
outputs for identical inputs.
__getstate__()
¶
Return picklable state.
The compiled eval_fn is a closure (and on the vectorized path
captures compiled :class:types.CodeType objects), which is not
portably picklable. Instead we serialize just the source
:class:NormalizedRhs and let :meth:__setstate__ recompile.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the source |
Source code in src/op_system/compile.py
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
__setstate__(state)
¶
Restore by recompiling from the pickled :class:NormalizedRhs.
Source code in src/op_system/compile.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
bind(params)
¶
Bind parameter values and return a 2-arg RHS: rhs(t, y) -> dydt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Mapping[str, object]
|
Mapping of parameter names to values. |
required |
Returns:
| Type | Description |
|---|---|
Callable[[object, object], Float64Array]
|
A callable |
Source code in src/op_system/compile.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
EvalFn
¶
Bases: Protocol
Callable RHS evaluator supporting runtime parameter kwargs.
Accepts a flat (n_state,) state array and returns a flat
(n_state,) derivative array in the same array namespace.
HistoryEvalFn
¶
Bases: Protocol
Callable RHS evaluator with history provider support.
Accepts y as a StateDict and a history_provider object
implementing a query(signal_id, body, **options) method.
Returns a StateDict of derivatives.
PytreeEvalFn
¶
Bases: Protocol
Callable RHS evaluator operating on shaped PyTree state dicts.
Accepts y as a StateDict (mapping from state-template base name
to a shaped array with the template's natural N-D shape) and returns a
StateDict of the same structure containing the derivative.
Enables the engine to skip the flatten/unflatten step entirely and
expose the full tensor structure to JAX/XLA.
ReactionPropensityFn
¶
Bases: Protocol
Callable propensity evaluator for one named reaction/transition.
Accepts y as a StateDict and returns an array shaped like the
reaction's from_axes (one independent rate per source cell) --
see :class:CompiledReaction.
compile_rhs(rhs, *, xp=None)
¶
Compile a normalized RHS into a runnable evaluation function.
Always uses the vectorized eval path that operates on shaped buffers
(one tensor expression per state template) for specs that declare axes.
Specs without axes (genuinely scalar models) fall back to the scalar path.
Raising :class:UnsupportedFeatureError if an axis-indexed spec cannot be
vectorized, rather than silently falling back to the catastrophically slow
scalar path.
The returned eval_fn is namespace-polymorphic: it infers its
array namespace from the input y at call time
(through array_api_compat.array_namespace), and returns arrays in that same
namespace. Calling it with JAX arrays (or tracers) yields a JAX-native
computation suitable for jax.jit / jax.vmap without any
correctness wrapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
NormalizedRhs
|
Normalized RHS produced by |
required |
xp
|
object | None
|
Deprecated. Formerly the compile-time array backend
namespace. Now ignored — the namespace is resolved per call
from the input |
None
|
Returns:
| Type | Description |
|---|---|
CompiledRhs
|
A |
CompiledRhs
|
For axis-indexed specs the returned object also carries |
CompiledRhs
|
|
CompiledRhs
|
axes but the vectorizer cannot build a plan an |
CompiledRhs
|
|
CompiledRhs
|
message) rather than silently degrading to the scalar path. |
Source code in src/op_system/compile.py
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 | |