Dear LAMMPS users and developers,
I am working on a custom compute function where I need to calculate the displacement of an atom relative to the center of its neighboring atoms. For instance, I want to compute the displacement vector of a carbon atom in a methane (CH₄) molecule, shifting away from the center of the CH₄ tetrahedral structure. The neighbor list is provided by an external file, which records the global IDs of both the central and neighboring atoms.
However, I’ve encountered a problem: when I use atom->map(global_id)
to retrieve the local index of an atom from its global ID, it consistently returns -1
. This occurs even though I am running LAMMPS with only one processor (also, I have set OMP_NUM_THREADS=1
).
Here is the core part of the code:
void ComputeCustomDisp::compute_peratom()
{
// check number of atoms
if (atom->nmax > nmax) {
memory->destroy(array_atom);
nmax = atom->nmax;
memory->create(array_atom, nmax, size_peratom_cols, "custom_disp:array_atom");
}
// reset the array_atom to 0
for (int i = 0; i < atom->natoms; i++) {
array_atom[i][0] = 0.0;
array_atom[i][1] = 0.0;
array_atom[i][2] = 0.0;
}
// assign the coord
double **x = atom->x;
// loop over all central atoms
size_t i,j;
for(i=0; i < central_id.size(); i++) {
int central_global_id = central_id[i];
int central_local_id = atom->map(central_global_id);
printf("central_global_id: %d\n", central_global_id);
printf("central_local_id: %d\n", central_local_id);
if (central_local_id < 0) continue; // skip if the central atom isn't in the processor
// loop over all neighbors of the central atom
double xn=0.0, yn=0.0, zn=0.0;
int neighbor_count = 0;
for(j = 0; j < neighbor_id[i].size(); j++) {
int neighbor_global_id = neighbor_id[i][j];
int neighbor_local_id = atom->map(neighbor_global_id);
printf("neighbor_global_id: %d\n", neighbor_global_id);
printf("neighbor_local_id: %d\n", neighbor_local_id);
if (neighbor_local_id < 0) continue; // skip if the neighbor atom isn't in the processor
xn += x[neighbor_local_id][0];
yn += x[neighbor_local_id][1];
zn += x[neighbor_local_id][2];
neighbor_count++;
}
xn /= neighbor_count;
yn /= neighbor_count;
zn /= neighbor_count;
double dx = x[central_local_id][0] - xn;
double dy = x[central_local_id][1] - yn;
double dz = x[central_local_id][2] - zn;
array_atom[central_local_id][0] = dx;
array_atom[central_local_id][1] = dy;
array_atom[central_local_id][2] = dz;
}
}
The issue is that for every central atom, the atom->map()
function always returns -1
, even though I am running on a single processor. Below is the output from my code:
central_global_id: 5
central_local_id: -1
central_global_id: 6
central_local_id: -1
central_global_id: 7
central_local_id: -1
central_global_id: 8
central_local_id: -1
central_global_id: 9
central_local_id: -1
central_global_id: 10
central_local_id: -1
central_global_id: 11
central_local_id: -1
central_global_id: 12
central_local_id: -1
I’m new to both C++ and LAMMPS development, so I would greatly appreciate any advice or suggestions to resolve this issue. Specifically, why is atom->map()
returning -1
even when running with a single processor?
Thank you in advance for your help!
Best regards,
denan