Error when launching code on more than 16 proc

Hello Lammps,

I want to exchange the position of 2 particles of different types at each step and calculate the energy.
Here the resume of some part of my code.

atom_modify   map array sort 0 0.0
# Simulation variables
....
#
 units           metal
  atom_style      atomic
  read_data       glass_2400.dat

# Interatomic potential information.

  neighbor        4.0 bin
  neigh_modify    every 1 check no  
#############
variable           xi equal x[v_i]
variable           yi equal y[v_i]
variable           zi equal z[v_i]

variable           xj equal x[v_j]
variable           yj equal y[v_j]
variable           zj equal z[v_j]


variable ii loop  1 1000000
label defloop

print              "##### LOOP STEP ${ii}"
variable           i equal ${irandom}
variable           j equal ${irandom}

variable           x0 equal ${xi}
variable           y0 equal ${yi}
variable           z0 equal ${zi}
set                atom $i x ${xj}
set                atom $i y ${yj}
set                atom $i z ${zj}
set                atom $j x ${x0}
set                atom $j y ${y0}
set                atom $j z ${z0}

run               1 

if a criterion is not satisfied i put back the particle 
"set                atom $j x ${xi}" &
                    "set                atom $j y ${yi}" &
                    "set                atom $j z ${zi}" &
                    "set                atom $i x ${x0}" &
                    "set                atom $i y ${y0}" &
                    "set                atom $i z ${z0}"
next ii
jump SELF defloop
label endloop

#####################################################

If i launch this code in 1,2,4,16 core is ok, for more than 16 i get this error:

Per MPI rank memory allocation (min/avg/max) = 324 | 324 | 324.1 Mbytes
Step Temp PotEng Fmax Press v_e v_elast
ERROR: Lost atoms: original 1000 current 998 (../thermo.cpp:435)
Last command: run               1 

which i believe is a communication error. I am calculating the energy at each exchange of position and also the neighbor list. Do you have any idea of what is going on and how to check the correctness of the code?
Thank you very much for your help, kind regards

What you are doing is very problematic as it conflicts with the domain decomposition parallelization that LAMMPS uses. Each processor only holds a subset of atoms and some copies of atoms from the neighboring sub-domains up to the communication cutoff value (usually the largest pairwise cutoff plus the neighbor skin). When you reassign your atom positions, you may move them beyond those limits and that means that during the next reneighboring procedure LAMMPS does not know how to transfer the information and thus “loses” them.
For more background on that please have a look at 4.4. Parallel algorithms — LAMMPS documentation

You may instead want to use the displace_atoms command — LAMMPS documentation
which includes the necessary communication to transfer atoms between sub-domains.

Thank you for the reply.

Regarding the displace_atoms command alternative, I read in the page the note

Care should be taken not to move atoms on top of other atoms.

However this is my original purpose, swap the positions of two atoms, do you suggest a workaround (such as displace the atom in another intermediate position) ?

Thank you for your help and kind regards.

Since you are not leaving the atoms on top of each other it is not a problem.

Mind you, a completely different but probably equivalent approach to the algorithm would be to not move atom positions at all, but rather swap atom types. For this also there is an automated procedure available with the fix atom/swap command — LAMMPS documentation

Thank you very much for the clarification.

As a last step I would like to confirm that everything is correctly working when I am changing the number of processors. To do this I would like to exactly reproduce the simulation results using different processors (minding the issue related to the max number of processors).

Since i am using the random function

variable        seed equal 7774 
variable        irandom equal floor(atoms*random(0.0,1.0,${seed})+1)
variable        i equal ${irandom}
variable        j equal ${irandom}

which procedure would you suggest?
E.g. should i provide an external file with fixed values for i and j ?
Thanks again and kind regards.

Why ask and why not just confirm this yourself by running with a different number of processors?

I probably would not be using explicit scripting at all but use fix atom/swap or create a modified version of it in case you have a different algorithm to swap atoms.