MDAnalysis线性密度模块的bug修复 - Bug fix for the MDAnalysis linear density module

MDAnalysis的线性密度模块有bug:模拟一盒Lammps模拟的均匀的水分子,计算出来的线性密度有一些向上突起的尖峰,如图所示:

There is a bug in the MDAnalysis linear density module: when calculating the linear density of a uniformly simulated box of water molecules using LAMMPS, the result includes some sharp upward spikes, as shown below.


其bug位于lineardensity.py中,LinearDensity类的__init__function,第218行:

The bug is located in the lineardensity.py file, within the __init__ function of the LinearDensity class, at line 218:

1
bins = (self.dimensions // self.binsize).astype(int)


self.dimensionsnumpy([20., 20., 20.])self.binsize0.1时,由于浮点精度误差bins不是200, 200, 200,而是199, 199, 199。这会导致后续np.histogram函数计算出现错误。

可将其替换为:

When self.dimensions is numpy([20., 20., 20.]) and self.binsize is 0.1, due to floating-point precision errors, bins becomes 199, 199, 199 instead of 200, 200, 200. This causes incorrect calculations in the subsequent np.histogram function.

This can be fixed by replacing the line with:

1
bins = np.round(self.dimensions / self.binsize).astype(int)


有关于这个问题的详细讨论在这个github issue

For more detailed discussion on this issue, please refer to this GitHub issue.