计算化学公社

 找回密码 Forget password
 注册 Register

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

查看数: 2778 | 评论数: 3 | 收藏 Add to favorites 16
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2024-5-2 14:33

正文摘要:

本帖最后由 ma455173220 于 2024-12-8 12:44 编辑 该脚本通过VMD对CP2K AIMD产生的轨迹文件进行统计分析,遍历轨迹文件并记录随时间变化的不同产物数量,以探究相关反应机理和路径。 本脚本参考了sob老师在CP2 ...

回复 Reply

wildon 发表于 Post on 2024-12-16 16:56:22
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
复制代码
wildon 发表于 Post on 2024-12-16 15:54:08
您好,使用您的脚本时,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

lywbanner 发表于 Post on 2024-10-9 20:55:40

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

GMT+8, 2025-8-16 13:36 , Processed in 0.774842 second(s), 26 queries , Gzip On.

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