在前面两篇文章中,我们分别介绍了分子动力学模拟软件MindSponge的软件架构和安装与使用教程。这里我们进入到实用化阶段,假定大家都已经在本地部署好了基于MindSpore的MindSponge的编程环境,开始用MindSponge去做一些真正的分子模拟的工作。那么分子模拟的第一步,我们就需要在MindSponge中去定义一个分子系统Molecule()。
我们先来看一下源代码中的Molecule这个类的自我介绍:
class Molecule(Cell):
r"""
Base class for molecular system, used as the "system module" in MindSPONGE.
The `Molecule` Cell can represent a molecule or a system consisting of multiple molecules.
The major components of the `Molecule` Cell is the `Residue` Cell. A `Molecule` Cell can
contain multiple `Residue` Cells.
Args:
atoms(Union[List[Union[str, int]], ndarray]): Array of atoms. The data in array can be str of atom
name or int of atomic number. Defulat: None
atom_name(Union[List[str], ndarray]): Array of atom name with data type `str`. Defulat: None
atom_type(Union[List[str], ndarray]): Array of atom type with data type `str`. Defulat: None
atom_mass(Union[Tensor, ndarray, List[float]]): Array of atom mass of shape `(B, A)` with data type
`float`. Defulat: None
atom_charge(Union[Tensor, ndarray, List[float]]): Array of atom charge of shape `(B, A)` with data type
`float`. Defulat: None
atomic_number(Union[Tensor, ndarray, List[float]]): Array of atomic number of shape `(B, A)` with data type
`int`. Defulat: None
bond(Union[Tensor, ndarray, List[int]]): Array of bond connection of shape `(B, b, 2)` with data
type `int`. Defulat: None
coordinate(Union[Tensor, ndarray, List[float]]): Tensor of atomic coordinates :math:`R` of shape
`(B, A, D)` with data type `float`. Default: None
pbc_box(Union[Tensor, ndarray, List[float]]): Tensor of box size :math:`\vec{L}` of periodic boundary
condition (PBC). The shape of tensor is `(B, D)`,
and the data type is `float`. Default: None
template(Union[dict, str, List[Union[dict, str]]]): Template for molecule. It can be a `dict` in MindSPONGE
template format or a `str` for the filename of a
MindSPONGE template file. If a `str` 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 the built-in template directory of
MindSPONGE (`mindsponge.data.template`).
Default: None.
residue(Union[Residue, List[Residue]]): Residue or a list of residues. If template is not None,
only the residues in the template will be used.
Default: None.
length_unit(str): Length unit. If `None` is given, the global length
units will be used. Default: None
Outputs:
- coordinate, Tensor of shape `(B, A, D)`. Data type is float.
- pbc_box, Tensor of shape `(B, D)`. Data type is float.
Supported Platforms:
``Ascend`` ``GPU``
Symbols:
B: Batchsize, i.e. number of walkers in simulation
A: Number of atoms.
b: Number of bonds.
D: Spatial dimension of the simulation system. Usually is 3.
"""
可以先看一下Molecule所接收的信息,其实可以主要分为以下几大类别:
上述主要是给Molecule的输入信息,输入给Molecule之后在内部构建build一次,才能得到一个最终的分子系统对象。接下来看看构建之后的Molecule的一些重要内置属性(self.xxx):
除了内置属性,Molecule还有一些内置函数可以关注一下:
关于MindSponge的安装和使用,可以参考这篇文章,在这里我们就不重复赘述了,假设大家已经完成了MindSponge的安装。但是需要提一句的是,在开始MindSponge模拟前,最好在python脚本的最前面加上这样一些环境变量的配置,否则容易报错:
import os
os.environ['GLOG_v']='4'
os.environ['MS_JIT_MODULES']='sponge'
接下来我们就可以简单的使用模板文件去创建一个新的分子:
from sponge import Molecule
system = Molecule(template='water.spce.yaml')
print ('The number of atoms in the system is: ', system.num_atoms)
print ('All the atom names in the system are: ', system.atom_name)
print ('The coordinates of atoms are: \n{}'.format(system.coordinate.asnumpy()))
输出的结果如下所示:
The number of atoms in the system is: 3
All the atom names in the system are: [['O' 'H1' 'H2']]
The coordinates of atoms are:
[[[ 0. 0. 0. ]
[ 0.08164904 0.0577359 0. ]
[-0.08164904 0.0577359 0. ]]]
这里因为water.spce.yaml
是一个预置的模板,类似的还有water.tip3p.yaml
。这种预置的模板我们可以直接当做template来创建,但如果是用户自行定义的模板文件,最好在这里写清楚yaml文件的绝对路径,否则会导致报错。相关yaml文件的内容如下所示:
template:
base: water_3p.yaml
WAT:
atom_mass: [15.9994, 1.008, 1.008]
atom_charge: [-0.8476, 0.4238, 0.4238]
settle:
mandatory: false
length_unit: nm
distance:
OW-HW: 0.1
HW-HW: 0.16330
molecule:
residue:
- WAT
length_unit: nm
coordinate:
- [0.0, 0.0, 0.0]
- [0.081649043, 0.057735897, 0.0]
- [-0.081649043, 0.057735897, 0.0]
这里的base是指向了另外一个较为基础的yaml参数文件:
template:
WAT:
atom_name: [O, H1, H2]
atom_type: [OW, HW, HW]
atom_mass: [16.00, 1.008, 1.008]
atomic_number: [8, 1, 1]
bond:
- [0, 1]
- [0, 2]
head_atom: null
tail_atom: null
有了这些参考,用户就可以自行定义一些模板,用于计算。
MindSponge也支持一些特定格式的分子导入,比如mol2格式的分子和pdb格式的蛋白质分子,这个章节介绍一下如何将文件导入为一个MindSponge的Molecule。比如我这里有一个非常简单的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来读取该pdb文件的方法为[*注:由于一般pdb文件中会忽略氢原子,因此加载的时候需要使用rebuild_hydrogen
将其重构成一个完整的分子]:
from sponge import Protein
system = Protein('case1.pdb', rebuild_hydrogen=True)
print ('The number of atoms in the system is: ', system.num_atoms)
print ('All the atom names in the system are: ', system.atom_name)
相应的输出结果为:
[MindSPONGE] Adding 57 hydrogen atoms for the protein molecule in 0.007 seconds.
The number of atoms in the system is: 57
All the atom names in the system are: [['N' 'CA' 'CB' 'C' 'O' 'H1' 'H2' 'H3' 'HA' 'HB1' 'HB2' 'HB3' 'N' 'CA'
'CB' 'CG' 'CD' 'NE' 'CZ' 'NH1' 'NH2' 'C' 'O' 'H' 'HA' 'HB2' 'HB3' 'HG2'
'HG3' 'HD2' 'HD3' 'HE' 'HH11' 'HH12' 'HH21' 'HH22' 'N' 'CA' 'CB' 'C'
'O' 'H' 'HA' 'HB1' 'HB2' 'HB3' 'N' 'CA' 'CB' 'C' 'O' 'OXT' 'H' 'HA'
'HB1' 'HB2' 'HB3']]
可以看到的是,在对应的位置上,我们将氢原子补在了一个相对合适的位置。一般情况下,重构完氢原子之后,需要对系统进行一个能量极小化,否则会导致初始系统的能量过于不稳定。具体的加氢效果可以看一下这个运行的结果:
其实加氢是很难做到一步到位的,但是我们可以尽可能的将氢原子摆放在一个相对合理的位置,便于后续的能量计算和优化。
由于python这一编程语言的灵活性,使得我们不仅支持从文件和模板文件中去定义一个分子系统,还可以直接用脚本的形式传一系列的python列表给Molecule来构建一个分子系统。比如我们只传原子类型和坐标还有键连关系,就能构建一个简单的水分子:
from sponge import Molecule
system = Molecule(atoms=['O', 'H', 'H'],
coordinate=[[0, 0, 0], [0.1, 0, 0], [-0.0333, 0.0943, 0]],
bonds=[[[0, 1], [0, 2]]])
print ('The number of atoms in the system is: ', system.num_atoms)
print ('All the atom names in the system are: ', system.atom_name)
print ('The coordinates of atoms are: \n{}'.format(system.coordinate.asnumpy()))
相应的输出结果如下所示:
The number of atoms in the system is: 3
All the atom names in the system are: [['O' 'H' 'H']]
The coordinates of atoms are:
[[[ 0. 0. 0. ]
[ 0.1 0. 0. ]
[-0.0333 0.0943 0. ]]]
本文通过解析MindSponge的源码实现,详细介绍了在MindSponge中Molecule基础分子类的内置属性和内置函数,以及三种相应的分子系统定义方法:我们既可以使用yaml模板文件来定义一个分子系统,也可以从mol2和pdb文件格式中直接加载一个Molecule,还可以直接使用python列表的形式传入一些手动定义的内容,直接构建一个Molecule。有了最基础的分子系统之后,后面就可以开始定义一些能量项和迭代器,开始分子动力学模拟。
本文首发链接为:https://www.cnblogs.com/dechinphy/p/mol-system.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/
打赏专用链接:https://www.cnblogs.com/dechinphy/gallery/image/379634.html
腾讯云专栏同步:https://cloud.tencent.com/developer/column/91958
CSDN同步链接:https://blog.csdn.net/baidu_37157624?spm=1008.2028.3001.5343
51CTO同步链接:https://blog.51cto.com/u_15561675
手机扫一扫
移动阅读更方便
你可能感兴趣的文章