"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;
}