From d61c94c0f3634fd6cc68fb1fdcd41f45fa25bf61 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 10 Jun 2024 14:14:36 -0400 Subject: [PATCH 1/6] move group2ndx and ndx2group to new EXTRA-COMMAND package. update docs. --- cmake/CMakeLists.txt | 1 + cmake/presets/all_off.cmake | 1 + cmake/presets/all_on.cmake | 1 + cmake/presets/mingw-cross.cmake | 1 + cmake/presets/most.cmake | 1 + cmake/presets/windows.cmake | 1 + doc/src/Packages_details.rst | 17 +++++++++++++++++ doc/src/Packages_list.rst | 5 +++++ doc/src/group2ndx.rst | 7 ++++--- src/{COLVARS => EXTRA-COMMAND}/group_ndx.cpp | 0 src/{COLVARS => EXTRA-COMMAND}/group_ndx.h | 0 src/{COLVARS => EXTRA-COMMAND}/ndx_group.cpp | 0 src/{COLVARS => EXTRA-COMMAND}/ndx_group.h | 0 src/Makefile | 2 ++ 14 files changed, 34 insertions(+), 3 deletions(-) rename src/{COLVARS => EXTRA-COMMAND}/group_ndx.cpp (100%) rename src/{COLVARS => EXTRA-COMMAND}/group_ndx.h (100%) rename src/{COLVARS => EXTRA-COMMAND}/ndx_group.cpp (100%) rename src/{COLVARS => EXTRA-COMMAND}/ndx_group.h (100%) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index f87c92396f..6421f5356f 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -256,6 +256,7 @@ set(STANDARD_PACKAGES DRUDE EFF ELECTRODE + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/cmake/presets/all_off.cmake b/cmake/presets/all_off.cmake index e078879f70..50a0bf846c 100644 --- a/cmake/presets/all_off.cmake +++ b/cmake/presets/all_off.cmake @@ -28,6 +28,7 @@ set(ALL_PACKAGES DRUDE ELECTRODE EFF + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/cmake/presets/all_on.cmake b/cmake/presets/all_on.cmake index 3f44a863f7..52a2a3a753 100644 --- a/cmake/presets/all_on.cmake +++ b/cmake/presets/all_on.cmake @@ -30,6 +30,7 @@ set(ALL_PACKAGES DRUDE ELECTRODE EFF + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/cmake/presets/mingw-cross.cmake b/cmake/presets/mingw-cross.cmake index f3565668b2..49e81868ae 100644 --- a/cmake/presets/mingw-cross.cmake +++ b/cmake/presets/mingw-cross.cmake @@ -24,6 +24,7 @@ set(WIN_PACKAGES DRUDE ELECTRODE EFF + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/cmake/presets/most.cmake b/cmake/presets/most.cmake index 2356e24764..c539ea1d3d 100644 --- a/cmake/presets/most.cmake +++ b/cmake/presets/most.cmake @@ -26,6 +26,7 @@ set(ALL_PACKAGES DRUDE EFF ELECTRODE + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/cmake/presets/windows.cmake b/cmake/presets/windows.cmake index 9655134e7f..360f26cb90 100644 --- a/cmake/presets/windows.cmake +++ b/cmake/presets/windows.cmake @@ -22,6 +22,7 @@ set(WIN_PACKAGES DRUDE EFF ELECTRODE + EXTRA-COMMAND EXTRA-COMPUTE EXTRA-DUMP EXTRA-FIX diff --git a/doc/src/Packages_details.rst b/doc/src/Packages_details.rst index a3d65d9d65..d96d73bcce 100644 --- a/doc/src/Packages_details.rst +++ b/doc/src/Packages_details.rst @@ -52,6 +52,7 @@ page gives those details. * :ref:`DRUDE ` * :ref:`EFF ` * :ref:`ELECTRODE ` + * :ref:`EXTRA-COMMAND ` * :ref:`EXTRA-COMPUTE ` * :ref:`EXTRA-DUMP ` * :ref:`EXTRA-FIX ` @@ -886,6 +887,22 @@ This package has :ref:`specific installation instructions ` on the ---------- +.. _PKG-EXTRA-COMMAND: + +EXTRA-COMMAND package +--------------------- + +**Contents:** + +Additional compute styles that are less commonly used. + +**Supporting info:** + +* src/EXTRA-COMMAND: filenames -> commands +* :doc:`general commands ` + +---------- + .. _PKG-EXTRA-COMPUTE: EXTRA-COMPUTE package diff --git a/doc/src/Packages_list.rst b/doc/src/Packages_list.rst index c0a1164513..34d6aff393 100644 --- a/doc/src/Packages_list.rst +++ b/doc/src/Packages_list.rst @@ -158,6 +158,11 @@ whether an extra library is needed to build and use the package: - :doc:`fix electrode/conp ` - PACKAGES/electrode - no + * - :ref:`EXTRA-COMMAND ` + - additional command styles + - :doc:`general commands ` + - n/a + - no * - :ref:`EXTRA-COMPUTE ` - additional compute styles - :doc:`compute ` diff --git a/doc/src/group2ndx.rst b/doc/src/group2ndx.rst index 077f88e3e8..8c28d43aa0 100644 --- a/doc/src/group2ndx.rst +++ b/doc/src/group2ndx.rst @@ -55,11 +55,12 @@ from the index file and restored. Restrictions """""""""""" -This command requires that atoms have atom IDs, since this is the +These commands require that atoms have atom IDs, since this is the information that is written to the index file. -These commands are part of the COLVARS package. They are only -enabled if LAMMPS was built with that package. See the :doc:`Build package ` page for more info. +These commands are part of the EXTRA-COMMAND package. They are only +enabled if LAMMPS was built with that package. See the +:doc:`Build package ` page for more info. Related commands """""""""""""""" diff --git a/src/COLVARS/group_ndx.cpp b/src/EXTRA-COMMAND/group_ndx.cpp similarity index 100% rename from src/COLVARS/group_ndx.cpp rename to src/EXTRA-COMMAND/group_ndx.cpp diff --git a/src/COLVARS/group_ndx.h b/src/EXTRA-COMMAND/group_ndx.h similarity index 100% rename from src/COLVARS/group_ndx.h rename to src/EXTRA-COMMAND/group_ndx.h diff --git a/src/COLVARS/ndx_group.cpp b/src/EXTRA-COMMAND/ndx_group.cpp similarity index 100% rename from src/COLVARS/ndx_group.cpp rename to src/EXTRA-COMMAND/ndx_group.cpp diff --git a/src/COLVARS/ndx_group.h b/src/EXTRA-COMMAND/ndx_group.h similarity index 100% rename from src/COLVARS/ndx_group.h rename to src/EXTRA-COMMAND/ndx_group.h diff --git a/src/Makefile b/src/Makefile index b9f1bcbdef..b56631a46a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -74,6 +74,7 @@ PACKAGE = \ dpd-smooth \ drude \ eff \ + extra-command \ extra-compute \ extra-dump \ extra-fix \ @@ -167,6 +168,7 @@ PACKMOST = \ dpd-smooth \ drude \ eff \ + extra-command \ extra-compute \ extra-dump \ extra-fix \ From 09bea938c5ef4361c63ce3a5604979674f0ebd57 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 10 Jun 2024 14:26:32 -0400 Subject: [PATCH 2/6] fix copy-n-paste error --- doc/src/Packages_details.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/Packages_details.rst b/doc/src/Packages_details.rst index d96d73bcce..d13925f5d0 100644 --- a/doc/src/Packages_details.rst +++ b/doc/src/Packages_details.rst @@ -894,7 +894,7 @@ EXTRA-COMMAND package **Contents:** -Additional compute styles that are less commonly used. +Additional command styles that are less commonly used. **Supporting info:** From 318b43f358424acbd9a57bd6a67d8b9c97772cfc Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 17 Jun 2024 15:02:43 -0400 Subject: [PATCH 3/6] update group2ndx/ndx2group docs --- doc/src/group2ndx.rst | 57 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/doc/src/group2ndx.rst b/doc/src/group2ndx.rst index 8c28d43aa0..19c472e109 100644 --- a/doc/src/group2ndx.rst +++ b/doc/src/group2ndx.rst @@ -34,21 +34,54 @@ Description Write or read a Gromacs style index file in text format that associates atom IDs with the corresponding group definitions. This index file can be used with in combination with Gromacs analysis tools or to import group -definitions into the :doc:`fix colvars ` input file. It can -also be used to save and restore group definitions for static groups. +definitions into the :doc:`fix colvars ` input file. + +It can also be used to save and restore group definitions for static groups +using the individual atom IDs. This may be important if the original +group definition depends on a region or otherwise on the geometry and thus +cannot be easily recreated. + +Another application would be to import atom groups defined for Gromacs +simulation into LAMMPS. When translating Gromacs topology and geometry +data to LAMMPS. The *group2ndx* command will write group definitions to an index file. -Without specifying any group IDs, all groups will be written to the index -file. When specifying group IDs, only those groups will be written to the -index file. In order to follow the Gromacs conventions, the group *all* -will be renamed to *System* in the index file. +Without specifying any group IDs, all groups will be written to the +index file. When specifying group IDs, only those groups will be +written to the index file. In order to follow the Gromacs conventions, +the group *all* will be renamed to *System* in the index file. -The *ndx2group* command will create of update group definitions from those -stored in an index file. Without specifying any group IDs, all groups except -*System* will be read from the index file and the corresponding groups -recreated. If a group of the same name already exists, it will be completely -reset. When specifying group IDs, those groups, if present, will be read -from the index file and restored. +The *ndx2group* command will create of update group definitions from +those stored in an index file. Without specifying any group IDs, all +groups except *System* will be read from the index file and the +corresponding groups recreated. If a group of the same name already +exists, it will be completely reset. When specifying group IDs, those +groups, if present, will be read from the index file and restored. + +File Format +""""""""""" + +The file format is equivalent and compatible with what is produced by +the `Gromacs make_ndx command `_. +and follows the `Gromacs definition of an ndx file `_ + +Each group definition begins with the group name in square brackets with +blanks, e.g. \[ water \] and is then followed by the list of atom +indices, which may be spread over multiple lines. Here is a small +example file: + +.. code-block:: ini + + [ Oxygen ] + 1 4 7 + [ Hydrogen ] + 2 3 5 6 + 8 9 + [ Water ] + 1 2 3 4 5 6 7 8 9 + +The index file defines 3 groups: Oxygen, Hydrogen, and Water and the +latter happens to be the union of the first two. ---------- From 9856ef7d81c06d4a92a921e588d6167e679770eb Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 17 Jun 2024 16:57:22 -0400 Subject: [PATCH 4/6] better error handling when processing index files with illegal group names --- src/EXTRA-COMMAND/ndx_group.cpp | 57 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/EXTRA-COMMAND/ndx_group.cpp b/src/EXTRA-COMMAND/ndx_group.cpp index 4170a9ea70..52721f9628 100644 --- a/src/EXTRA-COMMAND/ndx_group.cpp +++ b/src/EXTRA-COMMAND/ndx_group.cpp @@ -34,14 +34,16 @@ static std::string find_section(FILE *fp, const std::string &name) { char linebuf[BUFLEN]; - std::string pattern = "^\\s*\\[\\s+\\S+\\s+\\]\\s*$"; - if (!name.empty()) - pattern = fmt::format("^\\s*\\[\\s+{}\\s+\\]\\s*$",name); - fgets(linebuf,BUFLEN,fp); while (!feof(fp)) { - if (utils::strmatch(linebuf,pattern)) - return Tokenizer(linebuf).as_vector()[1]; + if (utils::strmatch(linebuf, "^\\s*\\[.*\\]\\s*$")) { + auto words = Tokenizer(linebuf).as_vector(); + if (words.size() != 3) + throw TokenizerException("Invalid group name in index file", + utils::trim(utils::strfind(linebuf,"[^\\[^\\]]+"))); + if (name.empty() || (name == words[1])) + return words[1]; + } fgets(linebuf,BUFLEN,fp); } return ""; @@ -51,12 +53,15 @@ static std::vector read_section(FILE *fp, std::string &name) { char linebuf[BUFLEN]; std::vector tagbuf; - std::string pattern = "^\\s*\\[\\s+\\S+\\s+\\]\\s*$"; while (fgets(linebuf,BUFLEN,fp)) { // start of new section. we are done, update "name" - if (utils::strmatch(linebuf,pattern)) { - name = Tokenizer(linebuf).as_vector()[1]; + if (utils::strmatch(linebuf, "^\\s*\\[.*\\]\\s*$")) { + auto words = Tokenizer(linebuf).as_vector(); + if (words.size() != 3) + throw TokenizerException("Invalid group name in index file", + utils::trim(utils::strfind(linebuf,"[^\\[^\\]]+"))); + name = words[1]; return tagbuf; } ValueTokenizer values(linebuf); @@ -93,12 +98,20 @@ void Ndx2Group::command(int narg, char **arg) if (narg == 1) { // restore all groups if (comm->me == 0) { - name = find_section(fp,""); - while (!name.empty()) { + try { + name = find_section(fp,""); + } catch (std::exception &e) { + error->one(FLERR, e.what()); + } + while (!name.empty()) { // skip over group "all", which is called "System" in gromacs if (name == "System") { - name = find_section(fp,""); + try { + name = find_section(fp,""); + } catch (std::exception &e) { + error->one(FLERR, e.what()); + } continue; } @@ -109,7 +122,12 @@ void Ndx2Group::command(int narg, char **arg) MPI_Bcast((void *)name.c_str(),len,MPI_CHAR,0,world); // read tags for atoms in group and broadcast - std::vector tags = read_section(fp,next); + std::vector tags; + try { + tags = read_section(fp,next); + } catch (std::exception &e) { + error->one(FLERR, e.what()); + } num = tags.size(); MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); MPI_Bcast((void *)tags.data(),num,MPI_LMP_TAGINT,0,world); @@ -145,7 +163,11 @@ void Ndx2Group::command(int narg, char **arg) // find named section, search from beginning of file rewind(fp); - name = find_section(fp,arg[idx]); + try { + name = find_section(fp,arg[idx]); + } catch (std::exception &e) { + error->one(FLERR, e.what()); + } utils::logmesg(lmp," {} group '{}'\n", name.size() ? "Processing" : "Skipping",arg[idx]); len = name.size()+1; @@ -154,7 +176,12 @@ void Ndx2Group::command(int narg, char **arg) MPI_Bcast((void *)name.c_str(),len,MPI_CHAR,0,world); // read tags for atoms in group and broadcast - std::vector tags = read_section(fp,next); + std::vector tags; + try { + tags = read_section(fp,next); + } catch (std::exception &e) { + error->one(FLERR, e.what()); + } num = tags.size(); MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); MPI_Bcast((void *)tags.data(),num,MPI_LMP_TAGINT,0,world); From c867bb3e28eabea1f803c9d0ae205b7d561f05f5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 17 Jun 2024 17:00:00 -0400 Subject: [PATCH 5/6] enable and apply clang-format --- src/EXTRA-COMMAND/group_ndx.cpp | 79 ++++++++++++------------- src/EXTRA-COMMAND/ndx_group.cpp | 102 +++++++++++++++----------------- 2 files changed, 87 insertions(+), 94 deletions(-) diff --git a/src/EXTRA-COMMAND/group_ndx.cpp b/src/EXTRA-COMMAND/group_ndx.cpp index 05a50a1596..cf6b13cb27 100644 --- a/src/EXTRA-COMMAND/group_ndx.cpp +++ b/src/EXTRA-COMMAND/group_ndx.cpp @@ -1,4 +1,3 @@ -// clang-format off // -*- c++ -*- /* ---------------------------------------------------------------------- @@ -34,12 +33,15 @@ using namespace LAMMPS_NS; static int cmptagint(const void *p1, const void *p2) { - const tagint i1 = * static_cast(p1); - const tagint i2 = * static_cast(p2); - if (i1 == i2) return 0; + const tagint i1 = *static_cast(p1); + const tagint i2 = *static_cast(p2); + if (i1 == i2) + return 0; else { - if (i1 < i2) return -1; - else return 1; + if (i1 < i2) + return -1; + else + return 1; } } @@ -49,27 +51,24 @@ void Group2Ndx::command(int narg, char **arg) { FILE *fp = nullptr; - if (narg < 1) error->all(FLERR,"Illegal group2ndx command"); + if (narg < 1) utils::missing_cmd_args(FLERR, "group2ndx", error); - if (atom->tag_enable == 0) - error->all(FLERR,"Must have atom IDs for group2ndx command"); + if (atom->tag_enable == 0) error->all(FLERR, "Must have atom IDs for group2ndx command"); if (comm->me == 0) { fp = fopen(arg[0], "w"); if (fp == nullptr) - error->one(FLERR,"Cannot open index file for writing: {}", utils::getsyserror()); - utils::logmesg(lmp,"Writing groups to index file {}:\n",arg[0]); + error->one(FLERR, "Cannot open index file for writing: {}", utils::getsyserror()); + utils::logmesg(lmp, "Writing groups to index file {}:\n", arg[0]); } - if (narg == 1) { // write out all groups - for (int i=0; i < group->ngroup; ++i) { - write_group(fp,i); - } - } else { // write only selected groups - for (int i=1; i < narg; ++i) { + if (narg == 1) { // write out all groups + for (int i = 0; i < group->ngroup; ++i) { write_group(fp, i); } + } else { // write only selected groups + for (int i = 1; i < narg; ++i) { int gid = group->find(arg[i]); if (gid < 0) error->all(FLERR, "Non-existing group requested"); - write_group(fp,gid); + write_group(fp, gid); } } @@ -86,21 +85,21 @@ void Group2Ndx::write_group(FILE *fp, int gid) int lnum, width, cols; if (fp) { - utils::logmesg(lmp," writing group {}...",group->names[gid]); + utils::logmesg(lmp, " writing group {}...", group->names[gid]); // the "all" group in LAMMPS is called "System" in Gromacs if (gid == 0) { fputs("[ System ]\n", fp); } else { - fmt::print(fp,"[ {} ]\n", group->names[gid]); + fmt::print(fp, "[ {} ]\n", group->names[gid]); } - width = log10((double) atom->natoms)+2; + width = log10((double) atom->natoms) + 2; cols = 80 / width; } if (gcount > 0) { - const int * const mask = atom->mask; - const tagint * const tag = atom->tag; + const int *const mask = atom->mask; + const tagint *const tag = atom->tag; const int groupbit = group->bitmask[gid]; const int nlocal = atom->nlocal; int i; @@ -111,45 +110,45 @@ void Group2Ndx::write_group(FILE *fp, int gid) for (i = 0; i < nlocal; i++) if (mask[i] & groupbit) sendlist[lnum++] = tag[i]; - int nrecv=0; + int nrecv = 0; bigint allrecv; if (comm->me == 0) { MPI_Status status; MPI_Request request; - for (i=0; i < lnum; i++) - recvlist[i] = sendlist[i]; + for (i = 0; i < lnum; i++) recvlist[i] = sendlist[i]; allrecv = lnum; - for (i=1; i < comm->nprocs; ++i) { - MPI_Irecv(recvlist+allrecv,gcount-allrecv,MPI_LMP_TAGINT,i,0, world,&request); - MPI_Send(&nrecv,0,MPI_INT,i,0,world); // block rank "i" until we are ready to receive - MPI_Wait(&request,&status); - MPI_Get_count(&status,MPI_LMP_TAGINT,&nrecv); + for (i = 1; i < comm->nprocs; ++i) { + MPI_Irecv(recvlist + allrecv, gcount - allrecv, MPI_LMP_TAGINT, i, 0, world, &request); + // block rank "i" until we are ready to receive + MPI_Send(&nrecv, 0, MPI_INT, i, 0, world); + MPI_Wait(&request, &status); + MPI_Get_count(&status, MPI_LMP_TAGINT, &nrecv); allrecv += nrecv; } // sort received list - qsort((void *)recvlist, allrecv, sizeof(tagint), cmptagint); + qsort((void *) recvlist, allrecv, sizeof(tagint), cmptagint); } else { - MPI_Recv(&nrecv,0,MPI_INT,0,0,world,MPI_STATUS_IGNORE); - MPI_Rsend(sendlist,lnum,MPI_LMP_TAGINT,0,0,world); + MPI_Recv(&nrecv, 0, MPI_INT, 0, 0, world, MPI_STATUS_IGNORE); + MPI_Rsend(sendlist, lnum, MPI_LMP_TAGINT, 0, 0, world); } - delete [] sendlist; + delete[] sendlist; } if (fp) { int i, j; - for (i=0, j=0; i < gcount; ++i) { - fmt::print(fp,"{:>{}}",recvlist[i],width); + for (i = 0, j = 0; i < gcount; ++i) { + fmt::print(fp, "{:>{}}", recvlist[i], width); ++j; if (j == cols) { - fputs("\n",fp); + fputs("\n", fp); j = 0; } } - if (j > 0) fputs("\n",fp); - utils::logmesg(lmp,"done\n"); + if (j > 0) fputs("\n", fp); + utils::logmesg(lmp, "done\n"); } if (gcount > 0) delete[] recvlist; } diff --git a/src/EXTRA-COMMAND/ndx_group.cpp b/src/EXTRA-COMMAND/ndx_group.cpp index 52721f9628..c5b0d3cf8a 100644 --- a/src/EXTRA-COMMAND/ndx_group.cpp +++ b/src/EXTRA-COMMAND/ndx_group.cpp @@ -1,4 +1,3 @@ -// clang-format off // -*- c++ -*- /* ---------------------------------------------------------------------- @@ -34,17 +33,16 @@ static std::string find_section(FILE *fp, const std::string &name) { char linebuf[BUFLEN]; - fgets(linebuf,BUFLEN,fp); + fgets(linebuf, BUFLEN, fp); while (!feof(fp)) { if (utils::strmatch(linebuf, "^\\s*\\[.*\\]\\s*$")) { auto words = Tokenizer(linebuf).as_vector(); if (words.size() != 3) throw TokenizerException("Invalid group name in index file", - utils::trim(utils::strfind(linebuf,"[^\\[^\\]]+"))); - if (name.empty() || (name == words[1])) - return words[1]; + utils::trim(utils::strfind(linebuf, "[^\\[^\\]]+"))); + if (name.empty() || (name == words[1])) return words[1]; } - fgets(linebuf,BUFLEN,fp); + fgets(linebuf, BUFLEN, fp); } return ""; } @@ -54,19 +52,18 @@ static std::vector read_section(FILE *fp, std::string &name) char linebuf[BUFLEN]; std::vector tagbuf; - while (fgets(linebuf,BUFLEN,fp)) { + while (fgets(linebuf, BUFLEN, fp)) { // start of new section. we are done, update "name" if (utils::strmatch(linebuf, "^\\s*\\[.*\\]\\s*$")) { auto words = Tokenizer(linebuf).as_vector(); if (words.size() != 3) throw TokenizerException("Invalid group name in index file", - utils::trim(utils::strfind(linebuf,"[^\\[^\\]]+"))); + utils::trim(utils::strfind(linebuf, "[^\\[^\\]]+"))); name = words[1]; return tagbuf; } ValueTokenizer values(linebuf); - while (values.has_next()) - tagbuf.push_back(values.next_tagint()); + while (values.has_next()) tagbuf.push_back(values.next_tagint()); } // set empty name to indicate end of file name = ""; @@ -82,24 +79,22 @@ void Ndx2Group::command(int narg, char **arg) FILE *fp; std::string name, next; - if (narg < 1) error->all(FLERR,"Illegal ndx2group command"); - if (atom->tag_enable == 0) - error->all(FLERR,"Must have atom IDs for ndx2group command"); + if (narg < 1) utils::missing_cmd_args(FLERR, "ndx2group", error); + if (atom->tag_enable == 0) error->all(FLERR, "Must have atom IDs for ndx2group command"); if (atom->map_style == Atom::MAP_NONE) - error->all(FLERR,"Must have an atom map for ndx2group command"); + error->all(FLERR, "Must have an atom map for ndx2group command"); if (comm->me == 0) { fp = fopen(arg[0], "r"); if (fp == nullptr) - error->one(FLERR,"Cannot open index file for reading: {}", - utils::getsyserror()); - utils::logmesg(lmp,"Reading groups from index file {}:\n",arg[0]); + error->one(FLERR, "Cannot open index file for reading: {}", utils::getsyserror()); + utils::logmesg(lmp, "Reading groups from index file {}:\n", arg[0]); } - if (narg == 1) { // restore all groups + if (narg == 1) { // restore all groups if (comm->me == 0) { try { - name = find_section(fp,""); + name = find_section(fp, ""); } catch (std::exception &e) { error->one(FLERR, e.what()); } @@ -108,95 +103,94 @@ void Ndx2Group::command(int narg, char **arg) // skip over group "all", which is called "System" in gromacs if (name == "System") { try { - name = find_section(fp,""); + name = find_section(fp, ""); } catch (std::exception &e) { error->one(FLERR, e.what()); } continue; } - utils::logmesg(lmp," Processing group '{}'\n",name); - len = name.size()+1; - MPI_Bcast(&len,1,MPI_INT,0,world); + utils::logmesg(lmp, " Processing group '{}'\n", name); + len = name.size() + 1; + MPI_Bcast(&len, 1, MPI_INT, 0, world); if (len > 1) { - MPI_Bcast((void *)name.c_str(),len,MPI_CHAR,0,world); + MPI_Bcast((void *) name.c_str(), len, MPI_CHAR, 0, world); // read tags for atoms in group and broadcast std::vector tags; try { - tags = read_section(fp,next); + tags = read_section(fp, next); } catch (std::exception &e) { error->one(FLERR, e.what()); } num = tags.size(); - MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); - MPI_Bcast((void *)tags.data(),num,MPI_LMP_TAGINT,0,world); - create(name,tags); + MPI_Bcast(&num, 1, MPI_LMP_BIGINT, 0, world); + MPI_Bcast((void *) tags.data(), num, MPI_LMP_TAGINT, 0, world); + create(name, tags); name = next; } } len = -1; - MPI_Bcast(&len,1,MPI_INT,0,world); + MPI_Bcast(&len, 1, MPI_INT, 0, world); } else { while (true) { - MPI_Bcast(&len,1,MPI_INT,0,world); + MPI_Bcast(&len, 1, MPI_INT, 0, world); if (len < 0) break; if (len > 1) { char *buf = new char[len]; - MPI_Bcast(buf,len,MPI_CHAR,0,world); - MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); + MPI_Bcast(buf, len, MPI_CHAR, 0, world); + MPI_Bcast(&num, 1, MPI_LMP_BIGINT, 0, world); tagint *tbuf = new tagint[num]; - MPI_Bcast(tbuf,num,MPI_LMP_TAGINT,0,world); - create(buf,std::vector(tbuf,tbuf+num)); + MPI_Bcast(tbuf, num, MPI_LMP_TAGINT, 0, world); + create(buf, std::vector(tbuf, tbuf + num)); delete[] buf; delete[] tbuf; } } } - } else { // restore selected groups + } else { // restore selected groups - for (int idx=1; idx < narg; ++idx) { + for (int idx = 1; idx < narg; ++idx) { if (comm->me == 0) { // find named section, search from beginning of file rewind(fp); try { - name = find_section(fp,arg[idx]); + name = find_section(fp, arg[idx]); } catch (std::exception &e) { error->one(FLERR, e.what()); } - utils::logmesg(lmp," {} group '{}'\n", name.size() - ? "Processing" : "Skipping",arg[idx]); - len = name.size()+1; - MPI_Bcast(&len,1,MPI_INT,0,world); + utils::logmesg(lmp, " {} group '{}'\n", name.size() ? "Processing" : "Skipping", arg[idx]); + len = name.size() + 1; + MPI_Bcast(&len, 1, MPI_INT, 0, world); if (len > 1) { - MPI_Bcast((void *)name.c_str(),len,MPI_CHAR,0,world); + MPI_Bcast((void *) name.c_str(), len, MPI_CHAR, 0, world); // read tags for atoms in group and broadcast std::vector tags; try { - tags = read_section(fp,next); + tags = read_section(fp, next); } catch (std::exception &e) { error->one(FLERR, e.what()); } num = tags.size(); - MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); - MPI_Bcast((void *)tags.data(),num,MPI_LMP_TAGINT,0,world); - create(name,tags); + MPI_Bcast(&num, 1, MPI_LMP_BIGINT, 0, world); + MPI_Bcast((void *) tags.data(), num, MPI_LMP_TAGINT, 0, world); + create(name, tags); name = next; } } else { - MPI_Bcast(&len,1,MPI_INT,0,world); + MPI_Bcast(&len, 1, MPI_INT, 0, world); if (len > 1) { char *buf = new char[len]; - MPI_Bcast(buf,len,MPI_CHAR,0,world); - MPI_Bcast(&num,1,MPI_LMP_BIGINT,0,world); + MPI_Bcast(buf, len, MPI_CHAR, 0, world); + MPI_Bcast(&num, 1, MPI_LMP_BIGINT, 0, world); tagint *tbuf = new tagint[num]; - MPI_Bcast(tbuf,num,MPI_LMP_TAGINT,0,world); - create(buf,std::vector(tbuf,tbuf+num)); + MPI_Bcast(tbuf, num, MPI_LMP_TAGINT, 0, world); + create(buf, std::vector(tbuf, tbuf + num)); delete[] buf; delete[] tbuf; } @@ -216,11 +210,11 @@ void Ndx2Group::create(const std::string &name, const std::vector &tags) // map from global to local const int nlocal = atom->nlocal; - int *flags = (int *)calloc(nlocal,sizeof(int)); - for (bigint i=0; i < (int)tags.size(); ++i) { + int *flags = (int *) calloc(nlocal, sizeof(int)); + for (bigint i = 0; i < (int) tags.size(); ++i) { const int id = atom->map(tags[i]); if (id < nlocal && id >= 0) flags[id] = 1; } - group->create(name,flags); + group->create(name, flags); free(flags); } From ab800b4e26600218fa9bb289e78a6d4cc91d46c5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 17 Jun 2024 17:23:05 -0400 Subject: [PATCH 6/6] skip over groups with whitepsace in their name so we don't create illegal index files --- src/EXTRA-COMMAND/group_ndx.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/EXTRA-COMMAND/group_ndx.cpp b/src/EXTRA-COMMAND/group_ndx.cpp index cf6b13cb27..1dc0d3af97 100644 --- a/src/EXTRA-COMMAND/group_ndx.cpp +++ b/src/EXTRA-COMMAND/group_ndx.cpp @@ -84,6 +84,10 @@ void Group2Ndx::write_group(FILE *fp, int gid) bigint gcount = group->count(gid); int lnum, width, cols; + if (utils::strmatch(group->names[gid], "\\s+")) { + if (fp) utils::logmesg(lmp, " skipping group {}...done", group->names[gid]); + return; + } if (fp) { utils::logmesg(lmp, " writing group {}...", group->names[gid]);