From 7300e5c4bd6632a5d9a1458a5003d93caeccc8d7 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 15 Apr 2021 18:33:21 -0400 Subject: [PATCH] Use C++ for AtomVec::Method --- src/atom_vec.cpp | 142 ++++++++++++++++++----------------------------- src/atom_vec.h | 22 ++++---- 2 files changed, 66 insertions(+), 98 deletions(-) diff --git a/src/atom_vec.cpp b/src/atom_vec.cpp index 7d7cfa4878..78903d6a7f 100644 --- a/src/atom_vec.cpp +++ b/src/atom_vec.cpp @@ -71,21 +71,6 @@ AtomVec::AtomVec(LAMMPS *lmp) : Pointers(lmp) default_create = "id type mask image x v"; default_data_atom = ""; default_data_vel = ""; - - // initializations - - init_method(&mgrow); - init_method(&mcopy); - init_method(&mcomm); - init_method(&mcomm_vel); - init_method(&mreverse); - init_method(&mborder); - init_method(&mborder_vel); - init_method(&mexchange); - init_method(&mrestart); - init_method(&mcreate); - init_method(&mdata_atom); - init_method(&mdata_vel); } /* ---------------------------------------------------------------------- */ @@ -129,19 +114,6 @@ AtomVec::~AtomVec() } } - destroy_method(&mgrow); - destroy_method(&mcopy); - destroy_method(&mcomm); - destroy_method(&mcomm_vel); - destroy_method(&mreverse); - destroy_method(&mborder); - destroy_method(&mborder_vel); - destroy_method(&mexchange); - destroy_method(&mrestart); - destroy_method(&mcreate); - destroy_method(&mdata_atom); - destroy_method(&mdata_vel); - delete [] threads; } @@ -2395,18 +2367,18 @@ void AtomVec::setup_fields() // populate field-based data struct for each method to use - create_method(ngrow,&mgrow); - create_method(ncopy,&mcopy); - create_method(ncomm,&mcomm); - create_method(ncomm_vel,&mcomm_vel); - create_method(nreverse,&mreverse); - create_method(nborder,&mborder); - create_method(nborder_vel,&mborder_vel); - create_method(nexchange,&mexchange); - create_method(nrestart,&mrestart); - create_method(ncreate,&mcreate); - create_method(ndata_atom,&mdata_atom); - create_method(ndata_vel,&mdata_vel); + init_method(ngrow,&mgrow); + init_method(ncopy,&mcopy); + init_method(ncomm,&mcomm); + init_method(ncomm_vel,&mcomm_vel); + init_method(nreverse,&mreverse); + init_method(nborder,&mborder); + init_method(nborder_vel,&mborder_vel); + init_method(nexchange,&mexchange); + init_method(nrestart,&mrestart); + init_method(ncreate,&mcreate); + init_method(ndata_atom,&mdata_atom); + init_method(ndata_vel,&mdata_vel); // create threads data struct for grow and memory_usage to use @@ -2480,7 +2452,6 @@ void AtomVec::setup_fields() int AtomVec::process_fields(char *str, const char *default_str, Method *method) { if (str == nullptr) { - method->index = nullptr; return 0; } @@ -2496,17 +2467,19 @@ int AtomVec::process_fields(char *str, const char *default_str, Method *method) Atom::PerAtom *peratom = atom->peratom; int nperatom = atom->nperatom; - int *index; + // allocate memory in method + method->resize(nfield); + + int *index = method->index; int match; - if (nfield) index = new int[nfield]; for (int i = 0; i < nfield; i++) { - const char * field = words[i].c_str(); + const std::string & field = words[i]; // find field in master Atom::peratom list for (match = 0; match < nperatom; match++) - if (strcmp(field, peratom[match].name) == 0) break; + if (field == peratom[match].name) break; if (match == nperatom) error->all(FLERR,fmt::format("Peratom field {} not recognized", field)); index[i] = match; @@ -2520,38 +2493,19 @@ int AtomVec::process_fields(char *str, const char *default_str, Method *method) // error if field is in default str for (match = 0; match < ndef; match++) - if (strcmp(field, def_words[match].c_str()) == 0) + if (field == def_words[match]) error->all(FLERR,fmt::format("Peratom field {} is a default", field)); } - if (nfield) method->index = index; - else method->index = nullptr; return nfield; } /* ---------------------------------------------------------------------- - create a method data structs for processing fields + init method data structs for processing fields ------------------------------------------------------------------------- */ -void AtomVec::create_method(int nfield, Method *method) +void AtomVec::init_method(int nfield, Method *method) { - if (nfield > 0) { - method->pdata = new void*[nfield]; - method->datatype = new int[nfield]; - method->cols = new int[nfield]; - method->maxcols = new int*[nfield]; - method->collength = new int[nfield]; - method->plength = new void*[nfield]; - } else { - method->pdata = nullptr; - method->datatype = nullptr; - method->cols = nullptr; - method->maxcols = nullptr; - method->collength = nullptr; - method->plength = nullptr; - return; - } - for (int i = 0; i < nfield; i++) { Atom::PerAtom *field = &atom->peratom[method->index[i]]; method->pdata[i] = (void *) field->address; @@ -2566,31 +2520,43 @@ void AtomVec::create_method(int nfield, Method *method) } /* ---------------------------------------------------------------------- - free memory in a method data structs + Method class members ------------------------------------------------------------------------- */ -void AtomVec::init_method(Method *method) +AtomVec::Method::~Method() { - method->pdata = nullptr; - method->datatype = nullptr; - method->cols = nullptr; - method->maxcols = nullptr; - method->collength = nullptr; - method->plength = nullptr; - method->index = nullptr; + delete [] pdata; + delete [] datatype; + delete [] cols; + delete [] maxcols; + delete [] collength; + delete [] plength; + delete [] index; } -/* ---------------------------------------------------------------------- - free memory in a method data structs -------------------------------------------------------------------------- */ +void AtomVec::Method::resize(int nfield) { + delete [] pdata; + delete [] datatype; + delete [] cols; + delete [] maxcols; + delete [] collength; + delete [] plength; + delete [] index; + pdata = nullptr; + datatype = nullptr; + cols = nullptr; + maxcols = nullptr; + collength = nullptr; + plength = nullptr; + index = nullptr; -void AtomVec::destroy_method(Method *method) -{ - delete [] method->pdata; - delete [] method->datatype; - delete [] method->cols; - delete [] method->maxcols; - delete [] method->collength; - delete [] method->plength; - delete [] method->index; + if (nfield > 0) { + pdata = new void*[nfield]; + datatype = new int[nfield]; + cols = new int[nfield]; + maxcols = new int*[nfield]; + collength = new int[nfield]; + plength = new void*[nfield]; + index = new int[nfield]; + } } diff --git a/src/atom_vec.h b/src/atom_vec.h index 8e670304ed..d1915ba508 100644 --- a/src/atom_vec.h +++ b/src/atom_vec.h @@ -191,13 +191,17 @@ class AtomVec : protected Pointers { const char *default_create,*default_data_atom,*default_data_vel; struct Method { - void **pdata; - int *datatype; - int *cols; - int **maxcols; - int *collength; - void **plength; - int *index; + void **pdata = nullptr; + int *datatype = nullptr; + int *cols = nullptr; + int **maxcols = nullptr; + int *collength = nullptr; + void **plength = nullptr; + int *index = nullptr; + + Method() = default; + ~Method(); + void resize(int nfield); }; Method mgrow,mcopy; @@ -219,9 +223,7 @@ class AtomVec : protected Pointers { int grow_nmax_bonus(int); void setup_fields(); int process_fields(char *, const char *, Method *); - void create_method(int, Method *); - void init_method(Method *); - void destroy_method(Method *); + void init_method(int, Method *); }; }