1#!/usr/bin/env python -i
2# preceding line should have path for Python on your machine
3
4# vizplotgui_gl.py
5# Purpose: viz running LAMMPS simulation via GL tool with plot and GUI
6# Syntax:  vizplotgui_gl.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  g.show(ntimestep)
38  value = lmp.extract_compute(compute,0,0)
39  xaxis.append(ntimestep)
40  yaxis.append(value)
41  gn.plot(xaxis,yaxis)
42
43# parse command line
44
45argv = sys.argv
46if len(argv) != 4:
47  print("Syntax: vizplotgui_gl.py in.lammps Nfreq compute-ID")
48  sys.exit()
49
50infile = sys.argv[1]
51nfreq = int(sys.argv[2])
52compute = sys.argv[3]
53
54me = 0
55
56from lammps import lammps
57lmp = lammps()
58
59# run infile all at once
60# assumed to have no run command in it
61# dump a file in native LAMMPS dump format for Pizza.py dump tool
62
63lmp.file(infile)
64lmp.command("thermo %d" % nfreq)
65lmp.command("dump python all atom %d tmp.dump" % nfreq)
66
67# initial 0-step run to generate initial 1-point plot, dump file, and image
68
69lmp.command("run 0 pre yes post no")
70value = lmp.extract_compute(compute,0,0)
71ntimestep = 0
72xaxis = [ntimestep]
73yaxis = [value]
74
75breakflag = 0
76runflag = 0
77temptarget = 1.0
78
79# wrapper on GL window via Pizza.py gl tool
80# just proc 0 handles reading of dump file and viz
81
82if me == 0:
83  try:
84    from Tkinter import *
85  except:
86    from tkinter import *
87  tkroot = Tk()
88  tkroot.withdraw()
89
90  from dump import dump
91  from gl import gl
92
93  d = dump("tmp.dump",0)
94  g = gl(d)
95  d.next()
96  d.unscale()
97  g.zoom(1)
98  g.shift(0,0)
99  g.rotate(0,270)
100  g.q(10)
101  g.box(1)
102  g.show(ntimestep)
103
104# display GUI with run/stop buttons and slider for temperature
105
106if me == 0:
107  try:
108    from Tkinter import *
109  except:
110    from tkinter import *
111  tkroot = Tk()
112  tkroot.withdraw()
113  root = Toplevel(tkroot)
114  root.title("LAMMPS GUI")
115
116  frame = Frame(root)
117  Button(frame,text="Run",command=run).pack(side=LEFT)
118  Button(frame,text="Stop",command=stop).pack(side=LEFT)
119  slider = Scale(frame,from_=0.0,to=5.0,resolution=0.1,
120                 orient=HORIZONTAL,label="Temperature")
121  slider.bind('<ButtonRelease-1>',settemp)
122  slider.set(temptarget)
123  slider.pack(side=LEFT)
124  Button(frame,text="Quit",command=quit).pack(side=RIGHT)
125  frame.pack()
126  tkroot.update()
127
128# wrapper on GnuPlot via Pizza.py gnu tool
129
130if me == 0:
131  from gnu import gnu
132  gn = gnu()
133  gn.plot(xaxis,yaxis)
134  gn.title(compute,"Timestep","Temperature")
135
136# endless loop, checking status of GUI settings every Nfreq steps
137# run with pre yes/no and post yes/no depending on go/stop status
138# re-invoke fix langevin with new seed when temperature slider changes
139# after re-invoke of fix langevin, run with pre yes
140
141running = 0
142temp = temptarget
143seed = 12345
144
145lmp.command("fix 2 all langevin %g %g 0.1 %d" % (temp,temp,seed))
146
147while 1:
148  if me == 0: tkroot.update()
149  if temp != temptarget:
150    temp = temptarget
151    seed += me+1
152    lmp.command("fix 2 all langevin %g %g 0.1 12345" % (temp,temp))
153    running = 0
154  if runflag and running:
155    lmp.command("run %d pre no post no" % nfreq)
156    ntimestep += nfreq
157    if me == 0: update(ntimestep)
158  elif runflag and not running:
159    lmp.command("run %d pre yes post no" % nfreq)
160    ntimestep += nfreq
161    if me == 0: update(ntimestep)
162  elif not runflag and running:
163    lmp.command("run %d pre no post yes" % nfreq)
164    ntimestep += nfreq
165    if me == 0: update(ntimestep)
166  if breakflag: break
167  if runflag: running = 1
168  else: running = 0
169  time.sleep(0.01)
170
171lmp.command("run 0 pre no post yes")
172