This repository contains Python code for simulating 1D advection-diffusion equations with divergence-dependent velocity fields using Monte Carlo methods. It serves as an exploratory sandbox for
- Testing numerical strategies
- Analyzing stochastic behavior of particle systems
- Comparing simulation output to analytical solutions
- Laying the groundwork for higher-dimensional implementations
The simulation solves PDEs of the form:
∂T/∂t = ∂/∂x (D(x) ∂T/∂x) − ∂/∂x (u(x) T)
where:
- D(x) is a spatially dependent diffusion coefficient
- u(x) is a divergence-dependent drift field
Particles undergo stochastic motion with path weights governed by the PDE. The final density is estimated via weighted histograms and compared with known exact solutions where possible.
1d-pde-simulation-sandbox/
├── data/ # Output particle data and weights
│ ├── reaction/
│ │ ├── T1.0_D1.0_alpha1.0_x0_0.0/
│ │ │ ├── positions.npy
│ │ │ └── weights.npy
│ │ └── ...
│ ├── advection/
│ │ ├── T1.0_D1.0_alpha1.0_x0_0.0/
│ │ │ ├── positions.npy
│ │ │ └── weights.npy
│ │ └── ...
├── docs/ # Documentation, example plots, and reference figures
│ ├── reaction/
│ │ ├── T1.0_D1.0_alpha1.0_x0_0.0.png
│ │ └── ...
│ ├── advection/
│ │ ├── T1.0_D1.0_alpha1.0_x0_0.0.png
│ │ └── ...
├── notebooks/ # Notebooks for visualization and exploration
│ ├── visualize_reaction.ipynb
│ └── visualize_advection.ipynb
├── src/
│ ├── __init__.py
│ ├── advection.py # Advection term logic
│ ├── diffusion.py # Shared core diffusion logic
│ ├── exact_solutions.py # Compute exact solutions for both linear advection and reaction equations
│ ├── parameters.py # Defines parameters for simulation
│ ├── plotting.py # Histogram, comparison plots, etc.
│ ├── reaction.py # Reaction term logic
│ └── utils.py # Loading, saving, data wrangling
├── run_batch_reaction.py # Script to run reaction-diffusion simulation
├── run_batch_advection.py # Script to run the advection-diffusion simulation
├── requirements.txt
├── .gitignore
├── README.md
└── tests/ # Unit and validation tests
│ ├── test_diffusion.py
│ ├── test_parameters.py
│ ├── test_reaction.py
│ ├── test_run_batch_advection.py
│ └── test_run_batch_reaction.py
git clone https://github.com/maddiepr/monte-carlo-1d.git
cd monte-carlo-1dpython -m venv env
source env/bin/activate # or env\Scripts\activate on Windows
pip install -r requirements.txtpython run_reaction.py
python run_advection.pyYou can adjust parameters in Parameters.py
This is an early-stage, research-driven codebase. While core logic is functional and documented, structure and performance optimizations are ongoing. Furture versions may modularize the code further and add 2D support (see related repo:monte-carlo-2d-mapping).
Madeline "Maddie" Preston www.linkedin.com/in/madeline-preston | https://github.com/maddiepr