Dear Steve,
I am trying to modify the simple compute_ke.cpp code for writing a new
compute command later.
So I replace the v[i][0],v[i][1] and v[i][2] with x[i][0],x[i][1] and
x[i][2] and then I compare the result with 0.5*m*(x^2 + y^2 +z^2) from
dump file. When I use a single processing, they are the same and when I
use multiprocessing, they are different. Would you please tell me how I
can get the same values? Here’s my modified code.
Regards,
Ali
#include "mpi.h"
#include "compute_ke.h"
#include "atom.h"
#include "force.h"
#include "domain.h"
#include "group.h"
#include "error.h"
using namespace LAMMPS_NS;
#define INVOKED_SCALAR 1
/* ---------------------------------------------------------------------- */
ComputeKE_new::ComputeKE_new(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg)
{
if (narg != 3) error->all("Illegal compute ke command");
scalar_flag = 1;
extscalar = 1;
}
/* ---------------------------------------------------------------------- */
void ComputeKE_new::init()
{
pfactor = 0.5 * force->mvv2e;
}
/* ---------------------------------------------------------------------- */
double ComputeKE_new::compute_scalar()
{
invoked |= INVOKED_SCALAR;
double **v = atom->v;
double *rmass = atom->rmass;
double *mass = atom->mass;
int *mask = atom->mask;
int *type = atom->type;
int nlocal = atom->nlocal;
double ke_new = 0.0;
if (mass) {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit)
ke_new += mass[type[i]] *
(x[i][0]*x[i][0] + x[i][1]*x[i][1] + x[i][2]*x[i][2]);
} else {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit)
ke_new += rmass[i] * (x[i][0]*x[i][0] + x[i][1]*x[i][1] +
x[i][2]*x[i][2]);
}
MPI_Allreduce(&ke_new,&scalar,1,MPI_DOUBLE,MPI_SUM,world);
scalar *= pfactor;
return scalar;
}