仓库源文站点原文


layout: post title: VMD加载GROMACS轨迹的速度信息与时间信息 categories:


Sobereva曾给过一个tcl脚本, 用于解决VMD不能读入GROMACS轨迹速度信息的问题, 具体参考 使VMD读入Gromacs产生的trr轨迹中速度信息的方法. 最近我需要用到VMD的这个功能, 就稍微看了下tcl的语法, 在原代码的基础上改进了一点, 使其使用更简单. 同时结合Sobereva的另一段代码 使VMD播放轨迹时同步显示帧号, 在播放轨迹的同时显示出模拟时间.

下面模拟的是C20和C60分子的相撞过程, 播放轨迹时对每个原子根据速度大小进行着色.

使用方法

  1. 运行MD前将grompp.mdp文件中trr与xtc的输出频率设为相同
  2. 使用gmx traj -f traj.trr -ov抽取traj.trr轨迹文件中的速度, 默认存为veloc.xvg
  3. 使用VMD加载初始的conf.gro文件和traj.xtc轨迹文件(直接使用trr文件可能更简单, 但速度稍慢). 也可直接使用命令vmd conf.gro traj.xtc
  4. vt.tcl脚本复制到轨迹文件所在目录下
  5. VMD命令行窗口中执行source vt.tcl使脚本生效
  6. VMD命令行窗口中执行loadveloc即可加载veloc.xvg文件中的速度. 如果速度文件的名称不是veloc.xvg, 则使用loadveloc 速度文件名即可
  7. 如果播放轨迹时需要显示时间, 可在VMD命令行窗口中执行showtime. 执行showtime off则关闭时间显示. 默认的时间间隔为0.5 fs, 起始时间为0, 如需更改, 可使用showtime on 时间间隔 起始时间
  8. 播放轨迹时对每个原子根据速度大小进行着色, 可通过Graphics | Representations... | Coloring Method | trajectory | Velocity. 如需根据某一方向速度大小着色, 可使用User(x方向), User2(y方向), User3(z方向)
  9. 更改颜色方案, 可使用Graphics | Colors... | Color Scale | Method.

几点说明

vt.tcl脚本

<table class="highlighttable"><th colspan="2" style="text-align:left">vt.tcl</th><tr><td><div class="linenodiv" style="background-color: #f0f0f0; padding-right: 10px"><pre style="line-height: 125%"> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54</pre></div></td><td class="code"><div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #AA22FF; font-weight: bold">proc</span> showtime <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">opt</span> on<span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">dtin</span> <span style="color: #666666">0.5</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">t0in</span> <span style="color: #666666">0</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">global</span> vmd_frame<span style="color: #AA22FF; font-weight: bold">;</span> <span style="color: #AA22FF; font-weight: bold">global</span> dt<span style="color: #AA22FF; font-weight: bold">;</span> <span style="color: #AA22FF; font-weight: bold">global</span> t0 <span style="color: #AA22FF; font-weight: bold">set</span> dt <span style="color: #B8860B">$dtin</span> <span style="color: #AA22FF; font-weight: bold">set</span> t0 <span style="color: #B8860B">$t0in</span> <span style="color: #AA22FF; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">$opt</span><span style="color: #666666">==</span><span style="color: #B8860B">on</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">trace</span> variable vmd_frame<span style="color: #AA22FF; font-weight: bold">([</span><span style="color: #B8860B">molinfo</span> top<span style="color: #AA22FF; font-weight: bold">])</span> w traceframe <span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">elseif</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">$opt</span><span style="color: #666666">==</span><span style="color: #B8860B">off</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #B8860B">draw</span> delete all <span style="color: #AA22FF; font-weight: bold">trace</span> vdelete vmd_frame<span style="color: #AA22FF; font-weight: bold">([</span><span style="color: #B8860B">molinfo</span> top<span style="color: #AA22FF; font-weight: bold">])</span> w traceframe <span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">}</span>

<span style="color: #AA22FF; font-weight: bold">proc</span> traceframe <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">name</span> elem ops<span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">global</span> vmd_frame<span style="color: #AA22FF; font-weight: bold">;</span> <span style="color: #AA22FF; font-weight: bold">global</span> dt<span style="color: #AA22FF; font-weight: bold">;</span> <span style="color: #AA22FF; font-weight: bold">global</span> t0 <span style="color: #B8860B">draw</span> delete all <span style="color: #B8860B">draw</span> color white <span style="color: #AA22FF; font-weight: bold">set</span> time <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #AA22FF">format</span> <span style="color: #BB4444">"%6.1f fs"</span> <span style="color: #AA22FF; font-weight: bold">[expr</span> <span style="color: #AA22FF; font-weight: bold">(</span><span style="color: #B8860B">$vmd_frame</span><span style="color: #AA22FF; font-weight: bold">([</span><span style="color: #B8860B">molinfo</span> top<span style="color: #AA22FF; font-weight: bold">])</span> <span style="color: #666666">*</span> <span style="color: #B8860B">$dt</span><span style="color: #AA22FF; font-weight: bold">)</span> <span style="color: #666666">+</span> <span style="color: #B8860B">$t0</span><span style="color: #AA22FF; font-weight: bold">]]</span> <span style="color: #B8860B">draw</span> text <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">0</span> <span style="color: #666666">-5</span> <span style="color: #666666">-5</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #BB4444">"$time"</span> size <span style="color: #666666">4</span> thickness <span style="color: #666666">4</span> <span style="color: #AA22FF; font-weight: bold">}</span>

<span style="color: #AA22FF; font-weight: bold">proc</span> loadveloc <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">Fxvg</span> veloc.xvg<span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span> <span style="color: #AA22FF; font-weight: bold">set</span> Mol <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #B8860B">atomselect</span> top all<span style="color: #AA22FF; font-weight: bold">]</span> <span style="color: #AA22FF; font-weight: bold">set</span> Natm <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #B8860B">$Mol</span> <span style="color: #B8860B">num</span><span style="color: #AA22FF; font-weight: bold">]</span> <span style="color: #AA22FF; font-weight: bold">set</span> Nfrm <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #B8860B">molinfo</span> top get numframes<span style="color: #AA22FF; font-weight: bold">]</span>

<span style="color: #AA22FF; font-weight: bold">set</span> Fxvg <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #AA22FF">open</span> <span style="color: #B8860B">$Fxvg</span> r<span style="color: #AA22FF; font-weight: bold">]</span>
<span style="color: #AA22FF">gets</span> <span style="color: #B8860B">$Fxvg</span> txt
<span style="color: #AA22FF; font-weight: bold">while</span> <span style="color: #AA22FF; font-weight: bold">{[</span><span style="color: #AA22FF">string</span> match <span style="color: #AA22FF; font-weight: bold">{[</span><span style="border: 1px solid #FF0000">\</span><span style="color: #008800; font-style: italic">#@]*} $txt]} {</span>
    <span style="color: #AA22FF">gets</span> <span style="color: #B8860B">$Fxvg</span> txt
<span style="border: 1px solid #FF0000">}</span>
<span style="color: #AA22FF; font-weight: bold">set</span> txt <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #AA22FF">split</span> <span style="color: #B8860B">$txt</span> <span style="border: 1px solid #FF0000">\</span>t<span style="color: #AA22FF; font-weight: bold">]</span>

<span style="color: #AA22FF; font-weight: bold">for</span> <span style="color: #AA22FF; font-weight: bold">{set</span> i <span style="color: #666666">1</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">$i</span><span style="color: #666666">&lt;</span><span style="color: #B8860B">$Nfrm</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #AA22FF">incr</span> i<span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #B8860B">frame</span> <span style="color: #B8860B">$i</span>
    <span style="color: #AA22FF; font-weight: bold">set</span> Vx <span style="color: #AA22FF; font-weight: bold">{};</span> <span style="color: #AA22FF; font-weight: bold">set</span> Vy <span style="color: #AA22FF; font-weight: bold">{};</span> <span style="color: #AA22FF; font-weight: bold">set</span> Vz <span style="color: #AA22FF; font-weight: bold">{}</span>
    <span style="color: #AA22FF; font-weight: bold">for</span> <span style="color: #AA22FF; font-weight: bold">{set</span> j <span style="color: #666666">0</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #B8860B">$j</span><span style="color: #666666">&lt;</span><span style="color: #B8860B">$Natm</span><span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span><span style="color: #AA22FF">incr</span> j<span style="color: #AA22FF; font-weight: bold">}</span> <span style="color: #AA22FF; font-weight: bold">{</span>
        <span style="color: #AA22FF">lappend</span> Vx <span style="color: #AA22FF; font-weight: bold">[</span> <span style="color: #AA22FF">lindex</span> <span style="color: #B8860B">$txt</span> <span style="color: #AA22FF; font-weight: bold">[expr</span> <span style="color: #666666">3*</span><span style="color: #B8860B">$j</span><span style="color: #666666">+1</span><span style="color: #AA22FF; font-weight: bold">]</span> <span style="color: #AA22FF; font-weight: bold">]</span>
        <span style="color: #AA22FF">lappend</span> Vy <span style="color: #AA22FF; font-weight: bold">[</span> <span style="color: #AA22FF">lindex</span> <span style="color: #B8860B">$txt</span> <span style="color: #AA22FF; font-weight: bold">[expr</span> <span style="color: #666666">3*</span><span style="color: #B8860B">$j</span><span style="color: #666666">+2</span><span style="color: #AA22FF; font-weight: bold">]</span> <span style="color: #AA22FF; font-weight: bold">]</span>
        <span style="color: #AA22FF">lappend</span> Vz <span style="color: #AA22FF; font-weight: bold">[</span> <span style="color: #AA22FF">lindex</span> <span style="color: #B8860B">$txt</span> <span style="color: #AA22FF; font-weight: bold">[expr</span> <span style="color: #666666">3*</span><span style="color: #B8860B">$j</span><span style="color: #666666">+3</span><span style="color: #AA22FF; font-weight: bold">]</span> <span style="color: #AA22FF; font-weight: bold">]</span>
    <span style="color: #AA22FF; font-weight: bold">}</span>

    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> vx <span style="color: #B8860B">$Vx</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> vy <span style="color: #B8860B">$Vy</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> vz <span style="color: #B8860B">$Vz</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> user  <span style="color: #B8860B">$Vx</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> user2 <span style="color: #B8860B">$Vy</span>
    <span style="color: #B8860B">$Mol</span> <span style="color: #AA22FF; font-weight: bold">set</span> user3 <span style="color: #B8860B">$Vz</span>

    <span style="color: #AA22FF">gets</span> <span style="color: #B8860B">$Fxvg</span> txt
    <span style="color: #AA22FF; font-weight: bold">set</span> txt <span style="color: #AA22FF; font-weight: bold">[</span><span style="color: #AA22FF">split</span> <span style="color: #B8860B">$txt</span> <span style="border: 1px solid #FF0000">\</span>t<span style="color: #AA22FF; font-weight: bold">]</span>
<span style="color: #AA22FF; font-weight: bold">}</span>

<span style="color: #AA22FF">close</span> <span style="color: #B8860B">$Fxvg</span>

<span style="border: 1px solid #FF0000">}</span> </pre></div> </td></tr></table>

评论