convert compute slice

This commit is contained in:
Axel Kohlmeyer
2022-10-07 04:26:51 -04:00
parent 0fc8475383
commit 68482ffe14
2 changed files with 164 additions and 188 deletions

View File

@ -1,4 +1,3 @@
// clang-format off
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories https://www.lammps.org/, Sandia National Laboratories
@ -27,96 +26,84 @@ using namespace LAMMPS_NS;
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
ComputeSlice::ComputeSlice(LAMMPS *lmp, int narg, char **arg) : ComputeSlice::ComputeSlice(LAMMPS *lmp, int narg, char **arg) : Compute(lmp, narg, arg)
Compute(lmp, narg, arg),
nvalues(0), which(nullptr), argindex(nullptr), value2index(nullptr), ids(nullptr)
{ {
if (narg < 7) error->all(FLERR,"Illegal compute slice command"); if (narg < 7) utils::missing_cmd_args(FLERR, "compute slice", error);
MPI_Comm_rank(world,&me);
nstart = utils::inumeric(FLERR, arg[3], false, lmp); nstart = utils::inumeric(FLERR, arg[3], false, lmp);
nstop = utils::inumeric(FLERR, arg[4], false, lmp); nstop = utils::inumeric(FLERR, arg[4], false, lmp);
nskip = utils::inumeric(FLERR, arg[5], false, lmp); nskip = utils::inumeric(FLERR, arg[5], false, lmp);
if (nstart < 1 || nstop < nstart || nskip < 1) if (nstart < 1) error->all(FLERR, "Invalid compute slice nstart value {} < 1", nstart);
error->all(FLERR,"Illegal compute slice command"); if (nstop < nstart) error->all(FLERR, "Invalid compute slice nstop value {} < {}", nstop, nstart);
if (nskip < 1) error->all(FLERR, "Invalid compute slice nskip value < 1: {}", nskip);
// parse remaining values until one isn't recognized // parse values
which = new int[narg-6];
argindex = new int[narg-6];
ids = new char*[narg-6];
value2index = new int[narg-6];
nvalues = 0;
values.clear();
for (int iarg = 6; iarg < narg; iarg++) { for (int iarg = 6; iarg < narg; iarg++) {
ArgInfo argi(arg[iarg]); ArgInfo argi(arg[iarg]);
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 slice argument: {}", arg[iarg]);
error->all(FLERR,"Illegal compute slice command");
nvalues++; values.push_back(val);
} }
// setup and error check // setup and error check
for (int i = 0; i < nvalues; i++) { for (auto &val : values) {
if (which[i] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(ids[i]); val.val.c = modify->get_compute_by_id(val.id);
if (icompute < 0) if (!val.val.c) error->all(FLERR, "Compute ID {} for compute slice does not exist", val.id);
error->all(FLERR,"Compute ID for compute slice does not exist"); if (val.val.c->vector_flag) {
if (modify->compute[icompute]->vector_flag) { if (val.argindex)
if (argindex[i]) error->all(FLERR, "Compute slice compute {} does not calculate a global array", val.id);
error->all(FLERR,"Compute slice compute does not " if (nstop > val.val.c->size_vector)
"calculate a global array"); error->all(FLERR, "Compute slice compute {} vector is accessed out-of-range", val.id);
if (nstop > modify->compute[icompute]->size_vector) } else if (val.val.c->array_flag) {
error->all(FLERR,"Compute slice compute vector is " if (val.argindex == 0)
"accessed out-of-range"); error->all(FLERR, "Compute slice compute {} does not calculate a global vector", val.id);
} else if (modify->compute[icompute]->array_flag) { if (val.argindex > val.val.c->size_array_cols)
if (argindex[i] == 0) error->all(FLERR, "Compute slice compute {} array is accessed out-of-range", val.id);
error->all(FLERR,"Compute slice compute does not " if (nstop > val.val.c->size_array_rows)
"calculate a global vector"); error->all(FLERR, "Compute slice compute {} array is accessed out-of-range", val.id);
if (argindex[i] > modify->compute[icompute]->size_array_cols) } else {
error->all(FLERR,"Compute slice compute array is " error->all(FLERR, "Compute slice compute {} does not calculate global vector or array",
"accessed out-of-range"); val.id);
if (nstop > modify->compute[icompute]->size_array_rows) }
error->all(FLERR,"Compute slice compute array is " } else if (val.which == ArgInfo::FIX) {
"accessed out-of-range"); val.val.f = modify->get_fix_by_id(val.id);
} else error->all(FLERR,"Compute slice compute does not calculate " if (!val.val.f) error->all(FLERR, "Fix ID {} for compute slice does not exist", val.id);
"global vector or array"); if (val.val.f->vector_flag) {
if (val.argindex)
} else if (which[i] == ArgInfo::FIX) { error->all(FLERR, "Compute slice fix {} does not calculate a global array", val.id);
auto ifix = modify->get_fix_by_id(ids[i]); if (nstop > val.val.f->size_vector)
if (!ifix) error->all(FLERR, "Compute slice fix {} vector is accessed out-of-range", val.id);
error->all(FLERR,"Fix ID {} for compute slice does not exist", ids[i]); } else if (val.val.f->array_flag) {
if (ifix->vector_flag) { if (val.argindex == 0)
if (argindex[i]) error->all(FLERR, "Compute slice fix {} does not calculate a global vector", val.id);
error->all(FLERR,"Compute slice fix {} does not calculate a global array", ids[i]); if (val.argindex > val.val.f->size_array_cols)
if (nstop > ifix->size_vector) error->all(FLERR, "Compute slice fix {} array is accessed out-of-range", val.id);
error->all(FLERR,"Compute slice fix {} vector is accessed out-of-range", ids[i]); if (nstop > val.val.f->size_array_rows)
} else if (ifix->array_flag) { error->all(FLERR, "Compute slice fix {} array is accessed out-of-range", val.id);
if (argindex[i] == 0) } else {
error->all(FLERR,"Compute slice fix {} does not calculate a global vector", ids[i]); error->all(FLERR, "Compute slice fix {} does not calculate global vector or array", val.id);
if (argindex[i] > ifix->size_array_cols) }
error->all(FLERR,"Compute slice fix {} array is accessed out-of-range", ids[i]); } else if (val.which == ArgInfo::VARIABLE) {
if (nstop > ifix->size_array_rows) val.val.v = input->variable->find(val.id.c_str());
error->all(FLERR,"Compute slice fix {} array is accessed out-of-range", ids[i]); if (val.val.v < 0)
} else error->all(FLERR,"Compute slice fix {} does not calculate global vector or array", ids[i]); error->all(FLERR, "Variable name {} for compute slice does not exist", val.id);
if (val.argindex == 0 && input->variable->vectorstyle(val.val.v) == 0)
} else if (which[i] == ArgInfo::VARIABLE) { error->all(FLERR, "Compute slice variable {} is not vector-style variable", val.id);
int ivariable = input->variable->find(ids[i]); if (val.argindex)
if (ivariable < 0) error->all(FLERR, "Compute slice vector variable {} cannot be indexed", val.id);
error->all(FLERR,"Variable name for compute slice does not exist");
if (argindex[i] == 0 && input->variable->vectorstyle(ivariable) == 0)
error->all(FLERR,"Compute slice variable is not vector-style variable");
if (argindex[i])
error->all(FLERR,"Compute slice vector variable cannot be indexed");
} }
} }
@ -124,68 +111,65 @@ ComputeSlice::ComputeSlice(LAMMPS *lmp, int narg, char **arg) :
// for vector, set intensive/extensive to mirror input values // for vector, set intensive/extensive to mirror input values
// for array, set intensive if all input values are intensive, else extensive // for array, set intensive if all input values are intensive, else extensive
if (nvalues == 1) { if (values.size() == 1) {
auto &val = values[0];
vector_flag = 1; vector_flag = 1;
size_vector = (nstop - nstart) / nskip; size_vector = (nstop - nstart) / nskip;
memory->create(vector, size_vector, "slice:vector"); memory->create(vector, size_vector, "slice:vector");
if (which[0] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(ids[0]); if (val.argindex == 0) {
if (argindex[0] == 0) { extvector = val.val.c->extvector;
extvector = modify->compute[icompute]->extvector; if (val.val.c->extvector == -1) {
if (modify->compute[icompute]->extvector == -1) {
extlist = new int[size_vector]; extlist = new int[size_vector];
int j = 0; int j = 0;
for (int i = nstart; i < nstop; i += nskip) for (int i = nstart; i < nstop; i += nskip) extlist[j++] = val.val.c->extlist[i - 1];
extlist[j++] = modify->compute[icompute]->extlist[i-1];
} }
} else extvector = modify->compute[icompute]->extarray; } else
} else if (which[0] == ArgInfo::FIX) { extvector = val.val.c->extarray;
auto ifix = modify->get_fix_by_id(ids[0]); } else if (val.which == ArgInfo::FIX) {
if (argindex[0] == 0) { if (val.argindex == 0) {
extvector = ifix->extvector; extvector = val.val.f->extvector;
if (ifix->extvector == -1) { if (val.val.f->extvector == -1) {
extlist = new int[size_vector]; extlist = new int[size_vector];
int j = 0; int j = 0;
for (int i = nstart; i < nstop; i += nskip) for (int i = nstart; i < nstop; i += nskip) extlist[j++] = val.val.f->extlist[i - 1];
extlist[j++] = ifix->extlist[i-1];
} }
} else extvector = ifix->extarray; } else
} else if (which[0] == ArgInfo::VARIABLE) { extvector = val.val.f->extarray;
} else if (val.which == ArgInfo::VARIABLE) {
extvector = 0; extvector = 0;
} }
} else { } else {
array_flag = 1; array_flag = 1;
size_array_rows = (nstop - nstart) / nskip; size_array_rows = (nstop - nstart) / nskip;
size_array_cols = nvalues; size_array_cols = values.size();
memory->create(array, size_array_rows, size_array_cols, "slice:array"); memory->create(array, size_array_rows, size_array_cols, "slice:array");
extarray = 0; extarray = 0;
for (int i = 0; i < nvalues; i++) { for (auto &val : values) {
if (which[i] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(ids[i]); if (val.argindex == 0) {
if (argindex[i] == 0) { if (val.val.c->extvector == 1) extarray = 1;
if (modify->compute[icompute]->extvector == 1) extarray = 1; if (val.val.c->extvector == -1) {
if (modify->compute[icompute]->extvector == -1) { for (int j = 0; j < val.val.c->size_vector; j++)
for (int j = 0; j < modify->compute[icompute]->size_vector; j++) if (val.val.c->extlist[j]) extarray = 1;
if (modify->compute[icompute]->extlist[j]) extarray = 1;
} }
} else { } else {
if (modify->compute[icompute]->extarray) extarray = 1; if (val.val.c->extarray) extarray = 1;
} }
} else if (which[i] == ArgInfo::FIX) { } else if (val.which == ArgInfo::FIX) {
auto ifix = modify->get_fix_by_id(ids[i]); if (val.argindex == 0) {
if (argindex[i] == 0) { if (val.val.f->extvector == 1) extarray = 1;
if (ifix->extvector == 1) extarray = 1; if (val.val.f->extvector == -1) {
if (ifix->extvector == -1) { for (int j = 0; j < val.val.f->size_vector; j++)
for (int j = 0; j < ifix->size_vector; j++) if (val.val.f->extlist[j]) extarray = 1;
if (ifix->extlist[j]) extarray = 1;
} }
} else { } else {
if (ifix->extarray) extarray = 1; if (val.val.f->extarray) extarray = 1;
} }
} else if (which[i] == ArgInfo::VARIABLE) { } else if (val.which == ArgInfo::VARIABLE) {
// variable is always intensive, does not change extarray // variable is always intensive, does not change extarray
} }
} }
@ -196,11 +180,6 @@ ComputeSlice::ComputeSlice(LAMMPS *lmp, int narg, char **arg) :
ComputeSlice::~ComputeSlice() ComputeSlice::~ComputeSlice()
{ {
delete [] which;
delete [] argindex;
for (int m = 0; m < nvalues; m++) delete [] ids[m];
delete [] ids;
delete [] value2index;
delete[] extlist; delete[] extlist;
memory->destroy(vector); memory->destroy(vector);
@ -213,22 +192,17 @@ void ComputeSlice::init()
{ {
// set indices and check validity of all computes,fixes // set indices and check validity of all computes,fixes
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 slice does not exist", val.id);
error->all(FLERR,"Compute ID for compute slice does not exist"); } else if (val.which == ArgInfo::FIX) {
value2index[m] = icompute; val.val.f = modify->get_fix_by_id(val.id);
} else if (which[m] == ArgInfo::FIX) { if (!val.val.f) error->all(FLERR, "Fix ID {} for compute slice does not exist", val.id);
int ifix = modify->find_fix(ids[m]); } else if (val.which == ArgInfo::VARIABLE) {
if (ifix < 0) val.val.v = input->variable->find(val.id.c_str());
error->all(FLERR,"Fix ID for compute slice does not exist"); if (val.val.v < 0)
value2index[m] = ifix; error->all(FLERR, "Variable name {} for compute slice does not exist", val.id);
} else if (which[m] == ArgInfo::VARIABLE) {
int ivariable = input->variable->find(ids[m]);
if (ivariable < 0)
error->all(FLERR,"Variable name for compute slice does not exist");
value2index[m] = ivariable;
} }
} }
} }
@ -248,8 +222,7 @@ void ComputeSlice::compute_array()
{ {
invoked_array = update->ntimestep; invoked_array = update->ntimestep;
for (int m = 0; m < nvalues; m++) for (int m = 0; m < values.size(); m++) extract_one(0, &array[m][0], values.size());
extract_one(0,&array[m][0],nvalues);
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
@ -259,34 +232,32 @@ void ComputeSlice::compute_array()
void ComputeSlice::extract_one(int m, double *vec, int stride) void ComputeSlice::extract_one(int m, double *vec, int stride)
{ {
int i,j; auto &val = values[m];
// invoke the appropriate compute if needed // invoke the appropriate compute if needed
if (which[m] == ArgInfo::COMPUTE) { if (val.which == ArgInfo::COMPUTE) {
Compute *compute = modify->compute[value2index[m]]; if (val.argindex == 0) {
if (!(val.val.c->invoked_flag & Compute::INVOKED_VECTOR)) {
if (argindex[m] == 0) { val.val.c->compute_vector();
if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) { val.val.c->invoked_flag |= Compute::INVOKED_VECTOR;
compute->compute_vector();
compute->invoked_flag |= Compute::INVOKED_VECTOR;
} }
double *cvector = compute->vector; double *cvector = val.val.c->vector;
j = 0; int j = 0;
for (i = nstart; i < nstop; i += nskip) { for (int i = nstart; i < nstop; i += nskip) {
vec[j] = cvector[i - 1]; vec[j] = cvector[i - 1];
j += stride; j += stride;
} }
} else { } else {
if (!(compute->invoked_flag & Compute::INVOKED_ARRAY)) { if (!(val.val.c->invoked_flag & Compute::INVOKED_ARRAY)) {
compute->compute_array(); val.val.c->compute_array();
compute->invoked_flag |= Compute::INVOKED_ARRAY; val.val.c->invoked_flag |= Compute::INVOKED_ARRAY;
} }
double **carray = compute->array; double **carray = val.val.c->array;
int icol = argindex[m]-1; int icol = val.argindex - 1;
j = 0; int j = 0;
for (i = nstart; i < nstop; i += nskip) { for (int i = nstart; i < nstop; i += nskip) {
vec[j] = carray[i - 1][icol]; vec[j] = carray[i - 1][icol];
j += stride; j += stride;
} }
@ -294,36 +265,33 @@ void ComputeSlice::extract_one(int m, double *vec, int stride)
// access fix fields, check if fix frequency is a match // access fix fields, check if fix frequency is a match
} else if (which[m] == ArgInfo::FIX) { } else if (val.which == ArgInfo::FIX) {
if (update->ntimestep % modify->fix[value2index[m]]->global_freq) if (update->ntimestep % val.val.f->global_freq)
error->all(FLERR,"Fix used in compute slice not " error->all(FLERR, "Fix {} used in compute slice not computed at compatible time", val.id);
"computed at compatible time");
Fix *fix = modify->fix[value2index[m]];
if (argindex[m] == 0) { if (val.argindex == 0) {
j = 0; int j = 0;
for (i = nstart; i < nstop; i += nskip) { for (int i = nstart; i < nstop; i += nskip) {
vec[j] = fix->compute_vector(i-1); vec[j] = val.val.f->compute_vector(i - 1);
j += stride; j += stride;
} }
} else { } else {
int icol = argindex[m]-1; int icol = val.argindex - 1;
j = 0; int j = 0;
for (i = nstart; i < nstop; i += nskip) { for (int i = nstart; i < nstop; i += nskip) {
vec[j] = fix->compute_array(i-1,icol); vec[j] = val.val.f->compute_array(i - 1, icol);
j += stride; j += stride;
} }
} }
// invoke vector-style variable // invoke vector-style variable
} else if (which[m] == ArgInfo::VARIABLE) { } else if (val.which == ArgInfo::VARIABLE) {
double *varvec; double *varvec;
int nvec = input->variable->compute_vector(value2index[m],&varvec); int nvec = input->variable->compute_vector(val.val.v, &varvec);
if (nvec < nstop) if (nvec < nstop) error->all(FLERR, "Compute slice variable {} is not long enough", val.id);
error->all(FLERR,"Compute slice variable is not long enough"); int j = 0;
j = 0; for (int i = nstart; i < nstop; i += nskip) {
for (i = nstart; i < nstop; i += nskip) {
vec[j] = varvec[i - 1]; vec[j] = varvec[i - 1];
j += stride; j += stride;
} }

View File

@ -33,10 +33,18 @@ class ComputeSlice : public Compute {
void compute_array() override; void compute_array() override;
private: private:
int me; struct value_t {
int nstart, nstop, nskip, nvalues; int which;
int *which, *argindex, *value2index; int argindex;
char **ids; std::string id;
union {
class Compute *c;
class Fix *f;
int v;
} val;
};
std::vector<value_t> values;
int nstart, nstop, nskip;
void extract_one(int, double *, int); void extract_one(int, double *, int);
}; };