Interactions
Describe interactions with Force objects (or a ForceField) and pass them to Integrator(...; forces=...). All parameters use the same consistent unit system as your particle configuration.
Pair and long-range interactions
| Force | Model | Example |
|---|---|---|
LennardJones | Attraction and short-range repulsion | LennardJones(epsilon=1.0, sigma=1.0, cutoff=2.5) |
WCA | Purely repulsive Lennard-Jones interaction | WCA(epsilon=1.0, sigma=1.0) |
SoftRepulsive | Soft repulsive particles | SoftRepulsive(epsilon=1.0, sigma=1.0, cutoff=1.0) |
PME, PPPM, ESP | Long-range 3D periodic Coulomb interactions | See Electrostatics |
DispersionESP, DispersionPME | Long-range 1/r⁶ lattice dispersion (LJPME) | See Electrostatics |
ExternalPotentialForce | Native-Julia GPU MLIPs & external fields | See External potentials and GPU MLIPs |
MorsePotential, YukawaPotential, custom AbstractPairPotential | Custom short-range GPU pair potentials | See Custom short-range pair potentials |
force = LennardJones(
epsilon=1.0, sigma=1.0, cutoff=2.5,
pairs=:neighborlist,
neighborlist=CellList(buffer=0.5, capacity=256, rebuild_interval=10),
)For small examples, pairs=:all avoids neighbor-list setup at quadratic cost; use neighbor lists (pairs=:neighborlist) for production systems.
Choose a neighbor list
ParticleDynamics supports two GPU nonbonded neighbor backends:
CellList(:ellbackend): slot-major ELL neighbor rows, one thread/warp traversal per particle. Supports 2D and 3D Lennard-Jones, WCA, soft repulsion, and customAbstractPairPotentialmodels. Setspatial_reorder=true(or:auto) withreorder_intervalto enable Hilbert/cell spatial sorting on runs that do not require fixed particle storage order.ClusterPairList(:clusterbackend): 3D Newton-3 8×8 particle cluster tiles in a single fused kernel evaluating Lennard-Jones (uniform orPairTable) together with real-space Coulomb and lattice dispersion (with exclusions and 1–4 scaling bit masks). Charged systems select:clusterautomatically; uncharged 3D LJ/WCA systems can opt in explicitly:
# Automatic displacement-triggered pruning & rebuilding
cpl = ClusterPairList(buffer=0.2, prune_buffer=0.1, force_accumulation=:fixed64)
# Fixed-interval GROMACS-style dual pair list: rebuild every 40 steps, prune every 10
cpl_fixed = ClusterPairList(buffer=0.3, prune_buffer=0.1, list_intervals=(40, 10))Key ClusterPairList options:
buffer: outer list skin beyond the largest active interaction cutoff.prune_buffer: inner pruned-list buffer (defaults tobuffer / 2).list_intervals=(nstlist, nstprune): opt-in fixed-interval rebuild/prune schedule (nstlist >= 1,0 <= nstprune <= nstlist;nstprune = 0prunes only on rebuilds). Ensurebufferandprune_bufferare sized conservatively for particle motion over those intervals.force_accumulation::float32(default) or:fixed64(accumulates pair forces per slot in 64-bit signed fixed point at2⁻³⁰resolution before converting back toFloat32, eliminating atomic summation order noise and making forces bitwise reproducible across launches).
For CellList, choose capacity for the densest configuration the run will visit (especially under pressure control): overflow stops the simulation with ParticleDynamics.NeighborLists.NeighborCapacityError instead of silently truncating neighbors.
Lennard-Jones energy at the cutoff
Choose the cutoff treatment explicitly via vdw_modifier (or low-level set_vdw_modifier!):
vdw_modifier=:none(default): truncates the potential and force at the cutoff.vdw_modifier=:potential_shift: subtractsu(r_c)from each interacting pair's energy (forces inside the cutoff are unchanged; matches GROMACS default).vdw_modifier=:force_switch: applies the Steinbach–Brooks / GROMACS C² force switch betweenswitch_distance(default0.85 * cutoff) andcutoff.vdw_modifier=:potential_switch: multiplies the pair potential by a quintic Hermite C² taper on[switch_distance, cutoff].
LennardJones(epsilon=1.0, sigma=1.0, cutoff=2.5,
vdw_modifier=:force_switch, switch_distance=2.1)All four modifiers work on both :ell and :cluster backends. To include the full periodic −c6/r⁶ tail without truncating at a short cutoff, pair LennardJones with DispersionESP or DispersionPME (both are subtypes of DispersionForce; see Lattice dispersion).
Per-pair 1–4 LJ and Coulomb scaling
Biomolecular force fields (AMBER, CHARMM, OPLS) exclude 1–2 and 1–3 nonbonded pairs and scale 1–4 Lennard-Jones and Coulomb interactions independently. On the :cluster backend, supply 4-tuples (i, j, coul_scale, lj_scale) to attach_electrostatics! (or call set_lj_exceptions! / pass scaled_pairs on LennardJones for uncharged systems):
# Exclude 1-2/1-3 pairs and apply AMBER 1-4 scaling (1/1.2 Coulomb, 0.5 LJ)
exceptions = [
(Int32(1), Int32(2), 0.0, 0.0),
(Int32(1), Int32(4), 1.0 / 1.2, 0.5),
]
attach_electrostatics!(st, espec; exceptions=exceptions)All four vdw_modifier modes, DispersionESP out-of-list cancellation, and GSD round-trip serialization preserve exact per-pair lj_scale factors. Set exclude_bonded_pairs=false on LennardJones, WCA, or SoftRepulsive for polymer models such as Kremer–Grest (FENE + WCA) where nonbonded repulsion also acts between bonded beads.
External potentials and GPU MLIPs
AbstractExternalPotential provides a direct protocol for both machine-learned interatomic potentials (MLIPs) and custom external fields (confining walls, optical traps, biasing potentials, electric fields). Attach one inside any workflow integrator with ExternalPotentialForce, or on a low-level state via attach_external_potential!:
integrator = Integrator(
dt=1.0e-3,
scheme=BAOAB(),
forces=[ExternalPotentialForce(my_mlip)],
methods=[Langevin(all; gamma=1.0, kT=1.0)],
)Protocol traits and CUDA-graph capture
| Hook | Default | Role |
|---|---|---|
external_force_mode(pot) | :replace | :replace replaces internal forces (MLIPs); :additive adds on top of internal pair, bonded, mesh, and constraint forces |
is_device_capturable(pot) | false | Return true when enqueue_external_forces! launches only CUDA-graph-capturable GPU kernels with zero host syncs |
enqueue_external_forces!(pot, run, energy, virial) | throws MethodError | Device-side kernel entry point captured directly into DeviceEngine CUDA graphs (step_many!, barostats, constraints, minimization) |
external_forces!(pot, st, compute_energy[, compute_virial]) | required for host-synced providers | Host-dispatched force evaluation (used when is_device_capturable(pot) == false, e.g. Python/PyTorch bridges like examples/mace/ and examples/orb/) |
provides_virial(pot) | false | Return true when pot writes/accumulates virials into run.virial (st.virial_nonbonded) or run.virial_global (st.virial_global), enabling StochasticCellRescaling NPT, PressureObservable, and virial_tensor |
needs_neighbor_list(pot) | false | Return true to keep DeviceEngine's GPU neighbor list (run.neighbors) maintained for pot across drifts, barostat box moves, and minimization |
external_energy(pot, st) | delegates to forces | Optional energy-only hook for fast MonteCarloBarostat rejection without force evaluation |
external_fingerprint(pot) | (typeof(pot), objectid(pot)) | Value hashed into StateEngine to detect parameter changes |
Minimal native-Julia GPU additive external field
using CUDA, ParticleDynamics
struct HarmonicTrap3D{T} <: AbstractExternalPotential
k::T
end
ParticleDynamics.external_force_mode(::HarmonicTrap3D) = :additive
ParticleDynamics.is_device_capturable(::HarmonicTrap3D) = true
ParticleDynamics.provides_virial(::HarmonicTrap3D) = false
function _trap_kernel!(fx, fy, fz, Epot, rx, ry, rz, k, do_e::Bool)
i = (blockIdx().x - 1) * blockDim().x + threadIdx().x
if i <= length(rx)
@inbounds begin
x, y, z = rx[i], ry[i], rz[i]
fx[i] -= k * x; fy[i] -= k * y; fz[i] -= k * z
if do_e
Epot[i] += typeof(x)(0.5) * k * (x*x + y*y + z*z)
end
end
end
return nothing
end
function ParticleDynamics.enqueue_external_forces!(pot::HarmonicTrap3D, run, energy::Bool, virial::Bool)
rx, ry, rz = run.r
fx, fy, fz = run.f
n = length(rx)
CUDA.@cuda threads=256 blocks=cld(n, 256) _trap_kernel!(
fx, fy, fz, run.energy, rx, ry, rz, eltype(rx)(pot.k), energy)
return nothing
endBecause external_force_mode is :additive and is_device_capturable is true, HarmonicTrap3D composes directly with LennardJones, ClusterPairList, ESP, bonded terms, RigidWaterConstraint, and FIRE/SteepestDescent minimization inside captured CUDA graphs with zero host synchronizations.
Custom short-range pair potentials
For user-defined short-range radial interactions (such as Buckingham, Mie, tabulated splines, or custom coarse-grained models), subtype AbstractPairPotential to run directly inside DeviceEngine's 2D & 3D cooperative warp traversal (:ell backend) with automatic virials, NPT barostats, bonded exclusions, spatial reordering, minimization, and CUDA graphs.
ParticleDynamics ships two built-in reference implementations ready to attach via build_simulation(...; pair_potential=pot) or attach_pair_potential!:
MorsePotential(; D0, alpha, r0, rcut, shift=true):U(r) = D0 * (exp(-2α(r - r0)) - 2 exp(-α(r - r0))) - U_shift.YukawaPotential(; A=1.0, kappa, rcut, charges=nothing, shift=true): screened Coulomb / DLVO potentialU(r) = A q_i q_j (exp(-κr)/r - u_shift). Per-particlechargesare registered automatically viasorted_particle_arrayssospatial_reorder=truepermutes them in lockstep with particle coordinates.
To write your own pair potential, implement pair_cutoff(pot) and @inlinepair_eval(pot, data, ctx, j, r2::T) -> (inside::Bool, f_over_r::T, u::T), plus optional pair_data, sorted_particle_arrays, pair_context, pair_self_energy, and pair_fingerprint.
Bonded models and mixtures
Use Topology to supply connectivity and combine pair forces with HarmonicBondForce, FENEBondForce, angle, dihedral, or improper descriptors (see Bonds, minimization and pressure). For type-dependent nonbonded parameters, supply a PairTable with per-type vectors (Lorentz–Berthelot mixing) or explicit ntypes × ntypes matrices.
FENE domain
FENE requires finite k ≥ 0, positive finite R0 with a representable positive finite square, and finite minimum-image bond lengths strictly below R0. Evaluation throws DomainError identifying an incident particle if a bond reaches or exceeds R0. The potential and force are not clamped or continued beyond the physical domain; choose the timestep and initial configuration accordingly.
Physical helpers
Helper constructors convert between common colloidal/Stokes parameters and characteristic timescales (StokesFrictionCoefficient, SphereMass, InertialTime, DiffusiveTime). Keep their inputs consistent with your chosen unit system.
Full API signatures live in the Potentials, MLIPs & Bonded Terms and Electrostatics, Dispersion & Pair Lists reference pages.