rxnfit.build_ode module
Numerical RHS builders from symbolic reaction-network ODEs.
RxnODEbuild subclasses RxnToODE to produce
lambdified right-hand sides and create_system_rhs() closures for
SciPy-based integrators and optional numbalsoda acceleration.
- class rxnfit.build_ode.RxnODEbuild(file_path, encoding=None, rate_const_overrides=None, rate_const_overrides_encoding=None)
Bases:
RxnToODELambdified RHS builders atop
RxnToODE.Resolved numeric rates feed
create_ode_system(); free symbols feedcreate_ode_system_with_rate_consts()for optimizers.Refer to
RxnToODEfor inherited attributes such asrate_consts_dictandfunction_names.- create_ode_system()
Build numerical ODE right-hand side functions for integration.
Rate constants that are expressions (e.g. k2=k1*2) are resolved to numbers via _resolve_rate_consts when all referenced symbols have numeric values; otherwise the expression or Symbol is left in place. Functions take (t, y) with y in function_names order.
- Returns:
Species name -> callable(t, *y) returning d(species)/dt.
- Return type:
dict
- Raises:
RuntimeError – If lambdify fails for any species (fail fast).
- create_ode_system_with_rate_consts()
Build ODE functions with rate constants as explicit arguments.
Used for fitting: rate constants are passed in at call time instead of being embedded. Only “free” parameters appear: keys that are Symbol or that appear inside expression-defined constants (e.g. k1 in k2=2*k1). Keys defined only by expressions (e.g. k2) do not appear in symbolic_rate_const_keys.
- Returns:
- Return type:
tuple
- Raises:
RuntimeError – If lambdify fails for any species (fail fast).
- debug_ode_system()
Collect debug information for the ODE system.
- Returns:
- function_names, rate_constants, ode_expressions,
lambdify_args, ode_functions_info (and test results).
- Return type:
dict
- get_numbalsoda_sympy_spec(rate_const_values=None, symbolic_rate_const_keys=None)
Package symbolic RHS data for numbalsoda
cfunccompilation.- Parameters:
rate_const_values –
Noneembeds numeric substitutions; supplying a dict together withsymbolic_rate_const_keysbuilds the augmented lambdify argument list(t, *species, *p).symbolic_rate_const_keys – Required whenever
rate_const_valuesis a dict.
- Returns:
Mapping with keys
expr_tuple,lambdify_args,n_state,n_p, andp_order(possibly empty).- Raises:
ValueError – When only one of the optional arguments is provided.
- get_ode_info(debug_info: bool = False)
Print a short summary of the ODE system (and optionally debug).
- Parameters:
debug_info (bool, optional) – If True, also print detailed debug (e.g. lambdify args, ODE expressions). Defaults to False.
- get_ode_system()
Return the full ODE construction for scipy.solve_ivp.
- Returns:
- (system_of_equations, sympy_symbol_dict, ode_system,
function_names, rate_consts_dict). ode_system is from create_ode_system() (rate constants resolved or embedded).
- Return type:
tuple
- get_symbolic_rate_const_keys()
Sorted free-parameter names implied by
rate_consts_dict.Symbols tied to expressions (for example
k1insidek2 = 2*k1) participate, whereas purely numeric literals do not.- Returns:
Lexicographically ordered strings matching the lambdify argument tail.
- rxnfit.build_ode.create_system_rhs(ode_functions_dict, function_names, rate_const_values=None, symbolic_rate_const_keys=None, *, numbalsoda_context=None)
Closure
(t, y) -> dy/dtconsumed by SciPy integrators.Optional rate arguments are appended as
(..., k0, k1, ...)followingsymbolic_rate_const_keys. Callables inrate_const_valuesexpose time-dependent kinetic parameters.- Parameters:
ode_functions_dict – Species-to-lambdify mapping from
RxnODEbuild.function_names – State component order mirrored in
y.rate_const_values – Constant dict or
(t) -> dict.symbolic_rate_const_keys – Required together with
rate_const_values.numbalsoda_context – Optional hooks for JIT RHS attachment.
- Returns:
Callable with SciPy signature
system_rhs(t, y).- Raises:
RuntimeError – Missing lambdified function or RHS evaluation failure.