NameError:未定义名称“ pbc”

问题描述

您好,我正在通过python-3.7学习MDAnalysis。您能检查一下我的代码并建议如何解决以下错误

Traceback (most recent call last):
  File "/home/pulokdeb/projects/def-sohrabz/pulokdeb/beluga_python/Closest_atom_Oxy_group.py",line 242,in <module>
    eigen_value = iio.eigen_vals()
  File "/home/pulokdeb/ENV/lib/python3.7/site-packages/MDAnalysis/core/topologyattrs.py",line 1347,in eigen_vals
    com = atomgroup.center_of_mass(pbc=pbc)

NameError: name 'pbc' is not defined

代码(部分)如下:

def radius_of_gyration(group,pbc=False,**kwargs):
    """Radius of gyration.

    Parameters
    ----------
    pbc : bool,optional
        If ``True``,move all atoms within the primary unit cell before
        calculation. [``False``]


    .. versionchanged:: 0.8 Added *pbc* keyword

    """
    atomgroup = group.atoms
    masses = atomgroup.masses

    com = atomgroup.center_of_mass(pbc=pbc)
    if pbc:
        recenteredpos = atomgroup.pack_into_Box(inplace=False) - com
    else:
        recenteredpos = atomgroup.positions - com

    rog_sq = np.sum(masses * np.sum(recenteredpos**2,axis=1)) / atomgroup.total_mass()

    return np.sqrt(rog_sq)

transplants[GroupBase].append(
    ('radius_of_gyration',radius_of_gyration))

解决方法

我在topologyattrs.py文件中更改了几行(def_eif_vals),并得到了结果。希望它对我将来的模拟有用。

def shape_parameter(group,pbc = False,** kwargs): “”“形状参数。

    See [Dima2004a]_ for background information.

    Parameters
    ----------
    pbc : bool,optional
        If ``True``,move all atoms within the primary unit cell before
        calculation. [``False``]


    References
    ----------
    .. [Dima2004a] Dima,R. I.,& Thirumalai,D. (2004). Asymmetry
       in the shapes of folded and denatured states of
       proteins. *J Phys Chem B*,108(21),6564-6570. doi:`10.1021/jp037128y
       <https://doi.org/10.1021/jp037128y>`_


    .. versionadded:: 0.7.7
    .. versionchanged:: 0.8 Added *pbc* keyword

    """
    atomgroup = group.atoms
    masses = atomgroup.masses

    com = atomgroup.center_of_mass(pbc=pbc)
    if pbc:
        recenteredpos = atomgroup.pack_into_box(inplace=False) - com
    else:
        recenteredpos = atomgroup.positions - com
    tensor = np.zeros((3,3))

    for x in range(recenteredpos.shape[0]):
        tensor += masses[x] * np.outer(recenteredpos[x,:],recenteredpos[x,:])
    tensor /= atomgroup.total_mass()
    eig_vals = np.linalg.eigvalsh(tensor)
    shape = 27.0 * np.prod(eig_vals - np.mean(eig_vals)
                           ) / np.power(np.sum(eig_vals),3)

    return shape

transplants[GroupBase].append(
    ('shape_parameter',shape_parameter))

def eigen_vals(group,pbc=False,**kwargs):
    """ Changed by Pulok Deb
    """
    atomgroup = group.atoms
    masses = atomgroup.masses

    com = atomgroup.center_of_mass(pbc=pbc)
    if pbc:
        recenteredpos = atomgroup.pack_into_box(inplace=False) - com
    else:
        recenteredpos = atomgroup.positions - com
    tensor = np.zeros((3,:])
    tensor /= atomgroup.total_mass()
    eig_vals = np.linalg.eigvalsh(tensor)


    return eig_vals

transplants[GroupBase].append(
    ('eigen_vals',eigen_vals))

@warn_if_not_unique
@check_pbc_and_unwrap