Python Command Questions

Hello all,

I am working on gluing LAMMPS to the PSI4 package via Python for some fun stuff I have planned. To that end I want to get the Python package working in LAMMPS. I can already call both packages through python in a couple ways (e.g. by making a library of lammps and importing it and then feeding the python to the PSI4 interpreter) I now need to be able to call Python from LAMMPS. I have followed the README for linking LAMMPS to my machine’s python and other system libraries in order to run “embedded” Python.

I have several questions.

1.)
The provided LAMMPS python example does not get parsed by input correctly. It (correctly) complains that whitespaces should follow end quotes (which is a lammps input requirement). This I assumed was taken care of by input.cpp asking python.cpp to parse it but I don’t see where this happens. It seems like parse() doesn’t look for python like stuff and ask for help if needed.

2.) If I comment out all the non LAMMPS input looking stuff and just get to the invoking of the python command I find that the command is not recognized. In looking in python.h I find that it isn’t declared as a CommandStyle and therefore isn’t added to style_command.h. If I add python as a command in the normal fashion I can now get lammps to call the Command function as expected; though in looking at the code this doesn’t seem to be a good idea. Defined functions will get cleaned up by the destructor before I can invoke them for instance. If I forge ahead anyway I find that no exceptions are thrown when invoking the py interpreter etc. and it seems that LAMMPS can find python just fine but the code does in fact throw out all function information before I have a chance to use it. So Pfuncs is empty (and nfunc==0) when python foo invoke tries to call foo.

3.)Given the Python “command” seems to work in a different fashion than other commands how is input supposed to recognize input invocations of it?

Sorry if I rambled excessively and thank you very much for your time and help,

David Hyde-Volpe

Which version of LAMMPS and which version of Python are you using?

The provided LAMMPS python example does not get parsed by input correctly.

Are you referring to examples/python/in.python? It works fine for me.

The python command is not a CommandStyle command. It is processed differently.

You shouldn’t have to do anything to the source code to get this to work,

assuming you built LAMMPS correctly with the PYTHON package installed.

You also have to build LAMMPS as a shared lib.

The one thing that is broken is the last Python function evaluation (in funcs.py)

invoked at the end of examples/python/in.python.

Axel - the code you added/changed in python/lammps.py broke this on my box.

This line in lammps.py:
modpath = dirname(abspath(getsourcefile(lambda:0)))
print “MOD”,modpath

now prints:
MOD /home/sjplimp/lammps/python

but the liblammps.so file is in lammps/src

On my box, LD_LIBRARY_PATH includes lammps/src

(but not lammps/python). So why is getsourcefile() not finding it?

Steve

Which version of LAMMPS and which version of Python are you using?

The provided LAMMPS python example does not get parsed by input correctly.

Are you referring to examples/python/in.python? It works fine for me.

The python command is not a CommandStyle command. It is processed
differently.
You shouldn't have to do anything to the source code to get this to work,
assuming you built LAMMPS correctly with the PYTHON package installed.
You also have to build LAMMPS as a shared lib.

The one thing that is broken is the last Python function evaluation (in
funcs.py)
invoked at the end of examples/python/in.python.

Axel - the code you added/changed in python/lammps.py broke this on my box.

This line in lammps.py:
    modpath = dirname(abspath(getsourcefile(lambda:0)))
    print "MOD",modpath
now prints:
    MOD /home/sjplimp/lammps/python
but the liblammps.so file is in lammps/src

On my box, LD_LIBRARY_PATH includes lammps/src
(but not lammps/python). So why is getsourcefile() not finding it?

because liblammps.so should be in the same location as lammps.py and
if you install it properly with "make install-python" exactly that
should happen.
using LD_LIBRARY_PATH to find liblammps.so can easily lead to picking
up an library version. other python packages do the same thing to make
sure they do not accidentally pick up a wrong shared object.

axel.

So the issue was this.

  1. I was using the 15May15 version of LAMMPS (with a lot of modifications of my own) and in this version input doesn’t have a python function as the current build does(through variable). This now allows input to get the help it needs.
  2. If I use a current version from github for instance this is resolved but I am having the same issue Dr. Plimpton had(my python packages directory has my lammps.py and .pyc but not my liblammps.so. This I resolved by manually copying the .so file into my python packages dir.

I thought I checked most of the python related differences between my dev version and the current but I forgot to check for differences in input (which was dumb of me). Everything seems to be working fine now just have to merge my builds. Thanks for all the help.

David Hyde-Volpe

See the Section_python doc page or the lammps/python/README file

for instructions on which files to put where and how to automate it.

The embedded Python capability was added after May 2015, so there

was nothing about that version that matched the current doc pages

in that respect.

Steve