A Python project to simulate stochastic chemical reaction networks using:
- Gillespie Stochastic Simulation Algorithm (SSA)
- Tau-leaping (approximate, faster)
The simulator reads a reaction system from a YAML configuration file and produces
a time series of molecule counts (output.csv) and a plot of the dynamics (output_plot.png), like this:
- Stochastic simulation of reaction networks
- Two algorithms:
- Exact Gillespie SSA
- Approximate Tau-leaping with a fixed time step
- Configuration via a simple YAML file:
- Initial species counts
- Reactions (reactants, products, rate)
- External events at specific times (optional)
- Algorithm choice and random seed
- Exports results to CSV and visualizes them with matplotlib
- Easy to adapt to new models by changing only the configuration file
- Language: Python 3.10+
- Core libraries: NumPy, pandas, matplotlib, PyYAML
- Paradigm: discrete-event stochastic simulation
- Input format: YAML configuration files
.
├─ main.py
├─ base_simulator.py
├─ gillespie_ssa.py
├─ tau_leaping.py
├─ reaction.py
├─ state.py
├─ event.py
├─ data/
│ └─ input.yaml
├─ output.csv # generated
└─ output_plot.png # generated
- Python 3.10+
Python libraries:
numpypandasmatplotlibpyyaml
Install the dependencies with:
pip install numpy pandas matplotlib pyyaml(Using a virtual environment is recommended but not required.)
-
Clone or download this repository.
-
Install the required packages:
pip install numpy pandas matplotlib pyyaml
-
Make sure the configuration file exists at:
data/input.yaml -
Run the simulator:
python main.py
The program will:
- run the simulation until
t_end = 0.1 - save the log to
output.csv - generate and save the plot
output_plot.png
algorithm: gillespie # 'gillespie' or 'tau'
events: []
reactions:
- products:
Y1: 2
rate: 10.0
reactants:
Y1: 1
- products:
Y2: 2
rate: 0.01
reactants:
Y1: 1
Y2: 1
- products:
Z: 1
reactants:
Y2: 1
rate: 1.0
seed: 20
species:
Y1: 100
Y2: 100
Z: 0
To run the Tau-leaping algorithm instead of Gillespie SSA, change the
algorithm field in data/input.yaml:
algorithm: tau
Defines the initial number of molecules for each species (Y1, Y2, Z).
Each reaction includes:
reactants— molecules consumedproducts— molecules producedrate— reaction rate constant
The simulator calculates propensities for each reaction based on the current state and decides which reaction occurs next.
Define external interventions at specific times:
events:
- time: 0.05
changes:
Y1: 10 # add 10 molecules of Y1
Y2: -5 # remove 5 molecules of Y2- Compute propensities for all reactions.
- Sample the time to the next reaction from an exponential distribution.
- Choose which reaction fires based on probabilities.
- Update molecule counts and repeat.
Pros: Exact stochastic behavior
Cons: Slow for systems with many frequent reactions
- Use a fixed step
τ. - Estimate how many times each reaction occurs in
τ(Poisson distribution). - Apply those reactions simultaneously.
- Advance time by
τ.
Pros: Much faster
Cons: Approximate for large τ values
Shahd Amer
Master’s student in Computer Science (Software Programme), University of Pisa (UniPi)
Feel free to open issues, share feedback, or suggest improvements.
This project was developed as an individual assignment to practice stochastic simulation algorithms and scientific Python programming.
