Hiya,
This email yesterday was a bit premature - I had forgotten that the CDLL() call was not run immediately on import, so my ‘test’ wasn’t actually testing anything - the problem was still there. Luckily, I’ve solved it!
I have two patches for the code. First, CDLL() doesn’t look in the python modules directory for C libraries, as it doesn’t think they live there, it looks on LD_LIBRARY_PATH, see these Stack Overflow questions about how that works:
http://stackoverflow.com/questions/9775425/ctypes-doesnt-find-fink-installed-libraries-in-sw-lib
http://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes
I’m not sure how this ever worked, but I suspect that you might have tested it in the install directory (which doesn’t import the installed version, but the local one) or something, when weird things happen and things seem to work when they don’t.
Anyway, in order to get this to work, you either need to move _lammps.so and/or _lammps_serial.so into LD_LIBRARY_PATH somewhere like /usr/lib, or you can hack the python module to know where it lives and give the absolute path to the C library, like this:
(python/lammps.py)
LMPINT = 0
LMPDOUBLE = 1
LMPIPTR = 2
LMPDPTR = 3
LMPDPTRPTR = 4
LOCATION = os.path.dirname(file)
class lammps:
def init(self,args=None):
attempt to load parallel library first, serial library next
could provide caller a flag to choose which library to load
try:
self.lib = CDLL(os.path.join(LOCATION, “_lammps.so”))
except:
#try:
self.lib = CDLL(os.path.join(LOCATION, “_lammps_serial.so”))
#except:
#raise OSError,“Could not load LAMMPS dynamic library”
(note Steve that this also includes my previous patch for the invalid try/except block.) I think this is the more ‘pythonic’ way of doing it, as you don’t need to mess around assuming anything about the system configuration for this to work, and you python libraries all live under site-packages where they belong.
Now, when you call
l = lammps.lammps()
you are actually loading the correct library - which brings me to my second patch.
When I run this I get a symbol error like this (illustrated in the interactive interpreter):
import lammps
l = lammps.lammps()
Traceback (most recent call last):
File “”, line 1, in
File “/Library/Python/2.6/site-packages/lammps.py”, line 38, in init
self.lib = CDLL(os.path.join(LOCATION, “_lammps_serial.so”))
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/init.py”, line 345, in init
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/Library/Python/2.6/site-packages/_lammps_serial.so, 6): Symbol not found: _MPI_Abort
Referenced from: /Library/Python/2.6/site-packages/_lammps_serial.so
Expected in: flat namespace
in /Library/Python/2.6/site-packages/_lammps_serial.so
The important part, “Symbol not found: _MPI_Abort” seems to indicate that the MPI stub library has not been included in the build correctly.
This is because there’s a subtle error in the setup_serial.py install script which means that the MPI library isn’t included! It’s here:
libfiles = glob.glob("s/src/*.cpp" path) +
glob.glob("s/src/STUBS/*.cpp" path)
Now, if you actually look in the STUBS folder, you’ll see that we have a simple mpi.c file, and no *.cpp matching ones! Rather than renaming it to mpi.cpp and wrapping all the function declarations in extern ‘C’ { … } blocks in the header, the smallest code change to fix this is just to change the above to:
libfiles = glob.glob("s/src/*.cpp" path) +
glob.glob("s/src/STUBS/*.c" path)
which gives us exactly what we wanted. We can now do everything with a simple sudo python setup_serial.py install and then run the following in the interactive interpreter as follows:
import lammps
l = lammps.lammps()
LAMMPS (8 Feb 2012)
and finally, we have a working python module. If anyone has a system other than OS X (10.6.8, python 2.6) to test this on, I’d be grateful, as the main platform for lammps seems to be linux, and I don’t have a linux box handy. I don’t expect there to be any platform issues with these changes though, as they’re all in python land, and I’ve managed to avoid any hard coded path issues.
Thanks, apologies about the millions of very long emails to the list, and please let me know if anyone would like these changes in diff format or anything.
Joe