Commit 453aba29 authored by sjplimp's avatar sjplimp
Browse files

git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@5201 f3b2605a-c512-4ea7-a41b-209d697bcdaa
parent b63b44e2
Loading
Loading
Loading
Loading

python/README

0 → 100644
+47 −0
Original line number Diff line number Diff line
This directory contains Python code which wraps LAMMPS as a library
and allows the library interface to be invoked from a Python, either
from a script or interactively.

Details on how to build and use this Python interface are given in
doc/Section_python.html.

Basically you have to extend the Python on your box to include the
LAMMPS wrappers:

python setup_serial.py build           # for serial LAMMPS and Python
sudo python setup_serial.py install

python setup.py build                  # for parallel LAMMPS and Python
sudo python setuppy install

but there are several issues to be aware of, as discussed in the doc
pages.

-------------------------------------------------------------------

Once you have successfully built and tested the wrappers, you can run
the Python scripts in the examples sub-directory:

trivial.py      read/run a LAMMPS input script thru Python
demo.py		invoke various LAMMPS library interface routines
simple.py	mimic operation of couple/simple/simple.cpp in Python
gui.py		GUI go/stop/temperature-slider to control LAMMPS
plot.py		real-time temeperature plot with GnuPlot via Pizza.py
viz.py		real-time viz from GL tool in Pizza.py
vizplotgui.py   combination of viz.py and plot.py and gui.py

Run them with the following input scripts and arguments:

trivial.py in.trivial
demo.py
simple.py in.simple
gui.py in.gui 100
plot.py in.plot 10 1000 thermo_temp
viz.py in.viz 100 5000
vizplotgui.py in.viz 100 thermo_temp

You can un-comment the Pypar calls if you want to run these in
parallel.

Each script has more documentation at the top of the file that
explains how to use it.
+66 −0
Original line number Diff line number Diff line
#!/usr/local/bin/python -i
# preceeding line should have path for Python on your machine

# demo.py
# Purpose: illustrate use of many library interface commands
# Syntax:  demo.py
#          uses in.demo as LAMMPS input script

import sys

# parse command line

argv = sys.argv
if len(argv) != 1:
  print "Syntax: demo.py"
  sys.exit()

me = 0
# uncomment if running in parallel via Pypar
#import pypar
#me = pypar.rank()
#nprocs = pypar.size()

from lammps import lammps
from lammps import LMPINT as INT
from lammps import LMPDOUBLE as DOUBLE
from lammps import LMPIPTR as IPTR
from lammps import LMPDPTR as DPTR
from lammps import LMPDPTRPTR as DPTRPTR

lmp = lammps()

# test out various library functions after running in.demo

lmp.file("in.demo")

if me == 0: print "\nPython output:"

natoms = lmp.extract_global("natoms",DOUBLE)
mass = lmp.extract_atom("mass",DPTR)
x = lmp.extract_atom("x",DPTRPTR)
print "Natoms, mass, x[0][0] coord =",natoms,mass[1],x[0][0]

temp = lmp.extract_compute("thermo_temp",0,0)
print "Temperature from compute =",temp

eng = lmp.extract_variable("eng",None,0)
print "Energy from equal-style variable =",eng

vy = lmp.extract_variable("vy","all",1)
print "Velocity component from atom-style variable =",vy[1]

natoms = lmp.get_natoms()
print "Natoms from get_natoms =",natoms

xc = lmp.get_coords()
print "Global coords from get_coords =",xc[0],xc[1],xc[31]

xc[0] = xc[0] + 1.0
lmp.put_coords(xc)

print "Changed x[0][0] via put_coords =",x[0][0]

# uncomment if running in parallel via Pypar
#print "Proc %d out of %d procs has" % (me,nprocs), lmp
#pypar.finalize()

python/examples/gui.py

0 → 100755
+109 −0
Original line number Diff line number Diff line
#!/usr/local/bin/python -i
# preceeding line should have path for Python on your machine

# gui.py
# Purpose: control a continuously running LAMMPS simulation via a Tkinter GUI
# Syntax:  gui.py in.lammps Nfreq
#          in.lammps = LAMMPS input script
#          Nfreq = query GUI every this many steps

import sys,time

# methods called by GUI

def go():
  global runflag
  runflag = 1
def stop():
  global runflag
  runflag = 0
def settemp(value):
  global temptarget
  temptarget = slider.get()
def quit():
  global breakflag
  breakflag = 1

# parse command line

argv = sys.argv
if len(argv) != 3:
  print "Syntax: gui.py in.lammps Nfreq"
  sys.exit()

infile = sys.argv[1]
nfreq = int(sys.argv[2])

me = 0
# uncomment if running in parallel via Pypar
#import pypar
#me = pypar.rank()
#nprocs = pypar.size()

from lammps import lammps
lmp = lammps()

# run infile all at once
# assumed to have no run command in it

lmp.file(infile)
lmp.command("thermo %d" % nfreq)

# display GUI with go/stop/quit buttons and slider for temperature
# just proc 0 handles GUI

breakflag = 0
runflag = 0
temptarget = 1.0

if me == 0:
  from Tkinter import *
  tkroot = Tk()
  tkroot.withdraw()
  root = Toplevel(tkroot)
  root.title("LAMMPS GUI")

  frame = Frame(root)
  Button(frame,text="Go",command=go).pack(side=LEFT)
  Button(frame,text="Stop",command=stop).pack(side=LEFT)
  slider = Scale(frame,from_=0.0,to=5.0,resolution=0.1,
                 orient=HORIZONTAL,label="Temperature")
  slider.bind('<ButtonRelease-1>',settemp)
  slider.set(temptarget)
  slider.pack(side=LEFT)
  Button(frame,text="Quit",command=quit).pack(side=RIGHT)
  frame.pack()
  tkroot.update()

# endless loop, checking status of GUI settings every Nfreq steps
# run with pre yes/no and post yes/no depending on go/stop status
# re-invoke fix langevin with new seed when temperature slider changes
# after re-invoke of fix langevin, run with pre yes

running = 0
temp = temptarget
seed = 12345

lmp.command("fix 2 all langevin %g %g 0.1 %d" % (temp,temp,seed))

while 1:
  if me == 0: tkroot.update()
  if temp != temptarget:
    temp = temptarget
    seed += me+1
    lmp.command("fix 2 all langevin %g %g 0.1 %d" % (temp,temp,seed))
    running = 0
  if runflag and running:
    lmp.command("run %d pre no post no" % nfreq)
  elif runflag and not running:
    lmp.command("run %d pre yes post no" % nfreq)
  elif not runflag and running:
    lmp.command("run %d pre no post yes" % nfreq)
  if breakflag: break
  if runflag: running = 1
  else: running = 0
  time.sleep(0.01)

# uncomment if running in parallel via Pypar
#print "Proc %d out of %d procs has" % (me,nprocs), lmp
#pypar.finalize()
+26 −0
Original line number Diff line number Diff line
# 3d Lennard-Jones melt

units		lj
atom_style	atomic
atom_modify	map hash

lattice		fcc 0.8442
region		box block 0 10 0 10 0 10
create_box	1 box
create_atoms	1 box
mass		1 1.0

velocity	all create 1.44 87287 loop geom

pair_style	lj/cut 2.5
pair_coeff	1 1 1.0 1.0 2.5

neighbor	0.3 bin
neigh_modify	delay 0 every 20 check no

fix		1 all nve

variable	eng equal pe
variable	vy atom vy

run		100

python/examples/in.gui

0 → 100644
+24 −0
Original line number Diff line number Diff line
# 3d Lennard-Jones melt

units		lj
dimension	2
atom_style	atomic

lattice		sq2 0.8442
region		box block 0 30 0 15 -0.5 0.5
create_box	1 box
create_atoms	1 box
mass		1 1.0

velocity	all create 1.44 87287 loop geom

pair_style	lj/cut 2.5
pair_coeff	1 1 1.0 1.0 2.5

neighbor	0.3 bin
neigh_modify	delay 0 every 1 check yes

timestep	0.003

fix		1 all nve
fix		3 all enforce2d
Loading