计算化学公社

 找回密码 Forget password
 注册 Register
Views: 2774|回复 Reply: 3
打印 Print 上一主题 Last thread 下一主题 Next thread

[CP2K] VMD脚本用于分析CP2K AIMD轨迹文件中分子种类和数量

[复制链接 Copy URL]

39

帖子

3

威望

527

eV
积分
626

Level 4 (黑子)

本帖最后由 ma455173220 于 2024-12-8 12:44 编辑

该脚本通过VMD对CP2K AIMD产生的轨迹文件进行统计分析,遍历轨迹文件并记录随时间变化的不同产物数量,以探究相关反应机理和路径。

本脚本参考了sob老师在CP2K培训班中第一性原理分子动力学部分的实例2:高温下烷烃裂解的模拟的产物各类片段数目的变化分析脚本MD\decane_pyrolysis\product_time.tcl,并做了相应修改。为了优化内存占用,结合了bigdcd脚本(ks.uiuc.edu/Research/vmd/script_library/scripts/bigdcd/bigdcd.tcl),以防止统计大量帧数时内存占用过高的问题。

使用方法:
  • 打开VMD,但不要不要不要加载轨迹文件。
  • 点击菜单中的Extensions -> Tk Console。
  • 在打开的VMD TkConsole窗口中点击File -> Load File,加载脚本。在加载脚本前,需要根据自己的需求进行一些修改。


需要修改的内容:
  • 第130行:设定 pbc_parameters,以符合你的系统设置。
  • 第132行:设定 timestep,表示加载的轨迹文件中每一帧的时间步长。
  • 第134-135行:设定要分析的起始帧和终止帧。
  • 第137-138行:设定文件路径。
  • 第17-45行:用来设定要分析的分子。根据自己的需求修改这部分内容。脚本中内容是我用来分析C2H5OH高温裂解的C2H2,C2H4,C2H6分子数量的,里面个人觉得写的蛮清晰的,所以这部分需要你自己来编译成你想分析的分子,不算特别难。


内存占用情况:
  • 下图是未结合bigdcd脚本的内存占用,我只分析了500帧,内存已经顶到了18GB,所以我就没有继续跑,之前测试我的32GB内存电脑大概跑个1500帧左右就炸了(当然了,应该跟你分析的系统大小有关):

  • 下图是结合了bigdcd脚本的内存占用,该例我分析了2000帧,可以看到内存只占用了500MB左右,继续算也基本不会有太大变化,分析个几万帧应该没啥压力:




补充:
  • 若分析的帧数过多,或者分析的帧数并不是从第一帧开始(比如分析的是5000帧到10000帧;换句话说就是脚本中的startframe并不是0),那么开始运行脚本时可能会卡顿一会儿才正式开始运行,因为那时脚本是正在加载轨迹文件,所以不要乱动。
  • 刚出炉的原数据曲线可能会非常凌乱,因为模拟体系较小,分子数目波动过于剧烈,所以需要手动后续用Origin进行平滑处理:Analysis -> Signal processing -> Smooth,Points of Windows按需设定。附一张示例图如下:



Count molecules.tcl

5.05 KB, 下载次数 Times of downloads: 115

评分 Rate

参与人数
Participants 2
威望 +1 eV +5 收起 理由
Reason
wildon + 5 好物!
sobereva + 1

查看全部评分 View all ratings

22

帖子

0

威望

209

eV
积分
231

Level 3 能力者

2#
发表于 Post on 2024-10-9 20:55:40 | 只看该作者 Only view this author

82

帖子

0

威望

937

eV
积分
1019

Level 4 (黑子)

3#
发表于 Post on 2024-12-16 15:54:08 | 只看该作者 Only view this author
您好,使用您的脚本时,VMD会提示下面的错误,请问是不是我哪里没有设置正确。我是通过CP2K进行CP2K计算后,获取了xyz轨迹文件,然后按照帖子中的内容对脚本进行了修改。运行后会出现下面的错误,生成的txt为空文件。
bigdcd aborting at frame 1
wrong # args: should be "set varName ?newValue?"

评分 Rate

参与人数
Participants 1
eV +1 收起 理由
Reason
wangyouliang + 1 您好,我跟你出现了一样的情况,你是咋解决.

查看全部评分 View all ratings

82

帖子

0

威望

937

eV
积分
1019

Level 4 (黑子)

4#
发表于 Post on 2024-12-16 16:56:22 | 只看该作者 Only view this author
wildon 发表于 2024-12-16 15:54
您好,使用您的脚本时,VMD会提示下面的错误,请问是不是我哪里没有设置正确。我是通过CP2K进行CP2K计算后 ...

感谢,已解决。
  1. # Define your analysis procedure
  2. proc my_analysis { frame } {
  3.     global all myfile timestep startframe pbc_parameters mol vmd_frame
  4.    
  5.     pbc set $pbc_parameters
  6.         
  7.     $all frame $frame
  8.     mol bondsrecalc top
  9.     mol reanalyze top
  10.     pbc wrap -compound fragment
  11.     pbc join fragment -bondlist
  12.     mol bondsrecalc top
  13.     mol reanalyze top   
  14.     puts "Doing frame $frame"
  15.    
  16.     set nfrag [llength [lsort -unique -integer [$all get fragment]]]
  17.    
  18. # This is where you set up your molecule analysis
  19.     set molecules {H2 H2O CO CO2 CH4 NH3 CHN C2H4}
  20.    
  21.     array set numc {}
  22.     foreach molecule $molecules {
  23.         set numc($molecule) 0
  24.     }
  25.    
  26.     for {set ifrag 0} {$ifrag < $nfrag} {incr ifrag} {
  27.         # 基本原子计数
  28.         set nhtmp [[atomselect top "fragment $ifrag and name H"] num]
  29.         set nctmp [[atomselect top "fragment $ifrag and name C"] num]
  30.         set notmp [[atomselect top "fragment $ifrag and name O"] num]
  31.         set nntmp [[atomselect top "fragment $ifrag and name N"] num]
  32.         
  33.         # 键合数计数
  34.         set H_nbtmp_1 [[atomselect top "fragment $ifrag and {name H and numbonds 1}"] num]
  35.         set O_nbtmp_2 [[atomselect top "fragment $ifrag and {name O and numbonds 2}"] num]
  36.         set C_nbtmp_2 [[atomselect top "fragment $ifrag and {name C and numbonds 2}"] num]
  37.         set C_nbtmp_3 [[atomselect top "fragment $ifrag and {name C and numbonds 3}"] num]
  38.         set C_nbtmp_4 [[atomselect top "fragment $ifrag and {name C and numbonds 4}"] num]
  39.         set N_nbtmp_3 [[atomselect top "fragment $ifrag and {name N and numbonds 3}"] num]
  40.         
  41.         # H2: 2个H原子,每个H有1个键合
  42.         if {$nhtmp == 2 && $nctmp == 0 && $notmp == 0 && $nntmp == 0 && $H_nbtmp_1 == 2} {
  43.             incr numc(H2)
  44.         # H2O: 2个H原子,1个O原子,O有2个键合
  45.         } elseif {$nhtmp == 2 && $nctmp == 0 && $notmp == 1 && $nntmp == 0 && $O_nbtmp_2 == 1} {
  46.             incr numc(H2O)
  47.         # CO: 1个C原子,1个O原子,C有2个键合
  48.         } elseif {$nhtmp == 0 && $nctmp == 1 && $notmp == 1 && $nntmp == 0 && $C_nbtmp_2 == 1} {
  49.             incr numc(CO)
  50.         # CO2: 1个C原子,2个O原子,C有4个键合
  51.         } elseif {$nhtmp == 0 && $nctmp == 1 && $notmp == 2 && $nntmp == 0 && $C_nbtmp_4 == 1} {
  52.             incr numc(CO2)
  53.         # CH4: 1个C原子,4个H原子,C有4个键合
  54.         } elseif {$nhtmp == 4 && $nctmp == 1 && $notmp == 0 && $nntmp == 0 && $C_nbtmp_4 == 1} {
  55.             incr numc(CH4)
  56.         # NH3: 3个H原子,1个N原子,N有3个键合
  57.         } elseif {$nhtmp == 3 && $nctmp == 0 && $notmp == 0 && $nntmp == 1 && $N_nbtmp_3 == 1} {
  58.             incr numc(NH3)
  59.         # CHN: 1个C原子,1个H原子,1个N原子,C和N都有2个键合
  60.         } elseif {$nhtmp == 1 && $nctmp == 1 && $notmp == 0 && $nntmp == 1 && $C_nbtmp_2 == 1} {
  61.             incr numc(CHN)
  62.         # C2H4: 2个C原子,4个H原子,C有3个键合
  63.         } elseif {$nctmp == 2 && $nhtmp == 4 && $notmp == 0 && $nntmp == 0 && $C_nbtmp_3 == 2} {
  64.             incr numc(C2H4)
  65.         }
  66.         
  67.         # 清理临时变量
  68.         unset nhtmp nctmp notmp nntmp H_nbtmp_1 O_nbtmp_2 C_nbtmp_2 C_nbtmp_3 C_nbtmp_4 N_nbtmp_3
  69.     }
  70.    
  71.     set frame_real [expr {$frame + $startframe - 1}]
  72.     puts -nonewline $myfile [expr $timestep * $frame_real]\
  73.     foreach molecule $molecules {
  74.         puts -nonewline $myfile $numc($molecule)\
  75.     }
  76.     puts $myfile " "

  77.     unset nfrag molecules numc ifrag frame_real
  78. }

  79. # Call bigdcd command to manage memory and call your analysis procedure for each frame
  80. proc bigdcd { script type args } {
  81.     global bigdcd_frame bigdcd_proc bigdcd_lastframe vmd_frame bigdcd_running startframe endframe
  82.    
  83.     set bigdcd_running 1
  84.     set bigdcd_frame 0
  85.     set bigdcd_lastframe [molinfo top get numframes]
  86.     set bigdcd_proc $script
  87.    
  88.     # backwards "compatibility". type flag is omitted.
  89.     if {[file exists $type]} {
  90.         set args [linsert $args 0 $type]
  91.         set type auto
  92.     }
  93.    
  94.     uplevel #0 trace variable vmd_frame w bigdcd_callback
  95.    
  96.     foreach dcd $args {
  97.         if { $type == "auto" } {
  98.             mol addfile $dcd first $startframe last $endframe waitfor 0
  99.         } else {
  100.             mol addfile $dcd type $type first $startframe last $endframe waitfor 0
  101.         }
  102.     }
  103.    
  104.     after idle bigdcd_wait
  105. }

  106. proc bigdcd_callback { tracedvar mol op } {
  107.     global bigdcd_frame bigdcd_proc bigdcd_lastframe vmd_frame
  108.     set msg {}
  109.    
  110.     set thisframe $vmd_frame($mol)
  111.    
  112.     incr bigdcd_frame
  113.     if { [catch {uplevel #0 $bigdcd_proc $bigdcd_frame} msg] } {
  114.         puts stderr "bigdcd aborting at frame $bigdcd_frame\n$msg"
  115.         bigdcd_done
  116.         return
  117.     }
  118.    
  119.     animate delete beg $thisframe end $thisframe $mol
  120.     return $msg
  121. }

  122. proc bigdcd_done { } {
  123.     global bigdcd_running
  124.    
  125.     if {$bigdcd_running > 0} {
  126.         uplevel #0 trace vdelete vmd_frame w bigdcd_callback
  127.         puts "End of frame"
  128.         puts "Bigdcd_done"
  129.         set bigdcd_running 0
  130.     }
  131. }

  132. proc bigdcd_wait { } {
  133.     global bigdcd_running bigdcd_frame bigdcd_lastframe
  134.    
  135.     while {$bigdcd_running > 0} {
  136.         global bigdcd_oldframe
  137.         set bigdcd_oldframe $bigdcd_frame
  138.         
  139.         # run global processing hooks (including loading of scheduled frames)
  140.         display update ui
  141.         
  142.         # Stop when reaching maximum frame
  143.         if { $bigdcd_frame >= $bigdcd_lastframe } {
  144.             bigdcd_done
  145.         }
  146.     }
  147. }

  148. # Set up periodic boundary condition
  149. set pbc_parameters {11.117 11.117 11.117  90.0000  90.0000  90.0000}
  150. # Set up timestep (fs)
  151. set timestep 0.5
  152. # Set up frame index (start from 0)
  153. set startframe 0
  154. set endframe 1604
  155. # Set up file path
  156. set inputfile "C:/Users/Lenovo/Desktop/1/qingniaosuan-pos-1.xyz"
  157. set outputfile "C:/Users/Lenovo/Desktop/1/qingniaosuan-pos-1.txt"

  158. # Check if input file exists
  159. if {![file exists $inputfile]} {
  160.     puts "Error: Input file not found: $inputfile"
  161.     return
  162. }

  163. # Job starts
  164. set myfile [open $outputfile a]
  165. set mol [mol new $inputfile first $startframe last $endframe type xyz waitfor all]
  166. set all [atomselect top all]

  167. # Call bigdcd command to manage memory and call your analysis procedure for each frame
  168. bigdcd my_analysis auto $inputfile

  169. # Wait for all frames to be processed
  170. bigdcd_wait

  171. # Close the result file
  172. close $myfile
复制代码

本版积分规则 Credits rule

手机版 Mobile version|北京科音自然科学研究中心 Beijing Kein Research Center for Natural Sciences|京公网安备 11010502035419号|计算化学公社 — 北京科音旗下高水平计算化学交流论坛 ( 京ICP备14038949号-1 )|网站地图

GMT+8, 2025-8-16 11:24 , Processed in 1.873253 second(s), 24 queries , Gzip On.

快速回复 返回顶部 返回列表 Return to list