How to sum the x velocity in a region?

Dear All,

There is already a function lammps_get_coords(void *, double *) in library.cpp through which we can get the coordinates of atoms. Now I want to write a new function lammps_get_vels(void *, char *) which can sum the x velocity of atoms in a specified region, and this new function is called by my own code (which is similar to the coupling scheme of couple\lammps_quest) every timestep. However, the sum of velocity seems not right for the specified region. Can someone help me? The code of lammps_get_vels(void *, char *) is as follows:

double lammps_get_vels(void *ptr, char *regID)
LAMMPS *lmp = (LAMMPS *) ptr;

double **x = lmp->atom->x;
double **v = lmp->atom->v;
int nlocal = lmp->atom->nlocal;

int iregion = lmp->domain->find_region(regID);
Region *region = lmp->domain->regions[iregion];

double vels = 0.0;

for (int i = 0; i < nlocal; i++) {
if (region->match(x[i][0],x[i][1],x[i][2])) {
vels += v[i][0];
return vels;


Best regards
Wenjing Zhou

That's the right kind of logic. If you are running in
parallel, you would have to sum "vels" across processors
with a MPI_Allreduce() to get the total answer.