GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 1. Introduction
  • 2. Install LAMMPS
  • 3. Build LAMMPS
  • 4. Run LAMMPS
  • 5. Errors
  • 6. Commands
  • 7. Accelerate performance
  • 8. Optional packages
  • 9. Auxiliary tools
  • 10. Howto discussions
  • 11. Example scripts
  • 1. LAMMPS Library Interfaces
  • 2. Use Python with LAMMPS
  • 2.1. Overview

This view

  • Original page
  • New address

GeezerVison is showing docs.lammps.org — 2.3.5. Executing commands — LAMMPS documentation

2.3.5. Executing commands

Once an instance of the lammps class is created, there are multiple ways to “feed” it commands. In a way that is not very different from running a LAMMPS input script, except that Python has many more facilities for structured programming than the LAMMPS input script syntax. Furthermore it is possible to “compute” what the next LAMMPS command should be.

Same as in the equivalent C library functions, commands can be read from a file, a single string, a list of strings and a block of commands in a single multi-line string. They are processed under the same boundary conditions as the C library counterparts. The example below demonstrates the use of lammps.file(), lammps.command(), lammps.commands_list(), and lammps.commands_string():

from lammps import lammps
lmp = lammps()

# read commands from file 'in.melt'
lmp.file('in.melt')

# issue a single command
lmp.command('variable zpos index 1.0')

# create 10 groups with 10 atoms each
cmds = [f"group g{i} id {10*i+1}:{10*(i+1)}" for i in range(10)]
lmp.commands_list(cmds)

# run commands from a multi-line string
block = """
clear
region  box block 0 2 0 2 0 2
create_box 1 box
create_atoms 1 single 1.0 1.0 ${zpos}
"""
lmp.commands_string(block)

For convenience, the lammps class also provides a command wrapper cmd that turns any LAMMPS command into a regular function call.

For instance, the following LAMMPS command

region box block 0 10 0 5 -0.5 0.5

would normally be executed with the following Python code:

from lammps import lammps

lmp = lammps()
lmp.command("region box block 0 10 0 5 -0.5 0.5")

With the cmd wrapper, any LAMMPS command can be split up into arbitrary parts. These parts are then passed to a member function with the name of the command. For the region command that means the region() method can be called. The arguments of the command can be passed as one string, or individually.

from lammps import lammps

L = lammps()

# pass command parameters as one string
L.cmd.region("box block 0 10 0 5 -0.5 0.5")

# OR pass them individually
L.cmd.region("box block", 0, 10, 0, 5, -0.5, 0.5)

In the latter example, all parameters except the first are Python floating-point literals. The member function takes the entire parameter list and transparently merges it to a single command string.

The benefit of this approach is avoiding redundant command calls and easier parameterization. With command, commands_list, and commands_string the parameterization needed to be done manually by creating formatted command strings.

lmp.command("region box block %f %f %f %f %f %f" % (xlo, xhi, ylo, yhi, zlo, zhi))

In contrast, methods of the cmd wrapper accept parameters directly and will convert them automatically to a final command string.

L.cmd.region("box block", xlo, xhi, ylo, yhi, zlo, zhi)

Note

When running in IPython you can use Tab-completion after L.cmd. to see all available LAMMPS commands.

Using these facilities, the previous example shown above can be rewritten as follows:

from lammps import lammps
L = lammps()

# read commands from file 'in.melt'
L.file('in.melt')

# issue a single command
L.cmd.variable('zpos', 'index', 1.0)

# create 10 groups with 10 atoms each
for i in range(10):
   L.cmd.group(f"g{i}", "id", f"{10*i+1}:{10*(i+1)}")

L.cmd.clear()
L.cmd.region("box block", 0, 2, 0, 2, 0, 2)
L.cmd.create_box(1, "box")
L.cmd.create_atoms(1, "single", 1.0, 1.0, "${zpos}")
Version release
LAMMPS Branch:
develop
stable
release
Downloads:
PDF
Source
Git Info:
13Nov2026
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.