Skip to main content

ReactorTwin

Physics-Constrained Neural Differential Equations for Chemical Reactor Digital Twins

PyPI version Python 3.10+ License: MIT CI Documentation codecov Code style: ruff


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:

Metadata

Release files for reactor-twin 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reactor-twin 1.0.0
File Size Uploaded Provenance Actions
reactor_twin-1.0.0.tar.gz 232.0 kB With provenance Details for reactor_twin-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reactor-twin 1.0.0
File Interpreter ABI Platform Actions
reactor_twin-1.0.0-py3-none-any.whl Python 3 none any Details for reactor_twin-1.0.0-py3-none-any.whl

Total release size: 417.3 kB

Release files / reactor_twin-1.0.0.tar.gz

Download URL reactor_twin-1.0.0.tar.gz
Size 232.0 kB
Tags Source
SHA-256 checksum
How to use checksums
064bc3c4ea4b71ee8f9ade7036ce37167b6ca4bbfcae15d1af3c0c9aac9acdc7
BLAKE2b-256 checksum
How to use checksums
a21f14f46c9e9a29fcd7af95a50a2a13a639095528843a88c56bde7a16f27776
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.

Transparency log

Release files / reactor_twin-1.0.0-py3-none-any.whl

Download URL reactor_twin-1.0.0-py3-none-any.whl
Size 185.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bbb23d6b2268ae0f80ef30e1a2bbb0694b0329a0da198daeafc2cac4d5bc6d81
BLAKE2b-256 checksum
How to use checksums
ccba4ef53d40993332924b96b3bf6fd5d61084ec1f89c682452e1ddbed606d0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.

Transparency log

Security for reactor-twin 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for reactor_twin-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page