Center molecule in box

I would like to be able to move a specific molecule’s COM inside my wrapped box before starting my next simulation. When using displace_atoms all move (-xcm(dat,x)+0.5*xhi) (-xcm(dat,y)+0.5yhi) $(-xcm(dat,z)+0.5zhi) units box, I realize that xcm returns the unwrapped coordinates, thus my shift makes little sense. Is there a way to return xcm and return wrapped coordinates so I can make the appropriate shift while still immediate as described in the manual?

I don’t get your logic. If the molecule was initially entered correctly you must use unwrapped coordinates to compute the center of mass, since otherwise you might “break the molecule” (if part is outside the box in unwrapped coordinates).

What matters is what your coordinates look like after the displacement, not what the value of the shift it.

Thank you for your reply. What I am attempting to do is take snapshots from an equilibrated simulation and perform simulations from the snapshots. What I want to do is calculate the shift required to place one specific molecule at the “center” of the periodic box and simulate the system with temperature coupling on a spherical region not including my molecule in question. I have some complicated python code to shift the molecule correctly, but would prefer to do this in a single lammps script, shifting the atom positions before performing the simulation. Is there an effective way to do this without editing the lammps code?

I cannot answer this because what you describe is confusing and not clear from your description.

The command you quoted will recenter the entire system so that the center of mass of the group “dat” will be in the center of the box. That makes sense and is working as advertised. If that is not what you want you need to provide more details and a better explanation of what you want and what you get instead.

Understood, I am confused on the best method forward and certainly it is coming out in my writing. I do in fact want to center the molecule in question in the box.

From the entered command I get the following from using #lmp -echo screen -i
displace_atoms all move (-xcm(dat,x)+0.5*xhi) (-xcm(dat,y)+0.5yhi) (-xcm(dat,z)+0.5*zhi) units box displace_atoms all move 484.828192162706614 (-xcm(dat,y)+0.5yhi) (-xcm(dat,z)+0.5*zhi) units box displace_atoms all move 484.828192162706614 -1787.5037003130407811 (-xcm(dat,z)+0.5*zhi) units box
displace_atoms all move 484.828192162706614 -1787.5037003130407811 3022.4432854929887071 units box

yet, when I check the COM of the molecule in question instead of being at (27,27,27) approx values it is at the initial value (1.8,48.9,32.3). And this is where I am lost. As you said it ought to center my molecule in the box, instead it is at the initial value. I have tried this using the restart file and by a data file following,

lmp -restart2data rst remap data

What I find most confusing from the output is that none of my atoms are beyond the box boundaries in the data file, they all exist between the boundaries xlo=ylo=zlo=0, xhi=yhi=zhi=54.

How do you check?

That is the expected behavior. LAMMPS stores coordinates in two parts: there is the position inside the principal box, and there are 3 image flags that count how many times an atom has passed through periodic boundaries in ix, iy, and iz. The unwrapped coordinates are the regular x + ix*boxx and correspondingly for y and z.

For your reference, here is an example recentering the peptide molecule in the peptide example.
Adding the following lines to in.peptide

print "COM $(xcm(peptide,x))  $(xcm(peptide,y))  $(xcm(peptide,z))"
print "COB $(0.5*lx+xlo)  $(0.5*ly+ylo)  $(0.5*lz+zlo)"
displace_atoms all move $(-xcm(peptide,x)+0.5*lx+xlo) &
                        $(-xcm(peptide,y)+0.5*ly+ylo) &
                        $(-xcm(peptide,z)+0.5*lz+zlo) units box
print "COM $(xcm(peptide,x))  $(xcm(peptide,y))  $(xcm(peptide,z))"
print "COB $(0.5*lx+xlo)  $(0.5*ly+ylo)  $(0.5*lz+zlo)"

Results in the following output:

COM 44.178076058980408902  54.426912107845517141  42.886166020809461941
COB 50.52587700000000126  54.699374500000004673  43.45377849999999853
Displacing atoms ...
COM 50.525876999999951522  54.699374500000004673  43.453778500000005636
COB 50.52587700000000126  54.699374500000004673  43.45377849999999853

After recentering the molecule, its center of mass coincides with the center of the box.
Since the lower boundaries of the box in this example are not on 0,0,0 I had to modify the computation of the center a little bit compared to your example.

1 Like

I check using the dump file that I have set to output “mol id x y z vx vy vz”.

That is inconsistent with the coordinates that are used for computing the center of mass. x, y, z are the coordinate of the periodic image that is inside the box. You want to output and compare to unwrapped coordinates, i.e. xu, yu, zu or also output the image flags ix,iy,iz.

Hey Axel,
Thank you for your help and the example code. My issue arose from using xhi instead of lx, once I edited the code to use lx/ly/lz I still see the large values for COM before dispacement, however, the COM of my molecule is in fact at the COB just as I desire following dispacement. I finally get why I was having large position values, I assumed that remap command would reduce the values of nx/ny/nz, my mistake–I needed to look closer.