Commit a936b7b2 authored by sjplimp's avatar sjplimp Committed by GitHub
Browse files

Merge pull request #419 from rbberger/python_fixes

Library interface fixes, Python example fixes and better Python 3 compatibility
parents a91b851f cda82138
Loading
Loading
Loading
Loading
+93 −0
Original line number Diff line number Diff line
#!/usr/bin/env python -i
# preceding line should have path for Python on your machine

# matplotlib_plot.py
# Purpose: plot Temp of running LAMMPS simulation via matplotlib
# Syntax:  plot.py in.lammps Nfreq Nsteps compute-ID
#          in.lammps = LAMMPS input script
#          Nfreq = plot data point every this many steps
#          Nsteps = run for this many steps
#          compute-ID = ID of compute that calculates temperature
#                       (or any other scalar quantity)

from __future__ import print_function
import sys
sys.path.append("./pizza")
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt

# parse command line

argv = sys.argv
if len(argv) != 5:
  print("Syntax: plot.py in.lammps Nfreq Nsteps compute-ID")
  sys.exit()

infile = sys.argv[1]
nfreq = int(sys.argv[2])
nsteps = int(sys.argv[3])
compute = sys.argv[4]

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)

# initial 0-step run to generate initial 1-point plot

lmp.command("run 0 pre yes post no")
value = lmp.extract_compute(compute,0,0)
ntimestep = 0
xaxis = [ntimestep]
yaxis = [value]

# create matplotlib plot
# just proc 0 handles plotting

if me == 0:
  fig = plt.figure()
  line, = plt.plot(xaxis, yaxis)
  plt.xlim([0, nsteps])
  plt.title(compute)
  plt.xlabel("Timestep")
  plt.ylabel("Temperature")
  plt.show(block=False)

# run nfreq steps at a time w/out pre/post, query compute, refresh plot
import time

while ntimestep < nsteps:
  lmp.command("run %d pre no post no" % nfreq)
  ntimestep += nfreq
  value = lmp.extract_compute(compute,0,0)
  xaxis.append(ntimestep)
  yaxis.append(value)
  if me == 0:
    line.set_xdata(xaxis)
    line.set_ydata(yaxis)
    ax = plt.gca()
    ax.relim()
    ax.autoscale_view(True, True, True)
    fig.canvas.draw()

lmp.command("run 0 pre no post yes")

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

if sys.version_info[0] == 3:
    input("Press Enter to exit...")
else:
    raw_input("Press Enter to exit...")
+33 −33
Original line number Diff line number Diff line
@@ -146,6 +146,8 @@ d.extra(data) extract bond/tri/line list from data
# History
#   8/05, Steve Plimpton (SNL): original version
#   12/09, David Hart (SNL): allow use of NumPy or Numeric
#   03/17, Richard Berger (Temple U): improve Python 3 compatibility,
#                                     simplify read_snapshot by using reshape

# ToDo list
#   try to optimize this line in read_snap: words += f.readline().split()
@@ -224,7 +226,7 @@ class dump:
    self.flist = []
    for word in words: self.flist += glob.glob(word)
    if len(self.flist) == 0 and len(list) == 1:
      raise StandardError("no dump file specified")
      raise Exception("no dump file specified")
    
    if len(list) == 1:
      self.increment = 0
@@ -299,7 +301,7 @@ class dump:

  def next(self):

    if not self.increment: raise StandardError("cannot read incrementally")
    if not self.increment: raise Exception("cannot read incrementally")

    # read next snapshot in current file using eof as pointer
    # if fail, try next file
@@ -344,13 +346,13 @@ class dump:
    try:
      snap = Snap()
      item = f.readline()
      snap.time = int(f.readline().split()[0])    # just grab 1st field
      snap.time = int(f.readline().decode().split()[0])    # just grab 1st field
      item = f.readline()
      snap.natoms = int(f.readline())
      snap.natoms = int(f.readline().decode())

      snap.aselect = np.zeros(snap.natoms)

      item = f.readline()
      item = f.readline().decode()
      words = f.readline().split()
      snap.xlo,snap.xhi = float(words[0]),float(words[1])
      words = f.readline().split()
@@ -358,7 +360,7 @@ class dump:
      words = f.readline().split()
      snap.zlo,snap.zhi = float(words[0]),float(words[1])

      item = f.readline()
      item = f.readline().decode()
      if len(self.names) == 0:
        words = item.split()[2:]
        if len(words):
@@ -372,24 +374,22 @@ class dump:
            else: self.names[words[i]] = i

      if snap.natoms:
        words = f.readline().split()
        words = f.readline().decode().split()
        ncol = len(words)
        for i in range(1,snap.natoms):
          words += f.readline().split()
          words += f.readline().decode().split()
        floats = map(float,words)
        if oldnumeric: atoms = np.zeros((snap.natoms,ncol),np.Float)
        else: atoms = np.zeros((snap.natoms,ncol),np.float)
        start = 0
        stop = ncol
        for i in range(snap.natoms):
          atoms[i] = floats[start:stop]
          start = stop
          stop += ncol
      else: atoms = None
      snap.atoms = atoms
        if oldnumeric:
          atom_data = np.array(list(floats),np.Float)
        else:
          atom_data = np.array(list(floats),np.float)

        snap.atoms = atom_data.reshape((snap.natoms, ncol))
      else:
        snap.atoms = None
      return snap
    except:
      return 0
      return None

  # --------------------------------------------------------------------
  # decide if snapshot i is scaled/unscaled from coords of first and last atom
@@ -417,7 +417,7 @@ class dump:
  
  def map(self,*pairs):
    if len(pairs) % 2 != 0:
      raise StandardError("dump map() requires pairs of mappings")
      raise Exception("dump map() requires pairs of mappings")
    for i in range(0,len(pairs),2):
      j = i + 1
      self.names[pairs[j]] = pairs[i]-1
@@ -734,7 +734,7 @@ class dump:
    for snap in self.snaps:
      if not snap.tselect: continue
      if snap.nselect != len(vec):
        raise StandardError("vec length does not match # of selected atoms")
        raise Exception("vec length does not match # of selected atoms")
      atoms = snap.atoms
      m = 0
      for i in range(snap.natoms):
@@ -800,7 +800,7 @@ class dump:

  def atom(self,n,*list):
    if len(list) == 0:
      raise StandardError("no columns specified")
      raise Exception("no columns specified")
    columns = []
    values = []
    for name in list:
@@ -816,7 +816,7 @@ class dump:
      for i in range(snap.natoms):
        if atoms[i][id] == n: break
      if atoms[i][id] != n:
        raise StandardError("could not find atom ID in snapshot")
        raise Exception("could not find atom ID in snapshot")
      for j in range(ncol):
        values[j][m] = atoms[i][columns[j]]
      m += 1
@@ -831,7 +831,7 @@ class dump:
    snap = self.snaps[self.findtime(n)]
    
    if len(list) == 0:
      raise StandardError("no columns specified")
      raise Exception("no columns specified")
    columns = []
    values = []
    for name in list:
@@ -957,9 +957,9 @@ class dump:
  # --------------------------------------------------------------------

  def findtime(self,n):
    for i in range(self.nsnaps):
      if self.snaps[i].time == n: return i
    raise StandardError("no step %d exists" % n)
    for i, snap in enumerate(self.snaps):
      if snap.time == n: return i
    raise Exception("no step %d exists" % n)

  # --------------------------------------------------------------------
  # return maximum box size across all selected snapshots
@@ -1008,7 +1008,7 @@ class dump:
        nbonds = int(f.readline())
        item = f.readline()
        if not re.search("BONDS",item):
          raise StandardError("could not read bonds from dump file")
          raise Exception("could not read bonds from dump file")

        words = f.readline().split()
        ncol = len(words)
@@ -1031,7 +1031,7 @@ class dump:
          self.bondflag = 1
          self.bondlist = bondlist
      except:
        raise StandardError("could not read from bond dump file")
        raise Exception("could not read from bond dump file")
      
    # request bonds from data object
    
@@ -1047,7 +1047,7 @@ class dump:
          self.bondflag = 1
          self.bondlist = bondlist
      except:
        raise StandardError("could not extract bonds from data object")
        raise Exception("could not extract bonds from data object")

    # request tris/lines from cdata object
    
@@ -1061,7 +1061,7 @@ class dump:
          self.lineflag = 1
          self.linelist = lines
      except:
        raise StandardError("could not extract tris/lines from cdata object")
        raise Exception("could not extract tris/lines from cdata object")

    # request tris from mdump object
    
@@ -1070,10 +1070,10 @@ class dump:
        self.triflag = 2
        self.triobj = arg
      except:
        raise StandardError("could not extract tris from mdump object")
        raise Exception("could not extract tris from mdump object")

    else:
      raise StandardError("unrecognized argument to dump.extra()")
      raise Exception("unrecognized argument to dump.extra()")
      
  # --------------------------------------------------------------------

+8 −1
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ index,time,flag = p.iterator(1)

# History
#   8/05, Steve Plimpton (SNL): original version
#   3/17, Richard Berger (Temple U): improve Python 3 compatibility

# ToDo list
#   for generic PDB file (no template) from a LJ unit system,
@@ -68,6 +69,12 @@ index,time,flag = p.iterator(1)
# Imports and external programs

import sys, types, glob, urllib
PY3 = sys.version_info[0] == 3

if PY3:
    string_types = str,
else:
    string_types = basestring

# Class definition

@@ -77,7 +84,7 @@ class pdbfile:

  def __init__(self,*args):
    if len(args) == 1:
      if type(args[0]) is types.StringType:
      if type(args[0]) is string_types:
        filestr = args[0]
        self.data = None
      else:
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ lmp = lammps()

lmp.file(infile)
lmp.command("thermo %d" % nfreq)
lmp.command("dump python all cfg %d tmp.cfg.* id type xs ys zs" % nfreq)
lmp.command("dump python all cfg %d tmp.cfg.* mass type xs ys zs id" % nfreq)

# initial 0-step run to generate dump file and image

+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ lmp = lammps()

lmp.file(infile)
lmp.command("thermo %d" % nfreq)
lmp.command("dump python all cfg %d tmp.cfg.* id type xs ys zs" % nfreq)
lmp.command("dump python all cfg %d tmp.cfg.* mass type xs ys zs id" % nfreq)

# initial 0-step run to generate initial 1-point plot, dump file, and image

Loading