Metadata-Version: 2.4
Name: pymonge
Version: 0.1.0
Summary: Minimum-work transport protocols for trapped active colloids near a wall (Ritz method with Adam and a genetic algorithm)
Author: Utkarsh Maurya, Kavya Swaminathan, Ejaz Ashraf, Rajesh Singh
Keywords: stochastic thermodynamics,optimal control,active matter,optical tweezers
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.22
Requires-Dist: matplotlib>=3.5
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# pymonge

Minimum-work transport protocols for trapped colloidal particles, found with the
**Ritz method**: the trap protocol λ(t) is expanded in Chebyshev polynomials and
its coefficients are optimised with **Adam** (exact pathwise gradients through
the stochastic integrator) or a **genetic algorithm**. Only the ability to
simulate stochastic trajectories is needed. The built-in model is an active
particle (stresslet) near a plane no-slip wall.

## Why Monge

Gaspard Monge (1746–1818) was a French mathematician, the founder of descriptive
geometry. In his 1781 memoir *Mémoire sur la théorie des déblais et des
remblais* he asked how to move a pile of earth into an excavation of the same
volume while doing the least total work of transport. This is the first
optimal-transport problem, and the question this package answers is of the same
kind: how to move a colloidal particle from one place to another, in a fixed
time, with the least work. The link is more than an analogy: for overdamped
dynamics, the minimum dissipation of a finite-time process is set by an
optimal-transport distance between the initial and final distributions.

Companion code for *Optimal protocols for the transport of an active particle
near a plane wall* (Maurya, Swaminathan, Ashraf & Singh).

## Install

```bash
pip install -e .            # from this folder; needs numpy and matplotlib
pip install -e .[test]      # with pytest
pytest                      # 12 tests, a few seconds
```

## Quick start

```python
import pymonge as pm

model   = pm.NearWallModel.paper(alpha=25)              # puller near a wall
problem = pm.TransportProblem(model, H0=2.0, dH=5.0)    # trap moves from 2 to 7

res = pm.optimize_adam(problem, n_trials=6, epochs=100)
lam = problem.protocol(res.coeffs.mean(0))              # full protocol, jumps included

opt = problem.evaluate(res.coeffs.mean(0), n_traj=100_000)
ss  = problem.evaluate(problem.ansatz.ramp_coeffs(), n_traj=100_000)   # bulk protocol
print(opt.mean / problem.W_star, ss.mean / problem.W_star)
```

## What is in the package

| module | contents |
|---|---|
| `model` | `NearWallModel` with pluggable `BrennerMobility` / `BulkMobility` and `StressletDrift` / `NoDrift`; `NearWallModel.paper(alpha)` builds the model of the paper |
| `protocol` | `TimeGrid`, `ChebyshevProtocol`, `PiecewiseLinearProtocol`, `schmiedl_seifert`, `schmiedl_seifert_work` |
| `simulate` | Itô–Euler–Maruyama integrator, `simulate` for trajectories, `WorkEvaluator` (Monte-Carlo ⟨W⟩ with common random numbers, multi-process) |
| `gradient` | `work_and_grad`: hand-written adjoint of the integrator; exact gradient of the discretised ⟨W⟩ with respect to every protocol value |
| `problem` | `TransportProblem`: physics, end points, time grid and ansatz in one object |
| `optimize` | `optimize_adam` (trials vectorised), `evaluate_history`, `optimize_ga` (two-stage genetic algorithm) |
| `plotting` | small matplotlib helpers used by the examples |

## Folders

| folder | contents |
|---|---|
| `src/pymonge/` | the package |
| `tests/` | unit tests (derivatives, adjoint against finite differences, bulk limit, reproducibility) |
| `examples/` | four short tutorials; each takes `--quick` |
| `paper/` | scripts, data and figures of the paper; see `paper/README.md` |

Examples:

| file | what it shows |
|---|---|
| `01_bulk_validation.py` | Adam started from a straight line recovers the Schmiedl–Seifert ramp and W* = 6.25 |
| `02_activity_near_wall.py` | pushers, passive particles and pullers close to the wall |
| `03_symmetry_breaking.py` | away against towards transport |
| `04_custom_physics.py` | adding your own drift (gravity) to the model |

## Conventions

**Notation.** Lengths are in units of the particle radius. In the paper the
initial trap position is Λ₀ and the displacement ΔΛ; in the code these are the
arguments `H0` and `dH` of `TransportProblem` (the name is historical: `H0`
is a trap position, not a particle height).

**Activity.** `alpha > 0` is a puller, whose wall-induced drift is towards the
wall; `alpha < 0` is a pusher, drifting away from the wall.

**Dynamics.** `dh = [−μ(h) k (h − λ) + v_A(h) + k_BT μ'(h)] dt + √(2 k_BT μ(h)) dB`
(Itô; the term k_BT μ' makes a passive particle relax to the Boltzmann distribution).

**Discretisation.** A protocol on `n_steps` steps is an array of length
`n_steps + 2`: `[λ_i, λ(t_1), …, λ(t_N), λ_f]`. At each step the trap moves
first, with the particle fixed, and then the particle moves for `dt`. The end
points are not part of the Chebyshev expansion, so the jumps at t = 0 and
t = t_f come out of the optimisation.

**Wall.** Trajectories are reflected at contact, `h_min = 1`. Change it with
`NearWallModel(..., h_min=...)`.

**Initial condition.** `TransportProblem(initial=...)`: `"boltzmann"` (default;
equilibrium in the initial trap, cut off at the wall), `"relaxed"` (relaxed with
the active dynamics) or `"trap"` (all particles at the trap centre). Near the
wall the work depends noticeably on this choice.

**Common random numbers.** `WorkEvaluator` with the same `seed` uses the same
initial positions and noise for every protocol, so differences between
protocols are resolved far better than each absolute value (whose standard
error is about 0.002 W* at 100,000 trajectories).

## Optimiser settings

Adam defaults (`AdamConfig`): 1000 trajectories per step, learning rate 0.05
with cosine decay to 10⁻³, gradient-norm clipping at 5, β₁ = 0.9, β₂ = 0.999,
initial protocol equal to the bulk ramp plus Gaussian noise of standard
deviation 0.05. The genetic algorithm (`GAConfig`) uses 150 individuals, 30
generations at 500 trajectories followed by 70 at 2000, 10% elites, tournament
size 10 and mutation annealed from 0.2 to 0.005; `hifi_top` and `elite_traj`
set how many candidates are re-evaluated at high fidelity in each generation.
 
