Update examples to use Python API constants

This commit is contained in:
Richard Berger
2020-09-16 16:10:43 -04:00
parent c82df186b5
commit e6703019bc
4 changed files with 33 additions and 34 deletions

View File

@ -20,7 +20,7 @@ neigh_modify every 20 delay 0 check no
python post_force_callback here """ python post_force_callback here """
from __future__ import print_function from __future__ import print_function
from lammps import lammps from lammps import lammps, LAMMPS_INT
def post_force_callback(lmp, v): def post_force_callback(lmp, v):
try: try:
@ -35,9 +35,9 @@ def post_force_callback(lmp, v):
#mylist = L.get_neighlist(0) #mylist = L.get_neighlist(0)
mylist = L.find_pair_neighlist("lj/cut", request=0) mylist = L.find_pair_neighlist("lj/cut", request=0)
print(pid_prefix, mylist) print(pid_prefix, mylist)
nlocal = L.extract_global("nlocal", 0) nlocal = L.extract_global("nlocal", LAMMPS_INT)
nghost = L.extract_global("nghost", 0) nghost = L.extract_global("nghost", LAMMPS_INT)
ntypes = L.extract_global("ntypes", 0) ntypes = L.extract_global("ntypes", LAMMPS_INT)
mass = L.numpy.extract_atom_darray("mass", ntypes+1) mass = L.numpy.extract_atom_darray("mass", ntypes+1)
atype = L.numpy.extract_atom_iarray("type", nlocal+nghost) atype = L.numpy.extract_atom_iarray("type", nlocal+nghost)
x = L.numpy.extract_atom_darray("x", nlocal+nghost, dim=3) x = L.numpy.extract_atom_darray("x", nlocal+nghost, dim=3)

View File

@ -1,12 +1,12 @@
from __future__ import print_function from __future__ import print_function
import lammps from lammps import lammps, LAMMPS_INT, LAMMPS_DOUBLE
import ctypes import ctypes
import traceback import traceback
import numpy as np import numpy as np
class LAMMPSFix(object): class LAMMPSFix(object):
def __init__(self, ptr, group_name="all"): def __init__(self, ptr, group_name="all"):
self.lmp = lammps.lammps(ptr=ptr) self.lmp = lammps(ptr=ptr)
self.group_name = group_name self.group_name = group_name
class LAMMPSFixMove(LAMMPSFix): class LAMMPSFixMove(LAMMPSFix):
@ -39,14 +39,14 @@ class NVE(LAMMPSFixMove):
assert(self.group_name == "all") assert(self.group_name == "all")
def init(self): def init(self):
dt = self.lmp.extract_global("dt", 1) dt = self.lmp.extract_global("dt", LAMMPS_DOUBLE)
ftm2v = self.lmp.extract_global("ftm2v", 1) ftm2v = self.lmp.extract_global("ftm2v", LAMMPS_DOUBLE)
self.ntypes = self.lmp.extract_global("ntypes", 0) self.ntypes = self.lmp.extract_global("ntypes", LAMMPS_INT)
self.dtv = dt self.dtv = dt
self.dtf = 0.5 * dt * ftm2v self.dtf = 0.5 * dt * ftm2v
def initial_integrate(self, vflag): def initial_integrate(self, vflag):
nlocal = self.lmp.extract_global("nlocal", 0) nlocal = self.lmp.extract_global("nlocal", LAMMPS_INT)
mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1) mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1)
atype = self.lmp.numpy.extract_atom_iarray("type", nlocal) atype = self.lmp.numpy.extract_atom_iarray("type", nlocal)
x = self.lmp.numpy.extract_atom_darray("x", nlocal, dim=3) x = self.lmp.numpy.extract_atom_darray("x", nlocal, dim=3)
@ -59,7 +59,7 @@ class NVE(LAMMPSFixMove):
x[i,:] += self.dtv * v[i,:] x[i,:] += self.dtv * v[i,:]
def final_integrate(self): def final_integrate(self):
nlocal = self.lmp.extract_global("nlocal", 0) nlocal = self.lmp.extract_global("nlocal", LAMMPS_INT)
mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1) mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1)
atype = self.lmp.numpy.extract_atom_iarray("type", nlocal) atype = self.lmp.numpy.extract_atom_iarray("type", nlocal)
v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3) v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3)
@ -77,15 +77,15 @@ class NVE_Opt(LAMMPSFixMove):
assert(self.group_name == "all") assert(self.group_name == "all")
def init(self): def init(self):
dt = self.lmp.extract_global("dt", 1) dt = self.lmp.extract_global("dt", LAMMPS_DOUBLE)
ftm2v = self.lmp.extract_global("ftm2v", 1) ftm2v = self.lmp.extract_global("ftm2v", LAMMPS_DOUBLE)
self.ntypes = self.lmp.extract_global("ntypes", 0) self.ntypes = self.lmp.extract_global("ntypes", LAMMPS_INT)
self.dtv = dt self.dtv = dt
self.dtf = 0.5 * dt * ftm2v self.dtf = 0.5 * dt * ftm2v
self.mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1) self.mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1)
def initial_integrate(self, vflag): def initial_integrate(self, vflag):
nlocal = self.lmp.extract_global("nlocal", 0) nlocal = self.lmp.extract_global("nlocal", LAMMPS_INT)
atype = self.lmp.numpy.extract_atom_iarray("type", nlocal) atype = self.lmp.numpy.extract_atom_iarray("type", nlocal)
x = self.lmp.numpy.extract_atom_darray("x", nlocal, dim=3) x = self.lmp.numpy.extract_atom_darray("x", nlocal, dim=3)
v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3) v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3)
@ -102,13 +102,12 @@ class NVE_Opt(LAMMPSFixMove):
x[:,d] += dtv * v[:,d] x[:,d] += dtv * v[:,d]
def final_integrate(self): def final_integrate(self):
nlocal = self.lmp.extract_global("nlocal", 0) nlocal = self.lmp.extract_global("nlocal", LAMMPS_INT)
mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1) mass = self.lmp.numpy.extract_atom_darray("mass", self.ntypes+1)
atype = self.lmp.numpy.extract_atom_iarray("type", nlocal) atype = self.lmp.numpy.extract_atom_iarray("type", nlocal)
v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3) v = self.lmp.numpy.extract_atom_darray("v", nlocal, dim=3)
f = self.lmp.numpy.extract_atom_darray("f", nlocal, dim=3) f = self.lmp.numpy.extract_atom_darray("f", nlocal, dim=3)
dtf = self.dtf dtf = self.dtf
dtv = self.dtv
mass = self.mass mass = self.mass
dtfm = dtf / np.take(mass, atype) dtfm = dtf / np.take(mass, atype)

View File

@ -16,7 +16,7 @@ if len(argv) != 1:
print("Syntax: demo.py") print("Syntax: demo.py")
sys.exit() sys.exit()
from lammps import lammps from lammps import lammps, LAMMPS_INT, LAMMPS_DOUBLE, LAMMPS_DOUBLE2D, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL, LMP_VAR_ATOM
lmp = lammps() lmp = lammps()
# test out various library functions after running in.demo # test out various library functions after running in.demo
@ -25,18 +25,18 @@ lmp.file("in.demo")
print("\nPython output:") print("\nPython output:")
natoms = lmp.extract_global("natoms",0) natoms = lmp.extract_global("natoms", LAMMPS_INT)
mass = lmp.extract_atom("mass",2) mass = lmp.extract_atom("mass", LAMMPS_DOUBLE)
x = lmp.extract_atom("x",3) x = lmp.extract_atom("x", LAMMPS_DOUBLE2D)
print("Natoms, mass, x[0][0] coord =",natoms,mass[1],x[0][0]) print("Natoms, mass, x[0][0] coord =",natoms,mass[1],x[0][0])
temp = lmp.extract_compute("thermo_temp",0,0) temp = lmp.extract_compute("thermo_temp", LMP_STYLE_GLOBAL, LAMMPS_INT)
print("Temperature from compute =",temp) print("Temperature from compute =",temp)
eng = lmp.extract_variable("eng",None,0) eng = lmp.extract_variable("eng",None, LMP_VAR_EQUAL)
print("Energy from equal-style variable =",eng) print("Energy from equal-style variable =",eng)
vy = lmp.extract_variable("vy","all",1) vy = lmp.extract_variable("vy","all", LMP_VAR_ATOM)
print("Velocity component from atom-style variable =",vy[1]) print("Velocity component from atom-style variable =",vy[1])
vol = lmp.get_thermo("vol") vol = lmp.get_thermo("vol")

View File

@ -27,7 +27,7 @@ if len(argv) != 2:
infile = sys.argv[1] infile = sys.argv[1]
from lammps import lammps from lammps import lammps, LAMMPS_INT, LAMMPS_DOUBLE2D, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL
lmp = lammps() lmp = lammps()
# run infile one line at a time # run infile one line at a time
@ -42,14 +42,14 @@ lmp.command("variable e equal pe")
lmp.command("run 0") lmp.command("run 0")
natoms = lmp.extract_global("natoms",0) natoms = lmp.extract_global("natoms",LAMMPS_INT)
emin = lmp.extract_compute("thermo_pe",0,0) / natoms emin = lmp.extract_compute("thermo_pe",LMP_STYLE_GLOBAL,LAMMPS_INT) / natoms
lmp.command("variable emin equal $e") lmp.command("variable emin equal $e")
# disorder the system # disorder the system
# estart = initial energy # estart = initial energy
x = lmp.extract_atom("x",3) x = lmp.extract_atom("x", LAMMPS_DOUBLE2D)
for i in range(natoms): for i in range(natoms):
x[i][0] += deltaperturb * (2*random.random()-1) x[i][0] += deltaperturb * (2*random.random()-1)
@ -58,10 +58,10 @@ for i in range(natoms):
lmp.command("variable elast equal $e") lmp.command("variable elast equal $e")
lmp.command("thermo_style custom step v_emin v_elast pe") lmp.command("thermo_style custom step v_emin v_elast pe")
lmp.command("run 0") lmp.command("run 0")
x = lmp.extract_atom("x",3) x = lmp.extract_atom("x", LAMMPS_DOUBLE2D)
lmp.command("variable elast equal $e") lmp.command("variable elast equal $e")
estart = lmp.extract_compute("thermo_pe",0,0) / natoms estart = lmp.extract_compute("thermo_pe", LMP_STYLE_GLOBAL, LAMMPS_INT) / natoms
# loop over Monte Carlo moves # loop over Monte Carlo moves
# extract x after every run, in case reneighboring changed ptr in LAMMPS # extract x after every run, in case reneighboring changed ptr in LAMMPS
@ -78,8 +78,8 @@ for i in range(nloop):
x[iatom][1] += deltamove * (2*random.random()-1) x[iatom][1] += deltamove * (2*random.random()-1)
lmp.command("run 1 pre no post no") lmp.command("run 1 pre no post no")
x = lmp.extract_atom("x",3) x = lmp.extract_atom("x", LAMMPS_DOUBLE2D)
e = lmp.extract_compute("thermo_pe",0,0) / natoms e = lmp.extract_compute("thermo_pe", LMP_STYLE_GLOBAL, LAMMPS_INT) / natoms
if e <= elast: if e <= elast:
elast = e elast = e
@ -96,10 +96,10 @@ for i in range(nloop):
# final energy and stats # final energy and stats
lmp.command("variable nbuild equal nbuild") lmp.command("variable nbuild equal nbuild")
nbuild = lmp.extract_variable("nbuild",None,0) nbuild = lmp.extract_variable("nbuild", None, LMP_VAR_EQUAL)
lmp.command("run 0") lmp.command("run 0")
estop = lmp.extract_compute("thermo_pe",0,0) / natoms estop = lmp.extract_compute("thermo_pe", LMP_STYLE_GLOBAL, LAMMPS_INT) / natoms
print("MC stats:") print("MC stats:")
print(" starting energy =",estart) print(" starting energy =",estart)