You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
InterRDF(norm="density") is nA times the documented single-particle density #5519
Hi, thanks for maintaining MDAnalysis. The module docs define norm="density" as the single particle density n_ab(r) = rho g_ab(r) (equation-nab). In InterRDF._conclude (package/MDAnalysis/analysis/rdf.py), both "rdf" and "density" divide by n_frames * shell volume, but only the "rdf" branch also applies N / box_vol with N = nA * nB. As a result, "density" returns the pair count per shell volume summed over all a particles, which is nA * n_ab(r).
Check with MDAnalysis 2.11.0: an ideal gas of 40 A and 200 B atoms placed at uniform random positions in a 30 Å box, 20 frames, range=(3, 12). Here n_ab should equal rho_B = nB/V = 0.00741 Å^-3.
mean over bins
norm="rdf"
0.999 (correct)
norm="density"
0.296, which is 40.0 x rho_B = nA x rho_B
Dividing by nA (adjusted for exclusions, as in the "rdf" branch) would match the docs. InterRDF_s computes per-pair counts, so it is not affected in the same way.
I found this while testing physverify, a small verification tool I'm working on, against public codes. The tests and this write-up were done with help from an AI assistant; the numbers come from actually running the code.
Hi @rcgiri-physics, thanks for catching this and opening the issue!
I investigated the implementation in package/MDAnalysis/analysis/rdf.py and can confirm your findings:
results.count stores the accumulated pairwise counts between all particles in g1 ($n_A$) and g2 ($n_B$).
For norm="rdf", norm is multiplied by normalization_density which accounts for the full pair capacity $N = n_A n_B$ (accounting for exclusions if specified).
For norm="density", the documentation defines the target output as the single-particle density $n_{ab}(r) = \rho_2 g_{ab}(r)$ around a reference $a$ particle. Because results.count aggregates across all $n_A$ reference particles, dividing solely by $T \cdot \Delta V$ returns the collective density $n_A \cdot n_{ab}(r)$, which is $n_A$ times the documented single-particle value.
Scaling norm *= nA in _conclude() when self.norm == "density" directly corrects this. With this fix, the fundamental identity: results_density == results_rdf * results.density_2
holds across all bins for unexcluded pairs.
I have tested this fix locally along with an analytical test verifying the relation to results.density_2 and updated reference values in test_rdf.py. I would be glad to submit a PR for this if maintainers would like to proceed.
Activity
sumitsaraswat362 commentedon Oct 24, 2026
Hi @rcgiri-physics, thanks for catching this and opening the issue!
I investigated the implementation in
package/MDAnalysis/analysis/rdf.pyand can confirm your findings:results.countstores the accumulated pairwise counts between all particles ing1(g2(norm="rdf",normis multiplied bynormalization_densitywhich accounts for the full pair capacitynorm="density", the documentation defines the target output as the single-particle densityresults.countaggregates across allScaling
norm *= nAin_conclude()whenself.norm == "density"directly corrects this. With this fix, the fundamental identity:results_density == results_rdf * results.density_2holds across all bins for unexcluded pairs.
I have tested this fix locally along with an analytical test verifying the relation to
results.density_2and updated reference values intest_rdf.py. I would be glad to submit a PR for this if maintainers would like to proceed.FIX: simplify density normalization branch to achieve full coverage (M…
FIX: simplify density normalization branch to achieve full coverage (M…
DOC: add versionchanged note for InterRDF norm=density and test Inter…