Repository navigation
Random reaction networks, Van de Vusse CSTR, and Gillespie SSA (#65) - #72
Merged
Merged
Conversation
Refs #65. Fills in koopman_graph.datasets.reaction.random_network, .cstr, and .gillespie: - random_network: sample_random_network builds compositions first (bare elements, then unique unions of existing species), enumerates every element-balanced reaction with order <= max_order and <= 2 products, and draws reactions and log-uniform rates from one seeded generator. SampledReactionNetwork records the seed and parameters. - cstr: VanDeVusseCSTR with Klatt & Engell (1998) Arrhenius kinetics (the 2A -> D pre-exponential is halved so mass action matches the literature balance), temperature and dilution-rate inputs with piecewise-constant schedules, RK4 simulation, and a Newton steady state that reproduces the benchmark operating point (c_A ~ 2.14, c_B ~ 1.09 at 114.2 C). - gillespie: seeded direct-method SSA with mass-action propensities (falling factorials, V^(1-order)) that converge to the ODE as V grows, absorbing-state and max_events handling, and last-value-hold binning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 15, 2026
…' into prop/random
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #65. Fifth PR for the reaction-network datasets. Stacked on #71 (→ #70 → #69). Merge in order #69 → #70 → #71 → this one. Each PR retargets to
mainwhen the one below it merges, and CI starts then.Changes
random_network.sample_random_network(...)builds the composition first, so every sampled reaction is element-balanced by construction.max_atomsatoms.max_orderreactants, and at most two products, enumerated in a fixed order.num_reactionscandidates without replacement and log-uniform rates, all from one seededtorch.Generator. A seed reproduces the network exactly.1 → 1isomerizations, somax_order=1gives a linear dissociation network. That network uses the exact matrix-exponential integrator.SampledReactionNetworkrecords the seed, every parameter, and the candidate count, and has a JSON-safeto_dict.cstr.van_de_vusse_cstr()/VanDeVusseCSTRmodelsA → B → Cand2 A → Dwith Klatt & Engell (1998) Arrhenius kinetics. Time is in hours and concentrations in mol/L.dc_A/dt ⊃ −k3 c_A², while mass action on2 A → Dconsumes twoAper event, so the pre-exponential here isk3⁰/2. A test checks the balance against the literature formula.F/V, either constant or as per-interval schedules.rhscomputesN r(c; T) + D (c_feed − c).simulateruns RK4 under piecewise-constant inputs, andsteady_stateuses damped Newton.F/V = 14.19 1/h,c_A0 = 5.1) reproducesc_A ≈ 2.14,c_B ≈ 1.09(Chen, Kremling & Allgöwer 1995).gillespie.gillespie_ssa(...)is the seeded direct-method SSA on molecule counts.a_j = k_j V^{1−o_j} ∏ n_s(n_s−1)…, consistent with the ODE rates inReactionSystem.reaction_rates.t_end, at an absorbing state, or atmax_events(markedtruncated=True).SSATrajectory.counts_on_grid/concentrations_on_gridandbin_to_gridhold the last value onto a regular grid.Tests
max_order=1gives a linear dissociation network, and RK4 on a sampled network conserves elements to 1e-10.dc_A/dtanddc_D/dtbalances match the literature, and simulation converges to the Newton steady state, which is linearly stable. Piecewise-constant inputs reach each regime's steady state, and batched simulation works.max_eventstruncates it, and grid binning holds the last value.Verification (macOS arm64, Python 3.13)
ruff checkandruff format --checkonsrc/ tests/pass.pytest -n auto -m "not slow and not distributed and not ray" --cov=koopman_graph --cov-fail-under=90: 3469 passed, 26 skipped, 97.27%.random_network.pyandgillespie.pyare at 100% andcstr.pyat 98%.sphinx-build -W --keep-goingpasses.🤖 Generated with Claude Code