Metadata-Version: 2.1
Name: pypolymer
Version: 0.2.0
Summary: LDPE polymerization reactor simulation (moments + chain-length distribution) and reinforcement-learning operating-condition optimisation.
Home-page: https://github.com/sebygaa/polymer-reactor
Author: sebygaa
Author-email: sebyga@gmail.com
License: MIT
Project-URL: Homepage, https://github.com/sebygaa/polymer-reactor
Project-URL: Repository, https://github.com/sebygaa/polymer-reactor
Project-URL: Issues, https://github.com/sebygaa/polymer-reactor/issues
Keywords: polymerization,reactor,LDPE,chemical-engineering,method-of-moments,chain-length-distribution,reinforcement-learning
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Chemistry
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Provides-Extra: plot
Provides-Extra: dev
License-File: LICENSE

# pypolymer

Simulation and reinforcement-learning optimisation of free-radical
polymerization reactors (LDPE-type chemistry), in pure NumPy + SciPy.

`pypolymer` provides one reactor class, `PolyRxn`, that simulates **Batch**,
**CSTR**, and tubular **PFR** reactors by the **method of moments**, plus a
**chain-length-distribution (CLD)** batch solver that integrates every chain
length explicitly to recover the full molecular-weight distribution. A set of
REINFORCE-based helpers optimise reactor operating conditions.

## Installation

```bash
pip install pypolymer
# with plotting extras (matplotlib):
pip install "pypolymer[plot]"
```

From a local checkout:

```bash
pip install -e ".[dev]"
```

## Reaction model

Free-radical polymerization with initiator decay, propagation, chain transfer
to monomer / polymer / agent (CTA), and termination by combination and
disproportionation:

| Step | Reaction |
|------|----------|
| Initiation | `I → 2 R·`, `R· + M → P₁` |
| Propagation | `Pₙ + M → Pₙ₊₁` |
| Transfer to monomer | `Pₙ + M → Dₙ + P₁` |
| Transfer to polymer | `Pₙ + Dₘ → Dₙ + Pₘ` |
| Transfer to agent (CTA) | `Pₙ + S → Dₙ + P₁` |
| Termination (combination) | `Pₙ + Pₘ → Dₙ₊ₘ` |
| Termination (disproportionation) | `Pₙ + Pₘ → Dₙ + Dₘ` |

## Quick start

### Method-of-moments batch

```python
from pypolymer import PolyRxn

rx = PolyRxn(kd=0.8, kp=15.0, ktc=0.02, ktd=0.02, ktrm=15e-3,
             ktrp=10e-3, kca=5e-3, f=0.8, dH_p=-1.0e5, Mw_mono=28.05)
rx.set_batch_params(Tc_const=473.15, rho=1.0e4, Cp_ass=1.5, U_heat=400.0, D=0.05)

out = rx.run_batch(mono_0=8000.0, ini_0=50.0, CTA_0=20.0,
                   T_0=473.15, t_end=5.0, dt=0.01)
print(out["Mn_final"], out["Mw_final"], out["PDI_final"])
```

### Chain-length-distribution (CLD) batch

`run_batch_cld` resolves every active (`Pₙ`) and dead (`Dₙ`) chain length up to
`N_max`, so the molecular-weight distribution and its **exact** moments come out
directly — no Hulburt–Katz closure. It is isothermal and needs no
`set_batch_params`.

```python
from pypolymer import PolyRxn

rx = PolyRxn(kd=10.0, kp=2.0, ktc=1e-3, ktd=1e-3, ktrm=8e-3,
             ktrp=1e-3, kca=0.0, f=1.0, dH_p=-1.0e5, Mw_mono=28.05)

out = rx.run_batch_cld(mono_0=180.0, ini_0=5e-2,
                       t_end=20.0, dt=0.1, N_max=1400)

print(out["Mn_final"], out["Mw_final"], out["PDI_final"])  # ≈ 6100, 12500, 2.05
mwd  = out["MWD_final"]          # full chain-length distribution P + D
nvec = out["chain_length"]       # 1 .. N_max
```

**Choosing `N_max`.** Faster propagation produces longer chains; pick `N_max`
large enough that the distribution has decayed to ~0 at the boundary (check
`out["MWD_final"][-1]` is small relative to the peak). Cost grows with `N_max`
(an `N_max × N_max` outer product per RHS evaluation).

### Operating-condition optimisation (RL)

```python
from pypolymer import run_batch_rl, run_cstr_rl, run_pfr_rl

run_batch_rl(n_eps=120)          # Batch
run_cstr_rl(n_eps=120)           # CSTR
run_pfr_rl(n_eps=150, plot=True) # LDPE PFR (heavier)
```

Or from the command line:

```bash
python -m pypolymer            # Batch + CSTR optimisation
python -m pypolymer pfr        # LDPE PFR optimisation
python -m pypolymer all        # all three
```

## License

MIT — see [LICENSE](LICENSE).


