"stringify" and "vectorize" per-atom data handling in Atom class

This commit is contained in:
Axel Kohlmeyer
2022-04-14 11:31:20 -04:00
parent 198d07e905
commit 500a3426ed
3 changed files with 33 additions and 80 deletions

View File

@ -47,7 +47,6 @@ using namespace LAMMPS_NS;
using namespace MathConst;
#define DELTA 1
#define DELTA_PERATOM 64
#define EPSILON 1.0e-6
/* ----------------------------------------------------------------------
@ -105,11 +104,6 @@ Atom::Atom(LAMMPS *lmp) : Pointers(lmp)
binhead = nullptr;
next = permute = nullptr;
// data structure with info on per-atom vectors/arrays
nperatom = maxperatom = 0;
peratom = nullptr;
// --------------------------------------------------------------------
// 1st customization section: customize by adding new per-atom variables
@ -285,11 +279,11 @@ Atom::Atom(LAMMPS *lmp) : Pointers(lmp)
Atom::~Atom()
{
delete [] atom_style;
delete[] atom_style;
delete avec;
delete avec_map;
delete [] firstgroupname;
delete[] firstgroupname;
memory->destroy(binhead);
memory->destroy(next);
memory->destroy(permute);
@ -302,12 +296,6 @@ Atom::~Atom()
memory->destroy(v);
memory->destroy(f);
// delete peratom data struct
for (int i = 0; i < nperatom; i++)
delete [] peratom[i].name;
memory->sfree(peratom);
// delete custom atom arrays
for (int i = 0; i < nivector; i++) {
@ -384,12 +372,7 @@ void Atom::settings(Atom *old)
void Atom::peratom_create()
{
for (int i = 0; i < nperatom; i++)
delete [] peratom[i].name;
memory->sfree(peratom);
peratom = nullptr;
nperatom = maxperatom = 0;
peratom.clear();
// --------------------------------------------------------------------
// 2nd customization section: add peratom variables here, order does not matter
@ -566,23 +549,11 @@ void Atom::peratom_create()
use add_peratom_vary() when column count varies per atom
------------------------------------------------------------------------- */
void Atom::add_peratom(const char *name, void *address,
void Atom::add_peratom(const std::string &name, void *address,
int datatype, int cols, int threadflag)
{
if (nperatom == maxperatom) {
maxperatom += DELTA_PERATOM;
peratom = (PerAtom *)
memory->srealloc(peratom,maxperatom*sizeof(PerAtom),"atom:peratom");
}
peratom[nperatom].name = utils::strdup(name);
peratom[nperatom].address = address;
peratom[nperatom].datatype = datatype;
peratom[nperatom].cols = cols;
peratom[nperatom].threadflag = threadflag;
peratom[nperatom].address_length = nullptr;
nperatom++;
PerAtom item = {name, address, nullptr, nullptr, datatype, cols, 0, threadflag};
peratom.push_back(item);
}
/* ----------------------------------------------------------------------
@ -591,15 +562,13 @@ void Atom::add_peratom(const char *name, void *address,
see atom_style tdpd as an example
------------------------------------------------------------------------- */
void Atom::add_peratom_change_columns(const char *name, int cols)
void Atom::add_peratom_change_columns(const std::string &name, int cols)
{
for (int i = 0; i < nperatom; i++) {
if (strcmp(name,peratom[i].name) == 0) {
peratom[i].cols = cols;
return;
}
}
error->all(FLERR,"Could not find name of peratom array for column change");
auto match = std::find_if(peratom.begin(), peratom.end(),
[&n = name] (const PerAtom &p) { return p.name == n; });
if (match != peratom.end()) (*match).cols = cols;
else error->all(FLERR,"Could not find per-atom array name {} for column change", name);
}
/* ----------------------------------------------------------------------
@ -614,25 +583,11 @@ void Atom::add_peratom_change_columns(const char *name, int cols)
e.g. nspecial
------------------------------------------------------------------------- */
void Atom::add_peratom_vary(const char *name, void *address,
void Atom::add_peratom_vary(const std::string &name, void *address,
int datatype, int *cols, void *length, int collength)
{
if (nperatom == maxperatom) {
maxperatom += DELTA_PERATOM;
peratom = (PerAtom *)
memory->srealloc(peratom,maxperatom*sizeof(PerAtom),"atom:peratom");
}
peratom[nperatom].name = utils::strdup(name);
peratom[nperatom].address = address;
peratom[nperatom].datatype = datatype;
peratom[nperatom].cols = -1;
peratom[nperatom].threadflag = 0;
peratom[nperatom].address_maxcols = cols;
peratom[nperatom].address_length = length;
peratom[nperatom].collength = collength;
nperatom++;
PerAtom item = {name, address, length, cols, datatype, -1, collength, 0};
peratom.push_back(item);
}
/* ----------------------------------------------------------------------
@ -2894,4 +2849,3 @@ double Atom::memory_usage()
return bytes;
}

View File

@ -208,7 +208,7 @@ class Atom : protected Pointers {
// per-atom data struct describing all per-atom vectors/arrays
struct PerAtom {
char *name;
std::string name;
void *address;
void *address_length;
int *address_maxcols;
@ -218,8 +218,7 @@ class Atom : protected Pointers {
int threadflag;
};
PerAtom *peratom;
int nperatom, maxperatom;
std::vector<PerAtom> peratom;
// custom vectors and arrays used by fix property/atom
@ -288,9 +287,9 @@ class Atom : protected Pointers {
void settings(class Atom *);
void peratom_create();
void add_peratom(const char *, void *, int, int, int threadflag = 0);
void add_peratom_change_columns(const char *, int);
void add_peratom_vary(const char *, void *, int, int *, void *, int collength = 0);
void add_peratom(const std::string &, void *, int, int, int threadflag = 0);
void add_peratom_change_columns(const std::string &, int);
void add_peratom_vary(const std::string &, void *, int, int *, void *, int collength = 0);
void create_avec(const std::string &, int, char **, int);
virtual AtomVec *new_avec(const std::string &, int, int &);

View File

@ -2280,7 +2280,7 @@ void AtomVec::setup_fields()
error->all(FLERR, "Atom style fields_data_vel must have 'id' and 'v' as first two fields");
// process field strings
// return # of fields and matching index into atom->peratom (in Method struct)
// return # of fields and matching index into atom.peratom (in Method struct)
ngrow = process_fields(fields_grow, default_grow, &mgrow);
ncopy = process_fields(fields_copy, default_copy, &mcopy);
@ -2317,8 +2317,8 @@ void AtomVec::setup_fields()
else
threads = nullptr;
for (int i = 0; i < ngrow; i++) {
Atom::PerAtom *field = &atom->peratom[mgrow.index[i]];
threads[i] = field->threadflag == 1;
const auto &field = atom->peratom[mgrow.index[i]];
threads[i] = field.threadflag == 1;
}
// set style-specific sizes
@ -2373,7 +2373,7 @@ void AtomVec::setup_fields()
size_data_atom = 0;
for (n = 0; n < ndata_atom; n++) {
cols = mdata_atom.cols[n];
if (strcmp(atom->peratom[mdata_atom.index[n]].name, "x") == 0) xcol_data = size_data_atom + 1;
if (atom->peratom[mdata_atom.index[n]].name == "x") xcol_data = size_data_atom + 1;
if (cols == 0)
size_data_atom++;
else
@ -2402,8 +2402,8 @@ int AtomVec::process_fields(const std::vector<std::string> &words,
// process fields one by one, add to index vector
Atom::PerAtom *peratom = atom->peratom;
int nperatom = atom->nperatom;
const auto &peratom = atom->peratom;
const int nperatom = peratom.size();
// allocate memory in method
method->resize(nfield);
@ -2442,14 +2442,14 @@ int AtomVec::process_fields(const std::vector<std::string> &words,
void AtomVec::init_method(int nfield, Method *method)
{
for (int i = 0; i < nfield; i++) {
Atom::PerAtom *field = &atom->peratom[method->index[i]];
method->pdata[i] = (void *) field->address;
method->datatype[i] = field->datatype;
method->cols[i] = field->cols;
const auto &field = atom->peratom[method->index[i]];
method->pdata[i] = (void *) field.address;
method->datatype[i] = field.datatype;
method->cols[i] = field.cols;
if (method->cols[i] < 0) {
method->maxcols[i] = field->address_maxcols;
method->collength[i] = field->collength;
method->plength[i] = field->address_length;
method->maxcols[i] = field.address_maxcols;
method->collength[i] = field.collength;
method->plength[i] = field.address_length;
}
}
}