How adicksonlab/openrxn works
1. What this repository is
OpenRXN is a Python library for building and simulating chemical reaction networks with compartment structure. The core architecture separates domain modeling (species, reactions, compartments, connectivity) from simulation engines (stochastic Gillespie and deterministic ODE), with examples showing how to assemble diffusion, reaction-diffusion, and birth/death systems.
2. Primary abstraction layer: compartments and arrays
The src/openrxn/compartments package is the spatial/discrete topology layer. It appears to define:
• a base compartment abstraction,
• identifier utilities (ID.py),
• array-based compartment layouts (arrays.py) for 1D/2D/3D grids.
This is the main boundary for representing “where” chemistry happens, independent of how it is simulated.
3. Connectivity between compartments
connections.py is the coupling layer that links compartments. This is where diffusion-like transport or adjacency relationships likely live. Architecturally, it sits between the spatial compartment model and the reaction/simulation layers, enabling a graph of connected reaction domains.
4. Reaction and model definition
reactions.py and model.py define the chemistry itself: species, reaction rules, and the assembled network/model object. model_templates.py likely provides convenience constructors for common patterns such as reaction-diffusion setups or standard kinetic motifs. This layer is the “source of truth” for system structure before simulation begins.
5. Numerical system engines
The src/openrxn/systems package is the execution layer. It contains two major simulation modes:
• GillespieSystem.py for stochastic discrete-event simulation,
• ODESystem.py for deterministic rate-equation integration.
system.py likely provides shared orchestration and interfaces, while state.py holds the evolving concentrations/counts and deriv.py likely computes ODE right-hand sides.
6. How model data flows into simulation
The normal flow is: define compartments/species/reactions in the model layer, connect compartments, then instantiate a system backend that transforms the model into a runtime state representation. The Gillespie path uses event-driven state updates, while the ODE path converts the same structural model into derivative calculations suitable for SciPy-style integration.
7. Diffusion and transport handling
propagators.py is likely the place where motion or transfer between compartments is translated into update rules or probability/flux operators. In practice, this is the bridge between a compartment graph and the two simulation backends, especially for reaction-diffusion examples.
8. Reporting and observability
systems/reporters.py suggests a results/output layer that records trajectories, time series, and possibly derived quantities. This is the boundary where internal simulation state becomes analyzable output, likely feeding pandas/numpy arrays and plotting workflows in the examples.
9. Runtime and library dependencies that matter
The architecture depends on:
• Pint for unit-aware quantities throughout the model,
• NumPy/SciPy for numerical state storage and ODE integration,
• NetworkX for graph visualization of compartment connectivity,
• pandas/matplotlib for analysis and plotting.
These are not incidental; they shape how state is represented, solved, and inspected.
10. Examples as integration points
The examples/ directory demonstrates the intended composition patterns:
• pure diffusion,
• reaction-diffusion,
• Gillespie stochastic systems,
• birth/death kinetics,
• degradation ODEs,
• membrane slab modeling.
These examples show that the package is designed as a modeling toolkit rather than a single-purpose solver.
11. Likely architectural boundaries
A useful diagram should show four clear zones:
• Domain model: compartments, reactions, model assembly,
• Topology/connectivity: arrays and connection graph,
• Simulation engines: stochastic vs deterministic backends,
• Outputs/analysis: reporters, plotting, and saved trajectories.
The key design idea is that the same chemical network can be executed by different solvers without changing the model definition.
12. Packaging and layout
The project uses a standard src/ layout with a single openrxn package and a top-level setup.py, indicating a distributable library rather than an application. That means the architecture is centered on reusable modules and example scripts, not a long-running service or UI.
Main entry points: model.py Model · GillespieSystem.py Gillespie · ODESystem.py ODE · compartment.py Compartment
Components 18
Domain model
Chemical network definitions and assembly.
Topology
Compartment structure and connectivity.
- Compartment · Core compartment object representing a reaction domain.
src/openrxn/compartments/compartment.py - IDs · Helpers for compartment and model identifiers.
src/openrxn/compartments/ID.py - Arrays · Array-based 1D/2D/3D compartment layouts.
src/openrxn/compartments/arrays.py - Connections · Links compartments and encodes adjacency or transport.
src/openrxn/connections.py
Simulation runtime
Shared state and solver backends.
- Propagators · Movement and transfer rules between compartments.
src/openrxn/propagators.py - System · Shared simulation orchestration and interfaces.
src/openrxn/systems/system.py - State · Evolving counts or concentrations during simulation.
src/openrxn/systems/state.py - Derivatives · Computes time derivatives for deterministic simulation.
src/openrxn/systems/deriv.py - Gillespie · Discrete-event stochastic simulation backend.
src/openrxn/systems/GillespieSystem.py - ODE · Deterministic rate-equation integration backend.
src/openrxn/systems/ODESystem.py
Outputs
Trajectory capture and analysis boundaries.
- Reporters · Records trajectories and outputs analysis-ready data.
src/openrxn/systems/reporters.py
Examples
Usage patterns demonstrating the library.
- Diffusion · Pure diffusion and transport example.
examples/1D_diffusion.py - Reac-diff · Reaction-diffusion example systems.
examples/1D_reac_diffusion.py - Stochastic · Gillespie-driven chemical system example.
examples/Gillespie_AB_system.py - ODE demo · Deterministic degradation and birth/death examples.
examples/degradation_ode.py
Connections 24
- Arrays → Compartment: builds · Array layouts instantiate compartment instances.
- IDs → Compartment: names · Identifier utilities support compartment identity.
- Compartment → Connections: links · Compartments participate in connectivity graphs.
- Connections → Propagators: drives · Connectivity is converted into transport logic.
- Reactions → Model: defines · Reaction rules are assembled into the model.
- Compartment → Model: contains · The model includes compartment structure.
- Connections → Model: couples · The model stores inter-compartment coupling.
- Templates → Model: creates · Templates generate common model configurations.
- Model → System: loads · Systems consume the assembled model.
- System → State: manages · The runtime owns evolving simulation state.
- Model → Gillespie: executes via · Stochastic backend runs the same structural model.
- Model → ODE: executes via · Deterministic backend runs the same structural model.
- Gillespie → State: updates · Event-driven state transitions.
- ODE → Derivatives: evaluates · ODE solver uses derivative calculations.
- Derivatives → State: reads · Derivative computation uses current state.
- Propagators → Gillespie: feeds · Transport rules can inform stochastic updates.
- Propagators → ODE: feeds · Transport rules can inform deterministic flux terms.
- Gillespie → Reporters: emits · Stochastic trajectories are recorded for analysis.
- ODE → Reporters: emits · Deterministic trajectories are recorded for analysis.
- Diffusion → Arrays: demonstrates · Shows grid-based compartment layouts.
- Diffusion → Connections: demonstrates · Shows diffusion coupling between compartments.
- Reac-diff → Model: demonstrates · Shows reaction-diffusion model assembly.
- Stochastic → Gillespie: demonstrates · Shows stochastic execution path.
- ODE demo → ODE: demonstrates · Shows deterministic execution path.