kups.observables.radial_distribution_function
¶
Radial distribution function (pair correlation function) calculations.
This module provides tools for computing the radial distribution function \(g(r)\), which measures the probability of finding a particle at distance \(r\) from another particle, normalized by the bulk density.
Key components:
- radial_distribution_function: Core RDF computation using neighbor lists
- RadialDistributionFunction: StateProperty wrapper for on-the-fly RDF calculation
- offline_radial_distribution_function: Batch processing for trajectory analysis
Radial Distribution Function¶
The RDF \(g(r)\) characterizes local structure and correlations in particle systems:
where \(\rho\) is the number density and \(N\) is the number of particles. The integral \(\int_0^{r_c} 4\pi r^2 \rho g(r) dr\) gives the average number of neighbors within distance \(r_c\) (coordination number).
Physical interpretation:
- \(g(r) = 0\): Excluded volume (particles cannot overlap)
- \(g(r) < 1\): Depletion (fewer neighbors than expected from random distribution)
- \(g(r) = 1\): Random distribution (ideal gas limit at large \(r\))
- \(g(r) > 1\): Enrichment (structural peaks indicating shells/ordering)
The RDF reveals phase transitions (liquid vs. solid), molecular clustering, and solvation structure.
RadialDistributionFunction
¶
Bases: StateProperty[State, Array]
Compute radial distribution function as a state property.
Wraps radial_distribution_function in the StateProperty interface for on-the-fly computation during simulations. Useful for tracking structural evolution or as input to other analyses.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
positions |
View[State, Table[ParticleId, HasPositionsAndSystemIndex]]
|
View extracting indexed particle positions from state. |
systems |
View[State, Table[SystemId, NeighborListSystems]]
|
View extracting indexed system data from state. |
rmax |
View[State, float]
|
View extracting maximum distance (or constant float). |
bins |
View[State, int]
|
View extracting number of bins (or constant int). |
neighborlist |
NearestNeighborList
|
Neighbor list algorithm for pair finding. |
Note
For offline trajectory analysis of large datasets, use offline_radial_distribution_function instead for better memory efficiency.
Source code in src/kups/observables/radial_distribution_function.py
__call__(key, state)
¶
Compute RDF from current state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key (unused) |
required |
state
|
State
|
Current simulation state |
required |
Returns:
| Type | Description |
|---|---|
Array
|
RDF array, shape |
Source code in src/kups/observables/radial_distribution_function.py
offline_radial_distribution_function(positions, unitcell, rmax, bins, *, batch_size=None)
¶
Compute RDF from trajectory data with automatic capacity management.
Processes large trajectory datasets efficiently by:
1. Automatically determining neighbor list capacity via trial runs
2. Processing frames in batches using jax.lax.map
3. Handling assertion failures and retrying with increased capacity
This function is not JIT-compilable and intended for offline analysis of stored trajectories (e.g., from HDF5 files). For on-the-fly computation, use RadialDistributionFunction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
Array
|
Particle positions, shape |
required |
unitcell
|
UnitCell
|
Unit cell for periodic boundary conditions (shared across frames) |
required |
rmax
|
float
|
Maximum distance for RDF calculation (cutoff radius) |
required |
bins
|
int
|
Number of histogram bins |
required |
batch_size
|
int | None
|
Number of frames to process simultaneously in jax.lax.map. Lower values reduce memory usage. Defaults to None (process all at once). |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
RDF array, shape |
Array
|
from input positions. |
Example
import h5py
from kups.observables.radial_distribution_function import offline_radial_distribution_function
# Load trajectory from file
with h5py.File("trajectory.h5", "r") as f:
positions = f["positions"][:] # Shape: (n_frames, n_particles, 3)
unitcell = TriclinicUnitCell.from_matrix(f["unitcell"][()])
# Compute time-averaged RDF
g_r = offline_radial_distribution_function(
positions, unitcell, rmax=10.0, bins=200, batch_size=100
) # Shape: (n_frames, 200)
# Average over trajectory
g_r_avg = g_r.mean(axis=0) # Shape: (200,)
# Plot
import matplotlib.pyplot as plt
r = jnp.linspace(0.025, 9.975, 200)
plt.plot(r, g_r_avg)
plt.xlabel("r (Å)")
plt.ylabel("g(r)")
Note
- Automatically retries with increased capacity if neighbor list is too small
- Memory usage scales with
n_frames * batch_size * n_particles² - For very large trajectories, use smaller batch_size to avoid OOM errors
Source code in src/kups/observables/radial_distribution_function.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
radial_distribution_function(positions, systems, rmax, bins, neighborlist, cutoffs=None)
¶
Compute radial distribution function \(g(r)\) from particle positions.
Calculates the pair correlation function by:
- Finding all particle pairs within rmax using neighbor lists
- Computing pairwise distances
- Binning distances into histogram
- Normalizing by ideal gas distribution: \(4\pi r^2 \rho dr\)
The normalization includes a finite bin width correction:
where \(n(r)\) is the pair count in bin at \(r\), \(\rho\) is number density, \(N\) is particle count, and \(\Delta r\) is bin width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
Table[ParticleId, HasPositionsAndSystemIndex]
|
Indexed particle positions with system assignments. |
required |
systems
|
Table[SystemId, NeighborListSystems]
|
Indexed system data (unit cells, cutoffs). |
required |
rmax
|
float
|
Maximum distance to consider (cutoff radius). |
required |
bins
|
int
|
Number of histogram bins for \(g(r)\). |
required |
neighborlist
|
NearestNeighborList
|
Neighbor list algorithm for finding pairs. |
required |
cutoffs
|
Table[SystemId, Array] | None
|
Per-system cutoff distances. Defaults to rmax for all systems. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
RDF array, shape |
Array
|
centered at \(r_i = (i + 0.5) imes dr\) where \(dr = r_{ ext{max}} / ext{bins}\). |
Note
- Excludes self-pairs automatically via neighbor list construction
- Uses periodic boundary conditions from unitcell
- Assumes uniform density within each system