---
name: rattle
description: "Advance a conservative mechanical system while keeping fixed distances or other position constraints. RATTLE adds position and velocity corrections to velocity Verlet. It needs a fixed step, constant masses, position-dependent forces, and independent differentiable constraints."
---

# RATTLE

## 1. What this solves

A Cartesian pendulum must stay on its circle and its velocity must stay tangent to that circle. The same requirements apply to fixed bond lengths. RATTLE adds both corrections to the kick–drift–kick form of `stormer-verlet`. It gives second-order trajectories for smooth problems and is reversible and symplectic on the constrained phase space when the constraint equations are solved exactly on the local branch.

## 2. When to use

Use a fixed step with positive constant masses, a smooth autonomous force $F(q)=-\nabla V(q)$, and smooth independent constraints $g(q)=0$. Set $G(q)=Dg(q)$ with one row per constraint. Start with both $g(q_0)=0$ and $G(q_0)v_0=0$ to the requested solve tolerance. The reference accepts diagonal masses, ordinary Cartesian velocities, and dense solves for the constraint multipliers. Repeat each particle mass for all of its Cartesian coordinates.

## 3. When not to use (and what to use instead)

| Situation | Use instead |
|---|---|
| Unconstrained conservative mechanics | `stormer-verlet` for resolved position-dependent forces or `gauss-legendre-4` for canonical nonseparable systems. |
| Constraints involving velocities independently of position, dependent constraint rows, or configuration-dependent masses | Use a constrained or DAE formulation designed for that system. Find an independent constraint set before applying this RATTLE form. |
| Langevin or velocity-dependent forces | Use a constrained splitting that includes the thermostat or velocity term. For an unconstrained nonstiff ODE, use `dormand-prince-rk45`. |
| Fast unconstrained modes give instability | Reduce the step to resolve them or reconsider the formulation. The constraint solve removes only motion normal to the prescribed manifold. |

## 4. Algorithm

Let $h$ be signed and $M$ the diagonal mass matrix. Both multiplier signs below are defined by the displayed equations.

1. Make the first Verlet half kick $u=v+\tfrac h2 M^{-1}F(q)$ and the free drift $q_* = q+h u$.
2. Starting from $\lambda=0$, solve $g(q_1)=0$ with $q_1=q_*+M^{-1}G(q)^T\lambda$. A Newton update solves $[G(q_1)M^{-1}G(q)^T]\delta=g(q_1)$, then subtracts $\delta$ from $\lambda$. Hold the **old** constraint directions $G(q)$ fixed in the drift correction. The same displacement changes the half-step velocity by $M^{-1}G(q)^T\lambda/h$.
3. Form the second half kick $w=u+M^{-1}G(q)^T\lambda/h+\tfrac h2 M^{-1}F(q_1)$. Solve $[G(q_1)M^{-1}G(q_1)^T]\mu=-G(q_1)w$ and set $v_1=w+M^{-1}G(q_1)^T\mu$.
4. Return $(q_1,v_1)$ only after both constraint residuals meet the tolerance. With no constraints the two Verlet kicks and drift are sufficient, as in `stormer-verlet`.

The local root tends to the starting configuration as $h$ tends to zero. A projection using the new normal in the drift may satisfy the position equation and still change the integrator and its geometric properties.

## 5. Parameters

| Parameter | How to choose it |
|---|---|
| Signed fixed step $h$ | Resolve the fastest motion in the remaining degrees of freedom. Halve $h$ and compare at the same physical time. A zero step returns the input. |
| Constraint tolerance | The reference uses absolute infinity-norm tolerance `1e-13` for scaled position and tangency residuals. Scale each constraint by a fixed characteristic magnitude when units differ. Tighten the solve and check whether the trajectory and energy measurements change. |
| Newton limit | The reference permits 20 updates. Report a failure on a singular matrix or unconverged residual. Use a smaller step or fix the constraint geometry before restarting. |

## 6. Applying it to your code

Port the two solves with the same mass weighting. If your simulator stores momenta, convert with $p=Mv$ or derive both kicks consistently in momenta. The second force evaluation can be reused as the first force of the next step. For a large constraint graph use your project's sparse or local constraint solver. Add the code comment `rattle v0.1.0, Andersen (1983)`.

## 7. Verification

Implement `step(q, v, h, mass, force, constraints, gradients) -> (q_next, v_next)` in `port_adapter.py`. Each callback takes a coordinate vector and the gradient callback returns a sequence of rows. Run `python <skill>/checks/check_port.py` from the adapter directory and show its output. Other languages can use the formulas and fixed cases in `checks/cases.json`. The checker also measures second order, signed-step reversal, residuals, and the energy envelope of a long pendulum run.

On your problem, track $\lVert g(q)\rVert_\infty$ and $\lVert G(q)v\rVert_\infty$ separately, compare at least two time steps, and compare the peak energy errors in early and late windows. The method usually has an oscillating energy error on smooth, well-resolved conservative trajectories. Quadratic-looking convergence or a small residual alone does not establish long-time energy behaviour.

## 8. Failure signatures

| Symptom | Likely cause |
|---|---|
| Distances stay fixed but velocities develop a normal component | The final velocity correction was omitted or used the old gradient. |
| Position Newton fails or jumps to a distant root | The free drift is too large, or constraint gradients are nearly dependent. |
| Forward–backward drift grows and energy error accumulates | Check convergence of both solves, consistent mass weighting, and the constraint directions used in the drift. |
| Residuals are tiny but the trajectory is wrong | Check the sign of the force and whether the step resolves the unconstrained dynamics. |

## 9. Files and commands

`reference/rattle.py` advances one signed step. `uv run examples/pendulum.py` exercises a Cartesian pendulum. `uv run verify/check_cases.py` checks the reference, three deliberately broken ports, and an infeasible drift. To regenerate fixtures, run `uv run checks/make_cases.py`; its output is the stored JSON.

## 10. Sources

- Andersen introduced the position and velocity corrections for the Verlet update. [Andersen (1983)](https://doi.org/10.1016/0021-9991%2883%2990014-1)
- Leimkuhler and Skeel prove that the converged constrained map is symplectic (Sect. 4) and state that it is time reversible. [Leimkuhler and Skeel (1994)](https://doi.org/10.1006/jcph.1994.1085)
