K. M. Mukut
Blog · · 5 min read

PyPack: building the starting box for a molecular simulation

Every reactive simulation starts with a box full of molecules at the right density, none of them overlapping. PyPack builds that box from SMILES strings or structure files and writes it out for LAMMPS, from a small GUI or the command line.

Before a reactive molecular dynamics simulation can tell you anything, someone has to build its starting point: a box filled with the right molecules, in the right numbers, at the right density, with no two atoms sitting on top of each other. For a box of thirty methane molecules that is a minor chore. For a mixture of several fuels and oxidizers, hundreds of molecules, at a chosen density, it is tedious and easy to get wrong, and an overlap that slips through can blow up the simulation in its first steps.

PyPack is the small Python toolkit I wrote to do that job. You list the molecules and how many of each you want; it builds them in 3D, packs them into a periodic box without collisions, and writes a file that LAMMPS can read, ready for simulations with reactive force fields such as ReaxFF.

How it works

A vertical sequence of six steps: define molecules and counts from SMILES strings or structure files; size the box from the target density; make a new conformer for each copy; place each copy with a random rotation while keeping a minimum distance; grow the box by 20 percent if packing fails, up to five times; write a LAMMPS data file or an .xyz file
From a list of molecules to a simulation-ready box. Diagram drawn for this post.
  • Molecules from SMILES or files. Each species can be given as a SMILES string, the one-line text notation chemists use for molecules (C for methane, C#C for acetylene), or as a structure file (.xyz, .mol, .sdf). For a SMILES entry, Open Babel builds a 3D template and saves it as <name>.xyz.
  • A box sized by density. You give a target density, and PyPack works out how big the box must be for the molecules you asked for.
  • A fresh shape for every copy. Real molecules are not identical rigid copies. For each molecule it places, PyPack generates a new conformer, one possible 3D arrangement of the same molecule, with RDKit and a quick Open Babel optimization, so a box of fifty heptane molecules does not contain fifty clones.
  • Collision-free placement. Each copy gets a random position and a random 3D rotation. A k-d tree (SciPy’s cKDTree) checks it against everything already placed, and the copy is kept only if every atom is at least a chosen minimum distance from all the others. Each molecule gets a set number of attempts.
  • A box that grows when needed. If the molecules cannot all be packed, PyPack enlarges the box by 20% and tries again, up to five times.
  • Output. A LAMMPS data file, with the atom style of your choice (charge, atomic or full) and the masses included, or an .xyz file for visualization.

Two ways to use it

The graphical interface is the easiest start. Add molecules row by row, choose SMILES or a file for each, set the count, the density, the minimum distance and the number of attempts, pick an output format, and press Run. A console at the bottom shows progress as it packs.

The PyPack window: a table of molecules (methane and n-heptane as SMILES, oxygen from a file, acetylene as SMILES) with counts, a parameters panel with density, temperature, minimum distance, maximum attempts, atom style and output format, an output file field, a Run button, and a console reporting the box size and the file written
The PyPack interface packing four species into a LAMMPS data file. Screenshot from the PyPack repository.

For scripts and batch jobs there is a command-line interface that reads the molecule list from a JSON file:

[
  {"name": "methane", "smiles": "C", "count": 30},
  {"name": "oxygen", "file": "oxygen.xyz", "count": 12}
]
python pypack.py --json molecules.json --format lammps-data --atom_style charge --output reax_input.data

Options set the target density, minimum distance and maximum attempts in the same way as the GUI.

A worked example

Take the mixture in the screenshot: 20 methane, 15 oxygen, 2 n-heptane and 28 acetylene molecules at 0.1 g/cm³. The box size follows from the total mass. With nin_i molecules of molar mass MiM_i and a target density ρ\rho, the edge of the cubic box is

L=(∑iniMiNA ρ)1/3L = \left( \frac{\sum_i n_i M_i}{N_A\,\rho} \right)^{1/3}

Here the molecules weigh 20 × 16.04 + 15 × 32.00 + 2 × 100.20 + 28 × 26.04 = 1,730.3 g/mol in all, or 2.873×10−212.873 \times 10^{-21} g for this one box. At 0.1 g/cm³ that fills 2.873×10−202.873 \times 10^{-20} cm³, which is 28,732 ų, so the box is 30.63 Å on each side: the number the console in the screenshot reports.

Running the same mixture through PyPack 1.0.0 from the command line packs it on the first attempt, in under two seconds on an ordinary desktop:

$ python pypack.py --json mix.json --format lammps-data \
      --atom_style charge --output reax_input.data
Packing attempt 1: box = 30.63 Å
Writing to reax_input.data in lammps-data format...
Done.
A wireframe cube holding scattered small molecules drawn as balls and sticks: dark carbon atoms, light hydrogen atoms and dark red oxygen pairs, with a long zigzag heptane chain near the middle and plenty of empty space between molecules
The box from that run: 288 atoms, with carbon in dark gray, hydrogen in light gray and oxygen in dark red. Atoms of a molecule that crosses a face reappear on the opposite face, as in any periodic box. Rendered for this post.

At this density the box is mostly empty, like a gas, a typical start for a gas-phase reactive simulation. The data file is ready for LAMMPS, with the box, the masses and one line per atom (ID, type, charge, position):

288 atoms
3 atom types

0.0      30.629999999999999  xlo xhi
0.0      30.629999999999999  ylo yhi
0.0      30.629999999999999  zlo zhi

Masses

1      12.010999996910238 # C
2      1.0079999997406976 # H
3      15.998999995884349 # O

Atoms # charge

     1   1   0.0      6.0069485721694562      12.664145414222277      4.3843627446849336
     2   2   0.0      5.5849262990872086      13.030578302697162      3.4459948120665791
...

Where it fits

PyPack covers the first step of a reactive simulation workflow: the starting box. LAMMPS then runs the chemistry, and a tool like MAFIA-MD can analyze the rings and molecules that come out at the end.

Limits

  • Very dense systems can fail. At high densities there may be no room left for the last molecules, even after the box has grown five times. Lower the density or the minimum distance, or allow more attempts.
  • File input must be readable by ASE, the Atomic Simulation Environment library PyPack uses to read structure files.
  • Random by design. Positions, rotations and conformers are random, so two runs give different boxes with the same composition and density.

Get it

PyPack is open source under the MIT license, on GitHub. It needs NumPy, SciPy, Open Babel, RDKit and ASE:

git clone https://github.com/kmmukut/PyPack.git
cd PyPack
pip install numpy scipy openbabel rdkit ase
python gui.py                        # the graphical interface

It builds on Open Babel, RDKit, ASE and LAMMPS, and wouldn’t exist without them.