Bohrium
robot
新建

空间站广场

论文
Notebooks
比赛
课程
Apps
我的主页
我的Notebooks
我的论文库
我的足迹

我的工作空间

任务
节点
文件
数据集
镜像
项目
数据库
公开
快速开始 DeepSPIN|训练NiO深度势能分子动力学模型
DeepSPIN
Tutorial
DeepSPINTutorial
bxu@gscaep.ac.cn
发布于 2023-07-04
赞 16
2
39
AI4SCUP-CNS-BBB(v1)

快速开始 DeepSPIN|训练NiO深度势能分子动力学模型

Open In Bohrium

作者: Ben Xu bxu@gscaep.ac.cn , Teng Yang, Zhengtao Huang, Wenlong Tang

推荐镜像:deepspin:final

推荐计算资源:CPU

内容:本教程主要介绍DeepSPIN相关操作,包括:NiO数据集的构建、模型训练及模型调用等。

使用方式: 您可在 Bohrium Notebook 上直接运行。您可以点击界面上方蓝色按钮 开始连接,选择 deepspin:final 镜像及任何一款节点配置,稍等片刻即可运行。如您遇到任何问题,请联系 bohrium@dp.tech

共享协议: 本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

代码
文本

0 框架介绍

代码
文本

DeepSPIN应用框架主要包含三大部分:

  1. 建立数据集:当确定磁性体系的基态构型后,生成对自旋与晶格的随机扰动构型,采用DeltaSpin进行第一性原理计算,收集结果得到数据集。
  2. 训练模型:将自旋与晶格信息共同映射为体系能量,通过能量、原子力、磁性力建立损失函数,进行DeepSPIN模型的训练。
  3. 应用模型:将训练好的DeepSPIN模型冻结后,可以在LAMMPS中进行自旋—晶格模拟,包括:能量最小化和自旋—晶格动力学计算。

在建立数据集和训练模型环节,DeepSPIN采用了类似DP-GEN的主动学习策略:

  1. 构型空间采样:利用现有模型对生成的随机扰动构型进行预测,根据原子力和磁性力的预测偏差筛选待学习构型。
  2. 第一性原理计算:对待学习构型利用DeltaSpin进行第一性原理计算,然后合并到数据集中。
  3. 训练模型:在合并后的数据集上对现有模型继续训练。
  4. 重复1-3步,直至数据集覆盖所关心的构型空间,并且模型取得良好训练效果。
代码
文本

图片 1.png

代码
文本

1 数据准备

我们已经为您准备了运行 DeepSPIN 计算所需的 初始数据,并将其放置在文件夹 DeepSPIN_Tutorial 中。

让我们通过以下命令获取本教程所需的数据。

代码
文本
[1]
! git clone https://github.com/hztttt/DeepSPIN_Tutorial.git && rm DeepSPIN_Tutorial/LICENSE
Cloning into 'DeepSPIN_Tutorial'...
remote: Enumerating objects: 1081, done.
remote: Counting objects: 100% (275/275), done.
remote: Compressing objects: 100% (174/174), done.
remote: Total 1081 (delta 122), reused 250 (delta 98), pack-reused 806
Receiving objects: 100% (1081/1081), 517.32 MiB | 4.74 MiB/s, done.
Resolving deltas: 100% (515/515), done.
Updating files: 100% (1411/1411), done.
代码
文本
[3]
! ls
DeepSPIN_Tutorial  deepspin.ipynb
代码
文本

让我们来查看下载的 DeepSPIN_Tutorial 文件夹:

代码
文本
[4]
! tree DeepSPIN_Tutorial -L 1
DeepSPIN_Tutorial
|-- 00.data
|-- 01.train
|-- 02.lmp
`-- 03.scripts

4 directories, 0 files
代码
文本

DeepSPIN_Tutorial 文件夹下有 00.data,01.train, 02.lmp 和 03.scritps共 四 个子文件夹。

  • 00.data 文件夹用于存放训练和测试数据,
  • 01.train 包含使用 DeepSPIN 训练模型的示例脚本,
  • 02.lmp 包含用于分子动力学模拟的 LAMMPS 示例脚本。
  • 03.scripts 包含用于提取数据脚本和lammps结果图。

让我们首先来查看 DeepSPIN_Tutorial/00.data 文件夹。

代码
文本
[5]
! tree DeepSPIN_Tutorial/00.data -L 1
DeepSPIN_Tutorial/00.data
`-- DeltaSpin

1 directory, 0 files
代码
文本

DeepSPIN 的训练数据来源于第一性原理计算数据,包含原子类型、模拟晶格、原子坐标、原子力、磁性力和系统能量。

00.data 文件夹下仅有 DeltaSpin 文件夹,DeltaSpin 文件夹是通过使用 VASP 进行自适应自旋约束DFT计算 (DeltaSPIN) 获得的。本教程中我们已经为您完成了NiO的计算。

DeepSPIN 需要针对磁性原子生成其对应的赝原子,并沿用DeepMD-kit中的压缩数据格式。该数据格式在 DeePMD-kit 手册中有详细解释,可以在DeePMD-kit Github中找到。

这里,我们提供了 get-raw-from-log.py 脚本,针对结构中的磁性原子进行处理,并得到 *.raw 文件。

代码
文本

接下来,我们使用 get-raw-from-log.py 脚本将 DeltaSpin 中的NiO数据转换为 *.raw 格式的数据,保存到 raw 文件夹中。

代码
文本
[6]
! python DeepSPIN_Tutorial/03.scripts/get-raw-from-log.py --dir_frames DeepSPIN_Tutorial/00.data/DeltaSpin/ \
--natoms 32 --ntypes 2 --ntypes_spin 1 --len_spin 0.4 --file_log tmp_log \
&& mkdir DeepSPIN_Tutorial/00.data/raw && mv *.raw DeepSPIN_Tutorial/00.data/raw
read frames: 200
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/1 converged
frame 1 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/10 converged
frame 2 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/100 converged
frame 3 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/101 converged
frame 4 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/102 converged
frame 5 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/103 converged
frame 6 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/104 converged
frame 7 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/105 converged
frame 8 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/106 converged
frame 9 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/107 converged
frame 10 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/108 converged
frame 11 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/109 converged
frame 12 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/11 converged
frame 13 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/110 converged
frame 14 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/111 converged
frame 15 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/112 converged
frame 16 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/113 converged
frame 17 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/114 converged
frame 18 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/115 converged
frame 19 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/116 converged
frame 20 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/117 converged
frame 21 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/118 converged
frame 22 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/119 converged
frame 23 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/12 converged
frame 24 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/120 converged
frame 25 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/121 converged
frame 26 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/122 converged
frame 27 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/123 converged
frame 28 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/124 converged
frame 29 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/125 converged
frame 30 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/126 converged
frame 31 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/127 converged
frame 32 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/128 converged
frame 33 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/129 converged
frame 34 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/13 converged
frame 35 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/130 converged
frame 36 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/131 converged
frame 37 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/132 converged
frame 38 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/133 converged
frame 39 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/134 converged
frame 40 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/135 converged
frame 41 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/136 converged
frame 42 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/137 converged
frame 43 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/138 converged
frame 44 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/139 converged
frame 45 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/14 converged
frame 46 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/140 converged
frame 47 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/141 converged
frame 48 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/142 converged
frame 49 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/143 converged
frame 50 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/144 converged
frame 51 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/145 converged
frame 52 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/146 converged
frame 53 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/147 converged
frame 54 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/148 converged
frame 55 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/149 converged
frame 56 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/15 converged
frame 57 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/150 converged
frame 58 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/151 converged
frame 59 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/152 converged
frame 60 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/153 converged
frame 61 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/154 converged
frame 62 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/155 converged
frame 63 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/156 converged
frame 64 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/157 converged
frame 65 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/158 converged
frame 66 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/159 converged
frame 67 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/16 converged
frame 68 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/160 converged
frame 69 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/161 converged
frame 70 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/162 converged
frame 71 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/163 converged
frame 72 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/164 converged
frame 73 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/165 converged
frame 74 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/166 converged
frame 75 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/167 converged
frame 76 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/168 converged
frame 77 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/169 converged
frame 78 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/17 converged
frame 79 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/170 converged
frame 80 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/171 converged
frame 81 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/172 converged
frame 82 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/173 converged
frame 83 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/174 converged
frame 84 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/175 converged
frame 85 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/176 converged
frame 86 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/177 converged
frame 87 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/178 converged
frame 88 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/179 converged
frame 89 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/18 converged
frame 90 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/180 converged
frame 91 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/181 converged
frame 92 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/182 converged
frame 93 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/183 converged
frame 94 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/184 converged
frame 95 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/185 converged
frame 96 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/186 converged
frame 97 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/187 converged
frame 98 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/188 converged
frame 99 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/189 converged
frame 100 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/19 converged
frame 101 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/190 converged
frame 102 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/191 converged
frame 103 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/192 converged
frame 104 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/193 converged
frame 105 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/194 converged
frame 106 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/195 converged
frame 107 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/196 converged
frame 108 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/197 converged
frame 109 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/198 converged
frame 110 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/199 converged
frame 111 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/2 converged
frame 112 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/20 converged
frame 113 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/200 converged
frame 114 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/21 converged
frame 115 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/22 converged
frame 116 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/23 converged
frame 117 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/24 converged
frame 118 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/25 converged
frame 119 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/26 converged
frame 120 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/27 converged
frame 121 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/28 converged
frame 122 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/29 converged
frame 123 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/3 converged
frame 124 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/30 converged
frame 125 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/31 converged
frame 126 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/32 converged
frame 127 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/33 converged
frame 128 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/34 converged
frame 129 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/35 converged
frame 130 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/36 converged
frame 131 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/37 converged
frame 132 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/38 converged
frame 133 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/39 converged
frame 134 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/4 converged
frame 135 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/40 converged
frame 136 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/41 converged
frame 137 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/42 converged
frame 138 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/43 converged
frame 139 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/44 converged
frame 140 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/45 converged
frame 141 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/46 converged
frame 142 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/47 converged
frame 143 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/48 converged
frame 144 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/49 converged
frame 145 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/5 converged
frame 146 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/50 converged
frame 147 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/51 converged
frame 148 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/52 converged
frame 149 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/53 converged
frame 150 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/54 converged
frame 151 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/55 converged
frame 152 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/56 converged
frame 153 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/57 converged
frame 154 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/58 converged
frame 155 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/59 converged
frame 156 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/6 converged
frame 157 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/60 converged
frame 158 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/61 converged
frame 159 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/62 converged
frame 160 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/63 converged
frame 161 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/64 converged
frame 162 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/65 converged
frame 163 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/66 converged
frame 164 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/67 converged
frame 165 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/68 converged
frame 166 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/69 converged
frame 167 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/7 converged
frame 168 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/70 converged
frame 169 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/71 converged
frame 170 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/72 converged
frame 171 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/73 converged
frame 172 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/74 converged
frame 173 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/75 converged
frame 174 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/76 converged
frame 175 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/77 converged
frame 176 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/78 converged
frame 177 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/79 converged
frame 178 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/8 converged
frame 179 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/80 converged
frame 180 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/81 converged
frame 181 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/82 converged
frame 182 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/83 converged
frame 183 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/84 converged
frame 184 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/85 converged
frame 185 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/86 converged
frame 186 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/87 converged
frame 187 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/88 converged
frame 188 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/89 converged
frame 189 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/9 converged
frame 190 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/90 converged
frame 191 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/91 converged
frame 192 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/92 converged
frame 193 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/93 converged
frame 194 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/94 converged
frame 195 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/95 converged
frame 196 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/96 converged
frame 197 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/97 converged
frame 198 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/98 converged
frame 199 succeed
/data/DeepSPIN_Tutorial/00.data/DeltaSpin/99 converged
frame 200 succeed
get raw files done
代码
文本

这里的参数含义为:

  • natoms: 原子的总数
  • ntypes: 原子类型数
  • ntypes_spin: 磁性原子类型数
  • len_spin: 磁性原子和其对应赝原子距离
  • file_log: 计算结果文件
代码
文本

运行完上述脚本后,我们再查看一下 00.data 文件夹,其中产生了新的文件夹 raw,接下来,让我们查看下 raw 文件夹。

代码
文本
[7]
! tree DeepSPIN_Tutorial/00.data/ -L 1
DeepSPIN_Tutorial/00.data/
|-- DeltaSpin
`-- raw

2 directories, 0 files
代码
文本
[8]
! tree DeepSPIN_Tutorial/00.data/raw -L 1
DeepSPIN_Tutorial/00.data/raw
|-- box.raw
|-- coord.raw
|-- energy.raw
|-- force.raw
`-- type.raw

0 directories, 5 files
代码
文本

让我们看下各个文件的作用。

  1. box.raw: 模拟晶格大小。
  2. coord.raw: 原子的坐标。
  3. energy.raw: 构型的能量。
  4. force.raw: 原子受力(原子力和磁性力)。
  5. type.raw: 原子的类型(以整数表示)。
代码
文本

下面让我们看下这些文件

代码
文本
[9]
! head -n 2 DeepSPIN_Tutorial/00.data/raw/box.raw
10.24614096 -0.00097986 -0.00026854 8.53538475 5.66808049 0.00041704 8.53527626 2.57406695 5.05024529
10.24614096 -0.00097986 -0.00026854 8.53538475 5.66808049 0.00041704 8.53527626 2.57406695 5.05024529
代码
文本

这告诉我们模拟NiO晶格的尺寸,每一行为一个NiO构型,晶格常数顺序为: XX XY XZ YX YY YZ ZX ZY ZZ

代码
文本
[10]
! head -n 2 DeepSPIN_Tutorial/00.data/raw/coord.raw
-0.00180931 0.00050088 -0.00043977 4.26763813 1.28703347 2.52512265 4.26854324 2.82978444 0.00077193 8.53533050 4.12107372 2.52533117 5.12307048 -0.00048993 -0.00013427 9.39070861 1.28654354 2.52498838 9.39076286 2.83355031 0.00007425 13.65840098 4.12058379 2.52519690 6.82920049 2.06029189 1.26259845 11.09467432 3.35140447 3.78765628 11.09689287 4.89433214 1.26280697 15.36453100 6.18136562 3.78792962 11.94789609 2.05446187 1.26454751 16.21990910 3.34683544 3.78758683 16.21996335 4.89384221 1.26267270 20.48760148 6.18087568 3.78779535 3.41460025 1.03014595 0.63129922 7.68223837 2.31717942 3.15642187 7.68229262 3.86418619 0.63150775 11.94993075 5.15121967 3.15663039 8.53767073 1.02965602 0.63116495 12.80014594 2.30884985 3.15690006 12.80536310 3.86369626 0.63137348 17.07300123 5.15072974 3.15649612 10.24380074 3.09043784 1.89389767 14.51143887 4.37747132 4.41902032 14.51149312 5.92447809 1.89410619 18.77913124 7.21151156 4.41922884 15.36687122 3.08994791 1.89376340 19.63450935 4.37698139 4.41888605 19.63456360 5.92398816 1.89397192 23.90220172 7.21102163 4.41909457 -0.32379285 -0.23193611 0.04751487 3.94565436 1.05459658 2.57307623 3.94655961 2.59734756 0.04872651 8.21334672 3.88863706 2.57328582 4.80108686 -0.23292667 0.04782099 9.04648651 1.08368266 2.54392534 9.06877930 2.60111334 0.04802883 13.33641818 3.88814599 2.57315259 7.15118404 2.29272894 1.21464419 11.41665833 3.58384090 3.73970214 11.41887680 5.12676866 1.21485266 15.68651485 6.41380210 3.73997456 12.26987950 2.28689908 1.21659307 16.54189282 3.57927216 3.73963213 16.54194663 5.12627969 1.21471868 20.80958469 6.41331294 3.73983978
0.00000000 0.00000000 0.00000000 4.26763813 1.28703347 2.52512265 4.26769238 2.83404025 0.00020852 8.53533050 4.12107372 2.52533117 5.12307048 -0.00048993 -0.00013427 9.39070861 1.28654354 2.52498838 9.39076286 2.83355031 0.00007425 13.65840098 4.12058379 2.52519690 6.82920049 2.06029189 1.26259845 11.09683862 3.34732537 3.78772110 11.09689287 4.89433214 1.26280697 15.36919196 6.18594395 3.78867369 11.95227097 2.05980196 1.26246418 16.21990910 3.34683544 3.78758683 16.21996335 4.89384221 1.26267270 20.48760148 6.18087568 3.78779535 3.41460025 1.03014595 0.63129922 7.68223837 2.31717942 3.15642187 7.68644527 3.86234485 0.63414667 11.94790217 5.14755263 3.15610588 8.53767073 1.02965602 0.63116495 12.80530885 2.31668949 3.15628760 12.80536310 3.86369626 0.63137348 17.07300123 5.15072974 3.15649612 10.24380074 3.09043784 1.89389767 14.51289159 4.37802138 4.42129652 14.51149312 5.92447809 1.89410619 18.77913124 7.21151156 4.41922884 15.36687122 3.08994791 1.89376340 19.63450935 4.37698139 4.41888605 19.63456360 5.92398816 1.89397192 23.90220172 7.21102163 4.41909457 0.05836959 0.32871003 0.22032410 4.29062588 1.60095931 2.77194674 4.33845596 3.17105126 0.20371797 8.60609438 4.45808523 2.72883968 5.19383358 0.33652234 0.20337326 9.46147168 1.62355562 2.72849623 9.46152621 3.17056204 0.20358258 13.72916458 4.45759594 2.72870445 6.74289538 1.71603272 1.07810713 11.02607408 3.01031328 3.58421377 11.02612825 4.55732012 1.05929956 15.29842785 5.84893112 3.58516744 11.88150707 1.72279042 1.05895572 16.14914531 3.00982395 3.58407825 16.12832793 4.56484764 1.05442880 20.41683828 5.84386340 3.58428788
代码
文本

coord.raw 每一行给出一个构型中各个原子的坐标信息,在我们NiO例子中,包含16个Ni原子、16个O原子和16个Ni赝原子,每个原子坐标分为x、y、z三个方向。故每行有个数据信息。

例如,第一行表示第一个构型的原子坐标为:

代码
文本
[11]
! head -n 2 DeepSPIN_Tutorial/00.data/raw/energy.raw
-166.31078603
-166.30895240
代码
文本

energy.raw 给出了每个NiO构型的能量信息。

代码
文本
[12]
! head -n 2 DeepSPIN_Tutorial/00.data/raw/force.raw
0.01879422 -0.01310723 0.00053585 -0.00867498 -0.00351402 0.00735577 -0.02115941 0.04838151 -0.00112284 -0.00237322 -0.00019873 -0.00294130 -0.00998428 -0.02342055 -0.00552161 -0.00120521 0.00129795 0.00928352 0.00101228 0.00948972 -0.00169888 -0.01762036 -0.02519123 -0.00830600 0.00211149 0.00010605 0.00288022 0.01964222 -0.05029723 0.00177637 0.00776651 -0.00930078 -0.01162228 -0.00206736 0.00212962 -0.00028495 0.04090250 0.05745414 -0.01841154 -0.00132879 -0.00347661 -0.00093944 -0.00415174 -0.00252736 -0.00222240 -0.00762998 -0.00599896 0.02159585 -0.00362291 -0.00698837 0.00148408 -0.00249927 0.00043358 -0.00353608 -0.00291713 -0.00146677 0.00095270 0.01191511 -0.00918452 -0.00348352 -0.00357133 0.00273220 0.00090007 0.04076518 0.07750834 -0.00498085 0.00310477 -0.01813462 0.01791405 -0.00081734 -0.00120353 -0.00047618 -0.01194237 0.00100254 0.00096253 0.00083673 -0.00906895 -0.00636045 0.00242878 0.00209788 0.00135863 -0.01469314 0.00798044 0.00208763 -0.00321585 -0.00134582 0.00514050 -0.01295942 0.00005568 0.00071536 -0.01689081 -0.02113713 -0.00408999 0.00004510 -0.00510725 0.00105521 -0.00687828 -0.00508654 0.00111571 -0.00778939 -0.00567344 0.00119009 -0.00783775 -0.00574006 0.00122533 -0.00721428 -0.00536331 0.00118964 -0.00797998 -0.00578059 0.00119198 -0.00560158 -0.01209434 0.00653353 -0.00738806 -0.00547542 0.00119076 -0.00829254 -0.00597961 0.00121498 0.00783178 0.00544564 -0.00103315 0.00783223 0.00546154 -0.00105336 0.01098251 0.00343667 0.00154793 0.00783149 0.00547896 -0.00102643 0.00781645 0.00544591 -0.00101689 0.01056069 0.00311121 0.00161941 0.00790725 0.00550629 -0.00105643 0.00776418 0.00541395 -0.00100871
0.00881232 0.00498696 0.00765984 -0.00168379 -0.01036499 0.00148299 0.00356926 -0.00344972 -0.00522600 0.01116370 0.00321837 0.00764964 0.00151391 0.00420705 0.00170006 0.00947883 0.00194861 -0.00416453 0.01341142 -0.00544782 0.00294587 0.00612178 0.00432528 0.00928090 -0.00014588 -0.00268799 -0.00103801 -0.00570170 -0.00745257 0.00094012 -0.00327739 -0.00319620 -0.00240736 -0.04257633 -0.05053956 0.00040279 -0.00174015 0.00031966 -0.00005634 -0.00674906 0.00945213 0.00219367 0.00340891 -0.00219712 0.00272354 -0.00106642 -0.00280197 -0.00057688 0.00275606 0.00119584 0.00538909 0.00419412 0.00559268 0.00366251 -0.03388881 0.02332775 -0.01902531 0.02115059 0.03441713 0.00498157 0.00268759 -0.01022120 0.00103565 0.00693075 -0.00517399 -0.00047301 0.00258486 -0.00152254 -0.00310312 -0.00001321 0.00231529 -0.00315317 0.00065551 -0.00515279 0.00010250 -0.00603027 0.00927219 -0.02379360 0.00183602 0.00104229 0.01038856 -0.00069362 0.00143452 -0.00185613 0.00053279 0.00446260 0.00249545 -0.00452544 -0.00048502 0.00060731 -0.00110776 0.00273685 -0.00026377 0.00839145 -0.00356174 -0.00050484 0.00560107 0.01079881 0.00016640 0.01227049 0.01682548 0.00217967 0.00195930 0.00864969 0.00504602 0.00189845 0.00793307 0.00462204 0.00194383 0.00833891 0.00493222 0.00183569 0.00756149 0.00438075 0.00385980 0.00669713 0.00472823 0.00354195 0.00965812 0.00344652 0.00211836 -0.00722832 -0.01076735 0.00263097 -0.00761214 -0.01021261 -0.00151233 -0.00813753 -0.00518199 -0.00146896 -0.00798242 -0.00514460 -0.00149310 -0.00804600 -0.00516192 -0.00145064 -0.00801416 -0.00512682 0.00577236 -0.01044832 -0.01006806 -0.00033856 -0.00741927 -0.00694383
代码
文本

force.raw 给出了每个NiO构型中每个原子的受力情况,每一行代表一个构型,每个原子的受力分为x、y、z三个方向,其中,受力分为Ni原子的原子力、O原子的原子力以及Ni原子的磁性力。

例如,第一行表示第一个构型的受力为:

代码
文本
[13]
! head -n 2 DeepSPIN_Tutorial/00.data/raw/type.raw
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
代码
文本

type.raw 文件给出了NiO结构的原子分布信息,这告诉我们这个例子中有48个原子(32个真实原子和16个赝原子),其中16个Ni原子由类型"0"表示,16个O原子由类型"1"表示,16个Ni赝原子由类型"2"表示。

代码
文本
[14]
import dpdata
import numpy as np

# 读入 DEEPMD/RAW 格式的数据
data = dpdata.LabeledSystem('DeepSPIN_Tutorial/00.data/raw/', fmt = 'deepmd/raw')
print('# 数据包含%d帧' % len(data))

# 随机选择40个索引作为验证集数据
np.random.seed(1)
index_validation = np.random.choice(200, size=40, replace=False)

# 其他索引作为训练集数据
index_training = list(set(range(200))-set(index_validation))
data_training = data.sub_system(index_training)
data_validation = data.sub_system(index_validation)

# 将所有训练数据放入文件夹"training_data"中
data_training.to_deepmd_npy('DeepSPIN_Tutorial/00.data/training_data')

# 将所有验证数据放入文件夹"validation_data"中
data_validation.to_deepmd_npy('DeepSPIN_Tutorial/00.data/validation_data')

print('# 训练数据包含%d帧' % len(data_training))
print('# 验证数据包含%d帧' % len(data_validation))

! rm DeepSPIN_Tutorial/00.data/validation_data/type_map.raw DeepSPIN_Tutorial/00.data/training_data/type_map.raw
# 数据包含200帧
# 训练数据包含160帧
# 验证数据包含40帧
代码
文本

这里,我们将200个构型划分为160个训数据和40个验证数据。

代码
文本

让我们再查看一下 00.data 文件夹,其中产生了新的文件,分别是 DeepSPIN 训练所需的训练集和验证集。

代码
文本
[15]
! tree DeepSPIN_Tutorial/00.data/ -L 1
DeepSPIN_Tutorial/00.data/
|-- DeltaSpin
|-- raw
|-- training_data
`-- validation_data

4 directories, 0 files
代码
文本
[16]
! tree DeepSPIN_Tutorial/00.data/training_data -L 1
DeepSPIN_Tutorial/00.data/training_data
|-- set.000
`-- type.raw

1 directory, 1 file
代码
文本

这些文件的作用如下:

  1. set.000:是一个目录,包含压缩格式的数据(NumPy压缩数组)。
  2. type.raw:是一个文件,包含原子的类型(以整数表示)。

让我们来看一下这些文件:

代码
文本
[17]
! cat DeepSPIN_Tutorial/00.data/training_data/type.raw
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
代码
文本

type.raw中,每一行给出了一个原子类型,其中16个Ni原子由类型"0"表示,16个O原子由类型"1"表示,16个Ni赝原子由类型"2"表示。

代码
文本

2 准备输入脚本

训练数据准备完成后,接下来就可以进行训练。DeepSPIN类似于DeePMD-kit 需要一个json格式的文件来指定训练参数。该文件称为 DeepSPIN 的输入脚本,让我们进入训练目录看一下该输入脚本:

代码
文本
[18]
! cd DeepSPIN_Tutorial/01.train/ && cat input.json
{
    "_comment": " model parameters",
    "model": {
	"type_map":	        ["Ni", "O"],
	"descriptor" : {
	    "type":		    "se_e2_a",
	    "sel":		    [50, 50],
	    "rcut_smth":	4.6,
	    "rcut":		    5.6,
	    "neuron":		[25, 50, 100],
	    "resnet_dt":	false,
	    "axis_neuron":	16,
	    "seed":		    22,
	    "_comment":		" that's all"
	},
	"fitting_net" : {
	    "neuron":		[240, 240, 240],
	    "resnet_dt":	true,
	    "seed":		    22,
	    "_comment":		" that's all"
	},
    "spin" : {
        "use_spin":     [true, false],
        "virtual_len":  [0.4],
        "spin_norm":    [1.2737],
        "_comment":		" that's all"
    },
	"_comment":	" that's all"
    },

    "learning_rate" : {
	    "type":		    "exp",
	    "decay_steps":	5000,
	    "start_lr":	    1.00e-3,	
	    "stop_lr":	    3.51e-8,
	    "_comment":	    " that's all"
    },

    "loss" : {
	    "type":		        "ener_spin",
	    "start_pref_e":	    0.02,
	    "limit_pref_e":	    1,
	    "start_pref_fr":	1000,
        "limit_pref_fr":	1.0,
	    "start_pref_fm":	1000,
	    "limit_pref_fm":	5.0,
	    "start_pref_v":	    0,
	    "limit_pref_v":	    0,
	    "_comment":	        " that's all"
    },

    "training" : {
	"training_data": {
		"systems":          ["../00.data/training_data"],
	    "batch_size":	    "auto",
	    "_comment":		    "that's all"
	},
	"validation_data": {
	    "systems":		    ["../00.data/validation_data"],
	    "batch_size":	    "auto",
	    "numb_btch":	    1,
	    "_comment":		    "that's all"
	},
	"numb_steps":	        1000000,
	"seed":		            22,
	"disp_file":	        "lcurve.out",
	"disp_freq":	        1000,
	"save_freq":        	100000,
	"_comment":	            "that's all"
    },    

    "_comment":		        "that's all"
}

代码
文本

在模型部分,指定了嵌入和拟合网络及磁性的参数。

"model": {
    "type_map":	        ["Ni", "O"],
    "descriptor" : {
        "type":		    "se_e2_a",
        "sel":		    [50, 50],
        "rcut_smth":	4.6,
        "rcut":		    5.6,
        "neuron":		[25, 50, 100],
        "resnet_dt":	false,
        "axis_neuron":	16,
        "seed":		    1,
        "_comment":		" that's all"
    },
    "fitting_net" : {
        "neuron":		[240, 240, 240],
        "resnet_dt":	true,
        "seed":		    1,
        "_comment":		" that's all"
    },
    "spin" : {
        "use_spin":     [true, false],
        "virtual_len":  [0.4],
        "spin_norm":    [1.2737],
        "_comment":		" that's all"
    },
    "_comment":	" that's all"
    },

部分参数的解释如下: 部分参数的解释如下:

参数 解释
type_map 每种原子的名称
descriptor > type 描述类型
descriptor > rcut 截断半径
descriptor > rcut_smth 平滑开始的位置
descriptor > sel 切割半径内第i种原子的最大数目
descriptor > neuron 嵌入神经网络的大小
descriptor > axis_neuron G矩阵的子矩阵大小(嵌入矩阵)
fitting_net > neuron 拟合神经网络的大小
spin > use_spin 原子是否具有磁性
spin > virtual_len 赝原子和真实原子距离
spin > spin_norm 磁性原子磁矩模长

使用se_e2_a描述符来训练DP模型。neurons的参数将描述符和拟合网络的大小分别设置为[25, 50, 100]和[240, 240, 240]。本地环境中的组成部分会在从4.6到5.6 Å的范围内平滑地趋于零。

使用spin的参数控制磁性原子的信息,该例中,Ni原子具有磁性,O原子不具有磁性,因此,设置use_spin的参数为[true, false],第一个值true表示对Ni原子打开磁性,第二个值false表示对O原子不打开磁性。后续virtual_lenspin_norm参数需要针对磁性的原子进行设置。本次NiO模型中,设置了Ni原子的距离为0.4 Å,磁矩大小为1.2737 uB。

以下是指定学习率和损失函数的参数。

    "learning_rate" : {
        "type":		    "exp",
        "decay_steps":	5000,
        "start_lr":	    1.00e-3,	
        "stop_lr":	    3.51e-8,
        "_comment":	    " that's all"
    },

    "loss" : {
        "type":		        "ener_spin",
        "start_pref_e":	    0.02,
        "limit_pref_e":	    200,
        "start_pref_fr":	1000,
        "limit_pref_fr":	1.0,
        "start_pref_fm":	1000,
        "limit_pref_fm":	5.0,
        "start_pref_v":	    0,
        "limit_pref_v":	    0,
        "_comment":	        " that's all"
    },

在损失函数中,pref_e从 0.02 逐渐增加到 200,pref_fr从 1000 逐渐减小到 1,pref_fm从 1000 逐渐减小到 5,这意味着原子力和磁性力项在开始时占主导地位,而能量项在最后变得重要。这种策略非常有效,并减少了总的训练时间。pref_v设置为 0,表示训练过程中不包括压力数据。起始学习率、终止学习率和衰减步数分别设置为 0.001、 和 5000。模型训练 1000000 步。

训练参数如下所示:

    "training" : {
        "training_data": {
            "systems":            ["../00.data/training_data"],     
            "batch_size":         "auto",                       
            "_comment":           "that's all"
        },
        "validation_data":{
            "systems":            ["../00.data/validation_data/"],
            "batch_size":         "auto",               
            "numb_btch":          1,
            "_comment":           "that's all"
        },
        "numb_steps":             1000000,                           
        "seed":                   10,
        "disp_file":              "lcurve.out",
        "disp_freq":              1000,
        "save_freq":              100000,
        },
代码
文本

3 训练模型

准备好训练脚本后,我们可以通过简单地运行 DeepSPIN 来开始训练。

代码
文本
[19]
# ########## Time Warning: 13 hours+ ##########
! cd DeepSPIN_Tutorial/01.train/ && dp train input.json
2023-06-29 14:22:53.013178: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 14:22:53.474420: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 14:22:53.475835: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-06-29 14:22:54.874845: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING:root:To get the best performance, it is recommended to adjust the number of threads by setting the environment variables OMP_NUM_THREADS, TF_INTRA_OP_PARALLELISM_THREADS, and TF_INTER_OP_PARALLELISM_THREADS. See https://deepmd.rtfd.io/parallelism/ for more information.
DEEPMD INFO    Calculate neighbor statistics... (add --skip-neighbor-stat to skip this step)
2023-06-29 14:23:00.618748: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: ghost idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
DEEPMD INFO    training data with min nbor dist: 0.39999999202726677
DEEPMD INFO    training data with max nbor size: [45 42 45]
DEEPMD INFO     _____               _____   __  __  _____           _     _  _   
DEEPMD INFO    |  __ \             |  __ \ |  \/  ||  __ \         | |   (_)| |  
DEEPMD INFO    | |  | |  ___   ___ | |__) || \  / || |  | | ______ | | __ _ | |_ 
DEEPMD INFO    | |  | | / _ \ / _ \|  ___/ | |\/| || |  | ||______|| |/ /| || __|
DEEPMD INFO    | |__| ||  __/|  __/| |     | |  | || |__| |        |   < | || |_ 
DEEPMD INFO    |_____/  \___| \___||_|     |_|  |_||_____/         |_|\_\|_| \__|
DEEPMD INFO    Please read and cite:
DEEPMD INFO    Wang, Zhang, Han and E, Comput.Phys.Comm. 228, 178-184 (2018)
DEEPMD INFO    Zeng et al, arXiv:2304.09409
DEEPMD INFO    See https://deepmd.rtfd.io/credits/ for details.
DEEPMD INFO    installed to:         /root/soft/deepspin/deepmd-kit/_skbuild/linux-x86_64-3.10/cmake-install
DEEPMD INFO    source :              v2.2.2
DEEPMD INFO    source brach:         master
DEEPMD INFO    source commit:        92ca097b
DEEPMD INFO    source commit at:     2023-05-24 13:45:03 +0800
DEEPMD INFO    build float prec:     double
DEEPMD INFO    build variant:        cpu
DEEPMD INFO    build with tf inc:    /opt/mamba/lib/python3.10/site-packages/tensorflow/include;/opt/mamba/lib/python3.10/site-packages/tensorflow/include
DEEPMD INFO    build with tf lib:    
DEEPMD INFO    ---Summary of the training---------------------------------------
DEEPMD INFO    running on:           bohrium-14073-1024925
DEEPMD INFO    computing device:     cpu:0
DEEPMD INFO    CUDA_VISIBLE_DEVICES: unset
DEEPMD INFO    Count of visible GPU: 0
DEEPMD INFO    num_intra_threads:    0
DEEPMD INFO    num_inter_threads:    0
DEEPMD INFO    -----------------------------------------------------------------
DEEPMD INFO    ---Summary of DataSystem: training     -----------------------------------------------
DEEPMD INFO    found 1 system(s):
DEEPMD INFO                                        system  natoms  bch_sz   n_bch   prob  pbc
DEEPMD INFO                      ../00.data/training_data      48       1     160  1.000    T
DEEPMD INFO    --------------------------------------------------------------------------------------
DEEPMD INFO    ---Summary of DataSystem: validation   -----------------------------------------------
DEEPMD INFO    found 1 system(s):
DEEPMD INFO                                        system  natoms  bch_sz   n_bch   prob  pbc
DEEPMD INFO                    ../00.data/validation_data      48       1      40  1.000    T
DEEPMD INFO    --------------------------------------------------------------------------------------
DEEPMD INFO    training without frame parameter
DEEPMD INFO    data stating... (this step may take long time)
DEEPMD INFO    built lr
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/util/dispatch.py:1176: accumulate_n (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.math.add_n` Instead
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/util/dispatch.py:1176: accumulate_n (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.math.add_n` Instead
DEEPMD INFO    built network
DEEPMD INFO    built training
WARNING:root:To get the best performance, it is recommended to adjust the number of threads by setting the environment variables OMP_NUM_THREADS, TF_INTRA_OP_PARALLELISM_THREADS, and TF_INTER_OP_PARALLELISM_THREADS. See https://deepmd.rtfd.io/parallelism/ for more information.
DEEPMD INFO    initialize model from scratch
DEEPMD INFO    start training at lr 1.00e-03 (== 1.00e-03), decay_step 5000, decay_rate 0.950006, final lr will be 3.51e-08
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/train/trainer.py:1477: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, there are two
    options available in V2.
    - tf.py_function takes a python function which manipulates tf eager
    tensors instead of numpy arrays. It's easy to convert a tf eager tensor to
    an ndarray (just call tensor.numpy()) but having access to eager tensors
    means `tf.py_function`s can use accelerators such as GPUs as well as
    being differentiable using a gradient tape.
    - tf.numpy_function maintains the semantics of the deprecated tf.py_func
    (it is not differentiable, and manipulates numpy arrays). It drops the
    stateful argument making all functions stateful.
    
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/train/trainer.py:1477: py_func (from tensorflow.python.ops.script_ops) is deprecated and will be removed in a future version.
Instructions for updating:
tf.py_func is deprecated in TF V2. Instead, there are two
    options available in V2.
    - tf.py_function takes a python function which manipulates tf eager
    tensors instead of numpy arrays. It's easy to convert a tf eager tensor to
    an ndarray (just call tensor.numpy()) but having access to eager tensors
    means `tf.py_function`s can use accelerators such as GPUs as well as
    being differentiable using a gradient tape.
    - tf.numpy_function maintains the semantics of the deprecated tf.py_func
    (it is not differentiable, and manipulates numpy arrays). It drops the
    stateful argument making all functions stateful.
    
DEEPMD INFO    batch    1000 training time 47.98 s, testing time 0.04 s, total wall time 49.23 s
DEEPMD INFO    batch    2000 training time 45.86 s, testing time 0.04 s, total wall time 46.27 s
^C
Traceback (most recent call last):
  File "/opt/mamba/bin/dp", line 8, in <module>
    sys.exit(main())
  File "/opt/mamba/lib/python3.10/site-packages/deepmd/entrypoints/main.py", line 623, in main
    train_dp(**dict_args)
  File "/opt/mamba/lib/python3.10/site-packages/deepmd/entrypoints/train.py", line 164, in train
    _do_work(jdata, run_opt, is_compress)
  File "/opt/mamba/lib/python3.10/site-packages/deepmd/entrypoints/train.py", line 278, in _do_work
    model.train(train_data, valid_data)
  File "/opt/mamba/lib/python3.10/site-packages/deepmd/train/trainer.py", line 998, in train
    _, next_train_batch_list = run_sess(
  File "/opt/mamba/lib/python3.10/site-packages/deepmd/utils/sess.py", line 30, in run_sess
    return sess.run(*args, **kwargs)
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 968, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1191, in _run
    results = self._do_run(handle, final_targets, final_fetches,
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1371, in _do_run
    return self._do_call(_run_fn, feeds, fetches, targets, options,
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1378, in _do_call
    return fn(*args)
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1361, in _run_fn
    return self._call_tf_sessionrun(options, feed_dict, fetch_list,
  File "/opt/mamba/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1454, in _call_tf_sessionrun
    return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,
KeyboardInterrupt
代码
文本

屏幕上会显示数据系统的信息,例如:

DEEPMD INFO    ---Summary of DataSystem: training     -----------------------------------------------
DEEPMD INFO    found 1 system(s):
DEEPMD INFO                                        system  natoms  bch_sz   n_bch   prob  pbc
DEEPMD INFO                      ../00.data/training_data      48       1      50  1.000    T
DEEPMD INFO    --------------------------------------------------------------------------------------
DEEPMD INFO    ---Summary of DataSystem: validation   -----------------------------------------------
DEEPMD INFO    found 1 system(s):
DEEPMD INFO                                        system  natoms  bch_sz   n_bch   prob  pbc
DEEPMD INFO                    ../00.data/validation_data      48       1       9  1.000    T
DEEPMD INFO    --------------------------------------------------------------------------------------

以及该训练的起始和最终学习率:

DEEPMD INFO    start training at lr 1.00e-03 (== 1.00e-03), decay_step 50, decay_rate 0.950006, final lr will be 3.51e-08

如果一切正常,您将看到每 1000 batch 打印的信息,例如:

DEEPMD INFO    batch    1000 training time 49.18 s, testing time 0.21 s, total wall time 50.79 s
DEEPMD INFO    batch    2000 training time 47.20 s, testing time 0.21 s, total wall time 47.78 s
DEEPMD INFO    batch    3000 training time 47.07 s, testing time 0.21 s, total wall time 47.64 s
DEEPMD INFO    batch    4000 training time 46.87 s, testing time 0.23 s, total wall time 47.47 s
DEEPMD INFO    batch    5000 training time 47.07 s, testing time 0.21 s, total wall time 47.65 s
DEEPMD INFO    batch    6000 training time 47.15 s, testing time 0.20 s, total wall time 47.72 s
DEEPMD INFO    batch    7000 training time 47.28 s, testing time 0.21 s, total wall time 47.86 s
DEEPMD INFO    batch    8000 training time 47.02 s, testing time 0.23 s, total wall time 47.61 s

它们显示了训练和测试时间计数。在每 100000 batch 结束时,模型将保存在 Tensorflow 的 checkpoint 文件 model.ckpt 中。

同时,训练和测试误差将在文件lcurve.out中呈现。

#  step      rmse_val    rmse_trn    rmse_e_val  rmse_e_trn   rmse_fr_val rmse_fr_trn   rmse_fm_val rmse_fm_trn         lr
      0      8.42e-01    1.50e+00      7.93e-02    7.93e-02      2.43e-02    2.27e-02      1.02e-02    4.15e-02    3.0e-03
   1000      4.42e-01    5.46e-01      1.67e-03    1.49e-03      9.05e-03    1.25e-02      1.03e-02    1.19e-02    3.0e-03
   2000      4.17e-01    3.92e-01      2.03e-02    2.07e-02      7.75e-03    8.39e-03      1.05e-02    9.09e-03    3.0e-03
   3000      4.16e-01    4.61e-01      2.66e-02    2.62e-02      6.81e-03    8.14e-03      1.10e-02    1.21e-02    3.0e-03
   4000      3.88e-01    3.69e-01      4.38e-02    4.39e-02      5.17e-03    5.40e-03      1.09e-02    1.03e-02    3.0e-03
   5000      3.35e-01    2.34e-01      4.87e-03    4.86e-03      4.83e-03    2.17e-03      9.59e-03    7.31e-03    2.8e-03
   6000      3.66e-01    3.88e-01      2.82e-03    2.93e-03      4.09e-03    5.53e-03      1.12e-02    1.14e-02    2.8e-03
   7000      3.70e-01    3.00e-01      5.74e-03    5.98e-03      5.85e-03    4.49e-03      1.05e-02    8.68e-03    2.8e-03
   8000      3.35e-01    1.30e+00      2.18e-02    2.19e-02      4.38e-03    1.24e-02      9.83e-03    4.05e-02    2.8e-03

该文件包含 10 列,从左到右依次是:

  1. 训练步数
  2. 验证损失
  3. 训练损失
  4. 能量的均方根(RMS)验证误差
  5. 能量的 RMS 训练误差
  6. 原子力的 RMS 验证误差
  7. 原子力的 RMS 训练误差
  8. 磁性力的 RMS 验证误差
  9. 磁性力的 RMS 训练误差
  10. 学习率

学习率是机器学习中的一个重要概念。在 DP 模型中,学习率会经历一个 从大到小指数衰减的过程。这样既能保证模型收敛的效率,也能保证模型的精度。因此在学习率的参数中,有起始学习率(start_lr)和结束学习率(end_rate) 两种。在上面的例子中,我们将起始学习率、结束学习率和学习率的衰减步长分别设置为 0.001,3.51e-8,和 50,那么模型学习率会从 0.001 开始,每 50 步降低一点,直到降低到 3.51e-8(或者训练结束)为止。

代码
文本

我们来看一下 lcurve.out 文件的初始与结束两行。

代码
文本
[20]
! cd DeepSPIN_Tutorial/01.train/ && head -n 2 lcurve_bak.out && tail -n 2 lcurve_bak.out
#  step      rmse_val    rmse_trn    rmse_e_val  rmse_e_trn   rmse_fr_val rmse_fr_trn   rmse_fm_val rmse_fm_trn         lr
      0      6.60e-01    8.06e-01      7.95e-02    7.93e-02      2.01e-02    2.49e-02      4.88e-03    4.80e-03    1.0e-03
   1000      9.67e-02    2.93e-01      1.07e-02    1.10e-02      2.30e-03    8.56e-03      1.98e-03    3.52e-03    1.0e-03
   2000      1.49e-01    2.13e-01      3.11e-03    3.14e-03      4.69e-03    6.64e-03      2.50e-04    1.07e-03    1.0e-03
代码
文本

下面我们对损失函数进行可视化展示

代码
文本
[42]
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

with open("./DeepSPIN_Tutorial/01.train/lcurve_bak.out") as f:
headers = f.readline().split()[1:]
lcurve = pd.DataFrame(np.loadtxt("./DeepSPIN_Tutorial/01.train/lcurve_bak.out"), columns=headers)
legends = ["rmse_e_val", "rmse_fr_val" , "rmse_fm_val" ]
legends_label = ["rmse_e_val (eV)", "rmse_fr_val (eV/A)" , "rmse_fm_val (eV/uB)" ]
fig, ax = plt.subplots()
for legend in legends:
ax.plot(lcurve["step"], lcurve[legend], label = legends_label[legends.index(legend)], linewidth=1)
ax.set_yscale('log')
ax.legend()
ax.set_xlabel("Training steps")
ax.set_ylabel("Loss")
plt.show()
代码
文本

4 冻结模型

在训练结束时,应该将保存在 TensorFlow 的 checkpoint 文件中的模型参数冻结为一个模型文件,通常以扩展名 .pb 结束。只需执行以下命令:

代码
文本
[3]
# # 进入 DeepSPIN_Tutorial/01.train/ 训练文件夹并冻结模型
! cd DeepSPIN_Tutorial/01.train/ && dp freeze -o graph.pb
2023-06-29 18:21:28.951520: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 18:21:29.007263: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 18:21:29.007936: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-06-29 18:21:30.447795: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING:root:To get the best performance, it is recommended to adjust the number of threads by setting the environment variables OMP_NUM_THREADS, TF_INTRA_OP_PARALLELISM_THREADS, and TF_INTER_OP_PARALLELISM_THREADS. See https://deepmd.rtfd.io/parallelism/ for more information.
2023-06-29 18:21:35.007303: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
DEEPMD INFO    The following nodes will be frozen: ['model_type', 'descrpt_attr/rcut', 'descrpt_attr/ntypes', 'model_attr/tmap', 'model_attr/model_type', 'model_attr/model_version', 'train_attr/min_nbor_dist', 'train_attr/training_script', 'o_energy', 'o_force', 'o_virial', 'o_atom_energy', 'o_atom_virial', 'spin_attr/ntypes_spin', 'fitting_attr/dfparam', 'fitting_attr/daparam']
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/entrypoints/freeze.py:367: convert_variables_to_constants (from tensorflow.python.framework.convert_to_constants) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/entrypoints/freeze.py:367: convert_variables_to_constants (from tensorflow.python.framework.convert_to_constants) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/framework/convert_to_constants.py:952: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/framework/convert_to_constants.py:952: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
DEEPMD INFO    2335 ops in the final graph.
代码
文本

它将在当前目录中输出一个名为 graph.pb 的模型文件。

代码
文本

到目前为止,我们就获得了一个使用 DeepSPIN 通过高精度的NiO数据获得的深度势能模型:DeepSPINt_Tutorial/01.train/graph.pb

代码
文本

5 测试模型

让我们来检查一下训练模型的效果:

代码
文本
[4]
! cd DeepSPIN_Tutorial/01.train/ && dp test -m graph.pb -s ../00.data/validation_data -d detail
2023-06-29 18:21:40.173359: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 18:21:40.228926: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-06-29 18:21:40.229567: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-06-29 18:21:41.112876: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
WARNING:root:To get the best performance, it is recommended to adjust the number of threads by setting the environment variables OMP_NUM_THREADS, TF_INTRA_OP_PARALLELISM_THREADS, and TF_INTER_OP_PARALLELISM_THREADS. See https://deepmd.rtfd.io/parallelism/ for more information.
2023-06-29 18:21:42.968629: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/utils/batch_size.py:61: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
WARNING:tensorflow:From /opt/mamba/lib/python3.10/site-packages/deepmd/utils/batch_size.py:61: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
DEEPMD WARNING You can use the environment variable DP_INFER_BATCH_SIZE tocontrol the inference batch size (nframes * natoms). The default value is 1024.
DEEPMD INFO    # ---------------output of dp test--------------- 
DEEPMD INFO    # testing system : ../00.data/validation_data
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
# warning: loc idx out of upper bound (ignored if warned for more than 10 times) 
# warning: loc idx out of lower bound (ignored if warned for more than 10 times) 
DEEPMD INFO    # number of test data : 40 
DEEPMD INFO    Energy MAE         : 4.218759e-04 eV
DEEPMD INFO    Energy RMSE        : 5.139633e-04 eV
DEEPMD INFO    Energy MAE/Natoms  : 8.789082e-06 eV
DEEPMD INFO    Energy RMSE/Natoms : 1.070757e-05 eV
DEEPMD INFO    Force atom MAE      : 4.454114e-04 eV/A
DEEPMD INFO    Force spin MAE      : 5.101605e-04 eV/uB
DEEPMD INFO    Force atom RMSE     : 7.330575e-04 eV/A
DEEPMD INFO    Force spin RMSE     : 8.777647e-04 eV/uB
DEEPMD INFO    Virial MAE         : 6.327634e+00 eV
DEEPMD INFO    Virial RMSE        : 1.061004e+01 eV
DEEPMD INFO    Virial MAE/Natoms  : 1.318257e-01 eV
DEEPMD INFO    Virial RMSE/Natoms : 2.210425e-01 eV
DEEPMD INFO    # ----------------------------------------------- 
代码
文本

DeepSPIN模型的能量、原子力、磁性力的预测结果保存在detail.e.outdetail.fr.outdetail.fm.out中。

下面让我们看下分别看下能量的预测结果,并进行可视化。

代码
文本
[5]
! head -n 5 DeepSPIN_Tutorial/01.train/detail.e.out
# ../00.data/validation_data: data_e pred_e
-1.663128020599999957e+02 -1.663124941474969773e+02
-1.663122044899999992e+02 -1.663111771631338911e+02
-1.663113797100000113e+02 -1.663108779744183607e+02
-1.663116977100000042e+02 -1.663117939932237164e+02
代码
文本

其中,第一列为构型DFT能量,第二列为模型预测的能量。

代码
文本
[6]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
e_out = pd.DataFrame(np.loadtxt("./DeepSPIN_Tutorial/01.train/detail.e.out"), columns=['data_e', 'pred_e'])
plt.scatter(e_out["data_e"]/32, e_out["pred_e"]/32, s=10)

x_range = np.linspace(plt.xlim()[0], plt.xlim()[1])

plt.plot(x_range, x_range, "r--", linewidth = 0.25)
plt.xlabel("Atomic energy of DFT (eV/atom)") # 设置 x 轴标题
plt.ylabel("Atomic energy predicted by DeepSPIN (eV/atom)") # 设置 y 轴标题
plt.show()
代码
文本

接下来我们看一下NiO模型的原子力预测结果。

代码
文本
[7]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fr_out = pd.DataFrame(np.loadtxt("./DeepSPIN_Tutorial/01.train/detail.fr.out"), columns=['data_frx', 'data_fry', 'data_frz', 'pred_frx', 'pred_fry', 'pred_frz'])
plt.scatter(fr_out["data_frx"], fr_out["pred_frx"], label='$F_{r_x}$', s=10)
plt.scatter(fr_out["data_fry"], fr_out["pred_fry"], label='$F_{r_y}$', s=10)
plt.scatter(fr_out["data_frz"], fr_out["pred_frz"], label='$F_{r_z}$', s=10)

x_range = np.linspace(plt.xlim()[0], plt.xlim()[1])

plt.plot(x_range, x_range, "r--", linewidth = 0.25)
plt.xlabel("Atomic force of DFT (eV/A)") # 设置 x 轴标题
plt.ylabel("Atomic force predicted by deep potential (eV/A)") # 设置 y 轴标题
plt.legend()
plt.show()
代码
文本

NiO模型磁性力的预测结果如下。

代码
文本
[8]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
fm_out = pd.DataFrame(np.loadtxt("./DeepSPIN_Tutorial/01.train/detail.fm.out"), columns=['data_fmx', 'data_fmy', 'data_fmz', 'pred_fmx', 'pred_fmy', 'pred_fmz'])
plt.scatter(fm_out["data_fmx"], fm_out["pred_fmx"], label='$F_{m_x}$', s=10)
plt.scatter(fm_out["data_fmy"], fm_out["pred_fmy"], label='$F_{m_y}$', s=10)
plt.scatter(fm_out["data_fmz"], fm_out["pred_fmz"], label='$F_{m_z}$', s=10)

x_range = np.linspace(plt.xlim()[0], plt.xlim()[1])

plt.plot(x_range, x_range, "r--", linewidth = 0.25)
plt.xlabel("Magnetic force of DFT (eV/uB)") # 设置 x 轴标题
plt.ylabel("Magnetic force predicted by deep potential (eV/uB)") # 设置 y 轴标题
plt.legend()
plt.show()
代码
文本

6 使用 LAMMPS 进行 SLD 计算

DeepSPIN模型可以在 LAMMPS 中调用,进行自旋—晶格动力学(SLD)模拟。这部分功能是通过修改 DeepSPIN 与 LAMMPS SPIN模块的C++接口实现的。

目前利用DeepSPIN模型可以进行能量最小化和自旋——晶格动力学演化两个计算任务。

下面,我们通过NiO晶界的例子说明如何进行能量最小化计算。

代码
文本

进行能量最小化计算的主要输入文件有: in.lammps data.lmp model.pb。其中,model.pb 就是训练好的DeepSPIN模型。

代码
文本
[40]
! cp ./DeepSPIN_Tutorial/01.train/graph.pb ./DeepSPIN_Tutorial/02.lmp/model.pb && tree ./DeepSPIN_Tutorial/02.lmp -L 1
./DeepSPIN_Tutorial/02.lmp
|-- 0.dump
|-- 67.dump
|-- data.lmp
|-- in.lammps
|-- log.lammps
`-- model.pb

0 directories, 6 files
代码
文本
[33]
! cd ./DeepSPIN_Tutorial/02.lmp && head -n 20 data.lmp
2880 atoms
2 atom types
   0.0000000000   80.0046997070 xlo xhi
   0.0000000000    8.4331998825 ylo yhi
   0.0000000000   38.7482986450 zlo zhi
   0.0000000000    0.0000000000    0.0000000000 xy xz yz

Masses

1 58.690000
2 16.000000

Atoms # spin

     1      1  11.3430700000 	2.0963200000 	0.0073600000   -0.51638874 -0.81647984 -0.25826989  1.2737  
     2      1  24.6774500000 	2.0963200000 	0.0073600000   -0.51638874 -0.81647984 -0.25826989  1.2737  
     3      1  4.6762700000 	2.0963200000 	0.0073600000   -0.51638874 -0.81647984 -0.25826989  1.2737
     4      1  18.0106600000 	2.0963200000 	0.0073600000   -0.51638874 -0.81647984 -0.25826989  1.2737
     5      1  38.0118300000 	2.0963200000 	0.0073600000   -0.51638874 -0.81647984 -0.25826989  1.2737
代码
文本

这里的data.lmp给出了NiO晶界模拟的初始构型。其中大部分内容和一般的LAMMSPS结构文件相同,但对于体系中的每个原子,除了需要给出原子位置坐标外,还需要给出原子磁矩在笛卡尔坐标系下3个方向的分量和磁矩模长,即Atoms # spin后6~9列的内容。

代码
文本
[34]
! cd ./DeepSPIN_Tutorial/02.lmp && cat in.lammps
#!/bin/sh
units             metal
dimension         3
boundary          p p p
atom_style        spin
atom_modify       map array
box               tilt large
read_data         data.lmp

pair_style        deepmd model.pb virtual_len 0.4 spin_norm 1.2737
pair_coeff        * *

neighbor          0.1 bin
neigh_modify      every 10 check yes delay 20

delete_atoms overlap 0.05 all all

compute           s all property/atom sp spx spy spz fmx fmy fmz fx fy fz

dump 1 all custom 100 *.dump id x y z c_s[1] c_s[2] c_s[3] c_s[4] c_s[5] c_s[6] c_s[7] c_s[8] c_s[9] c_s[10]

thermo_style      custom vol pe etotal
thermo            100
thermo_modify     line one format float %10.8f

min_style       spin/lbfgs
min_modify      line spin_cubic discrete_factor 10.0
minimize        1.0e-12 1.0e-12 10000 20000

代码
文本

in.lammps是LAMMPS的输入文件,用来优化NiO晶界的磁矩方向以实现能量最小化,其中大部分命令和通常的 LAMMPS 分子动力学模拟输入文件类似(关于 LAMMPS 分子动力学模拟输入文件的更多信息,可参阅LAMMPS手册)

其中,调用势函数的命令与标准的DeepMD类似,不过,在DeepSPIN势函数中,需要额外指定 virtual_lenspin_norm 两个参数,设置与DeepSPIN模型训练脚本中的spin参数保持一致。

pair_style  deepmd model.pb virtual_len 0.4 spin_norm 1.2737
pair_coeff  * *

与能量最小化相关的命令为最后三行,其中,min_stylemin_modify 共同指定了自旋优化的算法和参数,可参阅LAMMPS SPIN手册minimize 指定了能量和原子力的收敛判据

min_style       spin/lbfgs
min_modify      line spin_cubic discrete_factor 10.0
minimize        1.0e-12 1.0e-12 10000 20000
代码
文本

在准备好输入文件后,执行以下命令即可提交 LAMMPS 任务进行NiO晶界能量最小化计算。

代码
文本
[1]
# ########## Time Warning: 3 min ##########
! cd /data/DeepSPIN_Tutorial/02.lmp && ~/soft/lammps_23Jun2022/src/lmp_mpi -i in.lammps
2023-06-29 18:52:31.346957: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
LAMMPS (23 Jun 2022 - Update 1)
Reading data file ...
  triclinic box = (0 0 0) to (80.0047 8.4331999 38.748299) with tilt (0 0 0)
  1 by 1 by 1 MPI processor grid
  reading atoms ...
  2880 atoms
  read_data CPU = 0.111 seconds
DeePMD-kit WARNING: Environmental variable TF_INTRA_OP_PARALLELISM_THREADS is not set. Tune TF_INTRA_OP_PARALLELISM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
DeePMD-kit WARNING: Environmental variable TF_INTER_OP_PARALLELISM_THREADS is not set. Tune TF_INTER_OP_PARALLELISM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
DeePMD-kit WARNING: Environmental variable OMP_NUM_THREADS is not set. Tune OMP_NUM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
Summary of lammps deepmd module ...
  >>> Info of deepmd-kit:
  installed to:       /root/soft/deepspin/deepmd_root
  source:             v2.2.2
  source branch:       master
  source commit:      92ca097b
  source commit at:   2023-05-24 13:45:03 +0800
  surpport model ver.:1.1 
  build variant:      cpu
  build with tf inc:  /opt/mamba/lib/python3.10/site-packages/tensorflow/include;/opt/mamba/lib/python3.10/site-packages/tensorflow/include
  build with tf lib:  /opt/mamba/lib/python3.10/site-packages/tensorflow/libtensorflow_cc.so.2
  set tf intra_op_parallelism_threads: 0
  set tf inter_op_parallelism_threads: 0
  >>> Info of lammps module:
  use deepmd-kit at:  /root/soft/deepspin/deepmd_root
  source:             v2.2.2
  source branch:      master
  source commit:      92ca097b
  source commit at:   2023-05-24 13:45:03 +0800
  build float prec:   double
  build with tf inc:  /opt/mamba/lib/python3.10/site-packages/tensorflow/include;/opt/mamba/lib/python3.10/site-packages/tensorflow/include
  build with tf lib:  /opt/mamba/lib/python3.10/site-packages/tensorflow/libtensorflow_cc.so.2
DeePMD-kit WARNING: Environmental variable TF_INTRA_OP_PARALLELISM_THREADS is not set. Tune TF_INTRA_OP_PARALLELISM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
DeePMD-kit WARNING: Environmental variable TF_INTER_OP_PARALLELISM_THREADS is not set. Tune TF_INTER_OP_PARALLELISM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
DeePMD-kit WARNING: Environmental variable OMP_NUM_THREADS is not set. Tune OMP_NUM_THREADS for the best performance. See https://deepmd.rtfd.io/parallelism/ for more information.
2023-06-29 18:52:32.427079: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-06-29 18:52:32.495243: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
  >>> Info of model(s):
  using   1 model(s): model.pb 
  rcut in model:      5.6
  ntypes in model:    3
  using compute id:        
System init for delete_atoms ...
Generated 0 of 1 mixed pair_coeff terms from geometric mixing rule
Neighbor list info ...
  update every 10 steps, delay 20 steps, check yes
  max neighbors/atom: 2000, page size: 100000
  master list distance cutoff = 5.7
  ghost atom cutoff = 5.7
  binsize = 2.85, bins = 29 3 14
  2 neighbor lists, perpetual/occasional/extra = 1 1 0
  (1) command delete_atoms, occasional, copy from (2)
      attributes: full, newton on
      pair build: copy
      stencil: none
      bin: none
  (2) pair deepmd, perpetual
      attributes: full, newton on
      pair build: full/bin/atomonly
      stencil: full/bin/3d
      bin: standard
Deleted 48 atoms, new total = 2832

CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE

Your simulation uses code contributions which should be cited:
- USER-DEEPMD package:
- min_style spin/lbfgs command:
The log file lists these citations in BibTeX format.

CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE

WARNING: Using 'neigh_modify every 1 delay 0 check yes' setting during minimization (../min.cpp:187)
WARNING: Line search incompatible gneb (../min_spin_lbfgs.cpp:108)
Generated 0 of 1 mixed pair_coeff terms from geometric mixing rule
Neighbor list info ...
  update every 1 steps, delay 0 steps, check yes
  max neighbors/atom: 2000, page size: 100000
  master list distance cutoff = 5.7
  ghost atom cutoff = 5.7
  binsize = 2.85, bins = 29 3 14
  1 neighbor lists, perpetual/occasional/extra = 1 0 0
  (1) pair deepmd, perpetual
      attributes: full, newton on
      pair build: full/bin/atomonly
      stencil: full/bin/3d
      bin: standard
Setting up spin/lbfgs style minimization ...
  Unit style    : metal
  Current step  : 0
Per MPI rank memory allocation (min/avg/max) = 8.86 | 8.86 | 8.86 Mbytes
    Volume         PotEng         TotEng    
26143.30753976 -14656.12464332 -14656.12464332
26143.30753976 -14673.15632414 -14673.15632414
Loop time of 132.703 on 1 procs for 67 steps with 2832 atoms

1234.9% CPU use with 1 MPI tasks x no OpenMP threads

Minimization stats:
  Stopping criterion = energy tolerance
  Energy initial, next-to-last, final = 
     -14656.1246433169  -14673.1563241283  -14673.1563241416
  Force two-norm initial, final = 59.508486 58.803457
  Force max component initial, final = 2.9980712 3.0199425
  Final line search alpha, max atom move = 0 0
  Iterations, force evaluations = 67 68

MPI task timing breakdown:
Section |  min time  |  avg time  |  max time  |%varavg| %total
---------------------------------------------------------------
Pair    | 132.62     | 132.62     | 132.62     |   0.0 | 99.93
Neigh   | 0          | 0          | 0          |   0.0 |  0.00
Comm    | 0.021493   | 0.021493   | 0.021493   |   0.0 |  0.02
Output  | 0          | 0          | 0          |   0.0 |  0.00
Modify  | 0          | 0          | 0          |   0.0 |  0.00
Other   |            | 0.06625    |            |       |  0.05

Nlocal:           2832 ave        2832 max        2832 min
Histogram: 1 0 0 0 0 0 0 0 0 0
Nghost:           6546 ave        6546 max        6546 min
Histogram: 1 0 0 0 0 0 0 0 0 0
Neighs:              0 ave           0 max           0 min
Histogram: 1 0 0 0 0 0 0 0 0 0
FullNghs:       230880 ave      230880 max      230880 min
Histogram: 1 0 0 0 0 0 0 0 0 0

Total # of neighbors = 230880
Ave neighs/atom = 81.525424
Neighbor list builds = 0
Dangerous builds = 0
Total wall time: 0:02:15
代码
文本

当打印出下述信息,表示能量最小化已经完成,可以看到计算完成后,已经达到了能量收敛判据。

Minimization stats:
  Stopping criterion = energy tolerance
  Energy initial, next-to-last, final = 
     -14656.1246433169  -14673.1563241283  -14673.1563241416
代码
文本

主要的输出文件为log.lammps 0.dump 67.dump,其中,log.lammps文件给出了LAMMPS运行时的输出信息;0.dump 67.dump文件就是上面提到的用dump命令输出的文件,里面包含每个时间步体系中每个原子的位置,磁矩,磁性力和原子力,数字代表minimize的迭代步数

代码
文本
[3]
! head -n 30 DeepSPIN_Tutorial/02.lmp/0.dump
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
2832
ITEM: BOX BOUNDS xy xz yz pp pp pp
0.0000000000000000e+00 8.0004699707000000e+01 0.0000000000000000e+00
0.0000000000000000e+00 8.4331998825000003e+00 0.0000000000000000e+00
0.0000000000000000e+00 3.8748298644999998e+01 0.0000000000000000e+00
ITEM: ATOMS id x y z c_s[1] c_s[2] c_s[3] c_s[4] c_s[5] c_s[6] c_s[7] c_s[8] c_s[9] c_s[10]
1 74.0139 8.42122 24.0879 0 0 0 1 0 0 0 -0.040416 0.00526837 -0.870171
2 63.3461 8.42122 29.4216 0 0 0 1 0 0 0 0.00186022 0.000116457 0.000917555
3 79.3471 8.42122 34.7553 0 0 0 1 0 0 0 -0.300093 0.00154569 -1.37227
4 59.3459 8.42122 21.4212 0 0 0 1 0 0 0 1.83373 0.0172092 -0.530049
5 75.3468 8.42122 26.7549 0 0 0 1 0 0 0 0.0205602 -0.00230077 0.112241
6 64.6798 8.42122 32.0886 0 0 0 1 0 0 0 0.0348865 0.00271567 0.13419
7 54.0128 8.42122 37.4223 0 0 0 1 0 0 0 0.951196 0.00522673 -2.99553
8 60.6796 8.42122 24.0879 0 0 0 1 0 0 0 -0.0393569 0.00531156 -0.870092
9 76.6805 8.42122 29.4216 0 0 0 1 0 0 0 -2.63076e-06 8.50603e-05 0.0013302
10 66.0135 8.42122 34.7553 0 0 0 1 0 0 0 -0.305564 0.00149207 -1.37128
11 72.6803 8.42122 21.4212 0 0 0 1 0 0 0 1.83279 0.0171116 -0.531242
12 62.0132 8.42122 26.7549 0 0 0 1 0 0 0 0.0168948 -0.00235642 0.112991
13 11.3431 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.5509 40.6885 22.9967 -0.726702 0.0831648 0.00153699
14 24.6775 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.3538 40.6862 22.9961 -0.728981 0.083172 0.00149955
15 4.67627 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.3553 40.6866 22.9965 -0.72894 0.0831724 0.00150025
16 18.0107 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.3444 40.6687 22.9869 -0.72812 0.0831795 0.00147424
17 38.0118 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.3463 40.6689 22.9873 -0.728065 0.0831792 0.00147494
18 51.3454 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.5509 40.6885 22.9967 -0.72669 0.0831651 0.00153707
19 31.3442 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.5503 40.6895 22.9971 -0.726666 0.0831641 0.00153771
20 44.6786 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.355 40.6865 22.9965 -0.728951 0.0831722 0.00150005
21 64.6798 2.09632 0.00736 1.2737 -0.516389 -0.81648 -0.25827 25.3544 40.6864 22.996 -0.728987 0.0831718 0.00149921
代码
文本

接下来,我们介绍下与自旋——晶格动力学计算相关的命令。

fix nve/spin设置了时间步积分方式moving 表示自旋演化与晶格演化是相互耦合的,即进行自旋——晶格动力学计算。若改成 frozen,则表示固定晶格,只进行自旋动力学计算。

timestep表示时间积分的步长,单位为ps,这里对应的是0.1 fs

最后,通过 run 命令设置动力学演化的步数。

fix             1 all nve/spin lattice moving

timestep        0.0001

run             1000

此外,对 NVT 系综的模拟可以通过 fix 命令加入 Langevin 热浴实现,可参阅fix_langevin_spin

代码
文本
[ ]

代码
文本
DeepSPIN
Tutorial
DeepSPINTutorial
已赞16
本文被以下合集收录
机器学习与DFT精华帖
gtang
更新于 2024-09-10
38 篇21 人关注
测试合集文章列表100篇
xingyanshi@dp.tech很长的名字xingyanshi@dp.tech很长的名字很长的
更新于 2024-08-04
104 篇2 人关注
推荐阅读
公开
Pytorch基础教程(一):Tensor
PyTorchDeep Learning
PyTorchDeep Learning
爱学习的王一博
发布于 2024-03-11
4 赞5 转存文件
公开
快速开始 Scikit-Learn|使用 Scikit-Learn 建立机器学习模型
Scikit-LearnML-Tutorial
Scikit-LearnML-Tutorial
MileAway
发布于 2023-06-09
7 赞20 转存文件
评论
 # 快速开始 DeepSPIN|训练Ni...

Hui_Zhou

10-19 02:04
找不到 deepspin 这个镜像
评论