convert compute chunk/spread/atom

This commit is contained in:
Axel Kohlmeyer
2022-09-30 21:42:04 -04:00
parent afc35aa7b0
commit 27d9e313f5
2 changed files with 88 additions and 85 deletions

View File

@ -32,8 +32,7 @@ using namespace LAMMPS_NS;
ComputeChunkSpreadAtom:: ComputeChunkSpreadAtom::
ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) : ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
Compute(lmp, narg, arg), Compute(lmp, narg, arg), idchunk(nullptr)
idchunk(nullptr), ids(nullptr), which(nullptr), argindex(nullptr), value2index(nullptr)
{ {
if (narg < 5) error->all(FLERR,"Illegal compute chunk/spread/atom command"); if (narg < 5) error->all(FLERR,"Illegal compute chunk/spread/atom command");
@ -54,32 +53,27 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
// parse values // parse values
which = new int[nargnew]; values.clear();
argindex = new int[nargnew]; for (iarg = 0; iarg < nargnew; ++iarg) {
ids = new char*[nargnew];
value2index = new int[nargnew];
nvalues = 0;
for (iarg = 0; iarg < nargnew; iarg++) {
ids[nvalues] = nullptr;
ArgInfo argi(arg[iarg], ArgInfo::COMPUTE|ArgInfo::FIX); ArgInfo argi(arg[iarg], ArgInfo::COMPUTE|ArgInfo::FIX);
which[nvalues] = argi.get_type(); value_t val;
argindex[nvalues] = argi.get_index1(); val.which = argi.get_type();
ids[nvalues] = argi.copy_name(); val.argindex = argi.get_index1();
val.id = argi.get_name();
val.val.c = nullptr;
if ((which[nvalues] == ArgInfo::UNKNOWN) || (which[nvalues] == ArgInfo::NONE) if ((val.which == ArgInfo::UNKNOWN) || (val.which == ArgInfo::NONE)
|| (argi.get_dim() > 1)) || (argi.get_dim() > 1))
error->all(FLERR,"Illegal compute chunk/spread/atom command"); error->all(FLERR,"Illegal compute chunk/spread/atom argument: {}", arg[iarg]);
nvalues++; values.push_back(val);
} }
// if wildcard expansion occurred, free earg memory from expand_args() // if wildcard expansion occurred, free earg memory from expand_args()
if (expand) { if (expand) {
for (int i = 0; i < nargnew; i++) delete [] earg[i]; for (int i = 0; i < nargnew; i++) delete[] earg[i];
memory->sfree(earg); memory->sfree(earg);
} }
@ -87,38 +81,45 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
// for compute, must calculate per-chunk values, i.e. style ends in "/chunk" // for compute, must calculate per-chunk values, i.e. style ends in "/chunk"
// for fix, assume a global vector or array is per-chunk data // for fix, assume a global vector or array is per-chunk data
for (int i = 0; i < nvalues; i++) { for (auto &val : values) {
if (which[i] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
auto icompute = modify->get_compute_by_id(ids[i]); auto icompute = modify->get_compute_by_id(val.id);
if (!icompute) if (!icompute)
error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", ids[i]); error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", val.id);
if (!utils::strmatch(icompute->style,"/chunk$")) if (!utils::strmatch(icompute->style,"/chunk$"))
error->all(FLERR,"Compute for compute chunk/spread/atom " error->all(FLERR,"Compute chunk/spread/atom compute {} does not calculate per-chunk values",
"does not calculate per-chunk values"); val.id);
if (argindex[i] == 0) { if (val.argindex == 0) {
if (!icompute->vector_flag) if (!icompute->vector_flag)
error->all(FLERR,"Compute chunk/spread/atom compute does not calculate global vector"); error->all(FLERR,"Compute chunk/spread/atom compute {} does not calculate global vector",
val.id);
} else { } else {
if (!icompute->array_flag) if (!icompute->array_flag)
error->all(FLERR,"Compute chunk/spread/atom compute does not calculate global array"); error->all(FLERR,"Compute chunk/spread/atom compute {} does not calculate global array",
if (argindex[i] > icompute->size_array_cols) val.id);
error->all(FLERR,"Compute chunk/spread/atom compute array is accessed out-of-range"); if (val.argindex > icompute->size_array_cols)
error->all(FLERR,"Compute chunk/spread/atom compute {} array is accessed out-of-range",
val.id);
} }
val.val.c = icompute;
} else if (which[i] == ArgInfo::FIX) { } else if (val.which == ArgInfo::FIX) {
auto ifix = modify->get_fix_by_id(ids[i]); auto ifix = modify->get_fix_by_id(val.id);
if (ifix) if (ifix)
error->all(FLERR,"Fix ID {} for compute chunk/spread/atom does not exist", ids[i]); error->all(FLERR,"Fix ID {} for compute chunk/spread/atom does not exist", val.id);
if (argindex[i] == 0) { if (val.argindex == 0) {
if (!ifix->vector_flag) if (!ifix->vector_flag)
error->all(FLERR,"Compute chunk/spread/atom fix does not calculate global vector"); error->all(FLERR,"Compute chunk/spread/atom {} fix does not calculate global vector",
val.id);
} else { } else {
if (!ifix->array_flag) if (!ifix->array_flag)
error->all(FLERR,"Compute chunk/spread/atom fix does not calculate global array"); error->all(FLERR,"Compute chunk/spread/atom {} fix does not calculate global array",
if (argindex[i] > ifix->size_array_cols) val.id);
error->all(FLERR,"Compute chunk/spread/atom fix array is accessed out-of-range"); if (val.argindex > ifix->size_array_cols)
error->all(FLERR,"Compute chunk/spread/atom fix {} array is accessed out-of-range",
val.id);
} }
} }
} }
@ -126,8 +127,8 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
// this compute produces a peratom vector or array // this compute produces a peratom vector or array
peratom_flag = 1; peratom_flag = 1;
if (nvalues == 1) size_peratom_cols = 0; if (values.size() == 1) size_peratom_cols = 0;
else size_peratom_cols = nvalues; else size_peratom_cols = values.size();
// per-atom vector or array // per-atom vector or array
@ -140,13 +141,7 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) :
ComputeChunkSpreadAtom::~ComputeChunkSpreadAtom() ComputeChunkSpreadAtom::~ComputeChunkSpreadAtom()
{ {
delete [] idchunk; delete[] idchunk;
delete [] which;
delete [] argindex;
for (int i = 0; i < nvalues; i++) delete [] ids[i];
delete [] ids;
delete [] value2index;
memory->destroy(vector_atom); memory->destroy(vector_atom);
memory->destroy(array_atom); memory->destroy(array_atom);
@ -160,18 +155,16 @@ void ComputeChunkSpreadAtom::init()
// set indices of all computes,fixes,variables // set indices of all computes,fixes,variables
for (int m = 0; m < nvalues; m++) { for (auto &val : values) {
if (which[m] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(ids[m]); val.val.c = modify->get_compute_by_id(val.id);
if (icompute < 0) if (!val.val.c)
error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", ids[m]); error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", val.id);
value2index[m] = icompute;
} else if (which[m] == ArgInfo::FIX) { } else if (val.which == ArgInfo::FIX) {
int ifix = modify->find_fix(ids[m]); val.val.f = modify->get_fix_by_id(val.id);
if (ifix < 0) if (!val.val.f)
error->all(FLERR,"Fix ID {} for compute chunk/spread/atom does not exist", ids[m]); error->all(FLERR,"Fix ID {} for compute chunk/spread/atom does not exist", val.id);
value2index[m] = ifix;
} }
} }
} }
@ -182,7 +175,8 @@ void ComputeChunkSpreadAtom::init_chunk()
{ {
cchunk = dynamic_cast<ComputeChunkAtom *>(modify->get_compute_by_id(idchunk)); cchunk = dynamic_cast<ComputeChunkAtom *>(modify->get_compute_by_id(idchunk));
if (!cchunk) if (!cchunk)
error->all(FLERR,"Chunk/atom compute does not exist for compute chunk/spread/atom {}", idchunk); error->all(FLERR,"Chunk/atom compute {} does not exist for compute chunk/spread/atom "
"or is of invalid style", idchunk);
if (strcmp(cchunk->style,"chunk/atom") != 0) if (strcmp(cchunk->style,"chunk/atom") != 0)
error->all(FLERR,"Compute chunk/spread/atom {} does not use chunk/atom compute", idchunk); error->all(FLERR,"Compute chunk/spread/atom {} does not use chunk/atom compute", idchunk);
} }
@ -196,14 +190,14 @@ void ComputeChunkSpreadAtom::compute_peratom()
// grow local vector_atom or array_atom if necessary // grow local vector_atom or array_atom if necessary
if (atom->nmax > nmax) { if (atom->nmax > nmax) {
if (nvalues == 1) { if (values.size() == 1) {
memory->destroy(vector_atom); memory->destroy(vector_atom);
nmax = atom->nmax; nmax = atom->nmax;
memory->create(vector_atom,nmax,"chunk/spread/atom:vector_atom"); memory->create(vector_atom,nmax,"chunk/spread/atom:vector_atom");
} else { } else {
memory->destroy(array_atom); memory->destroy(array_atom);
nmax = atom->nmax; nmax = atom->nmax;
memory->create(array_atom,nmax,nvalues,"chunk/spread/atom:array_atom"); memory->create(array_atom,nmax,values.size(),"chunk/spread/atom:array_atom");
} }
} }
@ -221,35 +215,35 @@ void ComputeChunkSpreadAtom::compute_peratom()
int *mask = atom->mask; int *mask = atom->mask;
int nlocal = atom->nlocal; int nlocal = atom->nlocal;
int i,m,n,index,nstride; int index,nstride;
double *ptr; double *ptr;
for (m = 0; m < nvalues; m++) { int m = 0;
n = value2index[m]; for (auto &val : values) {
// copy compute/fix values into vector_atom or array_atom // copy compute/fix values into vector_atom or array_atom
// nstride between values for each atom // nstride between values for each atom
if (nvalues == 1) { if (values.size() == 1) {
ptr = vector_atom; ptr = vector_atom;
nstride = 1; nstride = 1;
} else { } else {
ptr = &array_atom[0][m]; ptr = &array_atom[0][m];
nstride = nvalues; nstride = values.size();
} }
// invoke compute if not previously invoked // invoke compute if not previously invoked
if (which[m] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
Compute *compute = modify->compute[n]; Compute *compute = val.val.c;
if (argindex[m] == 0) { if (val.argindex == 0) {
if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) { if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) {
compute->compute_vector(); compute->compute_vector();
compute->invoked_flag |= Compute::INVOKED_VECTOR; compute->invoked_flag |= Compute::INVOKED_VECTOR;
} }
double *cvector = compute->vector; double *cvector = compute->vector;
for (i = 0; i < nlocal; i++, ptr += nstride) { for (int i = 0; i < nlocal; i++, ptr += nstride) {
*ptr = 0.0; *ptr = 0.0;
if (!(mask[i] & groupbit)) continue; if (!(mask[i] & groupbit)) continue;
index = ichunk[i]-1; index = ichunk[i]-1;
@ -262,9 +256,9 @@ void ComputeChunkSpreadAtom::compute_peratom()
compute->compute_array(); compute->compute_array();
compute->invoked_flag |= Compute::INVOKED_ARRAY; compute->invoked_flag |= Compute::INVOKED_ARRAY;
} }
int icol = argindex[m]-1; int icol = val.argindex-1;
double **carray = compute->array; double **carray = compute->array;
for (i = 0; i < nlocal; i++, ptr += nstride) { for (int i = 0; i < nlocal; i++, ptr += nstride) {
*ptr = 0.0; *ptr = 0.0;
if (!(mask[i] & groupbit)) continue; if (!(mask[i] & groupbit)) continue;
index = ichunk[i]-1; index = ichunk[i]-1;
@ -277,15 +271,15 @@ void ComputeChunkSpreadAtom::compute_peratom()
// are assuming the fix global vector/array is per-chunk data // are assuming the fix global vector/array is per-chunk data
// check if index exceeds fix output length/rows // check if index exceeds fix output length/rows
} else if (which[m] == ArgInfo::FIX) { } else if (val.which == ArgInfo::FIX) {
auto &fix = modify->get_fix_list()[n]; Fix *fix = val.val.f;
if (update->ntimestep % fix->global_freq) if (update->ntimestep % fix->global_freq)
error->all(FLERR,"Fix used in compute chunk/spread/atom not " error->all(FLERR,"Fix {} used in compute chunk/spread/atom not computed at compatible time",
"computed at compatible time"); val.id);
if (argindex[m] == 0) { if (val.argindex == 0) {
int nfix = fix->size_vector; int nfix = fix->size_vector;
for (i = 0; i < nlocal; i++, ptr += nstride) { for (int i = 0; i < nlocal; i++, ptr += nstride) {
*ptr = 0.0; *ptr = 0.0;
if (!(mask[i] & groupbit)) continue; if (!(mask[i] & groupbit)) continue;
index = ichunk[i]-1; index = ichunk[i]-1;
@ -294,9 +288,9 @@ void ComputeChunkSpreadAtom::compute_peratom()
} }
} else { } else {
int icol = argindex[m]-1; int icol = val.argindex-1;
int nfix = fix->size_array_rows; int nfix = fix->size_array_rows;
for (i = 0; i < nlocal; i++, ptr += nstride) { for (int i = 0; i < nlocal; i++, ptr += nstride) {
*ptr = 0.0; *ptr = 0.0;
if (!(mask[i] & groupbit)) continue; if (!(mask[i] & groupbit)) continue;
index = ichunk[i]-1; index = ichunk[i]-1;
@ -305,6 +299,7 @@ void ComputeChunkSpreadAtom::compute_peratom()
} }
} }
} }
++m;
} }
} }
@ -314,6 +309,7 @@ void ComputeChunkSpreadAtom::compute_peratom()
double ComputeChunkSpreadAtom::memory_usage() double ComputeChunkSpreadAtom::memory_usage()
{ {
double bytes = (double)nmax*nvalues * sizeof(double); double bytes = (double)nmax*values.size() * sizeof(double);
bytes += values.size() * sizeof(value_t);
return bytes; return bytes;
} }

View File

@ -33,13 +33,20 @@ class ComputeChunkSpreadAtom : public Compute {
double memory_usage() override; double memory_usage() override;
protected: protected:
int mode, nvalues; struct value_t {
char *idchunk; int which;
char **ids; int argindex;
int *which, *argindex, *value2index; std::string id;
union {
class Compute *c;
class Fix *f;
} val;
};
std::vector<value_t> values;
int nmax; char *idchunk;
class ComputeChunkAtom *cchunk; class ComputeChunkAtom *cchunk;
int nmax;
void init_chunk(); void init_chunk();
}; };