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: RxnToODE

Lambdified RHS builders atop RxnToODE.

Resolved numeric rates feed create_ode_system(); free symbols feed create_ode_system_with_rate_consts() for optimizers.

Refer to RxnToODE for inherited attributes such as rate_consts_dict and function_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:

(ode_functions, symbolic_rate_const_keys)
  • ode_functions: Species name -> callable(t, *y, *rate_consts).

  • symbolic_rate_const_keys: List of free rate constant names in order (e.g. [‘k1’, ‘k3’] when k2=k1*2).

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 cfunc compilation.

Parameters:
  • rate_const_values – None embeds numeric substitutions; supplying a dict together with symbolic_rate_const_keys builds the augmented lambdify argument list (t, *species, *p).

  • symbolic_rate_const_keys – Required whenever rate_const_values is a dict.

Returns:

Mapping with keys expr_tuple, lambdify_args, n_state, n_p, and p_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 k1 inside k2 = 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/dt consumed by SciPy integrators.

Optional rate arguments are appended as (..., k0, k1, ...) following symbolic_rate_const_keys. Callables in rate_const_values expose 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.