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
- Molecules from SMILES or files. Each species can be given as a SMILES string, the one-line text notation chemists use for molecules (
Cfor methane,C#Cfor 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,atomicorfull) and the masses included, or an.xyzfile 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.

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 molecules of molar mass and a target density , the edge of the cubic box is
Here the molecules weigh 20 × 16.04 + 15 × 32.00 + 2 × 100.20 + 28 × 26.04 = 1,730.3 g/mol in all, or g for this one box. At 0.1 g/cm³ that fills 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.

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.