Skip to content

Commit a8a4bbb

Browse files
authored
Add integrators in docs (#290)
1 parent 6063d9d commit a8a4bbb

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ final_state = ts.integrate(
5858
n_steps=50,
5959
timestep=0.002,
6060
temperature=1000,
61-
integrator=ts.integrators.nvt_langevin,
61+
integrator=ts.Integrator.nvt_langevin,
6262
trajectory_reporter=dict(filenames=trajectory_files, state_frequency=10),
6363
)
6464
final_atoms_list = final_state.to_atoms()

docs/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Overview of the TorchSim API.
1616
properties.correlations
1717
elastic
1818
io
19+
integrators
1920
math
2021
models
2122
monte_carlo

torch_sim/integrators/__init__.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
Each integrator handles batched simulations efficiently using PyTorch tensors and
66
supports periodic boundary conditions.
77
8+
NVE:
9+
- Velocity Verlet integrator for constant energy simulations :func:`nve.nve_step`
10+
NVT:
11+
- Langevin thermostat integrator :func:`nvt.nvt_langevin_step`
12+
using BAOAB scheme [1]
13+
- Nosé-Hoover thermostat integrator :func:`nvt.nvt_nose_hoover_step` from [2]
14+
NPT:
15+
- Langevin barostat integrator :func:`npt.npt_langevin_step`
16+
- Nosé-Hoover barostat integrator :func:`npt.npt_nose_hoover_step` from [2]
17+
18+
References:
19+
[1] Leimkuhler B, Matthews C.2016 Efficient molecular dynamics using geodesic
20+
integration and solvent-solute splitting. Proc. R. Soc. A 472: 20160138
21+
[2] Martyna, G. J., Tuckerman, M. E., Tobias, D. J., & Klein, M. L. (1996).
22+
Explicit reversible integrators for extended systems dynamics.
23+
Molecular Physics, 87(5), 1117-1157.
24+
825
Examples:
926
>>> import torch_sim as ts
1027
>>> state = ts.nvt_langevin_init(model, initial_state, kT=300.0 * units.temperature)
@@ -16,6 +33,8 @@
1633
Notes:
1734
All integrators support batched operations for efficient parallel simulation
1835
of multiple systems.
36+
37+
1938
"""
2039

2140
# ruff: noqa: F401
@@ -47,7 +66,26 @@
4766

4867

4968
class Integrator(StrEnum):
50-
"""Flavor of molecular dynamics simulation."""
69+
"""Enumeration of available molecular dynamics (MD) integrators.
70+
71+
Each member represents a different simulation ensemble or thermostat/barostat
72+
scheme. These values are used as keys in :data:`INTEGRATOR_REGISTRY`
73+
to select the corresponding initialization and stepping functions.
74+
75+
Available options:
76+
- ``nve``: Constant energy (microcanonical) ensemble.
77+
- ``nvt_langevin``: Langevin thermostat for constant temperature.
78+
- ``nvt_nose_hoover``: Nosé-Hoover thermostat for constant temperature.
79+
- ``npt_langevin``: Langevin barostat for constant temperature and pressure.
80+
- ``npt_nose_hoover``: Nosé-Hoover barostat for constant temperature
81+
and constant pressure.
82+
83+
Example:
84+
>>> integrator = Integrator.nvt_langevin
85+
>>> print(integrator.value)
86+
'nvt_langevin'
87+
88+
"""
5189

5290
nve = "nve"
5391
nvt_langevin = "nvt_langevin"
@@ -56,7 +94,29 @@ class Integrator(StrEnum):
5694
npt_nose_hoover = "npt_nose_hoover"
5795

5896

59-
# Integrator registry - maps integrator names to (init_fn, step_fn) pairs
97+
#: Integrator registry - maps integrator names to (init_fn, step_fn) pairs.
98+
#:
99+
#: This dictionary associates each :class:`Integrator` enum value with a pair
100+
#: of callables:
101+
#:
102+
#: - **init_fn**: A function used to initialize the integrator state.
103+
#: - **step_fn**: A function that advances the state by one simulation step.
104+
#:
105+
#: Example:
106+
#:
107+
#: >>> init_fn, step_fn = INTEGRATOR_REGISTRY[Integrator.nvt_langevin]
108+
#: >>> state = init_fn(...)
109+
#: >>> new_state = step_fn(state, ...)
110+
#:
111+
#: The available integrators are:
112+
#:
113+
#: - ``Integrator.nve``: Velocity Verlet (microcanonical)
114+
#: - ``Integrator.nvt_langevin``: Langevin thermostat
115+
#: - ``Integrator.nvt_nose_hoover``: Nosé-Hoover thermostat
116+
#: - ``Integrator.npt_langevin``: Langevin barostat
117+
#: - ``Integrator.npt_nose_hoover``: Nosé-Hoover barostat
118+
#:
119+
#: :type: dict[Integrator, tuple[Callable[..., Any], Callable[..., Any]]]
60120
INTEGRATOR_REGISTRY: Final[
61121
dict[Integrator, tuple[Callable[..., Any], Callable[..., Any]]]
62122
] = {

torch_sim/integrators/md.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ class MDState(SimState):
2121
2222
Attributes:
2323
positions (torch.Tensor): Particle positions [n_particles, n_dim]
24-
momenta (torch.Tensor): Particle momenta [n_particles, n_dim]
25-
energy (torch.Tensor): Total energy of the system [n_systems]
26-
forces (torch.Tensor): Forces on particles [n_particles, n_dim]
2724
masses (torch.Tensor): Particle masses [n_particles]
2825
cell (torch.Tensor): Simulation cell matrix [n_systems, n_dim, n_dim]
2926
pbc (bool): Whether to use periodic boundary conditions
3027
system_idx (torch.Tensor): System indices [n_particles]
3128
atomic_numbers (torch.Tensor): Atomic numbers [n_particles]
29+
momenta (torch.Tensor): Particle momenta [n_particles, n_dim]
30+
energy (torch.Tensor): Total energy of the system [n_systems]
31+
forces (torch.Tensor): Forces on particles [n_particles, n_dim]
3232
3333
Properties:
3434
velocities (torch.Tensor): Particle velocities [n_particles, n_dim]
@@ -184,7 +184,7 @@ def velocity_verlet[T: MDState](state: T, dt: torch.Tensor, model: ModelInterfac
184184
Updated state after one complete velocity Verlet step
185185
186186
Notes:
187-
- Time-reversible and symplectic integrator
187+
- Time-reversible and symplectic integrator of second order accuracy
188188
- Conserves energy in the absence of numerical errors
189189
- Handles periodic boundary conditions if enabled in state
190190
"""
@@ -240,7 +240,25 @@ class NoseHooverChainFns:
240240
update_mass: Callable
241241

242242

243-
# Suzuki-Yoshida weights for multi-timestep integration
243+
#: Suzuki-Yoshida composition weights for higher-order symplectic integrators.
244+
#:
245+
#: These coefficients are used to construct high-order operator-splitting
246+
#: schemes (Suzuki-Yoshida compositions) in molecular dynamics and Hamiltonian
247+
#: simulations.
248+
#:
249+
#: The coefficients define how lower-order symplectic integrators (e.g., leapfrog)
250+
#: can be recursively composed to achieve higher-order accuracy while preserving
251+
#: symplectic structure.
252+
#:
253+
#: References:
254+
#: - M. Suzuki, *General Decomposition Theory of Ordered Exponentials*,
255+
#: Proc. Japan Acad. 69, 161 (1993).
256+
#: - H. Yoshida, *Construction of higher order symplectic integrators*,
257+
#: Phys. Lett. A 150, 262-268 (1990).
258+
#: - M. Tuckerman, *Statistical Mechanics: Theory and Molecular Simulation*,
259+
#: Oxford University Press (2010). Section 4.11
260+
#:
261+
#: :type: dict[int, torch.Tensor]
244262
SUZUKI_YOSHIDA_WEIGHTS = {
245263
1: torch.tensor([1.0]),
246264
3: torch.tensor([0.828981543588751, -0.657963087177502, 0.828981543588751]),

torch_sim/integrators/nvt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ class NVTNoseHooverState(MDState):
207207
208208
Attributes:
209209
positions: Particle positions with shape [n_particles, n_dimensions]
210-
momenta: Particle momenta with shape [n_particles, n_dimensions]
211-
energy: Energy of the system
212-
forces: Forces on particles with shape [n_particles, n_dimensions]
213210
masses: Particle masses with shape [n_particles]
214211
cell: Simulation cell matrix with shape [n_dimensions, n_dimensions]
215212
pbc: Whether to use periodic boundary conditions
213+
momenta: Particle momenta with shape [n_particles, n_dimensions]
214+
energy: Energy of the system
215+
forces: Forces on particles with shape [n_particles, n_dimensions]
216216
chain: State variables for the Nose-Hoover chain thermostat
217217
218218
Properties:

0 commit comments

Comments
 (0)