2. Use Python with LAMMPS — LAMMPS documentation

\(\renewcommand{\AA}{\text{Å}}\)

# 2. Use Python with LAMMPS

These pages describe various ways that LAMMPS and Python can be used together.

- 2.1. Overview
- 2.2. Installation
- 2.3. Run LAMMPS from Python
- 2.4. The `lammps` Python module
- 2.5. Extending the Python interface
- 2.6. Calling Python from LAMMPS
- 2.7. Output Readers
- 2.8. Example Python scripts
- 2.9. Handling LAMMPS errors
- 2.10. Troubleshooting

If you are not familiar with Python, it is a powerful scripting and programming language which can do almost everything that compiled languages like C, C++, or Fortran can do in fewer lines of code. It also comes with a large collection of add-on modules for many purposes (either bundled or easily installed from Python code repositories). The major drawback is slower execution speed of the script code compared to compiled programming languages. But when the script code is interfaced to optimized compiled code, performance can be on par with a standalone executable, for as long as the scripting is restricted to high-level operations. Thus Python is also convenient to use as a “glue” language to “drive” a program through its library interface, or to hook multiple pieces of software together, such as a simulation code and a visualization tool, or to run a coupled multi-scale or multi-physics model.

See the Coupling LAMMPS to other codes page for more ideas about coupling LAMMPS to other codes. See the LAMMPS Library Interfaces page for a description of the LAMMPS library interfaces. That interface is exposed to Python either when calling LAMMPS from Python or when calling Python from a LAMMPS input script and then calling back to LAMMPS from Python code. The C-library interface is designed to be easy to add functionality to, thus the Python interface to LAMMPS is easy to extend as well.

If you create interesting Python scripts that run LAMMPS or interesting Python functions that can be called from a LAMMPS input script, that you think would be generally useful, please post them as a pull request to our GitHub site, and they can be added to the LAMMPS distribution or web page.