kups.md.integrators
¶
CSVRStep
¶
Bases: Propagator[State]
Canonical Sampling through Velocity Rescaling (CSVR) thermostat step.
Implements the Bussi-Donadio-Parrinello algorithm for canonical sampling by stochastically rescaling velocities to maintain the target temperature. This produces correct canonical ensemble sampling unlike deterministic velocity rescaling (Berendsen thermostat).
The scaling factor \(\alpha^2\) is sampled from the conditional distribution:
The algorithm uses:
where:
- \(c_1 = e^{-\Delta t/\tau}\) — exponential decay factor
- \(c_2 = (1-c_1) \cdot K_{\text{target}}/(K_{\text{current}} \cdot N_{\text{dof}})\) — correction factor
- \(R_1 \sim \mathcal{N}(0,1)\) — Gaussian random variable
- \(R_2 \sim \chi^2(N_{\text{dof}}-1)\) — chi-squared random variable
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
particles |
Lens[State, Table[ParticleId, IsCSVRParticleData]]
|
Lens to get/set indexed particle data (momenta \(\\mathbf{p}\), masses \(m\)) |
systems |
View[State, Table[SystemId, _CSVRSystemData]]
|
View to extract system data (time step \(\\Delta t\), temperature \(T\), degrees of freedom \(N_{\\text{dof}}\), thermostat time constant \(\\tau\)) |
References
Bussi, G., Donadio, D., & Parrinello, M. (2007). Canonical sampling through velocity rescaling. J. Chem. Phys., 126(1), 014101. DOI: 10.1063/1.2408420
Source code in src/kups/md/integrators.py
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
__call__(key, state)
¶
Apply CSVR stochastic velocity rescaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key for generating random noise |
required |
state
|
State
|
Current simulation state |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with rescaled momenta matching target temperature distribution |
Source code in src/kups/md/integrators.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
Flow
¶
Bases: Protocol
Protocol for position update flows with boundary conditions.
A flow defines how positions evolve under velocity updates, potentially including boundary conditions like periodic wrapping or reflections.
Source code in src/kups/md/integrators.py
__call__(state, dt, primal, tangent)
¶
Apply flow to update positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state. |
required |
dt
|
Time
|
Timestep \(\Delta t\) (units: time). |
required |
primal
|
PyTree
|
Position \(\mathbf{r}\) (units: length). |
required |
tangent
|
PyTree
|
Velocity \(\mathbf{v}\) (units: length/time). |
required |
Returns:
| Type | Description |
|---|---|
PyTree
|
Updated position (units: length). |
Source code in src/kups/md/integrators.py
IsMDState
¶
Bases: Protocol
State protocol for molecular dynamics step computation.
Source code in src/kups/md/integrators.py
MinimumImageConventionFlow
¶
Bases: Flow[State, PyTree]
Flow with periodic boundary conditions using minimum image convention.
Wraps the base flow to apply periodic boundary conditions, ensuring particles
remain within the primary simulation cell. After updating positions via the
underlying flow, applies the unit cell's wrap method to fold positions
back into the box.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
PyTree
|
JAX PyTree type for positions |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
unitcell |
View[State, UnitCell]
|
View to extract the UnitCell from state |
flow |
Flow[State, PyTree]
|
Underlying flow operator (typically euclidean_flow) |
Example
Source code in src/kups/md/integrators.py
MomentumStep
¶
Bases: Propagator[State]
Update momenta using forces according to Newton's second law.
Implements the 'B' operator in splitting schemes, applying forces to update particle momenta. This is the dynamical update step that couples to the potential energy landscape.
The momentum update follows:
where \(\mathbf{F} = -\nabla U\) is the force derived from potential energy \(U\).
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
particles |
Lens[State, Table[ParticleId, IsMomentumStepData]]
|
Lens to get/set indexed particle data (momenta \(\mathbf{p}\), forces \(\mathbf{F}\)) |
systems |
View[State, Table[SystemId, HasTimeStep]]
|
View to extract system data with time step \(\Delta t\) |
Source code in src/kups/md/integrators.py
__call__(key, state)
¶
Apply momentum update step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key (unused in this deterministic step). |
required |
state
|
State
|
Current simulation state. |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with new momenta. |
Source code in src/kups/md/integrators.py
PositionStep
¶
Bases: Propagator[State]
Update positions using velocities in molecular dynamics.
Implements the 'A' operator in splitting schemes, propagating positions forward in time using the current velocities. This is the kinematic update step in velocity Verlet and related integrators.
The position update follows:
where \(\mathbf{v} = \mathbf{p}/m\) is the velocity derived from momentum.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
particles |
Lens[State, Table[ParticleId, _PositionStepData]]
|
Lens to get/set indexed particle data (momenta \(\mathbf{p}\), positions \(\mathbf{r}\), masses \(m\)) |
systems |
View[State, Table[SystemId, HasTimeStep]]
|
View to extract system data with time step \(\Delta t\) |
flow |
Flow[State, Array]
|
Flow operator defining how positions evolve (handles boundary conditions) |
Source code in src/kups/md/integrators.py
__call__(key, state)
¶
Apply position update step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key (unused in this deterministic step). |
required |
state
|
State
|
Current simulation state. |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with new positions. |
Source code in src/kups/md/integrators.py
StochasticCellRescalingStep
¶
Bases: Propagator[State]
Stochastic cell rescaling barostat for NPT ensemble sampling.
Implements the isotropic stochastic cell rescaling algorithm (Bernetti & Bussi, 2020) that correctly samples the NPT ensemble. This first-order barostat includes a stochastic term to ensure proper volume fluctuations, unlike the Berendsen barostat which artificially suppresses fluctuations.
The algorithm scales both the simulation box and particle positions by a factor \(\mu\) determined by:
where:
- \(\tau_P\) = pressure coupling time constant
- \(P\) = instantaneous pressure
- \(P_0\) = target pressure
- \(\beta\) = isothermal compressibility
- \(k_B T\) = thermal energy
- \(V\) = box volume
- \(R \sim \mathcal{N}(0,1)\) = Gaussian random noise
The scaling is applied to both box and positions:
Important: The UnitCell must be reconstructed after scaling to ensure the cached volume is recomputed correctly.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
particles |
Lens[State, Table[ParticleId, _BarostatParticleData]]
|
Lens to get/set indexed particle data (positions \(\mathbf{r}\), momenta \(\mathbf{p}\), masses \(m\)) |
systems |
Lens[State, Table[SystemId, _StochasticCellRescalingSystemData]]
|
Lens to get/set system data (lattice vectors \(\mathbf{L}\), stress tensor \(\mathbf{W}\), time step \(\Delta t\), temperature \(T\), target pressure \(P_0\), barostat time constant \(\tau_P\), compressibility \(\beta\), minimum scale factor \(\mu_{\text{min}}\)) |
References
Bernetti, M., & Bussi, G. (2020). Pressure control using stochastic cell rescaling. J. Chem. Phys., 153(11), 114107. DOI: 10.1063/5.0020514
Source code in src/kups/md/integrators.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | |
__call__(key, state)
¶
Apply stochastic cell rescaling for pressure control.
Scales the simulation box and particle positions by a factor determined from pressure deviation and stochastic fluctuations. The UnitCell is reconstructed to ensure cached volume is updated correctly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key for generating volume fluctuation noise |
required |
state
|
State
|
Current simulation state |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with rescaled box and positions matching NPT ensemble |
Source code in src/kups/md/integrators.py
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | |
StochasticStep
¶
Bases: Propagator[State]
Langevin thermostat stochastic step with exact Ornstein-Uhlenbeck solution.
Implements the 'O' operator in the BAOAB splitting scheme. This step exactly solves the Ornstein-Uhlenbeck stochastic differential equation:
The exact solution for timestep \(\Delta t\) is:
where \(\eta \sim \mathcal{N}(0,1)\) is Gaussian white noise. This preserves the correct Maxwell-Boltzmann distribution at temperature \(T\).
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
particles |
Lens[State, Table[ParticleId, IsStochasticParticleData]]
|
Lens to get/set indexed particle data (momenta \(\mathbf{p}\), masses \(m\)) |
system |
View[State, Table[SystemId, _StochasticSysData]]
|
View to extract system data (time step \(\Delta t\), temperature \(T\), friction coefficient \(\gamma\)) |
References
Leimkuhler, B., & Matthews, C. (2013). Rational construction of stochastic numerical methods for molecular sampling. Appl. Math. Res. Express, 2013(1), 34-56. DOI: 10.1093/amrx/abs010
Source code in src/kups/md/integrators.py
__call__(key, state)
¶
Apply stochastic Ornstein-Uhlenbeck thermostat step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key for generating random noise |
required |
state
|
State
|
Current simulation state |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with thermostated momenta |
Source code in src/kups/md/integrators.py
euclidean_flow(state, dt, primal, tangent)
¶
Flow in unbounded Euclidean space without boundary conditions.
Implements simple kinematic update:
Use this for non-periodic systems or when positions are handled differently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Any
|
Current simulation state (unused but required by Flow protocol) |
required |
dt
|
Time
|
Timestep \(\Delta t\) (units: time) |
required |
primal
|
Array
|
Position \(\mathbf{r}\) (units: length) |
required |
tangent
|
Array
|
Velocity \(\mathbf{v}\) (units: length/time) |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Updated position \(\mathbf{r}_{\text{new}}\) (units: length) |
Source code in src/kups/md/integrators.py
make_baoab_langevin_step(particles, systems, derivative_computation, flow)
¶
Create BAOAB Langevin integrator for canonical (NVT) ensemble sampling.
BAOAB is a second-order splitting scheme for Langevin dynamics that provides efficient sampling of the canonical ensemble. The name comes from the sequence of operators: B (momentum kick), A (position update), O (Ornstein-Uhlenbeck), A (position update), B (momentum kick).
Algorithm steps:
- B: \(\mathbf{p}(t+\Delta t/4) = \mathbf{p}(t) + \mathbf{F}(t) \cdot \Delta t/2\) — half momentum step
- A: \(\mathbf{r}(t+\Delta t/2) = \mathbf{r}(t) + \mathbf{p}(t+\Delta t/4)/m \cdot \Delta t/2\) — half position step
- O: $\mathbf{p}(t+3\Delta t/4) = $ exact OU solution — stochastic thermostat
- A: \(\mathbf{r}(t+\Delta t) = \mathbf{r}(t+\Delta t/2) + \mathbf{p}(t+3\Delta t/4)/m \cdot \Delta t/2\) — half position step
- Compute \(\mathbf{F}(t+\Delta t)\) — force evaluation
- B: \(\mathbf{p}(t+\Delta t) = \mathbf{p}(t+3\Delta t/4) + \mathbf{F}(t+\Delta t) \cdot \Delta t/2\) — half momentum step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Lens[State, Table[ParticleId, _MDParticleData]]
|
Lens to get/set indexed particle data (momenta \(\\mathbf{p}\), positions \(\\mathbf{r}\), forces \(\\mathbf{F}\), masses \(m\)) |
required |
systems
|
View[State, Table[SystemId, _StochasticSysData]]
|
View to extract system data (time step \(\\Delta t\), thermal energy \(k_B T\), friction coefficient \(\\gamma\)) |
required |
derivative_computation
|
Propagator[State]
|
Propagator to compute forces \(\\mathbf{F}\) from state |
required |
flow
|
Flow[State, Array]
|
Flow operator for position updates (handles boundary conditions) |
required |
Returns:
| Type | Description |
|---|---|
SequentialPropagator[State]
|
SequentialPropagator implementing the BAOAB algorithm |
References
Leimkuhler, B., & Matthews, C. (2013). Rational construction of stochastic numerical methods for molecular sampling. Appl. Math. Res. Express, 2013(1), 34-56. DOI: 10.1093/amrx/abs010
Source code in src/kups/md/integrators.py
make_csvr_npt_step(particles, systems, derivative_computation, flow)
¶
Create NPT integrator for isothermal-isobaric (NPT) ensemble sampling.
Combines CSVR thermostat for temperature control with stochastic cell rescaling (Bernetti-Bussi 2020) for pressure control, integrated with velocity Verlet dynamics. This correctly samples the NPT ensemble with proper volume fluctuations.
Algorithm sequence per timestep:
- Apply CSVR velocity rescaling (temperature control)
- Velocity Verlet integration:
- \(\mathbf{p}(t+\Delta t/2) = \mathbf{p}(t) + \mathbf{F}(t) \cdot \Delta t/2\) — half momentum step
- \(\mathbf{r}(t+\Delta t) = \mathbf{r}(t) + \mathbf{p}(t+\Delta t/2)/m \cdot \Delta t\) — full position step
- Compute \(\mathbf{F}(t+\Delta t)\) — force evaluation
- \(\mathbf{p}(t+\Delta t) = \mathbf{p}(t+\Delta t/2) + \mathbf{F}(t+\Delta t) \cdot \Delta t/2\) — half momentum step
- Stochastic cell rescaling (pressure control)
- Recompute forces and stress after box/position scaling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Lens[State, Table[ParticleId, _BarostatParticleData]]
|
Lens to get/set indexed particle data (momenta \(\\mathbf{p}\), positions \(\\mathbf{r}\), forces \(\\mathbf{F}\), masses \(m\)) |
required |
systems
|
Lens[State, Table[SystemId, IsCSVRNPTSystemData]]
|
Lens to get/set system data (lattice vectors \(\\mathbf{L}\), stress tensor \(\\mathbf{W}\), time step \(\\Delta t\), temperature \(T\), target pressure \(P_0\), barostat time constant \(\\tau_P\), compressibility \(\\beta\), minimum scale factor \(\\mu_{\\text{min}}\), degrees of freedom \(N_{\\text{dof}}\), thermostat time constant \(\\tau_T\)) |
required |
derivative_computation
|
Propagator[State]
|
Propagator to compute forces \(\\mathbf{F}\) and stress tensor \(\\mathbf{W}\) from state |
required |
flow
|
Flow[State, Array]
|
Flow operator for position updates (handles boundary conditions) |
required |
Returns:
| Type | Description |
|---|---|
SequentialPropagator[State]
|
SequentialPropagator implementing the CSVR-NPT algorithm |
References
CSVR: Bussi, G., Donadio, D., & Parrinello, M. (2007). Canonical sampling through velocity rescaling. J. Chem. Phys., 126(1), 014101. DOI: 10.1063/1.2408420 SCR: Bernetti, M., & Bussi, G. (2020). Pressure control using stochastic cell rescaling. J. Chem. Phys., 153(11), 114107. DOI: 10.1063/5.0020514
Source code in src/kups/md/integrators.py
make_csvr_step(particles, systems, derivative_computation, flow)
¶
Create CSVR integrator for canonical (NVT) ensemble sampling.
Combines the CSVR thermostat with velocity Verlet integration to sample the canonical ensemble at constant temperature. The algorithm applies stochastic velocity rescaling before each velocity Verlet step.
Algorithm steps:
- Apply CSVR velocity rescaling (thermostat)
- Velocity Verlet integration:
- \(\mathbf{p}(t+\Delta t/2) = \mathbf{p}(t) + \mathbf{F}(t) \cdot \Delta t/2\) — half momentum step
- \(\mathbf{r}(t+\Delta t) = \mathbf{r}(t) + \mathbf{p}(t+\Delta t/2)/m \cdot \Delta t\) — full position step
- Compute \(\mathbf{F}(t+\Delta t)\) — force evaluation
- \(\mathbf{p}(t+\Delta t) = \mathbf{p}(t+\Delta t/2) + \mathbf{F}(t+\Delta t) \cdot \Delta t/2\) — half momentum step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Lens[State, Table[ParticleId, _MDParticleData]]
|
Lens to get/set indexed particle data (momenta \(\\mathbf{p}\), positions \(\\mathbf{r}\), forces \(\\mathbf{F}\), masses \(m\)) |
required |
systems
|
View[State, Table[SystemId, _CSVRSystemData]]
|
View to extract system data (time step \(\\Delta t\), temperature \(T\), degrees of freedom \(N_{\\text{dof}}\), thermostat time constant \(\\tau\)) |
required |
derivative_computation
|
Propagator[State]
|
Propagator to compute forces \(\\mathbf{F}\) from state |
required |
flow
|
Flow[State, Array]
|
Flow operator for position updates (handles boundary conditions) |
required |
Returns:
| Type | Description |
|---|---|
SequentialPropagator[State]
|
SequentialPropagator implementing the CSVR+Verlet algorithm |
References
Bussi, G., Donadio, D., & Parrinello, M. (2007). Canonical sampling through velocity rescaling. J. Chem. Phys., 126(1), 014101. DOI: 10.1063/1.2408420
Source code in src/kups/md/integrators.py
make_md_step_from_state(state, derivative_computation, integrator)
¶
Build a single MD integration step from a typed state.
Constructs the appropriate integrator propagator by extracting views for
particles and systems from state and wrapping them with a
MinimumImageConventionFlow
for periodic-boundary-condition-aware distance computations.
Supported integrators:
"verlet"— Velocity Verlet (NVE ensemble, no thermostat)."baoab_langevin"— BAOAB Langevin (NVT via Langevin friction/noise)."csvr"— CSVR (NVT via canonical-sampling velocity rescaling, constant volume)."csvr_npt"— CSVR-NPT (NPT via CSVR thermostat with barostat).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Lens[State, IsMDState]
|
Lens into the sub-state satisfying
IsMDState (needs |
required |
derivative_computation
|
Propagator[State]
|
Propagator that computes forces/gradients and updates the state (e.g. a wrapped potential). |
required |
integrator
|
Integrator
|
String key selecting the integration algorithm. |
required |
Returns:
| Type | Description |
|---|---|
Propagator[State]
|
Propagator that advances the |
Propagator[State]
|
simulation by one time step. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/kups/md/integrators.py
make_velocity_verlet_step(particles, systems, derivative_computation, flow)
¶
Create a velocity Verlet integrator for molecular dynamics (NVE ensemble).
The velocity Verlet algorithm is a symplectic, time-reversible integrator that provides second-order accuracy in both positions and velocities. It conserves total energy and samples the microcanonical (NVE) ensemble.
Algorithm steps:
- \(\mathbf{p}(t+\Delta t/2) = \mathbf{p}(t) + \mathbf{F}(t) \cdot \Delta t/2\) — momentum half-step
- \(\mathbf{r}(t+\Delta t) = \mathbf{r}(t) + \mathbf{p}(t+\Delta t/2)/m \cdot \Delta t\) — position full-step
- Compute \(\mathbf{F}(t+\Delta t)\) — force evaluation
- \(\mathbf{p}(t+\Delta t) = \mathbf{p}(t+\Delta t/2) + \mathbf{F}(t+\Delta t) \cdot \Delta t/2\) — momentum half-step
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
particles
|
Lens[State, Table[ParticleId, _MDParticleData]]
|
Lens to get/set indexed particle data (momenta \(\\mathbf{p}\), positions \(\\mathbf{r}\), forces \(\\mathbf{F}\), masses \(m\)) |
required |
systems
|
View[State, Table[SystemId, HasTimeStep]]
|
View to extract system data with time step \(\\Delta t\) |
required |
derivative_computation
|
Propagator[State]
|
Propagator to compute forces \(\\mathbf{F}\) from state |
required |
flow
|
Flow[State, Array]
|
Flow operator for position updates (handles boundary conditions) |
required |
Returns:
| Type | Description |
|---|---|
SequentialPropagator[State]
|
SequentialPropagator implementing the velocity Verlet algorithm |
References
Swope, W. C., Andersen, H. C., Berens, P. H., & Wilson, K. R. (1982). A computer simulation method for the calculation of equilibrium constants for the formation of physical clusters of molecules: Application to small water clusters. J. Chem. Phys., 76(1), 637-649. DOI: 10.1063/1.442716