1#!/usr/bin/env python -i
2# preceding line should have path for Python on your machine
3
4# vizplotgui_pymol.py
5# Purpose: viz running LAMMPS simulation via PyMol with plot and GUI
6# Syntax:  vizplotgui_pymol.py in.lammps Nfreq compute-ID
7#          in.lammps = LAMMPS input script
8#          Nfreq = plot data point and viz shapshot every this many steps
9#          compute-ID = ID of compute that calculates temperature
10#                       (or any other scalar quantity)
11
12from __future__ import print_function
13import sys,time
14sys.path.append("./pizza")
15
16# methods called by GUI
17
18def run():
19  global runflag
20  runflag = 1
21def stop():
22  global runflag
23  runflag = 0
24def settemp(value):
25  global temptarget
26  temptarget = slider.get()
27def quit():
28  global breakflag
29  breakflag = 1
30
31# method called by timestep loop every Nfreq steps
32# read dump snapshot and viz it, update plot with compute value
33
34def update(ntimestep):
35  d.next()
36  d.unscale()
37  p.single(ntimestep)
38  pm.load("tmp.pdb")
39  pm.forward()
40  value = lmp.extract_compute(compute,0,0)
41  xaxis.append(ntimestep)
42  yaxis.append(value)
43  gn.plot(xaxis,yaxis)
44
45# parse command line
46
47argv = sys.argv
48if len(argv) != 4:
49  print("Syntax: vizplotgui_pymol.py in.lammps Nfreq compute-ID")
50  sys.exit()
51
52infile = sys.argv[1]
53nfreq = int(sys.argv[2])
54compute = sys.argv[3]
55
56me = 0
57
58from lammps import lammps
59lmp = lammps()
60
61# run infile all at once
62# assumed to have no run command in it
63# dump a file in native LAMMPS dump format for Pizza.py dump tool
64
65lmp.file(infile)
66lmp.command("thermo %d" % nfreq)
67lmp.command("dump python all atom %d tmp.dump" % nfreq)
68
69# initial 0-step run to generate initial 1-point plot, dump file, and image
70
71lmp.command("run 0 pre yes post no")
72value = lmp.extract_compute(compute,0,0)
73ntimestep = 0
74xaxis = [ntimestep]
75yaxis = [value]
76
77breakflag = 0
78runflag = 0
79temptarget = 1.0
80
81# wrapper on PyMol
82# just proc 0 handles reading of dump file and viz
83
84if me == 0:
85  import pymol
86  pymol.finish_launching()
87
88  from dump import dump
89  from pdbfile import pdbfile
90  from pymol import cmd as pm
91
92  d = dump("tmp.dump",0)
93  p = pdbfile(d)
94  d.next()
95  d.unscale()
96  p.single(ntimestep)
97  pm.load("tmp.pdb")
98  pm.show("spheres","tmp")
99
100# display GUI with run/stop buttons and slider for temperature
101
102if me == 0:
103  try:
104    from Tkinter import *
105  except:
106    from tkinter import *
107  tkroot = Tk()
108  tkroot.withdraw()
109  root = Toplevel(tkroot)
110  root.title("LAMMPS GUI")
111
112  frame = Frame(root)
113  Button(frame,text="Run",command=run).pack(side=LEFT)
114  Button(frame,text="Stop",command=stop).pack(side=LEFT)
115  slider = Scale(frame,from_=0.0,to=5.0,resolution=0.1,
116                 orient=HORIZONTAL,label="Temperature")
117  slider.bind('<ButtonRelease-1>',settemp)
118  slider.set(temptarget)
119  slider.pack(side=LEFT)
120  Button(frame,text="Quit",command=quit).pack(side=RIGHT)
121  frame.pack()
122  tkroot.update()
123
124# wrapper on GnuPlot via Pizza.py gnu tool
125
126if me == 0:
127  from gnu import gnu
128  gn = gnu()
129  gn.plot(xaxis,yaxis)
130  gn.title(compute,"Timestep","Temperature")
131
132# endless loop, checking status of GUI settings every Nfreq steps
133# run with pre yes/no and post yes/no depending on go/stop status
134# re-invoke fix langevin with new seed when temperature slider changes
135# after re-invoke of fix langevin, run with pre yes
136
137running = 0
138temp = temptarget
139seed = 12345
140
141lmp.command("fix 2 all langevin %g %g 0.1 %d" % (temp,temp,seed))
142
143while 1:
144  if me == 0: tkroot.update()
145  if temp != temptarget:
146    temp = temptarget
147    seed += me+1
148    lmp.command("fix 2 all langevin %g %g 0.1 12345" % (temp,temp))
149    running = 0
150  if runflag and running:
151    lmp.command("run %d pre no post no" % nfreq)
152    ntimestep += nfreq
153    if me == 0: update(ntimestep)
154  elif runflag and not running:
155    lmp.command("run %d pre yes post no" % nfreq)
156    ntimestep += nfreq
157    if me == 0: update(ntimestep)
158  elif not runflag and running:
159    lmp.command("run %d pre no post yes" % nfreq)
160    ntimestep += nfreq
161    if me == 0: update(ntimestep)
162  if breakflag: break
163  if runflag: running = 1
164  else: running = 0
165  time.sleep(0.01)
166
167lmp.command("run 0 pre no post yes")
168