new accessor APIs for fixes and computes in Modify plus a few applications

This commit is contained in:
Axel Kohlmeyer
2021-08-08 17:25:06 -04:00
parent ef04f6ca69
commit 5b40e4cb38
20 changed files with 397 additions and 388 deletions

View File

@ -1,4 +1,3 @@
// clang-format off
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
@ -13,33 +12,34 @@
------------------------------------------------------------------------- */
#include "compute_centroid_stress_atom.h"
#include <cstring>
#include "atom.h"
#include "update.h"
#include "comm.h"
#include "force.h"
#include "pair.h"
#include "bond.h"
#include "angle.h"
#include "atom.h"
#include "bond.h"
#include "comm.h"
#include "dihedral.h"
#include "error.h"
#include "fix.h"
#include "force.h"
#include "improper.h"
#include "kspace.h"
#include "modify.h"
#include "fix.h"
#include "memory.h"
#include "error.h"
#include "modify.h"
#include "pair.h"
#include "update.h"
#include <cstring>
using namespace LAMMPS_NS;
enum{NOBIAS,BIAS};
enum { NOBIAS, BIAS };
/* ---------------------------------------------------------------------- */
ComputeCentroidStressAtom::ComputeCentroidStressAtom(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg),
id_temp(nullptr), stress(nullptr)
Compute(lmp, narg, arg), id_temp(nullptr), stress(nullptr)
{
if (narg < 4) error->all(FLERR,"Illegal compute centroid/stress/atom command");
if (narg < 4) error->all(FLERR, "Illegal compute centroid/stress/atom command");
peratom_flag = 1;
size_peratom_cols = 9;
@ -50,17 +50,16 @@ ComputeCentroidStressAtom::ComputeCentroidStressAtom(LAMMPS *lmp, int narg, char
// store temperature ID used by stress computation
// insure it is valid for temperature computation
if (strcmp(arg[3],"NULL") == 0) id_temp = nullptr;
if (strcmp(arg[3], "NULL") == 0)
id_temp = nullptr;
else {
id_temp = utils::strdup(arg[3]);
int icompute = modify->find_compute(id_temp);
if (icompute < 0)
error->all(FLERR,"Could not find compute centroid/stress/atom temperature ID");
if (modify->compute[icompute]->tempflag == 0)
error->all(FLERR,
"Compute centroid/stress/atom temperature ID does not "
"compute temperature");
auto compute = modify->get_compute_by_id(id_temp);
if (!compute)
error->all(FLERR, "Could not find compute centroid/stress/atom temperature ID {}", id_temp);
if (compute->tempflag == 0)
error->all(FLERR, "Compute centroid/stress/atom temperature ID does not compute temperature");
}
// process optional args
@ -79,19 +78,28 @@ ComputeCentroidStressAtom::ComputeCentroidStressAtom(LAMMPS *lmp, int narg, char
fixflag = 0;
int iarg = 4;
while (iarg < narg) {
if (strcmp(arg[iarg],"ke") == 0) keflag = 1;
else if (strcmp(arg[iarg],"pair") == 0) pairflag = 1;
else if (strcmp(arg[iarg],"bond") == 0) bondflag = 1;
else if (strcmp(arg[iarg],"angle") == 0) angleflag = 1;
else if (strcmp(arg[iarg],"dihedral") == 0) dihedralflag = 1;
else if (strcmp(arg[iarg],"improper") == 0) improperflag = 1;
else if (strcmp(arg[iarg],"kspace") == 0) kspaceflag = 1;
else if (strcmp(arg[iarg],"fix") == 0) fixflag = 1;
else if (strcmp(arg[iarg],"virial") == 0) {
if (strcmp(arg[iarg], "ke") == 0)
keflag = 1;
else if (strcmp(arg[iarg], "pair") == 0)
pairflag = 1;
else if (strcmp(arg[iarg], "bond") == 0)
bondflag = 1;
else if (strcmp(arg[iarg], "angle") == 0)
angleflag = 1;
else if (strcmp(arg[iarg], "dihedral") == 0)
dihedralflag = 1;
else if (strcmp(arg[iarg], "improper") == 0)
improperflag = 1;
else if (strcmp(arg[iarg], "kspace") == 0)
kspaceflag = 1;
else if (strcmp(arg[iarg], "fix") == 0)
fixflag = 1;
else if (strcmp(arg[iarg], "virial") == 0) {
pairflag = 1;
bondflag = angleflag = dihedralflag = improperflag = 1;
kspaceflag = fixflag = 1;
} else error->all(FLERR,"Illegal compute centroid/stress/atom command");
} else
error->all(FLERR, "Illegal compute centroid/stress/atom command");
iarg++;
}
}
@ -103,7 +111,7 @@ ComputeCentroidStressAtom::ComputeCentroidStressAtom(LAMMPS *lmp, int narg, char
ComputeCentroidStressAtom::~ComputeCentroidStressAtom()
{
delete [] id_temp;
delete[] id_temp;
memory->destroy(stress);
}
@ -115,13 +123,15 @@ void ComputeCentroidStressAtom::init()
// fixes could have changed or compute_modify could have changed it
if (id_temp) {
int icompute = modify->find_compute(id_temp);
if (icompute < 0)
error->all(FLERR,"Could not find compute centroid/stress/atom temperature ID");
temperature = modify->compute[icompute];
if (temperature->tempbias) biasflag = BIAS;
else biasflag = NOBIAS;
} else biasflag = NOBIAS;
temperature = modify->get_compute_by_id(id_temp);
if (!temperature)
error->all(FLERR, "Could not find compute centroid/stress/atom temperature ID {}",id_temp);
if (temperature->tempbias)
biasflag = BIAS;
else
biasflag = NOBIAS;
} else
biasflag = NOBIAS;
// check if force components and fixes support centroid atom stress
// all bond styles support it as CENTROID_SAME
@ -158,12 +168,12 @@ void ComputeCentroidStressAtom::init()
void ComputeCentroidStressAtom::compute_peratom()
{
int i,j;
int i, j;
double onemass;
invoked_peratom = update->ntimestep;
if (update->vflag_atom != invoked_peratom)
error->all(FLERR,"Per-atom virial was not tallied on needed timestep");
error->all(FLERR, "Per-atom virial was not tallied on needed timestep");
// grow local stress array if necessary
// needs to be atom->nmax in length
@ -171,7 +181,7 @@ void ComputeCentroidStressAtom::compute_peratom()
if (atom->nmax > nmax) {
memory->destroy(stress);
nmax = atom->nmax;
memory->create(stress,nmax,9,"centroid/stress/atom:stress");
memory->create(stress, nmax, 9, "centroid/stress/atom:stress");
array_atom = stress;
}
@ -194,8 +204,7 @@ void ComputeCentroidStressAtom::compute_peratom()
// clear local stress array
for (i = 0; i < ntotal; i++)
for (j = 0; j < 9; j++)
stress[i][j] = 0.0;
for (j = 0; j < 9; j++) stress[i][j] = 0.0;
// add in per-atom contributions from all force components and fixes
@ -205,15 +214,12 @@ void ComputeCentroidStressAtom::compute_peratom()
if (force->pair->centroidstressflag == CENTROID_AVAIL) {
double **cvatom = force->pair->cvatom;
for (i = 0; i < npair; i++)
for (j = 0; j < 9; j++)
stress[i][j] += cvatom[i][j];
for (j = 0; j < 9; j++) stress[i][j] += cvatom[i][j];
} else {
double **vatom = force->pair->vatom;
for (i = 0; i < npair; i++) {
for (j = 0; j < 6; j++)
stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++)
stress[i][j] += vatom[i][j-3];
for (j = 0; j < 6; j++) stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++) stress[i][j] += vatom[i][j - 3];
}
}
}
@ -226,41 +232,34 @@ void ComputeCentroidStressAtom::compute_peratom()
if (bondflag && force->bond) {
double **vatom = force->bond->vatom;
for (i = 0; i < nbond; i++) {
for (j = 0; j < 6; j++)
stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++)
stress[i][j] += vatom[i][j-3];
for (j = 0; j < 6; j++) stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++) stress[i][j] += vatom[i][j - 3];
}
}
if (angleflag && force->angle) {
double **cvatom = force->angle->cvatom;
for (i = 0; i < nbond; i++)
for (j = 0; j < 9; j++)
stress[i][j] += cvatom[i][j];
for (j = 0; j < 9; j++) stress[i][j] += cvatom[i][j];
}
if (dihedralflag && force->dihedral) {
double **cvatom = force->dihedral->cvatom;
for (i = 0; i < nbond; i++)
for (j = 0; j < 9; j++)
stress[i][j] += cvatom[i][j];
for (j = 0; j < 9; j++) stress[i][j] += cvatom[i][j];
}
if (improperflag && force->improper) {
double **cvatom = force->improper->cvatom;
for (i = 0; i < nbond; i++)
for (j = 0; j < 9; j++)
stress[i][j] += cvatom[i][j];
for (j = 0; j < 9; j++) stress[i][j] += cvatom[i][j];
}
if (kspaceflag && force->kspace && force->kspace->compute_flag) {
double **vatom = force->kspace->vatom;
for (i = 0; i < nkspace; i++) {
for (j = 0; j < 6; j++)
stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++)
stress[i][j] += vatom[i][j-3];
for (j = 0; j < 6; j++) stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++) stress[i][j] += vatom[i][j - 3];
}
}
@ -279,18 +278,15 @@ void ComputeCentroidStressAtom::compute_peratom()
double **vatom = fix[ifix]->vatom;
if (vatom)
for (i = 0; i < nlocal; i++) {
for (j = 0; j < 6; j++)
stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++)
stress[i][j] += vatom[i][j-3];
for (j = 0; j < 6; j++) stress[i][j] += vatom[i][j];
for (j = 6; j < 9; j++) stress[i][j] += vatom[i][j - 3];
}
}
}
// communicate ghost virials between neighbor procs
if (force->newton ||
(force->kspace && force->kspace->tip4pflag && force->kspace->compute_flag))
if (force->newton || (force->kspace && force->kspace->tip4pflag && force->kspace->compute_flag))
comm->reverse_comm_compute(this);
// zero virial of atoms not in group
@ -327,30 +323,30 @@ void ComputeCentroidStressAtom::compute_peratom()
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
onemass = mvv2e * rmass[i];
stress[i][0] += onemass*v[i][0]*v[i][0];
stress[i][1] += onemass*v[i][1]*v[i][1];
stress[i][2] += onemass*v[i][2]*v[i][2];
stress[i][3] += onemass*v[i][0]*v[i][1];
stress[i][4] += onemass*v[i][0]*v[i][2];
stress[i][5] += onemass*v[i][1]*v[i][2];
stress[i][6] += onemass*v[i][1]*v[i][0];
stress[i][7] += onemass*v[i][2]*v[i][0];
stress[i][8] += onemass*v[i][2]*v[i][1];
stress[i][0] += onemass * v[i][0] * v[i][0];
stress[i][1] += onemass * v[i][1] * v[i][1];
stress[i][2] += onemass * v[i][2] * v[i][2];
stress[i][3] += onemass * v[i][0] * v[i][1];
stress[i][4] += onemass * v[i][0] * v[i][2];
stress[i][5] += onemass * v[i][1] * v[i][2];
stress[i][6] += onemass * v[i][1] * v[i][0];
stress[i][7] += onemass * v[i][2] * v[i][0];
stress[i][8] += onemass * v[i][2] * v[i][1];
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
onemass = mvv2e * mass[type[i]];
stress[i][0] += onemass*v[i][0]*v[i][0];
stress[i][1] += onemass*v[i][1]*v[i][1];
stress[i][2] += onemass*v[i][2]*v[i][2];
stress[i][3] += onemass*v[i][0]*v[i][1];
stress[i][4] += onemass*v[i][0]*v[i][2];
stress[i][5] += onemass*v[i][1]*v[i][2];
stress[i][6] += onemass*v[i][1]*v[i][0];
stress[i][7] += onemass*v[i][2]*v[i][0];
stress[i][8] += onemass*v[i][2]*v[i][1];
stress[i][0] += onemass * v[i][0] * v[i][0];
stress[i][1] += onemass * v[i][1] * v[i][1];
stress[i][2] += onemass * v[i][2] * v[i][2];
stress[i][3] += onemass * v[i][0] * v[i][1];
stress[i][4] += onemass * v[i][0] * v[i][2];
stress[i][5] += onemass * v[i][1] * v[i][2];
stress[i][6] += onemass * v[i][1] * v[i][0];
stress[i][7] += onemass * v[i][2] * v[i][0];
stress[i][8] += onemass * v[i][2] * v[i][1];
}
}
@ -359,41 +355,40 @@ void ComputeCentroidStressAtom::compute_peratom()
// invoke temperature if it hasn't been already
// this insures bias factor is pre-computed
if (keflag && temperature->invoked_scalar != update->ntimestep)
temperature->compute_scalar();
if (keflag && temperature->invoked_scalar != update->ntimestep) temperature->compute_scalar();
if (rmass) {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
temperature->remove_bias(i,v[i]);
temperature->remove_bias(i, v[i]);
onemass = mvv2e * rmass[i];
stress[i][0] += onemass*v[i][0]*v[i][0];
stress[i][1] += onemass*v[i][1]*v[i][1];
stress[i][2] += onemass*v[i][2]*v[i][2];
stress[i][3] += onemass*v[i][0]*v[i][1];
stress[i][4] += onemass*v[i][0]*v[i][2];
stress[i][5] += onemass*v[i][1]*v[i][2];
stress[i][6] += onemass*v[i][1]*v[i][0];
stress[i][7] += onemass*v[i][2]*v[i][0];
stress[i][8] += onemass*v[i][2]*v[i][1];
temperature->restore_bias(i,v[i]);
stress[i][0] += onemass * v[i][0] * v[i][0];
stress[i][1] += onemass * v[i][1] * v[i][1];
stress[i][2] += onemass * v[i][2] * v[i][2];
stress[i][3] += onemass * v[i][0] * v[i][1];
stress[i][4] += onemass * v[i][0] * v[i][2];
stress[i][5] += onemass * v[i][1] * v[i][2];
stress[i][6] += onemass * v[i][1] * v[i][0];
stress[i][7] += onemass * v[i][2] * v[i][0];
stress[i][8] += onemass * v[i][2] * v[i][1];
temperature->restore_bias(i, v[i]);
}
} else {
for (i = 0; i < nlocal; i++)
if (mask[i] & groupbit) {
temperature->remove_bias(i,v[i]);
temperature->remove_bias(i, v[i]);
onemass = mvv2e * mass[type[i]];
stress[i][0] += onemass*v[i][0]*v[i][0];
stress[i][1] += onemass*v[i][1]*v[i][1];
stress[i][2] += onemass*v[i][2]*v[i][2];
stress[i][3] += onemass*v[i][0]*v[i][1];
stress[i][4] += onemass*v[i][0]*v[i][2];
stress[i][5] += onemass*v[i][1]*v[i][2];
stress[i][6] += onemass*v[i][1]*v[i][0];
stress[i][7] += onemass*v[i][2]*v[i][0];
stress[i][8] += onemass*v[i][2]*v[i][1];
temperature->restore_bias(i,v[i]);
stress[i][0] += onemass * v[i][0] * v[i][0];
stress[i][1] += onemass * v[i][1] * v[i][1];
stress[i][2] += onemass * v[i][2] * v[i][2];
stress[i][3] += onemass * v[i][0] * v[i][1];
stress[i][4] += onemass * v[i][0] * v[i][2];
stress[i][5] += onemass * v[i][1] * v[i][2];
stress[i][6] += onemass * v[i][1] * v[i][0];
stress[i][7] += onemass * v[i][2] * v[i][0];
stress[i][8] += onemass * v[i][2] * v[i][1];
temperature->restore_bias(i, v[i]);
}
}
}
@ -420,7 +415,7 @@ void ComputeCentroidStressAtom::compute_peratom()
int ComputeCentroidStressAtom::pack_reverse_comm(int n, int first, double *buf)
{
int i,m,last;
int i, m, last;
m = 0;
last = first + n;
@ -442,7 +437,7 @@ int ComputeCentroidStressAtom::pack_reverse_comm(int n, int first, double *buf)
void ComputeCentroidStressAtom::unpack_reverse_comm(int n, int *list, double *buf)
{
int i,j,m;
int i, j, m;
m = 0;
for (i = 0; i < n; i++) {
@ -465,6 +460,6 @@ void ComputeCentroidStressAtom::unpack_reverse_comm(int n, int *list, double *bu
double ComputeCentroidStressAtom::memory_usage()
{
double bytes = (double)nmax*9 * sizeof(double);
double bytes = (double) nmax * 9 * sizeof(double);
return bytes;
}