Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions package/MDAnalysis/analysis/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ def _prepare(self):
if self.norm == "rdf":
# Cumulative volume for rdf normalization
self.volume_cum = 0
self._minrange = self.rdf_settings["range"][0] if self.rdf_settings["range"][0] > 0 else 0.0
self._maxrange = self.rdf_settings["range"][1]

def _single_frame(self):
Expand All @@ -641,13 +642,24 @@ def _single_frame(self):
ag1.positions,
ag2.positions,
self._maxrange,
self._minrange,
box=self._ts.dimensions,
backend=self.backend,
)

# Different people write code for different purposes. For my needs, the following two lines are sufficient:
# count, _ = np.histogram(dist, **self.rdf_settings)
# self.results.count[i][0, 0, :] += count

# The following is an optimized version based on the old logic.
bins = self.rdf_settings["bins"]
minv, maxv = self._minrange, self._maxrange
# Calculate bin indices for each value in dist, similar to np.histogram's bin assignment.
bin_indices = ((dist - minv) * bins / (maxv - minv)).astype(np.int64)

for j, (idx1, idx2) in enumerate(pairs):
count, _ = np.histogram(dist[j], **self.rdf_settings)
self.results.count[i][idx1, idx2, :] += count
self.results.count[i][idx1, idx2, bin_indices[j]] += 1
self.results.count[i][0, 0, bin_indices[j]] += 1

if self.norm == "rdf":
self.volume_cum += self._ts.volume
Expand All @@ -669,8 +681,10 @@ def _conclude(self):

for i, (ag1, ag2) in enumerate(self.ags):
# Number of each selection
if self.norm == "rdf":
N = len(ag1) * len(ag2)
self.results.indices.append([ag1.indices, ag2.indices])
self.results.rdf.append(self.results.count[i] / norm)
self.results.rdf.append(self.results.count[i] / (norm * N)) # Modify to make it consistent with InterRDF

def get_cdf(self):
r"""Calculate the cumulative counts for all sites.
Expand Down
Loading