# loop over nearest neighbors in compute(): why index j takes on values that may be larger then the total number of particles?

Dear community,

in any pair potential style there is a loop over nearestneigbors.

to get the idea of what is happening I have run lammps in serial on a one core and put out the correspondent indices in the double loop for the force calculation in the function compute (the output is attached).

as I assumed index i has values in the range 0 <= i < N, where N=512 is the total number of atoms.

but index j is sometimes the way higher that N. Why does it happen?

It looks to me strange because there are the line of code

delx = xtmp - x[j][0];
dely = ytmp - x[j][1];
delz = ztmp - x[j][2];

where index j is directly used to get the particle coordinates from array atom->x, which is a local array. and when we do not run lammps in paralel there should not be ghost atoms and this is why the size of atom->x should be nlocal which for a single processor in my case is N=512, is this right?

Thank you!

Anton

output_indices_i_j_from_compute (549 KB)

Dear community,

in any pair potential style there is a loop over nearestneigbors.
to get the idea of what is happening I have run lammps in serial on a one
core and put out the correspondent indices in the double loop for the force
calculation in the function compute (the output is attached).
as I assumed index i has values in the range 0 <= i < N, where N=512 is the
total number of atoms.
but index j is sometimes the way higher that N. Why does it happen?

because of ghost atoms.

It looks to me strange because there are the line of code

delx = xtmp - x[j][0];
dely = ytmp - x[j][1];
delz = ztmp - x[j][2];

where index j is directly used to get the particle coordinates from array
atom->x, which is a local array. and when we do not run lammps in paralel
there should not be ghost atoms and this is why the size of atom->x should
be nlocal which for a single processor in my case is N=512, is this right?

no. LAMMPS will create ghost atoms from periodic replica until you
reach the cutoff plus "skin" distance. thus it is not subject to
minimum image conventions and the number of ghost atoms can by far
exceed the number of local atoms.

axel.