Generating the data file using python

Dear Lammps users, I hope you are all doing well.

I want to make a data file for polymer, which should contain 5 chains of 100 beads each. And I was able to generate the coordinates of one chain of 100 beads. Now I am unable to set the molecular topology which contains the bonds between the beads. Can anybody help me with this, I am using python to generate the data.

Thanks in advance.

import numpy as np

natoms= 100


positions= []

for i in range(natoms):


with open(‘’,‘w’) as fdata:

fdata.write(‘Random atoms\n\n’)


fdata.write(’{} atoms\n’.format(natoms))

fdata.write(’{} atom types\n’.format(1))

#Box dimensions

fdata.write(’{} {} xlo xhi\n’.format(0.0,system_size))

fdata.write(’{} {} ylo yhi\n’.format(0.0,system_size))

fdata.write(’{} {} zlo zhi\n’.format(0.0,system_size))


Atoms section


for i,pos in enumerate(positions):

fdata.write(’{} 1 {} {} {}\n’.format(i+1,*pos))



I wouldn’t say unable, you just haven’t written the code, yet. How I would do this is simply create bonds between the atoms such that each atom can have two bonds. This approach doesn’t care about position and so you’ll have to do a minimization in lammps upon reading in the data file.

Hope that helps,

apart from that, this is about the worst possible algorithm to generate a random bead-spring chain. bonds must not be longer than the neighborlist cutoff, or else LAMMPS will complain about lost bond atoms or pick the wrong periodic copy. a better algorithm (add atoms at constant distance but random direction from the previous two and so there is a minimum distance between 1-3 atom pairs) is implemented in the “chain.f” tool in the tools folder. since you add atoms in sequence, building the bond topology is trivial. but again, why re-invent an existing, well-tested wheel?

I definitely don't want to discourage you from creating the DATA file
yourself. (If you are just creating a string of masses connected by
springs, it should be pretty easy to create the data file from
scratch.) However for more complex systems you might also take a look
at the tools here:
(Disclaimer: I wrote one of these tools)
Creating the data file complete from scratch can get tedious if you
have a complex all-atom polymer, or even a simple coarse-grained
polymer with side chains. There are a lot of tools and examples on
these web pages showing how to generate angle, dihedral, and
impropers, and automatically assign force field parameters.
(I hope this helps. Please forgive the self-promotion.)