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