Merge branch 'master' into lammps-icms
Resolved Conflicts: lib/meam/Makefile.gfortran lib/poems/Makefile.g++ lib/reax/Makefile.gfortran python/lammps.py src/USER-CUDA/cuda.cpp
This commit is contained in:
@ -3,41 +3,29 @@ and allows the LAMMPS library interface to be invoked from Python,
|
||||
either from a script or interactively.
|
||||
|
||||
Details on the Python interface to LAMMPS and how to build LAMMPS as a
|
||||
shared library for use with Python are given in
|
||||
doc/Section_python.html.
|
||||
shared library, for use with Python, are given in
|
||||
doc/Section_python.html and in doc/Section_start.html#start_5.
|
||||
|
||||
Basically you need to follow these 3 steps:
|
||||
Basically you need to follow these steps in the src directory:
|
||||
|
||||
a) Add paths to environment variables in your shell script
|
||||
% make makeshlib # creates Makefile.shlib
|
||||
% make -f Makefile.shlib g++ # or whatever machine target you wish
|
||||
% make install-python # may need to do this via sudo
|
||||
|
||||
For example, for csh or tcsh, add something like this to ~/.cshrc:
|
||||
You can replace the last step with running the python/install.py
|
||||
script directly to give you more control over where two relevant files
|
||||
are installed, or by setting environment variables in your shell
|
||||
script. See doc/Section_python.html for details.
|
||||
|
||||
setenv PYTHONPATH ${PYTHONPATH}:/home/sjplimp/lammps/python
|
||||
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/sjplimp/lammps/src
|
||||
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/home/sjplimp/lammps/src/STUBS
|
||||
|
||||
The latter is only necessary if you will use the MPI stubs library
|
||||
instead of an MPI installed on your machine.
|
||||
|
||||
b) Build LAMMPS as a dynamic library, including dynamic versions of
|
||||
any libraries it includes for the packages you have installed,
|
||||
e.g. STUBS, MPI, FFTW, JPEG, package libs.
|
||||
|
||||
From the src directory:
|
||||
|
||||
% make makeshlib
|
||||
% make -f Makefile.shlib g++
|
||||
|
||||
If successful, this results in the file src/liblmp_g++.so
|
||||
|
||||
c) Launch Python and import the LAMMPS wrapper
|
||||
You can then launch Python and instantiate an instance of LAMMPS:
|
||||
|
||||
% python
|
||||
>>> from lammps import lammps
|
||||
>>> lmp = lammps()
|
||||
|
||||
If that gives no errors, you have succesfully wrapped LAMMPS with
|
||||
Python.
|
||||
Python. See doc/Section_python.html#py_5 for tests you can then use
|
||||
to run LAMMPS both in serial or parallel thru Python.
|
||||
|
||||
-------------------------------------------------------------------
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@ if len(argv) != 2:
|
||||
infile = sys.argv[1]
|
||||
|
||||
me = 0
|
||||
|
||||
# uncomment if running in parallel via Pypar
|
||||
#import pypar
|
||||
#me = pypar.rank()
|
||||
@ -38,12 +39,11 @@ for line in lines: lmp.command(line)
|
||||
# run a single step with changed coords
|
||||
|
||||
lmp.command("run 10")
|
||||
x = lmp.get_coords()
|
||||
x = lmp.gather_atoms("x",1,3)
|
||||
epsilon = 0.1
|
||||
x[0] += epsilon
|
||||
lmp.put_coords(x)
|
||||
lmp.scatter_atoms("x",1,3,x)
|
||||
lmp.command("run 1");
|
||||
lmp.command("run 1")
|
||||
|
||||
# uncomment if running in parallel via Pypar
|
||||
#print "Proc %d out of %d procs has" % (me,nprocs), lmp
|
||||
|
||||
35
python/install.py
Normal file
35
python/install.py
Normal file
@ -0,0 +1,35 @@
|
||||
#!/usr/local/bin/python
|
||||
|
||||
# copy LAMMPS shared library src/liblammps.so and lammps.py to system dirs
|
||||
# Syntax: python install.py [libdir] [pydir]
|
||||
# libdir = target dir for src/liblammps.so, default = /usr/local/lib
|
||||
# pydir = target dir for lammps.py, default = Python site-packages dir
|
||||
|
||||
import sys,commands
|
||||
|
||||
if len(sys.argv) > 3:
|
||||
print "Syntax: python install.py [libdir] [pydir]"
|
||||
sys.exit()
|
||||
|
||||
if len(sys.argv) >= 2: libdir = sys.argv[1]
|
||||
else: libdir = "/usr/local/lib"
|
||||
|
||||
if len(sys.argv) == 3: pydir = sys.argv[2]
|
||||
else:
|
||||
paths = sys.path
|
||||
for i,path in enumerate(paths):
|
||||
index = path.rfind("site-packages")
|
||||
if index < 0: continue
|
||||
if index == len(path) - len("site-packages"): break
|
||||
pydir = paths[i]
|
||||
|
||||
str = "cp ../src/liblammps.so %s" % libdir
|
||||
print str
|
||||
outstr = commands.getoutput(str)
|
||||
if len(outstr.strip()): print outstr
|
||||
|
||||
str = "cp ../python/lammps.py %s" % pydir
|
||||
print str
|
||||
outstr = commands.getoutput(str)
|
||||
if len(outstr.strip()): print outstr
|
||||
|
||||
@ -17,23 +17,15 @@ import types
|
||||
from ctypes import *
|
||||
import os.path
|
||||
|
||||
LMPINT = 0
|
||||
LMPDOUBLE = 1
|
||||
LMPIPTR = 2
|
||||
LMPDPTR = 3
|
||||
LMPDPTRPTR = 4
|
||||
|
||||
LOCATION = os.path.dirname(__file__)
|
||||
|
||||
class lammps:
|
||||
def __init__(self,name="",cmdlineargs=None):
|
||||
def __init__(self,name="",cmdargs=None):
|
||||
|
||||
# load liblmp.so by default
|
||||
# if name = "g++", load liblmp_g++.so
|
||||
# load liblammps.so by default
|
||||
# if name = "g++", load liblammps_g++.so
|
||||
|
||||
try:
|
||||
if not name: self.lib = CDLL("liblmp.so")
|
||||
else: self.lib = CDLL("liblmp_%s.so" % name)
|
||||
if not name: self.lib = CDLL("liblammps.so")
|
||||
else: self.lib = CDLL("liblammps_%s.so" % name)
|
||||
except:
|
||||
raise OSError,"Could not load LAMMPS dynamic library"
|
||||
|
||||
@ -42,10 +34,10 @@ class lammps:
|
||||
# no_mpi call lets LAMMPS use MPI_COMM_WORLD
|
||||
# cargs = array of C strings from args
|
||||
|
||||
if cmdlineargs:
|
||||
cmdlineargs.insert(0,"lammps.py")
|
||||
narg = len(cmdlineargs)
|
||||
cargs = (c_char_p*narg)(*cmdlineargs)
|
||||
if cmdargs:
|
||||
cmdargs.insert(0,"lammps.py")
|
||||
narg = len(cmdargs)
|
||||
cargs = (c_char_p*narg)(*cmdargs)
|
||||
self.lmp = c_void_p()
|
||||
self.lib.lammps_open_no_mpi(narg,cargs,byref(self.lmp))
|
||||
else:
|
||||
@ -68,30 +60,26 @@ class lammps:
|
||||
self.lib.lammps_command(self.lmp,cmd)
|
||||
|
||||
def extract_global(self,name,type):
|
||||
if type == LMPDOUBLE:
|
||||
self.lib.lammps_extract_global.restype = POINTER(c_double)
|
||||
ptr = self.lib.lammps_extract_global(self.lmp,name)
|
||||
return ptr[0]
|
||||
if type == LMPINT:
|
||||
if type == 0:
|
||||
self.lib.lammps_extract_global.restype = POINTER(c_int)
|
||||
ptr = self.lib.lammps_extract_global(self.lmp,name)
|
||||
return ptr[0]
|
||||
return None
|
||||
elif type == 1:
|
||||
self.lib.lammps_extract_global.restype = POINTER(c_double)
|
||||
else: return None
|
||||
ptr = self.lib.lammps_extract_global(self.lmp,name)
|
||||
return ptr[0]
|
||||
|
||||
def extract_atom(self,name,type):
|
||||
if type == LMPDPTRPTR:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_double))
|
||||
ptr = self.lib.lammps_extract_atom(self.lmp,name)
|
||||
return ptr
|
||||
if type == LMPDPTR:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(c_double)
|
||||
ptr = self.lib.lammps_extract_atom(self.lmp,name)
|
||||
return ptr
|
||||
if type == LMPIPTR:
|
||||
if type == 0:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(c_int)
|
||||
ptr = self.lib.lammps_extract_atom(self.lmp,name)
|
||||
return ptr
|
||||
return None
|
||||
elif type == 1:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_int))
|
||||
elif type == 2:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(c_double)
|
||||
elif type == 3:
|
||||
self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_double))
|
||||
else: return None
|
||||
ptr = self.lib.lammps_extract_atom(self.lmp,name)
|
||||
return ptr
|
||||
|
||||
def extract_compute(self,id,style,type):
|
||||
if type == 0:
|
||||
@ -153,18 +141,26 @@ class lammps:
|
||||
return result
|
||||
return None
|
||||
|
||||
# return total number of atoms in system
|
||||
|
||||
def get_natoms(self):
|
||||
return self.lib.lammps_get_natoms(self.lmp)
|
||||
|
||||
def get_coords(self):
|
||||
nlen = 3 * self.lib.lammps_get_natoms(self.lmp)
|
||||
coords = (c_double*nlen)()
|
||||
self.lib.lammps_get_coords(self.lmp,coords)
|
||||
return coords
|
||||
# return vector of atom properties gathered across procs, ordered by atom ID
|
||||
|
||||
# assume coords is an array of c_double, as created by get_coords()
|
||||
# could check if it is some other Python object and create c_double array?
|
||||
# constructor for c_double array can take an arg to use to fill it?
|
||||
|
||||
def put_coords(self,coords):
|
||||
self.lib.lammps_put_coords(self.lmp,coords)
|
||||
def gather_atoms(self,name,type,count):
|
||||
natoms = self.lib.lammps_get_natoms(self.lmp)
|
||||
if type == 0:
|
||||
data = ((count*natoms)*c_double)()
|
||||
self.lib.lammps_gather_atoms(self.lmp,name,type,count,data)
|
||||
elif type == 1:
|
||||
data = ((count*natoms)*c_double)()
|
||||
self.lib.lammps_gather_atoms(self.lmp,name,type,count,data)
|
||||
else: return None
|
||||
return data
|
||||
|
||||
# scatter vector of atom properties across procs, ordered by atom ID
|
||||
# assume vector is of correct type and length, as created by gather_atoms()
|
||||
|
||||
def scatter_atoms(self,name,type,count,data):
|
||||
self.lib.lammps_scatter_atoms(self.lmp,name,type,count,data)
|
||||
|
||||
Reference in New Issue
Block a user