Physics-constrained Neural DEs for chemical reactor digital twins
Project description
ReactorTwin
Physics-Constrained Neural Differential Equations for Chemical Reactor Digital Twins
Overview
ReactorTwin is a framework for building digital twins of chemical reactors using Neural Differential Equations with hard physics constraints. It combines:
- 5 Neural DE variants: Standard, Latent, Augmented, SDE, CDE
- 4 reactor types: CSTR, Batch, Semi-batch, PFR (plug flow with Method of Lines)
- 7 hard conservation laws: Mass, energy, thermodynamics, stoichiometry, port-Hamiltonian, GENERIC, positivity
- 6 kinetics models: Arrhenius, Michaelis-Menten, Power Law, Langmuir-Hinshelwood, Reversible, Monod
- Digital twin features: EKF state estimation, 4-level fault detection, MPC control, online adaptation, meta-learning
- 10-page Streamlit dashboard for interactive simulation and analysis
Key differentiator: Architectural projection onto constraint manifolds ensures physical laws are satisfied exactly, not approximately.
Quick Start
Installation
# From PyPI
pip install reactor-twin
# With all optional dependencies
pip install reactor-twin[all]
# Or from source
git clone https://github.com/ktubhyam/reactor-twin.git
cd reactor-twin
pip install -e ".[dev]"
30-Second Example
from reactor_twin import CSTRReactor, ArrheniusKinetics, NeuralODE
import torch
# Define CSTR with A -> B kinetics
reactor = CSTRReactor(
name="my_cstr",
num_species=2,
params={"V": 100, "F": 10, "C_feed": [1.0, 0.0], "T_feed": 350},
kinetics=ArrheniusKinetics(
name="A_to_B",
num_reactions=1,
params={"k0": [1e10], "Ea": [50000], "stoich": [[-1, 1]]},
),
)
# Train Neural ODE to learn dynamics
model = NeuralODE(state_dim=2, solver="dopri5", adjoint=True)
predictions = model(z0=torch.randn(32, 2), t_span=torch.linspace(0, 10, 50))
# Predictions are 1000x faster than scipy integration!
Run the full quickstart: python examples/00_quickstart.py
Features
Neural DE Variants
| Variant | Use Case | Status |
|---|---|---|
| Neural ODE | Standard continuous-time dynamics | ✅ Complete |
| Latent Neural ODE | High-dimensional systems (encoder/decoder) | ✅ Complete |
| Augmented Neural ODE | Topology-breaking expressivity | ✅ Complete |
| Neural SDE | Uncertainty quantification (stochastic) | ✅ Complete |
| Neural CDE | Irregular sensor data | ✅ Complete |
Reactor Types
| Reactor | Description | Status |
|---|---|---|
| CSTR | Continuous stirred-tank reactor | ✅ Complete |
| Batch | Time-varying volume for gas-phase reactions | ✅ Complete |
| Semi-batch | Continuous feed + batch (pharmaceutical) | ✅ Complete |
| PFR | Plug flow with Method of Lines discretization | ✅ Complete |
Kinetics Models
| Model | Use Case | Status |
|---|---|---|
| Arrhenius | Standard temperature-dependent reactions | ✅ Complete |
| Michaelis-Menten | Enzyme-catalyzed reactions | ✅ Complete |
| Power Law | Empirical rate expressions | ✅ Complete |
| Langmuir-Hinshelwood | Heterogeneous catalysis | ✅ Complete |
| Reversible | Equilibrium-limited reactions | ✅ Complete |
| Monod | Microbial growth kinetics | ✅ Complete |
Physics Constraints
| Constraint | Hard Mode | Soft Mode | Status |
|---|---|---|---|
| Positivity | Softplus/ReLU projection | Penalty | ✅ Complete |
| Mass Balance | Stoichiometric projection | Penalty | ✅ Complete |
| Energy Balance | Soft mode only | Penalty | ✅ Complete |
| Thermodynamics | Entropy/Gibbs/equilibrium | Penalty | ✅ Complete |
| Stoichiometry | Predict rates not species | Penalty | ✅ Complete |
| Port-Hamiltonian | Structure-preserving | N/A | ✅ Complete |
| GENERIC | Reversible-irreversible coupling | Penalty | ✅ Complete |
Digital Twin Features
| Feature | Description | Status |
|---|---|---|
| State Estimation | EKF with Neural ODE fusion, autograd Jacobians | ✅ Complete |
| Fault Detection | 4-level: SPC, residual, isolation, ML classification | ✅ Complete |
| MPC Control | Gradient-based with constraint handling | ✅ Complete |
| Online Adaptation | Replay buffer + Elastic Weight Consolidation | ✅ Complete |
| Meta-Learning | Reptile for cross-reactor transfer | ✅ Complete |
Benchmark Systems
| System | Type | Status |
|---|---|---|
| Exothermic A→B | Non-isothermal CSTR | ✅ Complete |
| Van de Vusse | Series-parallel CSTR | ✅ Complete |
| Bioreactor | Monod growth kinetics | ✅ Complete |
| Consecutive A→B→C | Selectivity optimization | ✅ Complete |
| Parallel A→B, A→C | Yield optimization | ✅ Complete |
Architecture
Plugin-Based Design
ReactorTwin uses a registry system for extensibility without modifying library code:
from reactor_twin import REACTOR_REGISTRY, AbstractReactor
# Register custom reactor
@REACTOR_REGISTRY.register("my_reactor")
class MyCustomReactor(AbstractReactor):
def ode_rhs(self, t, y, u):
# Your reactor equations
return dydt
# Use anywhere
reactor = REACTOR_REGISTRY.get("my_reactor")(...)
Project Structure
reactor-twin/
├── src/reactor_twin/
│ ├── core/ # Neural DE Engine (Neural ODE, Latent, SDE, CDE)
│ ├── physics/ # 7 hard/soft constraints
│ ├── reactors/ # 4 reactor types + 6 kinetics models + 5 benchmarks
│ ├── training/ # Training engine + losses + data generation
│ ├── digital_twin/ # EKF, fault detection, MPC, online adaptation, meta-learning
│ └── dashboard/ # 10-page Streamlit app
├── tests/ # Test suite
├── examples/ # Runnable examples
└── pyproject.toml # Package configuration
See the documentation for complete architecture details.
Dashboard
Launch the interactive Streamlit dashboard:
pip install -e ".[dashboard]"
reactor-twin-dashboard
# Or: streamlit run src/reactor_twin/dashboard/app.py
10 pages: Reactor Simulator, Phase Portraits, Bifurcation Diagrams, RTD Analysis, Parameter Sweeps, Sensitivity Analysis, Pareto Optimization, Fault Monitoring, Model Validation, Latent Space Explorer.
Benchmarks
Accuracy (vs ground truth):
- CSTR exothermic: RMSE < 5% (target), < 1% (stretch)
- PFR 1D: RMSE < 8% (target), < 3% (stretch)
Physics Compliance:
- Mass balance error: < 10^-8 (hard), < 10^-3 (soft)
- Positivity violations: 0 (hard), < 0.1% (soft)
Speed:
- Single trajectory: < 5 ms (100x faster than scipy)
- Parameter sweep (10K conditions): < 5 s (1000x faster)
- MPC optimization: < 100 ms (real-time capable)
Development
Setup
# Install development dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
Testing
# Run tests
pytest
# With coverage
pytest --cov=reactor_twin --cov-report=html
Linting & Formatting
# Lint with ruff
ruff check src/
# Format with ruff
ruff format src/
# Type check with mypy
mypy src/
Roadmap
- Phase 1 (Weeks 1-2): Core Neural ODE + CSTR benchmark ✅
- Phase 2 (Weeks 3-4): 7 physics constraints + training infrastructure ✅
- Phase 3 (Weeks 5-6): Advanced DEs (Latent/Augmented/SDE/CDE) ✅
- Phase 4 (Weeks 7-8): Batch, Semi-batch, PFR + 4 kinetics + 3 benchmarks ✅
- Phase 5 (Weeks 9-10): Digital twin features + 10-page dashboard ✅
- Phase 6 (Weeks 11-12): Tests, examples, docs, PyPI release ✅
See ROADMAP.md for details.
Citation
If you use ReactorTwin in your research, please cite:
@software{reactortwin2026,
author = {Karthikeyan, Tubhyam},
title = {ReactorTwin: Physics-Constrained Neural Differential Equations for Chemical Reactor Digital Twins},
year = {2026},
url = {https://github.com/ktubhyam/reactor-twin}
}
License
MIT License - see LICENSE for details.
Contact
Tubhyam Karthikeyan Email: takarthikeyan25@gmail.com GitHub: @ktubhyam Website: tubhyam.dev
Acknowledgments
Built on:
- torchdiffeq - Neural ODE solver
- PyTorch - Deep learning framework
- Inspired by DeepXDE (plugin architecture) and Wang et al. 2025 (foundation models for reactors)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reactor_twin-0.3.1.tar.gz.
File metadata
- Download URL: reactor_twin-0.3.1.tar.gz
- Upload date:
- Size: 178.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac0037eed09dec5d7f7d29b3f50ea51196bc3c818ccde6e52ea46328beca5385
|
|
| MD5 |
8a946a2fa1178ee383e4f2fadd676625
|
|
| BLAKE2b-256 |
6a767e3afdc80c098e72fda4a2e95f9773e76eefee91f5a64594be9b3faa4e95
|
File details
Details for the file reactor_twin-0.3.1-py3-none-any.whl.
File metadata
- Download URL: reactor_twin-0.3.1-py3-none-any.whl
- Upload date:
- Size: 166.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4296f8fc86cadd2d0337ec7a5c281d3f3bb43526676e97d2e253f5c6f30eefc5
|
|
| MD5 |
ce9a7717d7fc54dd1f7bf77d0cdb5f73
|
|
| BLAKE2b-256 |
ffa14247637c23fe2c539c11d1af9ca29cef160cbdd320b4b45876d82503bbd0
|