#include "compute_temp_chunk.h"
#include <mpi.h>
#include <cstring>
#include "atom.h"
#include "update.h"
#include "force.h"
#include "modify.h"
#include "compute_chunk_atom.h"
#include "domain.h"
#include "memory.h"
#include "error.h"
using namespace LAMMPS_NS;
enum{TEMP,KECOM,INTERNAL};
ComputeTempChunk::ComputeTempChunk(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg),
which(NULL), idchunk(NULL), id_bias(NULL), sum(NULL), sumall(NULL), count(NULL),
countall(NULL), massproc(NULL), masstotal(NULL), vcm(NULL), vcmall(NULL)
{
if (narg < 4) error->all(FLERR,"Illegal compute temp/chunk command");
scalar_flag = vector_flag = 1;
size_vector = 6;
extscalar = 0;
extvector = 1;
tempflag = 1;
int n = strlen(arg[3]) + 1;
idchunk = new char[n];
strcpy(idchunk,arg[3]);
biasflag = 0;
init();
nvalues = narg-4;
which = new int[nvalues];
nvalues = 0;
int iarg = 4;
while (iarg < narg) {
if (strcmp(arg[iarg],"temp") == 0) which[nvalues] = TEMP;
else if (strcmp(arg[iarg],"kecom") == 0) which[nvalues] = KECOM;
else if (strcmp(arg[iarg],"internal") == 0) which[nvalues] = INTERNAL;
else break;
iarg++;
nvalues++;
}
comflag = 0;
biasflag = 0;
id_bias = NULL;
adof = domain->dimension;
cdof = 0.0;
while (iarg < narg) {
if (strcmp(arg[iarg],"com") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal compute temp/chunk command");
if (strcmp(arg[iarg+1],"yes") == 0) comflag = 1;
else if (strcmp(arg[iarg+1],"no") == 0) comflag = 0;
else error->all(FLERR,"Illegal compute temp/chunk command");
iarg += 2;
} else if (strcmp(arg[iarg],"bias") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal compute temp/chunk command");
biasflag = 1;
int n = strlen(arg[iarg+1]) + 1;
id_bias = new char[n];
strcpy(id_bias,arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg],"adof") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal compute temp/chunk command");
adof = force->numeric(FLERR,arg[iarg+1]);
iarg += 2;
} else if (strcmp(arg[iarg],"cdof") == 0) {
if (iarg+2 > narg)
error->all(FLERR,"Illegal compute temp/chunk command");
cdof = force->numeric(FLERR,arg[iarg+1]);
iarg += 2;
} else error->all(FLERR,"Illegal compute temp/chunk command");
}
if (biasflag) {
int i = modify->find_compute(id_bias);
if (i < 0)
error->all(FLERR,"Could not find compute ID for temperature bias");
tbias = modify->compute[i];
if (tbias->tempflag == 0)
error->all(FLERR,"Bias compute does not calculate temperature");
if (tbias->tempbias == 0)
error->all(FLERR,"Bias compute does not calculate a velocity bias");
}
if (comflag && biasflag)
error->all(FLERR,"Cannot use both com and bias with compute temp/chunk");
if (comflag) tempbias = 1;
vector = new double[size_vector];
nchunk = 1;
maxchunk = 0;
if (nvalues) {
array_flag = 1;
size_array_cols = nvalues;
size_array_rows = 0;
size_array_rows_variable = 1;
extarray = 0;
}
allocate();
comstep = -1;
}
ComputeTempChunk::~ComputeTempChunk()
{
delete [] idchunk;
delete [] which;
delete [] id_bias;
delete [] vector;
memory->destroy(sum);
memory->destroy(sumall);
memory->destroy(count);
memory->destroy(countall);
memory->destroy(array);
memory->destroy(massproc);
memory->destroy(masstotal);
memory->destroy(vcm);
memory->destroy(vcmall);
}
void ComputeTempChunk::init()
{
int icompute = modify->find_compute(idchunk);
if (icompute < 0)
error->all(FLERR,"Chunk/atom compute does not exist for "
"compute temp/chunk");
cchunk = (ComputeChunkAtom *) modify->compute[icompute];
if (strcmp(cchunk->style,"chunk/atom") != 0)
error->all(FLERR,"Compute temp/chunk does not use chunk/atom compute");
if (biasflag) {
int i = modify->find_compute(id_bias);
if (i < 0)
error->all(FLERR,"Could not find compute ID for temperature bias");
tbias = modify->compute[i];
}
}
double ComputeTempChunk::compute_scalar()
{
int i,index;
invoked_scalar = update->ntimestep;
nchunk = cchunk->setup_chunks();
cchunk->compute_ichunk();
int *ichunk = cchunk->ichunk;
if (nchunk > maxchunk) allocate();
if (biasflag) {
if (tbias->invoked_scalar != update->ntimestep) tbias->compute_scalar();
tbias->remove_bias_all();
}
if (comflag && comstep != update->ntimestep) vcm_compute();
double **v = atom->v;
double *mass = atom->mass;
double *rmass = atom->rmass;
int *type = atom->type;
int *mask = atom->mask;
int nlocal = atom->nlocal;
double t = 0.0;
int mycount = 0;
if (!comflag) {
if (rmass) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
t += (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]) *
rmass[i];
mycount++;
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
t += (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]) *
mass[type[i]];
mycount++;
}
}
} else {
double vx,vy,vz;
if (rmass) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
t += (vx*vx + vy*vy + vz*vz) * rmass[i];
mycount++;
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
t += (vx*vx + vy*vy + vz*vz) * mass[type[i]];
mycount++;
}
}
}
if (biasflag) tbias->restore_bias_all();
MPI_Allreduce(&t,&scalar,1,MPI_DOUBLE,MPI_SUM,world);
double rcount = mycount;
double allcount;
MPI_Allreduce(&rcount,&allcount,1,MPI_DOUBLE,MPI_SUM,world);
double dof = nchunk*cdof + adof*allcount;
double tfactor = 0.0;
if (dof > 0.0) tfactor = force->mvv2e / (dof * force->boltz);
if (dof < 0.0 && allcount > 0.0)
error->all(FLERR,"Temperature compute degrees of freedom < 0");
scalar *= tfactor;
return scalar;
}
void ComputeTempChunk::compute_vector()
{
int i,index;
invoked_vector = update->ntimestep;
nchunk = cchunk->setup_chunks();
cchunk->compute_ichunk();
int *ichunk = cchunk->ichunk;
if (nchunk > maxchunk) allocate();
if (biasflag) {
if (tbias->invoked_scalar != update->ntimestep) tbias->compute_scalar();
tbias->remove_bias_all();
}
if (comflag && comstep != update->ntimestep) vcm_compute();
double **v = atom->v;
double *mass = atom->mass;
double *rmass = atom->rmass;
int *type = atom->type;
int *mask = atom->mask;
int nlocal = atom->nlocal;
double massone,t[6];
for (i = 0; i < 6; i++) t[i] = 0.0;
if (!comflag) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
if (rmass) massone = rmass[i];
else massone = mass[type[i]];
t[0] += massone * v[i][0]*v[i][0];
t[1] += massone * v[i][1]*v[i][1];
t[2] += massone * v[i][2]*v[i][2];
t[3] += massone * v[i][0]*v[i][1];
t[4] += massone * v[i][0]*v[i][2];
t[5] += massone * v[i][1]*v[i][2];
}
} else {
double vx,vy,vz;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
if (rmass) massone = rmass[i];
else massone = mass[type[i]];
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
t[0] += massone * vx*vx;
t[1] += massone * vy*vy;
t[2] += massone * vz*vz;
t[3] += massone * vx*vy;
t[4] += massone * vx*vz;
t[5] += massone * vy*vz;
}
}
if (biasflag) tbias->restore_bias_all();
MPI_Allreduce(t,vector,6,MPI_DOUBLE,MPI_SUM,world);
for (i = 0; i < 6; i++) vector[i] *= force->mvv2e;
}
void ComputeTempChunk::compute_array()
{
invoked_array = update->ntimestep;
nchunk = cchunk->setup_chunks();
cchunk->compute_ichunk();
if (nchunk > maxchunk) allocate();
size_array_rows = nchunk;
if (biasflag) {
if (tbias->invoked_scalar != update->ntimestep) tbias->compute_scalar();
tbias->remove_bias_all();
}
if (comstep != update->ntimestep) vcm_compute();
for (int i = 0; i < nvalues; i++) {
if (which[i] == TEMP) temperature(i);
else if (which[i] == KECOM) kecom(i);
else if (which[i] == INTERNAL) internal(i);
}
if (biasflag) tbias->restore_bias_all();
}
void ComputeTempChunk::vcm_compute()
{
int i,index;
double massone;
comstep = update->ntimestep;
int *ichunk = cchunk->ichunk;
for (int i = 0; i < nchunk; i++) {
vcm[i][0] = vcm[i][1] = vcm[i][2] = 0.0;
massproc[i] = 0.0;
}
double **v = atom->v;
int *mask = atom->mask;
int *type = atom->type;
double *mass = atom->mass;
double *rmass = atom->rmass;
int nlocal = atom->nlocal;
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
if (rmass) massone = rmass[i];
else massone = mass[type[i]];
vcm[index][0] += v[i][0] * massone;
vcm[index][1] += v[i][1] * massone;
vcm[index][2] += v[i][2] * massone;
massproc[index] += massone;
}
MPI_Allreduce(&vcm[0][0],&vcmall[0][0],3*nchunk,MPI_DOUBLE,MPI_SUM,world);
MPI_Allreduce(massproc,masstotal,nchunk,MPI_DOUBLE,MPI_SUM,world);
for (i = 0; i < nchunk; i++) {
if (masstotal[i] > 0.0) {
vcmall[i][0] /= masstotal[i];
vcmall[i][1] /= masstotal[i];
vcmall[i][2] /= masstotal[i];
} else {
vcmall[i][0] = vcmall[i][1] = vcmall[i][2] = 0.0;
}
}
}
void ComputeTempChunk::temperature(int icol)
{
int i,index;
int *ichunk = cchunk->ichunk;
for (int i = 0; i < nchunk; i++) {
count[i] = 0;
sum[i] = 0.0;
}
double **v = atom->v;
double *mass = atom->mass;
double *rmass = atom->rmass;
int *mask = atom->mask;
int *type = atom->type;
int nlocal = atom->nlocal;
if (!comflag) {
if (rmass) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
sum[index] += (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]) *
rmass[i];
count[index]++;
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
sum[index] += (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]) *
mass[type[i]];
count[index]++;
}
}
} else {
double vx,vy,vz;
if (rmass) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * rmass[i];
count[index]++;
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * mass[type[i]];
count[index]++;
}
}
}
MPI_Allreduce(sum,sumall,nchunk,MPI_DOUBLE,MPI_SUM,world);
MPI_Allreduce(count,countall,nchunk,MPI_INT,MPI_SUM,world);
double dof,tfactor;
double mvv2e = force->mvv2e;
double boltz = force->boltz;
for (int i = 0; i < nchunk; i++) {
dof = cdof + adof*countall[i];
if (dof > 0.0) tfactor = mvv2e / (dof * boltz);
else tfactor = 0.0;
array[i][icol] = tfactor * sumall[i];
}
}
void ComputeTempChunk::kecom(int icol)
{
int index;
int *ichunk = cchunk->ichunk;
for (int i = 0; i < nchunk; i++) sum[i] = 0.0;
double *mass = atom->mass;
double *rmass = atom->rmass;
int *mask = atom->mask;
int *type = atom->type;
int nlocal = atom->nlocal;
double vx,vy,vz;
if (rmass) {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = vcmall[index][0];
vy = vcmall[index][1];
vz = vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * rmass[i];
}
} else {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = vcmall[index][0];
vy = vcmall[index][1];
vz = vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * mass[type[i]];
}
}
MPI_Allreduce(sum,sumall,nchunk,MPI_DOUBLE,MPI_SUM,world);
double mvv2e = force->mvv2e;
for (int i = 0; i < nchunk; i++)
array[i][icol] = 0.5 * mvv2e * sumall[i];
}
void ComputeTempChunk::internal(int icol)
{
int index;
int *ichunk = cchunk->ichunk;
for (int i = 0; i < nchunk; i++) sum[i] = 0.0;
double **v = atom->v;
double *mass = atom->mass;
double *rmass = atom->rmass;
int *mask = atom->mask;
int *type = atom->type;
int nlocal = atom->nlocal;
double vx,vy,vz;
if (rmass) {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * rmass[i];
}
} else {
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
vx = v[i][0] - vcmall[index][0];
vy = v[i][1] - vcmall[index][1];
vz = v[i][2] - vcmall[index][2];
sum[index] += (vx*vx + vy*vy + vz*vz) * mass[type[i]];
}
}
MPI_Allreduce(sum,sumall,nchunk,MPI_DOUBLE,MPI_SUM,world);
double mvv2e = force->mvv2e;
for (int i = 0; i < nchunk; i++)
array[i][icol] = 0.5 * mvv2e * sumall[i];
}
void ComputeTempChunk::remove_bias(int i, double *v)
{
int index = cchunk->ichunk[i]-1;
if (index < 0) return;
v[0] -= vcmall[index][0];
v[1] -= vcmall[index][1];
v[2] -= vcmall[index][2];
}
void ComputeTempChunk::remove_bias_all()
{
int index;
int *ichunk = cchunk->ichunk;
double **v = atom->v;
int *mask = atom->mask;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
v[i][0] -= vcmall[index][0];
v[i][1] -= vcmall[index][1];
v[i][2] -= vcmall[index][2];
}
}
void ComputeTempChunk::restore_bias(int i, double *v)
{
int index = cchunk->ichunk[i]-1;
if (index < 0) return;
v[0] += vcmall[index][0];
v[1] += vcmall[index][1];
v[2] += vcmall[index][2];
}
void ComputeTempChunk::restore_bias_all()
{
int index;
int *ichunk = cchunk->ichunk;
double **v = atom->v;
int *mask = atom->mask;
int nlocal = atom->nlocal;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
index = ichunk[i]-1;
if (index < 0) continue;
v[i][0] += vcmall[index][0];
v[i][1] += vcmall[index][1];
v[i][2] += vcmall[index][2];
}
}
void ComputeTempChunk::lock_enable()
{
cchunk->lockcount++;
}
void ComputeTempChunk::lock_disable()
{
int icompute = modify->find_compute(idchunk);
if (icompute >= 0) {
cchunk = (ComputeChunkAtom *) modify->compute[icompute];
cchunk->lockcount--;
}
}
int ComputeTempChunk::lock_length()
{
nchunk = cchunk->setup_chunks();
return nchunk;
}
void ComputeTempChunk::lock(Fix *fixptr, bigint startstep, bigint stopstep)
{
cchunk->lock(fixptr,startstep,stopstep);
}
void ComputeTempChunk::unlock(Fix *fixptr)
{
cchunk->unlock(fixptr);
}
void ComputeTempChunk::allocate()
{
memory->destroy(sum);
memory->destroy(sumall);
memory->destroy(count);
memory->destroy(countall);
memory->destroy(array);
maxchunk = nchunk;
memory->create(sum,maxchunk,"temp/chunk:sum");
memory->create(sumall,maxchunk,"temp/chunk:sumall");
memory->create(count,maxchunk,"temp/chunk:count");
memory->create(countall,maxchunk,"temp/chunk:countall");
memory->create(array,maxchunk,nvalues,"temp/chunk:array");
if (comflag || nvalues) {
memory->destroy(massproc);
memory->destroy(masstotal);
memory->destroy(vcm);
memory->destroy(vcmall);
memory->create(massproc,maxchunk,"vcm/chunk:massproc");
memory->create(masstotal,maxchunk,"vcm/chunk:masstotal");
memory->create(vcm,maxchunk,3,"vcm/chunk:vcm");
memory->create(vcmall,maxchunk,3,"vcm/chunk:vcmall");
}
}
double ComputeTempChunk::memory_usage()
{
double bytes = (bigint) maxchunk * 2 * sizeof(double);
bytes += (bigint) maxchunk * 2 * sizeof(int);
bytes += (bigint) maxchunk * nvalues * sizeof(double);
if (comflag || nvalues) {
bytes += (bigint) maxchunk * 2 * sizeof(double);
bytes += (bigint) maxchunk * 2*3 * sizeof(double);
}
return bytes;
}