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