MindSponge分子动力学模拟——计算单点能(2023.08)
阅读原文时间:2023年08月31日阅读:1

技术背景

在前面的几篇文章中,我们分别介绍了MindSponge的软件架构MindSponge的安装与使用如何在MindSponge中定义一个分子系统。那么就像深度学习中的损失函数,或者目标函数,这里分子力学的主要目标函数就是势能(也有动能项,但动能项更多的来源于分子动力学模拟的过程,而不是实验中的参数)。这篇文章我们主要探讨一下,在MindSponge中如何计算给定体系的单点能。

浅析ForceField类

ForceField是用于构建分子力场的基础类,首先我们还是来看一下ForceField这个类的内部实现,主要关注下需要哪些输入,以及这些输入的含义是什么。

class ForceField(ForceFieldBase):
    r"""Potential energy of classical force field. It is a subclass of `ForceFieldBase`.

        The `ForceField` class use force field parameter files to build the potential energy.

    Args:

        system (Molecule):      Simulation system.

        parameters (Union[dict, str, List[Union[dict, str]]]):
                                Force field parameters. It can be a `dict` of force field parameters,
                                a `str` of filename of a force field file in MindSPONGE YAML format,
                                or a `list` or `tuple` containing multiple `dict` or `str`.
                                If a filename is given, it will first look for a file with the same name
                                in the current directory. If the file does not exist, it will search
                                in MindSPONGE's built-in force field.
                                If multiple sets of parameters are given and the same atom type
                                is present in different parameters, then the atom type in the parameter
                                at the back of the array will replace the one at the front.

        cutoff (float):         Cutoff distance. Default: None

        rebuild_system (bool):  Whether to rebuild the atom types and bond connection of the system
                                based on the template in parameters.
                                Default: True

        length_unit (str):      Length unit. If None is given, it will be assigned with the global length unit.
                                Default: None

        energy_unit (str):      Energy unit. If None is given, it will be assigned with the global energy unit.
                                Default: None

    Returns:

        energy (Tensor):    Tensor of shape `(B, E)`. Data type is float.

    Supported Platforms:

        ``Ascend`` ``GPU``

    Symbols:

        B:  Batchsize, i.e. number of walkers in simulation.
        E:  Number of energy terms.

    """

在这里面重点关注下这三个输入信息:

  1. 分子系统。这里面包含了原子类型、成键关系等构建力场所必须的参数,因此我们可以直接把构建好的Molecule对象直接传入ForceField。
  2. 力场参数。我们已经在MindSponge内部转化了一些常用的Amber力场参数文件,包含amber.ff03,amber.ff14sb,amber.ff96和amber.gaff等,还有两个常用的水分子力场spce和tip3p。我们在构建ForceField对象的时候,只需要用字符串给parameters这一项赋值即可调用相关的力场文件。如果是自己做了一个力场文件,那么传入该力场文件的绝对路径即可。能被成功调用的前提条件是,完全按照MindSponge的力场文件格式要求做的力场。
  3. 截断半径。在力场里面的长程相互作用力的计算中会使用到近邻表的计算,因为对大体系而言我们不太可能考虑全连接图,因此需要用一个截断半径对长程相互作用的范围进行一定的截断。

除了ForceField类之外,我们还需要关注一个相关的类是MindSponge中的WithEnergyCell,这也是在计算单点能中一个非常重要的类。

class WithEnergyCell(Cell):
    r"""
    Cell that wraps the simulation system with the potential energy function.
    This Cell calculates the value of the potential energy of the system at the current coordinates and returns it.

    Args:
        system(Molecule):               Simulation system.
        potential(PotentialCell):       Potential energy function cell.
        bias(Union[Bias, List[Bias]]):  Bias potential function cell. Default: None
        cutoff(float):                  Cut-off distance for neighbour list. If None is given, it will be assigned
                                        as the cutoff value of the of potential energy.
                                        Defulat: None
        neighbour_list(NeighbourList):  Neighbour list. Default: None
        wrapper(EnergyWrapper):         Network to wrap and process potential and bias.
                                        Default: None

    Inputs:
        - **\*inputs** (Tuple(Tensor)) - Tuple of input tensors of 'WithEnergyCell'.

    Outputs:
        energy, Tensor of shape `(B, 1)`. Data type is float. Total potential energy.

    Supported Platforms:
        ``Ascend`` ``GPU``

    Symbols:
        B:  Batchsize, i.e. number of walkers of the simulation.
        A:  Number of the atoms in the simulation system.
        N:  Number of the maximum neighbouring atoms.
        U:  Number of potential energy terms.
        V:  Number of bias potential terms.
    """

在前面一部我们构建好的ForceField里面,实际上更多的是建模过程,在分子构象空间和拓扑连接性上面查找相应的对象,并匹配到相应的力场参数,最终作为一个能量计算环节的参数输入。而WithEnergyCell则可以根据system和forcefield两者提供的信息,直接计算能量。关于WithEnergyCell类的输入,我们重点关注下这几个参数:

  1. 分子系统。就是前面构造好的system参数,或者Molecule对象。
  2. 力场模型。根据上一个介绍的ForceField类中构建的力场模型,有了这个力场模型,就可以直接计算各项力场的能量和力的大小。
  3. 偏置势。这一项虽然看起来只是一个参数,但其实最能够体现使用AI框架来做分子动力学模拟的优势,后面会专门写一篇文章来介绍。其主要作用是添加一些偏置势能项,用于约束分子系统,向期望的方向演化。比如我们可以添加一个球形谐振子势,把体系约束在一个给定体积大小的球体中。也可以通过bias添加Meta Dynamics,用于快速模拟化学反应的过程。
  4. 近邻表。相比于给定一个cutoff,这里给定一个近邻表会更加直接。由于近邻表在分子模拟的过程中有可能直接决定了分子模拟的速度,或者是可模拟体系的大小,因为近邻表所产生的最大Tensor维度有可能为(B,A,N,D),是所有Tensor中最大的。熟悉GPU计算的童鞋可能都知道,在GPU上占用显存过大有可能导致不可计算的问题。
  5. 修饰器。wrapper这个参数,为力场的使用增添了许多的灵活性。跟bias的功能是很相似的,但是bias相当于是添加了一个新的势能函数,而wrapper则是直接对原始力场的能量进行修饰。

以上,就是ForceField和WithEnergyCell的基本介绍,有了这两个工具,我们就可以导入一些已有的力场,来完成单点能的计算。

计算单点能

给定一个初始的分子构象,求这个构象的分子势能。一般情况,这个问题可能需要通过计算化学的方式来进行求解,比如CCSD(T)之类的方法。但是我们做分子模拟,需要快速的演化和迭代,如果使用计算化学的方法,速度是无法满足计算的需求的。因此将实验数据或者是计算化学的数据拟合成一系列的力场参数,目前来说还是一个相对比较快速而且不损失太多精度的方案。接下来我们展示一下如何使用MindSponge来计算单点能。

from sponge import Protein, ForceField, WithEnergyCell
system = Protein('case1.pdb', rebuild_hydrogen=True)
energy = ForceField(system, parameters='amber.ff14sb')
with_energy = WithEnergyCell(system, energy)
print (with_energy.energy_names)
print (with_energy.calc_energies())

在上面这个案例中,先是用MindSponge内置的Protein类加载了case1.pdb这个案例作为示例。这个pdb文件的内容为:

REMARK   Generated By Xponge (Molecule)
ATOM      1    N ALA     1      -0.095 -11.436  -0.780
ATOM      2   CA ALA     1      -0.171 -10.015  -0.507
ATOM      3   CB ALA     1       1.201  -9.359  -0.628
ATOM      4    C ALA     1      -1.107  -9.319  -1.485
ATOM      5    O ALA     1      -1.682  -9.960  -2.362
TER

运行结束之后会在case1.pdb同路径下生成一个补氢的文件case1_addH.pdb,内容如下所示:

MODEL     1
ATOM      1  N   ALA A   1      -0.095 -11.436  -0.780   1.0   0.0           N
ATOM      2  CA  ALA A   1      -0.171 -10.015  -0.507   1.0   0.0           C
ATOM      3  CB  ALA A   1       1.201  -9.359  -0.628   1.0   0.0           C
ATOM      4  C   ALA A   1      -1.107  -9.319  -1.485   1.0   0.0           C
ATOM      5  O   ALA A   1      -1.682  -9.960  -2.362   1.0   0.0           O
ATOM      6  H   ALA A   1      -0.994 -11.866  -0.701   1.0   0.0           H
ATOM      7  HA  ALA A   1      -0.767  -9.928   0.292   1.0   0.0           H
ATOM      8  HB1 ALA A   1       1.816  -9.816   0.015   1.0   0.0           H
ATOM      9  HB2 ALA A   1       1.407  -8.427  -0.329   1.0   0.0           H
ATOM     10  HB3 ALA A   1       1.797  -9.446  -1.427   1.0   0.0           H
TER
ENDMDL
END

上述单点能的计算结果如下所示:

[MindSPONGE] Adding 10 hydrogen atoms for the protein molecule in 0.003 seconds.
Warrning! The head_atom of residue 0 is not None but the tail_atom of residue -1 is None.
Warrning! The head_atom of residue 0 is not None but the tail_atom of residue -1 is None.
['bond_energy', 'angle_energy', 'dihedral_energy', 'coulomb_energy', 'lj_energy', 'nb_pair_energy']
[[  45.856487     76.87538       2.9642215  -100.20307      -0.50251234
   206.13483   ]]

而这里如果我们还是依旧使用这个代码,只是替换一个输入的pdb文件,比如如下的一个多肽:

REMARK   Generated By Xponge (Molecule)
ATOM      1    N ALA     1      -0.095 -11.436  -0.780
ATOM      2   CA ALA     1      -0.171 -10.015  -0.507
ATOM      3   CB ALA     1       1.201  -9.359  -0.628
ATOM      4    C ALA     1      -1.107  -9.319  -1.485
ATOM      5    O ALA     1      -1.682  -9.960  -2.362
ATOM      6    N ARG     2      -1.303  -8.037  -1.397
ATOM      7   CA ARG     2      -2.194  -7.375  -2.328
ATOM      8   CB ARG     2      -3.606  -7.943  -2.235
ATOM      9   CG ARG     2      -4.510  -7.221  -3.228
ATOM     10   CD ARG     2      -5.923  -7.789  -3.136
ATOM     11   NE ARG     2      -6.831  -7.111  -4.087
ATOM     12   CZ ARG     2      -8.119  -7.421  -4.205
ATOM     13  NH1 ARG     2      -8.686  -8.371  -3.468
ATOM     14  NH2 ARG     2      -8.844  -6.747  -5.093
ATOM     15    C ARG     2      -2.273  -5.882  -2.042
ATOM     16    O ARG     2      -1.630  -5.388  -1.119
ATOM     17    N ALA     3      -3.027  -5.119  -2.777
ATOM     18   CA ALA     3      -3.103  -3.697  -2.505
ATOM     19   CB ALA     3      -1.731  -3.041  -2.625
ATOM     20    C ALA     3      -4.039  -3.001  -3.483
ATOM     21    O ALA     3      -4.614  -3.643  -4.359
ATOM     22    N ALA     4      -4.235  -1.719  -3.394
ATOM     23   CA ALA     4      -5.126  -1.057  -4.325
ATOM     24   CB ALA     4      -6.538  -1.625  -4.233
ATOM     25    C ALA     4      -5.205   0.436  -4.039
ATOM     26    O ALA     4      -4.561   0.930  -3.116
ATOM     27  OXT ALA     4      -5.915   1.166  -4.728
TER

这样得到的单点能的计算结果如下所示:

[MindSPONGE] Adding 57 hydrogen atoms for the protein molecule in 0.008 seconds.
['bond_energy', 'angle_energy', 'dihedral_energy', 'improper_energy', 'coulomb_energy', 'lj_energy', 'nb_pair_energy']
[[ 231.55423  1049.3224    194.43379    20.708637 -689.10315  4051.5076
   163.09871 ]]

在MindSponge的单点能的计算过程中,我们保留了每一项能量的name和value,便于结果的比对。

需要注意的是,不同的分子模拟软件所使用的默认输出单位也是不一样的。所以我们用MindSponge进行分子模拟的时候,最好在代码的最前头把单位配置加上:

from sponge import set_global_units
set_global_units('nm', 'kj/mol')

比如像上面这样操作一下,就把全局的能量单位配置成了kj/mol,全局的长度单位配置成了nm。常用的长度单位还有A,常用的能量单位还有kcal/mol,主要就是这里两组单位之间进行切换。

总结概要

本文主要衔接前面的文章,继“MindSponge的安装与使用”、“MindSponge软件架构”以及“MindSponge中定义一个分子系统”系列文章之后,再讲解一下如何根据一个定义好的分子系统进行力场建模,使用力场来计算单点能,就是一个比较简单的案例。

版权声明

本文首发链接为:https://www.cnblogs.com/dechinphy/p/single-point-energy.html

作者ID:DechinPhy

更多原著文章:https://www.cnblogs.com/dechinphy/

请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章