diff --git a/src/neigh_request.cpp b/src/neigh_request.cpp index 96ec568469..cab07b656d 100644 --- a/src/neigh_request.cpp +++ b/src/neigh_request.cpp @@ -1,4 +1,3 @@ -// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -13,14 +12,17 @@ ------------------------------------------------------------------------- */ #include "neigh_request.h" + #include "atom.h" #include "memory.h" +#include "neighbor.h" using namespace LAMMPS_NS; +using namespace NeighConst; /* ---------------------------------------------------------------------- */ -NeighRequest::NeighRequest(LAMMPS *lmp) : Pointers(lmp) +NeighRequest::NeighRequest(LAMMPS *_lmp) : Pointers(_lmp) { // default ID = 0 @@ -96,9 +98,27 @@ NeighRequest::NeighRequest(LAMMPS *lmp) : Pointers(lmp) /* ---------------------------------------------------------------------- */ +NeighRequest::NeighRequest(LAMMPS *_lmp, int idx, void *ptr, int num) + : NeighRequest(_lmp) +{ + index = idx; + requestor = ptr; + requestor_instance = num; +} + +/* ---------------------------------------------------------------------- */ + +NeighRequest::NeighRequest(NeighRequest *old) + : NeighRequest(old->lmp) +{ + copy_request(old,1); +} + +/* ---------------------------------------------------------------------- */ + NeighRequest::~NeighRequest() { - delete [] iskip; + delete[] iskip; memory->destroy(ijskip); } @@ -165,15 +185,13 @@ int NeighRequest::identical(NeighRequest *other) int NeighRequest::same_skip(NeighRequest *other) { - int i,j; - - int ntypes = atom->ntypes; + const int ntypes = atom->ntypes; int same = 1; - for (i = 1; i <= ntypes; i++) + for (int i = 1; i <= ntypes; i++) if (iskip[i] != other->iskip[i]) same = 0; - for (i = 1; i <= ntypes; i++) - for (j = 1; j <= ntypes; j++) + for (int i = 1; i <= ntypes; i++) + for (int j = 1; j <= ntypes; j++) if (ijskip[i][j] != other->ijskip[i][j]) same = 0; return same; @@ -206,7 +224,7 @@ void NeighRequest::copy_request(NeighRequest *other, int skipflag) size = other->size; history = other->history; granonesided = other->granonesided; - respainner = other->respainner; + respainner = other->respainner; respamiddle = other->respamiddle; respaouter = other->respaouter; bond = other->bond; @@ -223,22 +241,47 @@ void NeighRequest::copy_request(NeighRequest *other, int skipflag) if (!skipflag) return; - int i,j; + int i, j; int ntypes = atom->ntypes; skip = other->skip; if (other->iskip) { - iskip = new int[ntypes+1]; - for (i = 1; i <= ntypes; i++) - iskip[i] = other->iskip[i]; + iskip = new int[ntypes + 1]; + for (i = 1; i <= ntypes; i++) iskip[i] = other->iskip[i]; } if (other->ijskip) { - memory->create(ijskip,ntypes+1,ntypes+1,"neigh_request:ijskip"); + memory->create(ijskip, ntypes + 1, ntypes + 1, "neigh_request:ijskip"); for (i = 1; i <= ntypes; i++) - for (j = 1; j <= ntypes; j++) - ijskip[i][j] = other->ijskip[i][j]; + for (j = 1; j <= ntypes; j++) ijskip[i][j] = other->ijskip[i][j]; } } +/* ---------------------------------------------------------------------- */ + +void NeighRequest::apply_flags(int flags) +{ + if (flags & REQ_FULL) { + half = 0; + full = 1; + } + if (flags & REQ_GHOST) { ghost = 1; } + if (flags & REQ_SIZE) { size = 1; } + if (flags & REQ_HISTORY) { history = 1; } + if (flags & REQ_OCCASIONAL) { occasional = 1; } + if (flags & REQ_RESPA_INOUT) { respainner = respaouter = 1; } + if (flags & REQ_RESPA_ALL) { respainner = respamiddle = respaouter = 1; } +} + +/* ---------------------------------------------------------------------- */ + +void NeighRequest::set_id(int _id) { + id = _id; +} + +void NeighRequest::set_cutoff(double _cutoff) +{ + cut = 1; + cutoff = _cutoff; +} diff --git a/src/neigh_request.h b/src/neigh_request.h index ff86cfa7fe..0bb0f1c822 100644 --- a/src/neigh_request.h +++ b/src/neigh_request.h @@ -112,11 +112,18 @@ class NeighRequest : protected Pointers { // methods NeighRequest(class LAMMPS *); + NeighRequest(class LAMMPS *, int, void *, int); + NeighRequest(NeighRequest *); ~NeighRequest() override; + void archive(); int identical(NeighRequest *); int same_skip(NeighRequest *); void copy_request(NeighRequest *, int); + + void apply_flags(int); + void set_id(int); + void set_cutoff(double); }; } // namespace LAMMPS_NS diff --git a/src/neighbor.cpp b/src/neighbor.cpp index 489d76eaed..3d82479c6c 100644 --- a/src/neighbor.cpp +++ b/src/neighbor.cpp @@ -1715,13 +1715,10 @@ void Neighbor::requests_new2old() old_nrequest = nrequest; old_requests = (NeighRequest **) - memory->smalloc(old_nrequest*sizeof(NeighRequest *), - "neighbor:old_requests"); + memory->smalloc(old_nrequest*sizeof(NeighRequest *),"neighbor:old_requests"); - for (int i = 0; i < old_nrequest; i++) { - old_requests[i] = new NeighRequest(lmp); - old_requests[i]->copy_request(requests[i],1); - } + for (int i = 0; i < old_nrequest; i++) + old_requests[i] = new NeighRequest(requests[i]); old_style = style; old_triclinic = triclinic; @@ -1731,7 +1728,8 @@ void Neighbor::requests_new2old() /* ---------------------------------------------------------------------- find and return request made by classptr - if not found or classpt = nullptr, return nullptr + if not found or classptr = nullptr, return nullptr + TODO: should have optional argument "id" to match ID if multiple requests ------------------------------------------------------------------------- */ NeighRequest *Neighbor::find_request(void *classptr) @@ -1744,6 +1742,22 @@ NeighRequest *Neighbor::find_request(void *classptr) return nullptr; } +/* ---------------------------------------------------------------------- + find and return list requested by classptr + if not found or classptr = nullptr, return nullptr + TODO: should have optional argument "id" to match ID if multiple requests +------------------------------------------------------------------------- */ + +NeighList *Neighbor::find_list(void *classptr) +{ + if (classptr == nullptr) return nullptr; + + for (int i = 0; i < nrequest; i++) + if (lists[i]->requestor == classptr) return lists[i]; + + return nullptr; +} + /* ---------------------------------------------------------------------- assign NBin class to a NeighList use neigh request settings to build mask @@ -2028,14 +2042,55 @@ int Neighbor::request(void *requestor, int instance) "neighbor:requests"); } - requests[nrequest] = new NeighRequest(lmp); - requests[nrequest]->index = nrequest; - requests[nrequest]->requestor = requestor; - requests[nrequest]->requestor_instance = instance; + requests[nrequest] = new NeighRequest(lmp, nrequest, requestor, instance); nrequest++; return nrequest-1; } +/* ---------------------------------------------------------------------- + called by other classes to request a pairwise neighbor list +------------------------------------------------------------------------- */ + +NeighRequest *Neighbor::add_request(Pair *requestor, int flags) +{ + int irequest = request(requestor, requestor->instance_me); + auto req = requests[irequest]; + req->apply_flags(flags); + return req; +} + +NeighRequest *Neighbor::add_request(Fix *requestor, int flags) +{ + int irequest = request(requestor, requestor->instance_me); + auto req = requests[irequest]; + req->pair = 0; + req->fix = 1; + req->apply_flags(flags); + return req; +} + +NeighRequest *Neighbor::add_request(Compute *requestor, int flags) +{ + int irequest = request(requestor, requestor->instance_me); + auto req = requests[irequest]; + req->pair = 0; + req->compute = 1; + req->apply_flags(flags); + return req; +} + +NeighRequest *Neighbor::add_request(Command *requestor, const char *style, int flags) +{ + int irequest = request(requestor, 0); + auto req = requests[irequest]; + req->pair = 0; + req->command = 1; + req->occasional = 1; + req->command_style = style; + req->apply_flags(flags); + return req; +} + /* ---------------------------------------------------------------------- setup neighbor binning and neighbor stencils called before run and every reneighbor if box size/shape changes @@ -2275,13 +2330,11 @@ void Neighbor::build_one(class NeighList *mylist, int preflag) // check if list structure is initialized if (mylist == nullptr) - error->all(FLERR,"Trying to build an occasional neighbor list " - "before initialization completed"); + error->all(FLERR,"Trying to build an occasional neighbor list before initialization complete"); // build_one() should never be invoked on a perpetual list - if (!mylist->occasional) - error->all(FLERR,"Neighbor build one invoked on perpetual list"); + if (!mylist->occasional) error->all(FLERR,"Neighbor::build_one() invoked on perpetual list"); // no need to build if already built since last re-neighbor // preflag is set by fix bond/create and fix bond/swap diff --git a/src/neighbor.h b/src/neighbor.h index 45468c0c9b..4b78522000 100644 --- a/src/neighbor.h +++ b/src/neighbor.h @@ -18,6 +18,10 @@ namespace LAMMPS_NS { +// forward declarations +class NeighRequest; +class NeighList; + class Neighbor : protected Pointers { public: enum { NSQ, BIN, MULTI_OLD, MULTI }; @@ -87,9 +91,9 @@ class Neighbor : protected Pointers { int nrequest; // # of requests, same as nlist int old_nrequest; // # of requests for previous run - class NeighList **lists; - class NeighRequest **requests; // from Pair,Fix,Compute,Command classes - class NeighRequest **old_requests; // copy of requests to compare to + NeighList **lists; + NeighRequest **requests; // from Pair,Fix,Compute,Command classes + NeighRequest **old_requests; // copy of requests to compare to // data from topology neighbor lists @@ -119,7 +123,16 @@ class Neighbor : protected Pointers { Neighbor(class LAMMPS *); ~Neighbor() override; virtual void init(); - int request(void *, int instance = 0); + + // old API for creating neighbor list requests + int request(void *, int instance_me = 0); + + // new API for creating neighbor list requests + NeighRequest *add_request(class Pair *, int); + NeighRequest *add_request(class Fix *, int); + NeighRequest *add_request(class Compute *, int); + NeighRequest *add_request(class Command *, const char *, int); + int decide(); // decide whether to build or not virtual int check_distance(); // check max distance moved since last build void setup_bins(); // setup bins based on box and cutoff @@ -134,7 +147,8 @@ class Neighbor : protected Pointers { void exclusion_group_group_delete(int, int); // rm a group-group exclusion int exclude_setting(); // return exclude value to accelerator pkg - class NeighRequest *find_request(void *); // find a neighbor request + NeighList *find_list(void *); // find a neighbor request + NeighRequest *find_request(void *); // find a neighbor request int any_full(); // Check if any old requests had full neighbor lists void build_collection(int); // build peratom collection array starting at the given index @@ -246,6 +260,7 @@ class Neighbor : protected Pointers { }; namespace NeighConst { + enum { NB_INTEL = 1 << 0, NB_KOKKOS_DEVICE = 1 << 1, @@ -297,6 +312,19 @@ namespace NeighConst { NP_OFF2ON = 1 << 24, NP_MULTI_OLD = 1 << 25 }; + + enum { + REQ_DEFAULT = 0, + REQ_FULL = 1 << 0, + REQ_GHOST = 1 << 1, + REQ_SIZE = 1 << 2, + REQ_HISTORY = 1 << 3, + REQ_OCCASIONAL = 1 << 4, + REQ_RESPA_INOUT = 1 << 5, + REQ_RESPA_ALL = 1 << 6, + REQ_NEWTON_ON = 1 << 8, + REQ_NEWTON_OFF = 1 << 9, + }; } // namespace NeighConst } // namespace LAMMPS_NS