From ca8b268ad5362d66d67c538c076c5bdcbaefbe8c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:01:51 -0500 Subject: [PATCH 01/22] new convencience function for checking valid IDs (includes unit tests) --- src/utils.cpp | 20 +++++++++++-- src/utils.h | 8 +++++ unittest/utils/test_utils.cpp | 55 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/utils.cpp b/src/utils.cpp index d9320f464e..7bb157afe5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -851,7 +851,7 @@ std::vector utils::split_words(const std::string &text) ------------------------------------------------------------------------- */ bool utils::is_integer(const std::string &str) { - if (str.size() == 0) { + if (str.empty()) { return false; } @@ -867,7 +867,7 @@ bool utils::is_integer(const std::string &str) { ------------------------------------------------------------------------- */ bool utils::is_double(const std::string &str) { - if (str.size() == 0) { + if (str.empty()) { return false; } @@ -880,6 +880,22 @@ bool utils::is_double(const std::string &str) { return true; } +/* ---------------------------------------------------------------------- + Return whether string is a valid ID string +------------------------------------------------------------------------- */ + +bool utils::is_id(const std::string &str) { + if (str.empty()) { + return false; + } + + for (auto c : str) { + if (isalnum(c) || (c == '_')) continue; + return false; + } + return true; +} + /* ---------------------------------------------------------------------- strip off leading part of path, return just the filename ------------------------------------------------------------------------- */ diff --git a/src/utils.h b/src/utils.h index 70a4c08cd3..eab81f1343 100644 --- a/src/utils.h +++ b/src/utils.h @@ -335,6 +335,14 @@ namespace LAMMPS_NS { bool is_double(const std::string &str); + /** Check if string is a valid ID + * ID strings may contain only letters, numbers, and underscores. + * + * \param str string that should be checked + * \return true, if string contains valid id, false otherwise */ + + bool is_id(const std::string &str); + /** Try to detect pathname from FILE pointer. * * Currently only supported on Linux, otherwise will report "(unknown)". diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index a0e4022d2c..5b509318c9 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -287,6 +287,61 @@ TEST(Utils, signed_double_and_d_exponential) ASSERT_FALSE(utils::is_double("-10D-22")); } +TEST(Utils, valid_id1) +{ + ASSERT_TRUE(utils::is_id("abc")); +} + +TEST(Utils, valid_id2) +{ + ASSERT_TRUE(utils::is_id("123")); +} + +TEST(Utils, valid_id3) +{ + ASSERT_TRUE(utils::is_id("abc123")); +} + +TEST(Utils, valid_id4) +{ + ASSERT_TRUE(utils::is_id("abc_123")); +} + +TEST(Utils, valid_id5) +{ + ASSERT_TRUE(utils::is_id("123_abc")); +} + +TEST(Utils, valid_id6) +{ + ASSERT_TRUE(utils::is_id("_123")); +} + +TEST(Utils, valid_id7) +{ + ASSERT_TRUE(utils::is_id("___")); +} + +TEST(Utils, invalid_id1) +{ + ASSERT_FALSE(utils::is_id("+abc")); +} + +TEST(Utils, invalid_id2) +{ + ASSERT_FALSE(utils::is_id("a[1]")); +} + +TEST(Utils, invalid_id3) +{ + ASSERT_FALSE(utils::is_id("b(c)")); +} + +TEST(Utils, invalid_id4) +{ + ASSERT_FALSE(utils::is_id("a$12")); +} + TEST(Utils, strmatch_beg) { ASSERT_TRUE(utils::strmatch("rigid/small/omp", "^rigid")); From e09f4b6e7ab8845afba7000221f40b35652dca9f Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:03:17 -0500 Subject: [PATCH 02/22] simplify checking for valid molecule ID --- src/molecule.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 5d71db68e6..c10cd79754 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -55,14 +55,10 @@ Molecule::Molecule(LAMMPS *lmp, int narg, char **arg, int &index) : if (index >= narg) error->all(FLERR,"Illegal molecule command"); - int n = strlen(arg[0]) + 1; - id = new char[n]; - strcpy(id,arg[0]); - - for (int i = 0; i < n-1; i++) - if (!isalnum(id[i]) && id[i] != '_') - error->all(FLERR,"Molecule template ID must be " - "alphanumeric or underscore characters"); + id = utils::strdup(arg[0]); + if (!utils::is_id(id)) + error->all(FLERR,"Molecule template ID must be " + "alphanumeric or underscore characters"); // parse args until reach unknown arg (next file) From 2d96a01bb2234e37c501029dc7a81943aaa0bfb7 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:03:51 -0500 Subject: [PATCH 03/22] use trim functions in utils to remove whitespace and comments --- src/molecule.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index c10cd79754..a1884cb9b7 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -411,15 +411,14 @@ void Molecule::read(int flag) readline(line); - // trim anything from '#' onward - // if line is blank, continue + // trim comments. if line is blank, continue - if ((ptr = strchr(line,'#'))) *ptr = '\0'; - if (strspn(line," \t\n\r") == strlen(line)) continue; + auto text = utils::trim(utils::trim_comment(line)); + if (text.empty()) continue; // search line for header keywords and set corresponding variable try { - ValueTokenizer values(line); + ValueTokenizer values(text); int nmatch = values.count(); int nwant = 0; From e941d0fd4a7f67b722f497df7277ee976f790ef9 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:04:17 -0500 Subject: [PATCH 04/22] simplify parsing of keywords --- src/molecule.cpp | 97 +++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index a1884cb9b7..a780fc4190 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -507,98 +507,98 @@ void Molecule::read(int flag) // grab keyword and skip next line - parse_keyword(0,line,keyword); + std::string keyword = parse_keyword(0,line); readline(line); // loop over sections of molecule file - while (strlen(keyword) > 0) { - if (strcmp(keyword,"Coords") == 0) { + while (!keyword.empty()) { + if (keyword == "Coords") { xflag = 1; if (flag) coords(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Types") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Types") { typeflag = 1; if (flag) types(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Molecules") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Molecules") { moleculeflag = 1; if (flag) molecules(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Fragments") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Fragments") { if (nfragments == 0) error->all(FLERR,"Molecule file has fragments but no nfragments setting"); fragmentflag = 1; if (flag) fragments(line); - else skip_lines(nfragments,line); - } else if (strcmp(keyword,"Charges") == 0) { + else skip_lines(nfragments,line,keyword); + } else if (keyword == "Charges") { qflag = 1; if (flag) charges(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Diameters") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Diameters") { radiusflag = 1; if (flag) diameters(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Masses") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Masses") { rmassflag = 1; if (flag) masses(line); - else skip_lines(natoms,line); + else skip_lines(natoms,line,keyword); - } else if (strcmp(keyword,"Bonds") == 0) { + } else if (keyword == "Bonds") { if (nbonds == 0) error->all(FLERR,"Molecule file has bonds but no nbonds setting"); bondflag = tag_require = 1; bonds(flag,line); - } else if (strcmp(keyword,"Angles") == 0) { + } else if (keyword == "Angles") { if (nangles == 0) error->all(FLERR,"Molecule file has angles but no nangles setting"); angleflag = tag_require = 1; angles(flag,line); - } else if (strcmp(keyword,"Dihedrals") == 0) { + } else if (keyword == "Dihedrals") { if (ndihedrals == 0) error->all(FLERR,"Molecule file has dihedrals " "but no ndihedrals setting"); dihedralflag = tag_require = 1; dihedrals(flag,line); - } else if (strcmp(keyword,"Impropers") == 0) { + } else if (keyword == "Impropers") { if (nimpropers == 0) error->all(FLERR,"Molecule file has impropers " "but no nimpropers setting"); improperflag = tag_require = 1; impropers(flag,line); - } else if (strcmp(keyword,"Special Bond Counts") == 0) { + } else if (keyword == "Special Bond Counts") { nspecialflag = 1; nspecial_read(flag,line); - } else if (strcmp(keyword,"Special Bonds") == 0) { + } else if (keyword == "Special Bonds") { specialflag = tag_require = 1; if (flag) special_read(line); - else skip_lines(natoms,line); + else skip_lines(natoms,line,keyword); - } else if (strcmp(keyword,"Shake Flags") == 0) { + } else if (keyword == "Shake Flags") { shakeflagflag = 1; if (flag) shakeflag_read(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Shake Atoms") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Shake Atoms") { shakeatomflag = tag_require = 1; if (shaketypeflag) shakeflag = 1; if (!shakeflagflag) error->all(FLERR,"Molecule file shake flags not before shake atoms"); if (flag) shakeatom_read(line); - else skip_lines(natoms,line); - } else if (strcmp(keyword,"Shake Bond Types") == 0) { + else skip_lines(natoms,line,keyword); + } else if (keyword == "Shake Bond Types") { shaketypeflag = 1; if (shakeatomflag) shakeflag = 1; if (!shakeflagflag) error->all(FLERR,"Molecule file shake flags not before shake bonds"); if (flag) shaketype_read(line); - else skip_lines(natoms,line); + else skip_lines(natoms,line,keyword); - } else if (strcmp(keyword,"Body Integers") == 0) { + } else if (keyword == "Body Integers") { if (bodyflag == 0 || nibody == 0) error->all(FLERR,"Molecule file has body params " "but no setting for them"); ibodyflag = 1; body(flag,0,line); - } else if (strcmp(keyword,"Body Doubles") == 0) { + } else if (keyword == "Body Doubles") { if (bodyflag == 0 || ndbody == 0) error->all(FLERR,"Molecule file has body params " "but no setting for them"); @@ -608,7 +608,7 @@ void Molecule::read(int flag) } else error->one(FLERR,fmt::format("Unknown section '{}' in molecule " "file", keyword)); - parse_keyword(1,line,keyword); + keyword = parse_keyword(1,line); } // error check @@ -1968,8 +1968,9 @@ void Molecule::readline(char *line) flag = 1, line has already been read ------------------------------------------------------------------------- */ -void Molecule::parse_keyword(int flag, char *line, char *keyword) +std::string Molecule::parse_keyword(int flag, char *line) { + char line2[MAXLINE]; if (flag) { // read upto non-blank line plus 1 following line @@ -1981,42 +1982,38 @@ void Molecule::parse_keyword(int flag, char *line, char *keyword) while (eof == 0 && strspn(line," \t\n\r") == strlen(line)) { if (fgets(line,MAXLINE,fp) == nullptr) eof = 1; } - if (fgets(keyword,MAXLINE,fp) == nullptr) eof = 1; + if (fgets(line2,MAXLINE,fp) == nullptr) eof = 1; } // if eof, set keyword empty and return MPI_Bcast(&eof,1,MPI_INT,0,world); if (eof) { - keyword[0] = '\0'; - return; + return std::string(""); } // bcast keyword line to all procs - int n; - if (me == 0) n = strlen(line) + 1; - MPI_Bcast(&n,1,MPI_INT,0,world); - MPI_Bcast(line,n,MPI_CHAR,0,world); + MPI_Bcast(line,MAXLINE,MPI_CHAR,0,world); } - // copy non-whitespace and non-comment portion of line into keyword + // return non-whitespace and non-comment portion of line - int start = strspn(line," \t\n\r"); - int stop = strcspn(line,"#") - 1; - while (line[stop] == ' ' || line[stop] == '\t' - || line[stop] == '\n' || line[stop] == '\r') stop--; - line[stop+1] = '\0'; - strcpy(keyword,&line[start]); + return utils::trim(utils::trim_comment(line)); } /* ---------------------------------------------------------------------- - skip N lines of file + skip N lines of file. Check if non-numeric content (e.g. keyword). ------------------------------------------------------------------------- */ -void Molecule::skip_lines(int n, char *line) +void Molecule::skip_lines(int n, char *line, const std::string §ion) { - for (int i = 0; i < n; i++) readline(line); + for (int i = 0; i < n; i++) { + readline(line); + if (utils::strmatch(utils::trim(utils::trim_comment(line)),"^[A-Za-z ]+$")) + error->one(FLERR,fmt::format("Unexpected line in molecule file while " + "skipping {} section:\n{}",section,line)); + } } /* ---------------------------------------------------------------------- From 371ee63c2c9f53c80e1b90bb0396a3b5ef208db6 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:06:24 -0500 Subject: [PATCH 05/22] begin refactor of parsing of sections --- src/molecule.cpp | 71 ++++++++++++++++++++++++++---------------------- src/molecule.h | 4 +-- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index a780fc4190..7f28a682a7 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -393,8 +393,8 @@ void Molecule::compute_inertia() void Molecule::read(int flag) { - char line[MAXLINE],keyword[MAXLINE]; - char *eof,*ptr; + char line[MAXLINE]; + char *eof; // skip 1st line of file @@ -493,7 +493,7 @@ void Molecule::read(int flag) // error checks if (natoms < 1) - error->all(FLERR,"No count or invalid atom count in molecule file"); + error->all(FLERR,"No or invalid atom count in molecule file"); if (nbonds < 0) error->all(FLERR,"Invalid bond count in molecule file"); if (nangles < 0) error->all(FLERR,"Invalid angle count in molecule file"); if (ndihedrals < 0) @@ -673,11 +673,14 @@ void Molecule::coords(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); - if (values.count() != 4) error->one(FLERR,"Invalid Coords section in molecule file"); + ValueTokenizer values(utils::trim_comment(line)); + if (values.count() != 4) + error->one(FLERR,fmt::format("Invalid line in Coords section of " + "molecule file: {}",line)); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Coords section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->one(FLERR,"Invalid Coords section in molecule file"); count[iatom]++; x[iatom][0] = values.next_double(); x[iatom][1] = values.next_double(); @@ -688,8 +691,8 @@ void Molecule::coords(char *line) x[iatom][2] *= sizescale; } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Coords section in molecule file\n" - "{}", e.what())); + error->one(FLERR,fmt::format("Invalid line in Coords section of " + "molecule file: {}. {}",e.what(),line)); } for (int i = 0; i < natoms; i++) @@ -714,29 +717,31 @@ void Molecule::types(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); - if (values.count() != 2) error->one(FLERR,"Invalid Types section in molecule file"); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); + error->one(FLERR,fmt::format("Invalid line in Types section of " + "molecule file: {}",line)); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Types section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->one(FLERR,"Invalid Types section in molecule file"); count[iatom]++; type[iatom] = values.next_int(); type[iatom] += toffset; } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Types section in molecule file\n" - "{}", e.what())); + error->one(FLERR, fmt::format("Invalid line in Types section of " + "molecule file: {}. {}", e.what(),line)); } - for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Types section in molecule file"); + for (int i = 0; i < natoms; i++) { + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Types " + "section of molecule file",i)); - for (int i = 0; i < natoms; i++) if ((type[i] <= 0) || (domain->box_exist && (type[i] > atom->ntypes))) - error->all(FLERR,"Invalid atom type in molecule file"); + error->all(FLERR,fmt::format("Invalid atom type {} in molecule file",type[i])); - for (int i = 0; i < natoms; i++) ntypes = MAX(ntypes,type[i]); + } } /* ---------------------------------------------------------------------- @@ -750,7 +755,7 @@ void Molecule::molecules(char *line) try { for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 2) error->one(FLERR,"Invalid Molecules section in molecule file"); int iatom = values.next_int() - 1; @@ -785,7 +790,7 @@ void Molecule::fragments(char *line) for (int i = 0; i < nfragments; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if ((int)values.count() > natoms+1) error->one(FLERR,"Invalid atom ID in Fragments section of molecule file"); @@ -816,7 +821,7 @@ void Molecule::charges(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if ((int)values.count() != 2) error->one(FLERR,"Invalid Charges section in molecule file"); int iatom = values.next_int() - 1; @@ -845,7 +850,7 @@ void Molecule::diameters(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 2) error->one(FLERR,"Invalid Diameters section in molecule file"); int iatom = values.next_int() - 1; @@ -880,7 +885,7 @@ void Molecule::masses(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 2) error->one(FLERR,"Invalid Masses section in molecule file"); int iatom = values.next_int() - 1; @@ -924,7 +929,7 @@ void Molecule::bonds(int flag, char *line) readline(line); try { - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 4) error->one(FLERR,"Invalid Bonds section in molecule file"); values.next_int(); itype = values.next_int(); @@ -992,7 +997,7 @@ void Molecule::angles(int flag, char *line) readline(line); try { - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 5) error->one(FLERR,"Invalid Angles section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1076,7 +1081,7 @@ void Molecule::dihedrals(int flag, char *line) readline(line); try { - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 6) error->one(FLERR,"Invalid Dihedrals section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1175,7 +1180,7 @@ void Molecule::impropers(int flag, char *line) readline(line); try { - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 6) error->one(FLERR,"Invalid Impropers section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1268,7 +1273,7 @@ void Molecule::nspecial_read(int flag, char *line) int c1, c2, c3; try { - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 4) error->one(FLERR,"Invalid Special Bond Counts section in molecule file"); values.next_int(); c1 = values.next_tagint(); @@ -1297,7 +1302,7 @@ void Molecule::special_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); int nwords = values.count(); if (nwords != nspecial[i][2]+1) @@ -1435,7 +1440,7 @@ void Molecule::shakeflag_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); if (values.count() != 2) error->one(FLERR,"Invalid Shake Flags section in molecule file"); @@ -1464,7 +1469,7 @@ void Molecule::shakeatom_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); nmatch = values.count(); switch (shake_flag[i]) { @@ -1538,7 +1543,7 @@ void Molecule::shaketype_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); nmatch = values.count(); switch (shake_flag[i]) { @@ -1616,7 +1621,7 @@ void Molecule::body(int flag, int pflag, char *line) while (nword < nparam) { readline(line); - ValueTokenizer values(line); + ValueTokenizer values(utils::trim(utils::trim_comment(line))); int ncount = values.count(); if (ncount == 0) diff --git a/src/molecule.h b/src/molecule.h index 3316cd3f1f..f744a438bc 100644 --- a/src/molecule.h +++ b/src/molecule.h @@ -163,8 +163,8 @@ class Molecule : protected Pointers { void open(char *); void readline(char *); - void parse_keyword(int, char *, char *); - void skip_lines(int, char *); + std::string parse_keyword(int, char *); + void skip_lines(int, char *, const std::string &); // void print(); }; From 01a32b67b0e057fe1625cee5f5044512f68d38c5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:07:03 -0500 Subject: [PATCH 06/22] add minimal unit test program for molecule files --- unittest/formats/CMakeLists.txt | 4 + unittest/formats/test_molecule_file.cpp | 129 ++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 unittest/formats/test_molecule_file.cpp diff --git a/unittest/formats/CMakeLists.txt b/unittest/formats/CMakeLists.txt index 3c1ed66c06..7b96cf2d32 100644 --- a/unittest/formats/CMakeLists.txt +++ b/unittest/formats/CMakeLists.txt @@ -7,6 +7,10 @@ add_executable(test_image_flags test_image_flags.cpp) target_link_libraries(test_image_flags PRIVATE lammps GTest::GMock GTest::GTest) add_test(NAME ImageFlags COMMAND test_image_flags WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +add_executable(test_molecule_file test_molecule_file.cpp) +target_link_libraries(test_molecule_file PRIVATE lammps GTest::GMock GTest::GTest) +add_test(NAME MoleculeFile COMMAND test_molecule_file WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + add_executable(test_pair_unit_convert test_pair_unit_convert.cpp) target_link_libraries(test_pair_unit_convert PRIVATE lammps GTest::GMock GTest::GTest) add_test(NAME PairUnitConvert COMMAND test_pair_unit_convert WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp new file mode 100644 index 0000000000..0b81178145 --- /dev/null +++ b/unittest/formats/test_molecule_file.cpp @@ -0,0 +1,129 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://lammps.sandia.gov/, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include "info.h" +#include "input.h" +#include "lammps.h" +#include "molecule.h" +#include "utils.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include +#include +#include +#include + +using namespace LAMMPS_NS; + +using testing::MatchesRegex; +using testing::StrEq; + +using utils::split_words; + +#if defined(OMPI_MAJOR_VERSION) +const bool have_openmpi = true; +#else +const bool have_openmpi = false; +#endif + +#define TEST_FAILURE(errmsg, ...) \ + if (Info::has_exceptions()) { \ + ::testing::internal::CaptureStdout(); \ + ASSERT_ANY_THROW({__VA_ARGS__}); \ + auto mesg = ::testing::internal::GetCapturedStdout(); \ + ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ + } else { \ + if (!have_openmpi) { \ + ::testing::internal::CaptureStdout(); \ + ASSERT_DEATH({__VA_ARGS__}, ""); \ + auto mesg = ::testing::internal::GetCapturedStdout(); \ + ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ + } \ + } + +// whether to print verbose output (i.e. not capturing LAMMPS screen output). +bool verbose = false; + +class MoleculeFileTest : public ::testing::Test { +protected: + LAMMPS *lmp; + + void SetUp() override + { + const char *args[] = {"MoleculeFileTest", "-log", "none", "-echo", "screen", "-nocite"}; + char **argv = (char **)args; + int argc = sizeof(args) / sizeof(char *); + if (!verbose) ::testing::internal::CaptureStdout(); + lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); + if (!verbose) ::testing::internal::GetCapturedStdout(); + ASSERT_NE(lmp, nullptr); + } + + void TearDown() override + { + if (!verbose) ::testing::internal::CaptureStdout(); + delete lmp; + if (!verbose) ::testing::internal::GetCapturedStdout(); + } + + void run_mol_cmd(const std::string &name, const std::string &args, const std::string &content) + { + std::string file = name + ".mol"; + std::ofstream mol(file, std::ofstream::trunc); + mol << content << std::endl; + mol.close(); + + lmp->input->one(fmt::format("molecule {} {} {}",name,file,args)); + remove(file.c_str()); + } +}; + +TEST_F(MoleculeFileTest, minimal) +{ + ::testing::internal::CaptureStdout(); + run_mol_cmd(test_info_->name(),"","Comment\n" + "1 atoms\n\n" + " Coords\n\n" + " 1 0.0 0.0 0.0\n"); + auto output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules" + ".*1 atoms.*0 bonds.*0. angles.*")); +} + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + if (have_openmpi && !LAMMPS_NS::Info::has_exceptions()) + std::cout << "Warning: using OpenMPI without exceptions. " + "Death tests will be skipped\n"; + + // handle arguments passed via environment variable + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = split_words(var); + for (auto arg : env) { + if (arg == "-v") { + verbose = true; + } + } + } + + if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; + + int rv = RUN_ALL_TESTS(); + MPI_Finalize(); + return rv; +} From 861ad834c6fd925750ca6f5a9489b7657922a9c8 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:22:43 -0500 Subject: [PATCH 07/22] fix typo --- unittest/formats/test_molecule_file.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index 0b81178145..5bfb8ff061 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -98,8 +98,7 @@ TEST_F(MoleculeFileTest, minimal) " 1 0.0 0.0 0.0\n"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules" - ".*1 atoms.*0 bonds.*0. angles.*")); + ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*1 atoms.*0 bonds.*")); } int main(int argc, char **argv) From 60694b2a94e51f5da32b793087915051ec646e79 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 11:59:54 -0500 Subject: [PATCH 08/22] better error check and error message when looking for section headers --- src/molecule.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 7f28a682a7..e25c10b3fb 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -604,10 +604,17 @@ void Molecule::read(int flag) "but no setting for them"); dbodyflag = 1; body(flag,1,line); + } else { - } else error->one(FLERR,fmt::format("Unknown section '{}' in molecule " - "file", keyword)); + // Error: Either a too long/short section or a typo in the keyword + if (utils::strmatch(keyword,"^[A-Za-z ]+$")) + error->one(FLERR,fmt::format("Unknown section '{}' in molecule " + "file\n",keyword)); + else error->one(FLERR,fmt::format("Unexpected line in molecule file " + "while looking for the next " + "section:\n{}",line)); + } keyword = parse_keyword(1,line); } From e4ce1de66bde2656d0d26c84076205f26d0bab12 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 12:16:12 -0500 Subject: [PATCH 09/22] add "death tests" for no molecule file and molecule file w/o atoms --- unittest/formats/test_molecule_file.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index 5bfb8ff061..b31b7e6738 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -31,6 +31,8 @@ using testing::StrEq; using utils::split_words; +#define test_name test_info_->name() + #if defined(OMPI_MAJOR_VERSION) const bool have_openmpi = true; #else @@ -89,13 +91,23 @@ protected: } }; +TEST_F(MoleculeFileTest, nofile) +{ + TEST_FAILURE(".*Cannot open molecule file nofile.mol.*", + lmp->input->one("molecule 1 nofile.mol");); +} + +TEST_F(MoleculeFileTest, noatom) +{ + TEST_FAILURE(".*ERROR: No or invalid atom count in molecule file.*", + run_mol_cmd(test_name,"","Comment\n0 atoms\n1 bonds\n\n" + " Coords\n\nBonds\n\n 1 1 2\n");); +} + TEST_F(MoleculeFileTest, minimal) { ::testing::internal::CaptureStdout(); - run_mol_cmd(test_info_->name(),"","Comment\n" - "1 atoms\n\n" - " Coords\n\n" - " 1 0.0 0.0 0.0\n"); + run_mol_cmd(test_name,"","Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*1 atoms.*0 bonds.*")); From eecc85659d27a367b0a34b2c6cb6970d44b9a97e Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 12:43:06 -0500 Subject: [PATCH 10/22] add missing line --- src/molecule.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/molecule.cpp b/src/molecule.cpp index e25c10b3fb..58d6bc9cc4 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -725,6 +725,7 @@ void Molecule::types(char *line) readline(line); ValueTokenizer values(utils::trim(utils::trim_comment(line))); + if (values.count() != 2) error->one(FLERR,fmt::format("Invalid line in Types section of " "molecule file: {}",line)); From e3942a0d48a34190309eb9d9fc0661547e38abc4 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 12:52:44 -0500 Subject: [PATCH 11/22] update test to use stdio consistently --- unittest/formats/test_molecule_file.cpp | 46 ++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index b31b7e6738..ea6a6ef544 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -21,7 +21,6 @@ #include #include -#include #include using namespace LAMMPS_NS; @@ -54,6 +53,45 @@ const bool have_openmpi = false; } \ } +static void create_molecule_files() +{ + // create molecule files + const char h2o_file[] = "# Water molecule. SPC/E model.\n\n3 atoms\n2 bonds\n1 angles\n\n" + "Coords\n\n1 1.12456 0.09298 1.27452\n" + "2 1.53683 0.75606 1.89928\n3 0.49482 0.56390 0.65678\n\n" + "Types\n\n1 1\n2 2\n3 2\n\n" + "Charges\n\n1 -0.8472\n2 0.4236\n3 0.4236\n\n" + "Bonds\n\n1 1 1 2\n2 1 1 3\n\n" + "Angles\n\n1 1 2 1 3\n\n" + "Shake Flags\n\n1 1\n2 1\n3 1\n\n" + "Shake Atoms\n\n1 1 2 3\n2 1 2 3\n3 1 2 3\n\n" + "Shake Bond Types\n\n1 1 1 1\n2 1 1 1\n3 1 1 1\n\n" + "Special Bond Counts\n\n1 2 0 0\n2 1 1 0\n3 1 1 0\n\n" + "Special Bonds\n\n1 2 3\n2 1 3\n3 1 2\n\n"; + const char co2_file[] = "# CO2 molecule file. TraPPE model.\n\n" + "3 atoms\n2 bonds\n1 angles\n\n" + "Coords\n\n1 0.0 0.0 0.0\n2 -1.16 0.0 0.0\n3 1.16 0.0 0.0\n\n" + "Types\n\n1 1\n2 2\n3 2\n\n" + "Charges\n\n1 0.7\n2 -0.35\n3 -0.35\n\n" + "Bonds\n\n1 1 1 2\n2 1 1 3\n\n" + "Angles\n\n1 1 2 1 3\n\n" + "Special Bond Counts\n\n1 2 0 0\n2 1 1 0\n3 1 1 0\n\n" + "Special Bonds\n\n1 2 3\n2 1 3\n3 1 2\n\n"; + + FILE *fp = fopen("tmp.h2o.mol", "w"); + if (fp) { + fputs(h2o_file, fp); + fclose(fp); + } + rename("tmp.h2o.mol", "h2o.mol"); + fp = fopen("tmp.co2.mol", "w"); + if (fp) { + fputs(co2_file, fp); + fclose(fp); + } + rename("tmp.co2.mol", "co2.mol"); +} + // whether to print verbose output (i.e. not capturing LAMMPS screen output). bool verbose = false; @@ -82,9 +120,9 @@ protected: void run_mol_cmd(const std::string &name, const std::string &args, const std::string &content) { std::string file = name + ".mol"; - std::ofstream mol(file, std::ofstream::trunc); - mol << content << std::endl; - mol.close(); + FILE *fp = fopen(file.c_str(), "w"); + fputs(content.c_str(),fp); + fclose(fp); lmp->input->one(fmt::format("molecule {} {} {}",name,file,args)); remove(file.c_str()); From d6dbdfdbe64f2083f98064afe9c88afbc9a84e09 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 15:53:17 -0500 Subject: [PATCH 12/22] detect and warn about unknown header keywords (instead of error out on an empty line) --- src/molecule.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 58d6bc9cc4..aede487868 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -480,10 +480,16 @@ void Molecule::read(int flag) nibody = values.next_int(); ndbody = values.next_int(); nwant = 3; - } else break; - + } else { + // unknown header keyword + if (utils::strmatch(text,"^\\d+\\s+\\S+")) { + values.next_int(); + auto keyword = values.next_string(); + error->one(FLERR,fmt::format("Invalid header keyword: {}",keyword)); + } else break; + } if (nmatch != nwant) - error->one(FLERR,"Invalid header in molecule file"); + error->one(FLERR,"Invalid header line format in molecule file"); } catch (TokenizerException &e) { error->one(FLERR, fmt::format("Invalid header in molecule file\n" "{}", e.what())); From 92ff812e9dec27c8d2256d7a2ecf24ebea7bc3c6 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 15:53:33 -0500 Subject: [PATCH 13/22] simplify --- src/molecule.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index aede487868..8c49c47655 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -730,7 +730,7 @@ void Molecule::types(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) error->one(FLERR,fmt::format("Invalid line in Types section of " "molecule file: {}",line)); @@ -769,7 +769,7 @@ void Molecule::molecules(char *line) try { for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) error->one(FLERR,"Invalid Molecules section in molecule file"); int iatom = values.next_int() - 1; @@ -804,7 +804,7 @@ void Molecule::fragments(char *line) for (int i = 0; i < nfragments; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if ((int)values.count() > natoms+1) error->one(FLERR,"Invalid atom ID in Fragments section of molecule file"); @@ -835,7 +835,7 @@ void Molecule::charges(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if ((int)values.count() != 2) error->one(FLERR,"Invalid Charges section in molecule file"); int iatom = values.next_int() - 1; @@ -864,7 +864,7 @@ void Molecule::diameters(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) error->one(FLERR,"Invalid Diameters section in molecule file"); int iatom = values.next_int() - 1; @@ -899,7 +899,7 @@ void Molecule::masses(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) error->one(FLERR,"Invalid Masses section in molecule file"); int iatom = values.next_int() - 1; @@ -943,7 +943,7 @@ void Molecule::bonds(int flag, char *line) readline(line); try { - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 4) error->one(FLERR,"Invalid Bonds section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1011,7 +1011,7 @@ void Molecule::angles(int flag, char *line) readline(line); try { - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 5) error->one(FLERR,"Invalid Angles section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1095,7 +1095,7 @@ void Molecule::dihedrals(int flag, char *line) readline(line); try { - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 6) error->one(FLERR,"Invalid Dihedrals section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1194,7 +1194,7 @@ void Molecule::impropers(int flag, char *line) readline(line); try { - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 6) error->one(FLERR,"Invalid Impropers section in molecule file"); values.next_int(); itype = values.next_int(); @@ -1287,7 +1287,7 @@ void Molecule::nspecial_read(int flag, char *line) int c1, c2, c3; try { - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 4) error->one(FLERR,"Invalid Special Bond Counts section in molecule file"); values.next_int(); c1 = values.next_tagint(); @@ -1316,7 +1316,7 @@ void Molecule::special_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); int nwords = values.count(); if (nwords != nspecial[i][2]+1) @@ -1454,7 +1454,7 @@ void Molecule::shakeflag_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) error->one(FLERR,"Invalid Shake Flags section in molecule file"); @@ -1483,7 +1483,7 @@ void Molecule::shakeatom_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); nmatch = values.count(); switch (shake_flag[i]) { @@ -1557,7 +1557,7 @@ void Molecule::shaketype_read(char *line) for (int i = 0; i < natoms; i++) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); nmatch = values.count(); switch (shake_flag[i]) { @@ -1635,7 +1635,7 @@ void Molecule::body(int flag, int pflag, char *line) while (nword < nparam) { readline(line); - ValueTokenizer values(utils::trim(utils::trim_comment(line))); + ValueTokenizer values(utils::trim_comment(line)); int ncount = values.count(); if (ncount == 0) From 16631a0c184108ea8c69c498cc49556654a0fb09 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 15:54:06 -0500 Subject: [PATCH 14/22] add tests for one file with two molecules and two files with one each --- unittest/formats/test_molecule_file.cpp | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index ea6a6ef544..ec2fc65d26 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -140,6 +140,7 @@ TEST_F(MoleculeFileTest, noatom) TEST_FAILURE(".*ERROR: No or invalid atom count in molecule file.*", run_mol_cmd(test_name,"","Comment\n0 atoms\n1 bonds\n\n" " Coords\n\nBonds\n\n 1 1 2\n");); + remove("noatom.mol"); } TEST_F(MoleculeFileTest, minimal) @@ -151,6 +152,35 @@ TEST_F(MoleculeFileTest, minimal) ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*1 atoms.*0 bonds.*")); } +TEST_F(MoleculeFileTest, twomols) +{ + ::testing::internal::CaptureStdout(); + run_mol_cmd(test_name,"","Comment\n2 atoms\n\n" + " Coords\n\n 1 0.0 0.0 0.0\n 2 0.0 0.0 1.0\n" + " Molecules\n\n 1 1\n 2 2\n\n Types\n\n 1 1\n 2 2\n\n"); + auto output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*2 molecules.*2 atoms " + "with max type 2.*0 bonds.*")); +} + +TEST_F(MoleculeFileTest, twofiles) +{ + ::testing::internal::CaptureStdout(); + create_molecule_files(); + lmp->input->one("molecule twomols h2o.mol co2.mol offset 2 1 1 0 0"); + auto output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output,MatchesRegex(".*Read molecule template twomols:.*1 molecules.*3 atoms " + "with max type 2.*2 bonds with max type 1.*" + "1 angles with max type 1.*0 dihedrals.*" + ".*Read molecule template twomols:.*1 molecules.*3 atoms " + "with max type 4.*2 bonds with max type 2.*" + "1 angles with max type 2.*0 dihedrals.*")); + remove("h2o.mol"); + remove("co2.mol"); +} + int main(int argc, char **argv) { MPI_Init(&argc, &argv); From 7ddb0c701467eaae84b772dc2a094f5dd87b2794 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 22:26:23 -0500 Subject: [PATCH 15/22] clarify some more error messages --- src/molecule.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 8c49c47655..1b0601c839 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -57,7 +57,7 @@ Molecule::Molecule(LAMMPS *lmp, int narg, char **arg, int &index) : id = utils::strdup(arg[0]); if (!utils::is_id(id)) - error->all(FLERR,"Molecule template ID must be " + error->all(FLERR,"Molecule template ID must have only " "alphanumeric or underscore characters"); // parse args until reach unknown arg (next file) @@ -499,7 +499,7 @@ void Molecule::read(int flag) // error checks if (natoms < 1) - error->all(FLERR,"No or invalid atom count in molecule file"); + error->all(FLERR,"No atoms or invalid atom count in molecule file"); if (nbonds < 0) error->all(FLERR,"Invalid bond count in molecule file"); if (nangles < 0) error->all(FLERR,"Invalid angle count in molecule file"); if (ndihedrals < 0) @@ -533,7 +533,7 @@ void Molecule::read(int flag) else skip_lines(natoms,line,keyword); } else if (keyword == "Fragments") { if (nfragments == 0) - error->all(FLERR,"Molecule file has fragments but no nfragments setting"); + error->all(FLERR,"Found Fragments section but no nfragments setting in header"); fragmentflag = 1; if (flag) fragments(line); else skip_lines(nfragments,line,keyword); @@ -552,22 +552,22 @@ void Molecule::read(int flag) } else if (keyword == "Bonds") { if (nbonds == 0) - error->all(FLERR,"Molecule file has bonds but no nbonds setting"); + error->all(FLERR,"Found Bonds section but no nbonds setting in header"); bondflag = tag_require = 1; bonds(flag,line); } else if (keyword == "Angles") { if (nangles == 0) - error->all(FLERR,"Molecule file has angles but no nangles setting"); + error->all(FLERR,"Found Angles section but no nangles setting in header"); angleflag = tag_require = 1; angles(flag,line); } else if (keyword == "Dihedrals") { - if (ndihedrals == 0) error->all(FLERR,"Molecule file has dihedrals " - "but no ndihedrals setting"); + if (ndihedrals == 0) error->all(FLERR,"Found Dihedrals section" + "but no ndihedrals setting in header"); dihedralflag = tag_require = 1; dihedrals(flag,line); } else if (keyword == "Impropers") { - if (nimpropers == 0) error->all(FLERR,"Molecule file has impropers " - "but no nimpropers setting"); + if (nimpropers == 0) error->all(FLERR,"Found Impropers section" + "but no nimpropers setting in header"); improperflag = tag_require = 1; impropers(flag,line); @@ -587,27 +587,25 @@ void Molecule::read(int flag) shakeatomflag = tag_require = 1; if (shaketypeflag) shakeflag = 1; if (!shakeflagflag) - error->all(FLERR,"Molecule file shake flags not before shake atoms"); + error->all(FLERR,"Shake Flags section must come before Shake Atoms section"); if (flag) shakeatom_read(line); else skip_lines(natoms,line,keyword); } else if (keyword == "Shake Bond Types") { shaketypeflag = 1; if (shakeatomflag) shakeflag = 1; if (!shakeflagflag) - error->all(FLERR,"Molecule file shake flags not before shake bonds"); + error->all(FLERR,"Shake Flags section must come before Shake Bonds section"); if (flag) shaketype_read(line); else skip_lines(natoms,line,keyword); } else if (keyword == "Body Integers") { if (bodyflag == 0 || nibody == 0) - error->all(FLERR,"Molecule file has body params " - "but no setting for them"); + error->all(FLERR,"Found Body Integers section but no setting in header"); ibodyflag = 1; body(flag,0,line); } else if (keyword == "Body Doubles") { if (bodyflag == 0 || ndbody == 0) - error->all(FLERR,"Molecule file has body params " - "but no setting for them"); + error->all(FLERR,"Found Body Doubles section but no setting in header"); dbodyflag = 1; body(flag,1,line); } else { @@ -693,7 +691,7 @@ void Molecule::coords(char *line) int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,"Invalid Coords section in molecule file"); + error->one(FLERR,"Invalid atom index in Coords section of molecule file"); count[iatom]++; x[iatom][0] = values.next_double(); x[iatom][1] = values.next_double(); From 6ab8de58bc15f2e85fe926e4bc68ce93be18dd6b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 22:26:40 -0500 Subject: [PATCH 16/22] add a few more tests --- unittest/formats/test_molecule_file.cpp | 90 +++++++++++++++++++++++-- 1 file changed, 86 insertions(+), 4 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index ec2fc65d26..7221b88444 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -106,6 +106,7 @@ protected: int argc = sizeof(args) / sizeof(char *); if (!verbose) ::testing::internal::CaptureStdout(); lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); + create_molecule_files(); if (!verbose) ::testing::internal::GetCapturedStdout(); ASSERT_NE(lmp, nullptr); } @@ -115,6 +116,8 @@ protected: if (!verbose) ::testing::internal::CaptureStdout(); delete lmp; if (!verbose) ::testing::internal::GetCapturedStdout(); + remove("h2o.mol"); + remove("co2.mol"); } void run_mol_cmd(const std::string &name, const std::string &args, const std::string &content) @@ -135,14 +138,63 @@ TEST_F(MoleculeFileTest, nofile) lmp->input->one("molecule 1 nofile.mol");); } +TEST_F(MoleculeFileTest, badid) +{ + TEST_FAILURE(".*Molecule template ID must have only " + "alphanumeric or underscore characters.*", + lmp->input->one("molecule @mol nofile.mol");); +} + +TEST_F(MoleculeFileTest, badargs) +{ + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"offset 1 2 3 4", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"toff", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"boff", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"aoff", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"doff", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"ioff", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE(".*Illegal molecule command.*", + run_mol_cmd(test_name,"scale", + "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + remove("badargs.mol"); +} + TEST_F(MoleculeFileTest, noatom) { - TEST_FAILURE(".*ERROR: No or invalid atom count in molecule file.*", + TEST_FAILURE(".*ERROR: No atoms or invalid atom count in molecule file.*", run_mol_cmd(test_name,"","Comment\n0 atoms\n1 bonds\n\n" " Coords\n\nBonds\n\n 1 1 2\n");); remove("noatom.mol"); } +TEST_F(MoleculeFileTest, empty) +{ + TEST_FAILURE(".*ERROR: Unexpected end of molecule file.*", + run_mol_cmd(test_name,"","Comment\n\n");); + remove("empty.mol"); +} + +TEST_F(MoleculeFileTest, nospecial) +{ + TEST_FAILURE(".*ERROR: Cannot auto-generate special bonds before simulation box is defined.*", + run_mol_cmd(test_name,"","Comment\n3 atoms\n\n2 bonds\n\n" + " Coords\n\n 1 1.0 1.0 1.0\n 2 1.0 1.0 0.0\n 3 1.0 0.0 1.0\n" + " Bonds\n\n 1 1 1 2\n 2 1 1 3\n");); + remove("nospecial.mol"); +} + TEST_F(MoleculeFileTest, minimal) { ::testing::internal::CaptureStdout(); @@ -167,7 +219,6 @@ TEST_F(MoleculeFileTest, twomols) TEST_F(MoleculeFileTest, twofiles) { ::testing::internal::CaptureStdout(); - create_molecule_files(); lmp->input->one("molecule twomols h2o.mol co2.mol offset 2 1 1 0 0"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; @@ -177,8 +228,39 @@ TEST_F(MoleculeFileTest, twofiles) ".*Read molecule template twomols:.*1 molecules.*3 atoms " "with max type 4.*2 bonds with max type 2.*" "1 angles with max type 2.*0 dihedrals.*")); - remove("h2o.mol"); - remove("co2.mol"); +} + +TEST_F(MoleculeFileTest, bonds) +{ + ::testing::internal::CaptureStdout(); + lmp->input->one("atom_style bond"); + lmp->input->one("region box block 0 1 0 1 0 1"); + lmp->input->one("create_box 2 box bond/types 2 extra/special/per/atom 2"); + run_mol_cmd(test_name,"","Comment\n" + "4 atoms\n" + "2 bonds\n\n" + " Coords\n\n" + " 1 1.0 1.0 1.0\n" + " 2 1.0 1.0 0.0\n" + " 3 1.0 0.0 1.0\n" + " 4 1.0 0.0 0.0\n" + " Types\n\n" + " 1 1\n" + " 2 1\n" + " 3 2\n" + " 4 2\n\n" + " Masses\n\n" + " 1 1.0\n" + " 2 2.0\n" + " 3 3.0\n" + " 4 4.0\n\n" + " Bonds\n\n" + " 1 1 1 2\n" + " 2 2 1 3\n\n"); + auto output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*4 atoms.*type.*2.*" + "2 bonds.*type.*2.*0 angles.*")); } int main(int argc, char **argv) From c44dbc567d8656762ff70e7977ea48e88b94f66c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 22:41:09 -0500 Subject: [PATCH 17/22] tweak test for creating atoms and bond from a molecule file --- unittest/formats/test_molecule_file.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index 7221b88444..7b3b05f83a 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -235,7 +235,8 @@ TEST_F(MoleculeFileTest, bonds) ::testing::internal::CaptureStdout(); lmp->input->one("atom_style bond"); lmp->input->one("region box block 0 1 0 1 0 1"); - lmp->input->one("create_box 2 box bond/types 2 extra/special/per/atom 2"); + lmp->input->one("create_box 2 box bond/types 2 extra/bond/per/atom 2 " + "extra/special/per/atom 4"); run_mol_cmd(test_name,"","Comment\n" "4 atoms\n" "2 bonds\n\n" @@ -249,11 +250,6 @@ TEST_F(MoleculeFileTest, bonds) " 2 1\n" " 3 2\n" " 4 2\n\n" - " Masses\n\n" - " 1 1.0\n" - " 2 2.0\n" - " 3 3.0\n" - " 4 4.0\n\n" " Bonds\n\n" " 1 1 1 2\n" " 2 2 1 3\n\n"); @@ -261,6 +257,12 @@ TEST_F(MoleculeFileTest, bonds) if (verbose) std::cout << output; ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*4 atoms.*type.*2.*" "2 bonds.*type.*2.*0 angles.*")); + + ::testing::internal::CaptureStdout(); + lmp->input->one("create_atoms 0 single 0.0 0.0 0.0 mol bonds 67235"); + output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output,MatchesRegex(".*Created 4 atoms.*")); } int main(int argc, char **argv) From 7b4034d07a9a3346cc38bba1dfe21be315220b0f Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 2 Mar 2021 23:05:27 -0500 Subject: [PATCH 18/22] reformat. compute/check some molecule properties --- unittest/formats/test_molecule_file.cpp | 107 ++++++++++++++---------- 1 file changed, 64 insertions(+), 43 deletions(-) diff --git a/unittest/formats/test_molecule_file.cpp b/unittest/formats/test_molecule_file.cpp index 7b3b05f83a..3bfc0dc4fa 100644 --- a/unittest/formats/test_molecule_file.cpp +++ b/unittest/formats/test_molecule_file.cpp @@ -11,6 +11,7 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ +#include "atom.h" #include "info.h" #include "input.h" #include "lammps.h" @@ -123,13 +124,13 @@ protected: void run_mol_cmd(const std::string &name, const std::string &args, const std::string &content) { std::string file = name + ".mol"; - FILE *fp = fopen(file.c_str(), "w"); - fputs(content.c_str(),fp); + FILE *fp = fopen(file.c_str(), "w"); + fputs(content.c_str(), fp); fclose(fp); - lmp->input->one(fmt::format("molecule {} {} {}",name,file,args)); + lmp->input->one(fmt::format("molecule {} {} {}", name, file, args)); remove(file.c_str()); - } + } }; TEST_F(MoleculeFileTest, nofile) @@ -148,48 +149,50 @@ TEST_F(MoleculeFileTest, badid) TEST_F(MoleculeFileTest, badargs) { TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"offset 1 2 3 4", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"toff", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"boff", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"aoff", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"doff", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"ioff", - "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); - TEST_FAILURE(".*Illegal molecule command.*", - run_mol_cmd(test_name,"scale", + run_mol_cmd(test_name, "offset 1 2 3 4", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "toff", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "boff", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "aoff", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "doff", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "ioff", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); + TEST_FAILURE( + ".*Illegal molecule command.*", + run_mol_cmd(test_name, "scale", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n");); remove("badargs.mol"); } TEST_F(MoleculeFileTest, noatom) { TEST_FAILURE(".*ERROR: No atoms or invalid atom count in molecule file.*", - run_mol_cmd(test_name,"","Comment\n0 atoms\n1 bonds\n\n" - " Coords\n\nBonds\n\n 1 1 2\n");); + run_mol_cmd(test_name, "", + "Comment\n0 atoms\n1 bonds\n\n" + " Coords\n\nBonds\n\n 1 1 2\n");); remove("noatom.mol"); } TEST_F(MoleculeFileTest, empty) { TEST_FAILURE(".*ERROR: Unexpected end of molecule file.*", - run_mol_cmd(test_name,"","Comment\n\n");); + run_mol_cmd(test_name, "", "Comment\n\n");); remove("empty.mol"); } TEST_F(MoleculeFileTest, nospecial) { TEST_FAILURE(".*ERROR: Cannot auto-generate special bonds before simulation box is defined.*", - run_mol_cmd(test_name,"","Comment\n3 atoms\n\n2 bonds\n\n" + run_mol_cmd(test_name, "", + "Comment\n3 atoms\n\n2 bonds\n\n" " Coords\n\n 1 1.0 1.0 1.0\n 2 1.0 1.0 0.0\n 3 1.0 0.0 1.0\n" " Bonds\n\n 1 1 1 2\n 2 1 1 3\n");); remove("nospecial.mol"); @@ -198,22 +201,23 @@ TEST_F(MoleculeFileTest, nospecial) TEST_F(MoleculeFileTest, minimal) { ::testing::internal::CaptureStdout(); - run_mol_cmd(test_name,"","Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n"); + run_mol_cmd(test_name, "", "Comment\n1 atoms\n\n Coords\n\n 1 0.0 0.0 0.0\n"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*1 atoms.*0 bonds.*")); + ASSERT_THAT(output, MatchesRegex(".*Read molecule template.*1 molecules.*1 atoms.*0 bonds.*")); } TEST_F(MoleculeFileTest, twomols) { ::testing::internal::CaptureStdout(); - run_mol_cmd(test_name,"","Comment\n2 atoms\n\n" + run_mol_cmd(test_name, "", + "Comment\n2 atoms\n\n" " Coords\n\n 1 0.0 0.0 0.0\n 2 0.0 0.0 1.0\n" " Molecules\n\n 1 1\n 2 2\n\n Types\n\n 1 1\n 2 2\n\n"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*2 molecules.*2 atoms " - "with max type 2.*0 bonds.*")); + ASSERT_THAT(output, MatchesRegex(".*Read molecule template.*2 molecules.*2 atoms " + "with max type 2.*0 bonds.*")); } TEST_F(MoleculeFileTest, twofiles) @@ -222,12 +226,12 @@ TEST_F(MoleculeFileTest, twofiles) lmp->input->one("molecule twomols h2o.mol co2.mol offset 2 1 1 0 0"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Read molecule template twomols:.*1 molecules.*3 atoms " - "with max type 2.*2 bonds with max type 1.*" - "1 angles with max type 1.*0 dihedrals.*" - ".*Read molecule template twomols:.*1 molecules.*3 atoms " - "with max type 4.*2 bonds with max type 2.*" - "1 angles with max type 2.*0 dihedrals.*")); + ASSERT_THAT(output, MatchesRegex(".*Read molecule template twomols:.*1 molecules.*3 atoms " + "with max type 2.*2 bonds with max type 1.*" + "1 angles with max type 1.*0 dihedrals.*" + ".*Read molecule template twomols:.*1 molecules.*3 atoms " + "with max type 4.*2 bonds with max type 2.*" + "1 angles with max type 2.*0 dihedrals.*")); } TEST_F(MoleculeFileTest, bonds) @@ -237,7 +241,8 @@ TEST_F(MoleculeFileTest, bonds) lmp->input->one("region box block 0 1 0 1 0 1"); lmp->input->one("create_box 2 box bond/types 2 extra/bond/per/atom 2 " "extra/special/per/atom 4"); - run_mol_cmd(test_name,"","Comment\n" + run_mol_cmd(test_name, "", + "Comment\n" "4 atoms\n" "2 bonds\n\n" " Coords\n\n" @@ -255,14 +260,30 @@ TEST_F(MoleculeFileTest, bonds) " 2 2 1 3\n\n"); auto output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Read molecule template.*1 molecules.*4 atoms.*type.*2.*" - "2 bonds.*type.*2.*0 angles.*")); + ASSERT_THAT(output, MatchesRegex(".*Read molecule template.*1 molecules.*4 atoms.*type.*2.*" + "2 bonds.*type.*2.*0 angles.*")); ::testing::internal::CaptureStdout(); - lmp->input->one("create_atoms 0 single 0.0 0.0 0.0 mol bonds 67235"); + lmp->input->one("mass * 2.0"); + lmp->input->one("create_atoms 0 single 0.5 0.5 0.5 mol bonds 67235"); + output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output, MatchesRegex(".*Created 4 atoms.*")); + + ::testing::internal::CaptureStdout(); + Molecule *mol = lmp->atom->molecules[0]; + ASSERT_EQ(mol->natoms, 4); + ASSERT_EQ(lmp->atom->natoms, 4); + mol->compute_mass(); + mol->compute_com(); + ASSERT_DOUBLE_EQ(mol->masstotal, 8.0); + EXPECT_DOUBLE_EQ(mol->com[0], 1.0); + EXPECT_DOUBLE_EQ(mol->com[1], 0.5); + EXPECT_DOUBLE_EQ(mol->com[2], 0.5); + EXPECT_DOUBLE_EQ(mol->maxextent, sqrt(2)); + EXPECT_EQ(mol->comatom, 1); output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; - ASSERT_THAT(output,MatchesRegex(".*Created 4 atoms.*")); } int main(int argc, char **argv) From f02b0cf09b139e7338d3b17305262b062418b335 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 3 Mar 2021 06:05:25 -0500 Subject: [PATCH 19/22] review error messages for Coords, Types, Molecules, and Fragments sections --- src/molecule.cpp | 53 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 1b0601c839..d2c5f6f0fe 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -703,16 +703,18 @@ void Molecule::coords(char *line) } } catch (TokenizerException &e) { error->one(FLERR,fmt::format("Invalid line in Coords section of " - "molecule file: {}. {}",e.what(),line)); + "molecule file: {}.\n {}",e.what(),line)); } for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Coords section in molecule file"); + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Coords " + "section of molecule file",i+1)); if (domain->dimension == 2) { for (int i = 0; i < natoms; i++) if (x[i][2] != 0.0) - error->all(FLERR,"Molecule file z coord must be 0.0 for 2d"); + error->all(FLERR,fmt::format("Z coord in molecule file for atom {} " + "must be 0.0 for 2d-simulation.",i+1)); } } @@ -735,22 +737,23 @@ void Molecule::types(char *line) int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,"Invalid Types section in molecule file"); + error->one(FLERR,"Invalid atom index in Types section of molecule file"); count[iatom]++; type[iatom] = values.next_int(); type[iatom] += toffset; } } catch (TokenizerException &e) { error->one(FLERR, fmt::format("Invalid line in Types section of " - "molecule file: {}. {}", e.what(),line)); + "molecule file: {}.\n {}", e.what(),line)); } for (int i = 0; i < natoms; i++) { if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Types " - "section of molecule file",i)); + "section of molecule file",i+1)); if ((type[i] <= 0) || (domain->box_exist && (type[i] > atom->ntypes))) - error->all(FLERR,fmt::format("Invalid atom type {} in molecule file",type[i])); + error->all(FLERR,fmt::format("Invalid atom type {} for atom {} " + "in molecule file",type[i],i+1)); ntypes = MAX(ntypes,type[i]); } @@ -768,25 +771,30 @@ void Molecule::molecules(char *line) for (int i = 0; i < natoms; i++) { readline(line); ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 2) error->one(FLERR,"Invalid Molecules section in molecule file"); + if (values.count() != 2) + error->one(FLERR,fmt::format("Invalid line in Molecules section of " + "molecule file: {}",line)); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Molecules section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->one(FLERR,"Invalid atom index in Molecules section of molecule file"); count[iatom]++; molecule[iatom] = values.next_tagint(); // molecule[iatom] += moffset; // placeholder for possible molecule offset } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Molecules section in molecule file\n" - "{}", e.what())); + error->one(FLERR, fmt::format("Invalid line in Molecules section of " + "molecule file: {}.\n {}", e.what(), line)); } for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Molecules section in molecule file"); + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Molecules " + "section of molecule file",i+1)); for (int i = 0; i < natoms; i++) - if (molecule[i] <= 0) - error->all(FLERR,"Invalid molecule ID in molecule file"); + if (molecule[i] < 0) + error->all(FLERR,fmt::format("Invalid molecule ID {} for atom {} " + "in molecule file",molecule[i],i+1)); for (int i = 0; i < natoms; i++) nmolecules = MAX(nmolecules,molecule[i]); @@ -805,20 +813,23 @@ void Molecule::fragments(char *line) ValueTokenizer values(utils::trim_comment(line)); if ((int)values.count() > natoms+1) - error->one(FLERR,"Invalid atom ID in Fragments section of molecule file"); + error->one(FLERR,"Too many atoms per fragment in Fragments " + "section of molecule file"); fragmentnames[i] = values.next_string(); while (values.has_next()) { - int atomID = values.next_int(); - if (atomID <= 0 || atomID > natoms) - error->one(FLERR,"Invalid atom ID in Fragments section of molecule file"); - fragmentmask[i][atomID-1] = 1; + int iatom = values.next_int()-1; + if (iatom < 0 || iatom >= natoms) + error->one(FLERR,fmt::format("Invalid atom ID {} for fragment {} in " + "Fragments section of molecule file", + iatom+1, fragmentnames[i])); + fragmentmask[i][iatom] = 1; } } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid atom ID in Fragments section of molecule file\n" - "{}", e.what())); + error->one(FLERR, fmt::format("Invalid atom ID in Fragments section of " + "molecule file: {}.\n {}", e.what(),line)); } } From 2db78823a03094cf81eeea60bd6615b9567bc04e Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 3 Mar 2021 08:43:23 -0500 Subject: [PATCH 20/22] open() method not really needed. --- src/molecule.cpp | 23 ++++++++--------------- src/molecule.h | 1 - 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index d2c5f6f0fe..8e067937b0 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -125,16 +125,21 @@ Molecule::Molecule(LAMMPS *lmp, int narg, char **arg, int &index) : initialize(); - // scan file for sizes of all fields and allocate them + // scan file for sizes of all fields and allocate storage for them - if (me == 0) open(arg[ifile]); + if (me == 0) { + fp = fopen(arg[ifile],"r"); + if (fp == nullptr) + error->one(FLERR,fmt::format("Cannot open molecule file {}: {}", + arg[ifile], utils::getsyserror())); + } read(0); if (me == 0) fclose(fp); allocate(); // read file again to populate all fields - if (me == 0) open(arg[ifile]); + if (me == 0) fp = fopen(arg[ifile],"r"); read(1); if (me == 0) fclose(fp); @@ -1962,18 +1967,6 @@ void Molecule::deallocate() memory->destroy(dbodyparams); } -/* ---------------------------------------------------------------------- - open molecule file -------------------------------------------------------------------------- */ - -void Molecule::open(char *file) -{ - fp = fopen(file,"r"); - if (fp == nullptr) - error->one(FLERR,fmt::format("Cannot open molecule file {}: {}", - file, utils::getsyserror())); -} - /* ---------------------------------------------------------------------- read and bcast a line ------------------------------------------------------------------------- */ diff --git a/src/molecule.h b/src/molecule.h index f744a438bc..00b2b123a0 100644 --- a/src/molecule.h +++ b/src/molecule.h @@ -161,7 +161,6 @@ class Molecule : protected Pointers { void allocate(); void deallocate(); - void open(char *); void readline(char *); std::string parse_keyword(int, char *); void skip_lines(int, char *, const std::string &); From 4af4c0a99bdaedefff3e90bddd12e9d240f75bef Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 3 Mar 2021 08:43:51 -0500 Subject: [PATCH 21/22] parsing is done on all MPI ranks, so we better use error->all() instead of error->one() --- src/molecule.cpp | 156 +++++++++++++++++++++++++---------------------- 1 file changed, 83 insertions(+), 73 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 8e067937b0..6b6345315e 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -691,12 +691,12 @@ void Molecule::coords(char *line) ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 4) - error->one(FLERR,fmt::format("Invalid line in Coords section of " + error->all(FLERR,fmt::format("Invalid line in Coords section of " "molecule file: {}",line)); int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,"Invalid atom index in Coords section of molecule file"); + error->all(FLERR,"Invalid atom index in Coords section of molecule file"); count[iatom]++; x[iatom][0] = values.next_double(); x[iatom][1] = values.next_double(); @@ -707,8 +707,8 @@ void Molecule::coords(char *line) x[iatom][2] *= sizescale; } } catch (TokenizerException &e) { - error->one(FLERR,fmt::format("Invalid line in Coords section of " - "molecule file: {}.\n {}",e.what(),line)); + error->all(FLERR,fmt::format("Invalid line in Coords section of " + "molecule file: {}\n{}",e.what(),line)); } for (int i = 0; i < natoms; i++) @@ -737,19 +737,19 @@ void Molecule::types(char *line) ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) - error->one(FLERR,fmt::format("Invalid line in Types section of " + error->all(FLERR,fmt::format("Invalid line in Types section of " "molecule file: {}",line)); int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,"Invalid atom index in Types section of molecule file"); + error->all(FLERR,"Invalid atom index in Types section of molecule file"); count[iatom]++; type[iatom] = values.next_int(); type[iatom] += toffset; } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid line in Types section of " - "molecule file: {}.\n {}", e.what(),line)); + error->all(FLERR, fmt::format("Invalid line in Types section of " + "molecule file: {}\n{}", e.what(),line)); } for (int i = 0; i < natoms; i++) { @@ -777,19 +777,19 @@ void Molecule::molecules(char *line) readline(line); ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) - error->one(FLERR,fmt::format("Invalid line in Molecules section of " + error->all(FLERR,fmt::format("Invalid line in Molecules section of " "molecule file: {}",line)); int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,"Invalid atom index in Molecules section of molecule file"); + error->all(FLERR,"Invalid atom index in Molecules section of molecule file"); count[iatom]++; molecule[iatom] = values.next_tagint(); // molecule[iatom] += moffset; // placeholder for possible molecule offset } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid line in Molecules section of " - "molecule file: {}.\n {}", e.what(), line)); + error->all(FLERR, fmt::format("Invalid line in Molecules section of " + "molecule file: {}\n{}",e.what(),line)); } for (int i = 0; i < natoms; i++) @@ -818,7 +818,7 @@ void Molecule::fragments(char *line) ValueTokenizer values(utils::trim_comment(line)); if ((int)values.count() > natoms+1) - error->one(FLERR,"Too many atoms per fragment in Fragments " + error->all(FLERR,"Too many atoms per fragment in Fragments " "section of molecule file"); fragmentnames[i] = values.next_string(); @@ -826,15 +826,15 @@ void Molecule::fragments(char *line) while (values.has_next()) { int iatom = values.next_int()-1; if (iatom < 0 || iatom >= natoms) - error->one(FLERR,fmt::format("Invalid atom ID {} for fragment {} in " + error->all(FLERR,fmt::format("Invalid atom ID {} for fragment {} in " "Fragments section of molecule file", iatom+1, fragmentnames[i])); fragmentmask[i][iatom] = 1; } } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid atom ID in Fragments section of " - "molecule file: {}.\n {}", e.what(),line)); + error->all(FLERR, fmt::format("Invalid atom ID in Fragments section of " + "molecule file: {}\n{}", e.what(),line)); } } @@ -850,20 +850,25 @@ void Molecule::charges(char *line) readline(line); ValueTokenizer values(utils::trim_comment(line)); - if ((int)values.count() != 2) error->one(FLERR,"Invalid Charges section in molecule file"); + if ((int)values.count() != 2) + error->all(FLERR,fmt::format("Invalid line in Charges section of " + "molecule file: {}",line)); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Charges section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->all(FLERR,"Invalid atom index in Charges section of molecule file"); + count[iatom]++; q[iatom] = values.next_double(); } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Charges section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Charges section of " + "molecule file: {}.\n{}",e.what(),line)); } for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Charges section in molecule file"); + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Charges " + "section of molecule file",i+1)); } /* ---------------------------------------------------------------------- @@ -879,10 +884,14 @@ void Molecule::diameters(char *line) readline(line); ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 2) error->one(FLERR,"Invalid Diameters section in molecule file"); + if (values.count() != 2) + error->all(FLERR,fmt::format("Invalid line in Diameters section of " + "molecule file: {}",line)); + int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Diameters section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->all(FLERR,"Invalid atom index in Diameters section of molecule file"); count[iatom]++; radius[iatom] = values.next_double(); radius[iatom] *= sizescale; @@ -890,16 +899,17 @@ void Molecule::diameters(char *line) maxradius = MAX(maxradius,radius[iatom]); } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Diameters section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Diameters section of " + "molecule file: {}\n{}",e.what(),line)); } - for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Diameters section in molecule file"); - - for (int i = 0; i < natoms; i++) + for (int i = 0; i < natoms; i++) { + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Diameters " + "section of molecule file",i+1)); if (radius[i] < 0.0) - error->all(FLERR,"Invalid atom diameter in molecule file"); + error->all(FLERR,fmt::format("Invalid atom diameter {} for atom {} " + "in molecule file", radius[i], i+1)); + } } /* ---------------------------------------------------------------------- @@ -914,16 +924,16 @@ void Molecule::masses(char *line) readline(line); ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 2) error->one(FLERR,"Invalid Masses section in molecule file"); + if (values.count() != 2) error->all(FLERR,"Invalid Masses section in molecule file"); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->one(FLERR,"Invalid Masses section in molecule file"); + if (iatom < 0 || iatom >= natoms) error->all(FLERR,"Invalid Masses section in molecule file"); count[iatom]++; rmass[iatom] = values.next_double(); rmass[iatom] *= sizescale*sizescale*sizescale; } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Masses section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Masses section in molecule file\n" "{}", e.what())); } @@ -958,13 +968,13 @@ void Molecule::bonds(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 4) error->one(FLERR,"Invalid Bonds section in molecule file"); + if (values.count() != 4) error->all(FLERR,"Invalid Bonds section in molecule file"); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); atom2 = values.next_tagint(); } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Bonds section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Bonds section in molecule file\n" "{}", e.what())); } @@ -972,9 +982,9 @@ void Molecule::bonds(int flag, char *line) if ((atom1 <= 0) || (atom1 > natoms) || (atom2 <= 0) || (atom2 > natoms) || (atom1 == atom2)) - error->one(FLERR,"Invalid atom ID in Bonds section of molecule file"); + error->all(FLERR,"Invalid atom ID in Bonds section of molecule file"); if ((itype <= 0) || (domain->box_exist && (itype > atom->nbondtypes))) - error->one(FLERR,"Invalid bond type in Bonds section of molecule file"); + error->all(FLERR,"Invalid bond type in Bonds section of molecule file"); if (flag) { m = atom1-1; @@ -1026,14 +1036,14 @@ void Molecule::angles(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 5) error->one(FLERR,"Invalid Angles section in molecule file"); + if (values.count() != 5) error->all(FLERR,"Invalid Angles section in molecule file"); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); atom2 = values.next_tagint(); atom3 = values.next_tagint(); } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Angles section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Angles section in molecule file\n" "{}", e.what())); } @@ -1043,9 +1053,9 @@ void Molecule::angles(int flag, char *line) (atom2 <= 0) || (atom2 > natoms) || (atom3 <= 0) || (atom3 > natoms) || (atom1 == atom2) || (atom1 == atom3) || (atom2 == atom3)) - error->one(FLERR,"Invalid atom ID in Angles section of molecule file"); + error->all(FLERR,"Invalid atom ID in Angles section of molecule file"); if ((itype <= 0) || (domain->box_exist && (itype > atom->nangletypes))) - error->one(FLERR,"Invalid angle type in Angles section of molecule file"); + error->all(FLERR,"Invalid angle type in Angles section of molecule file"); if (flag) { m = atom2-1; @@ -1110,7 +1120,7 @@ void Molecule::dihedrals(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 6) error->one(FLERR,"Invalid Dihedrals section in molecule file"); + if (values.count() != 6) error->all(FLERR,"Invalid Dihedrals section in molecule file"); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); @@ -1118,7 +1128,7 @@ void Molecule::dihedrals(int flag, char *line) atom3 = values.next_tagint(); atom4 = values.next_tagint(); } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Dihedrals section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Dihedrals section in molecule file\n" "{}", e.what())); } @@ -1130,10 +1140,10 @@ void Molecule::dihedrals(int flag, char *line) (atom4 <= 0) || (atom4 > natoms) || (atom1 == atom2) || (atom1 == atom3) || (atom1 == atom4) || (atom2 == atom3) || (atom2 == atom4) || (atom3 == atom4)) - error->one(FLERR, + error->all(FLERR, "Invalid atom ID in dihedrals section of molecule file"); if ((itype <= 0) || (domain->box_exist && (itype > atom->ndihedraltypes))) - error->one(FLERR,"Invalid dihedral type in Dihedrals section of molecule file"); + error->all(FLERR,"Invalid dihedral type in Dihedrals section of molecule file"); if (flag) { m = atom2-1; @@ -1209,7 +1219,7 @@ void Molecule::impropers(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 6) error->one(FLERR,"Invalid Impropers section in molecule file"); + if (values.count() != 6) error->all(FLERR,"Invalid Impropers section in molecule file"); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); @@ -1217,7 +1227,7 @@ void Molecule::impropers(int flag, char *line) atom3 = values.next_tagint(); atom4 = values.next_tagint(); } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Impropers section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Impropers section in molecule file\n" "{}", e.what())); } @@ -1229,10 +1239,10 @@ void Molecule::impropers(int flag, char *line) (atom4 <= 0) || (atom4 > natoms) || (atom1 == atom2) || (atom1 == atom3) || (atom1 == atom4) || (atom2 == atom3) || (atom2 == atom4) || (atom3 == atom4)) - error->one(FLERR, + error->all(FLERR, "Invalid atom ID in impropers section of molecule file"); if ((itype <= 0) || (domain->box_exist && (itype > atom->nimpropertypes))) - error->one(FLERR,"Invalid improper type in Impropers section of molecule file"); + error->all(FLERR,"Invalid improper type in Impropers section of molecule file"); if (flag) { m = atom2-1; @@ -1302,13 +1312,13 @@ void Molecule::nspecial_read(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 4) error->one(FLERR,"Invalid Special Bond Counts section in molecule file"); + if (values.count() != 4) error->all(FLERR,"Invalid Special Bond Counts section in molecule file"); values.next_int(); c1 = values.next_tagint(); c2 = values.next_tagint(); c3 = values.next_tagint(); } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Special Bond Counts section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Special Bond Counts section in molecule file\n" "{}", e.what())); } @@ -1334,7 +1344,7 @@ void Molecule::special_read(char *line) int nwords = values.count(); if (nwords != nspecial[i][2]+1) - error->one(FLERR,"Molecule file special list " + error->all(FLERR,"Molecule file special list " "does not match special count"); values.next_int(); // ignore @@ -1343,11 +1353,11 @@ void Molecule::special_read(char *line) special[i][m-1] = values.next_tagint(); if (special[i][m-1] <= 0 || special[i][m-1] > natoms || special[i][m-1] == i+1) - error->one(FLERR,"Invalid special atom index in molecule file"); + error->all(FLERR,"Invalid special atom index in molecule file"); } } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Molecule file special list\n" + error->all(FLERR, fmt::format("Invalid Molecule file special list\n" "{}", e.what())); } } @@ -1379,7 +1389,7 @@ void Molecule::special_generate() nspecial[i][0]++; nspecial[atom2][0]++; if (count[i] >= atom->maxspecial || count[atom2] >= atom->maxspecial) - error->one(FLERR,"Molecule auto special bond generation overflow"); + error->all(FLERR,"Molecule auto special bond generation overflow"); tmpspecial[i][count[i]++] = atom2 + 1; tmpspecial[atom2][count[atom2]++] = i + 1; } @@ -1391,7 +1401,7 @@ void Molecule::special_generate() atom1 = i; atom2 = bond_atom[i][j]; if (count[atom1] >= atom->maxspecial) - error->one(FLERR,"Molecule auto special bond generation overflow"); + error->all(FLERR,"Molecule auto special bond generation overflow"); tmpspecial[i][count[atom1]++] = atom2; } } @@ -1414,7 +1424,7 @@ void Molecule::special_generate() } if (!dedup) { if (count[i] >= atom->maxspecial) - error->one(FLERR,"Molecule auto special bond generation overflow"); + error->all(FLERR,"Molecule auto special bond generation overflow"); tmpspecial[i][count[i]++] = tmpspecial[tmpspecial[i][m]-1][j]; nspecial[i][1]++; } @@ -1438,7 +1448,7 @@ void Molecule::special_generate() } if (!dedup) { if (count[i] >= atom->maxspecial) - error->one(FLERR,"Molecule auto special bond generation overflow"); + error->all(FLERR,"Molecule auto special bond generation overflow"); tmpspecial[i][count[i]++] = tmpspecial[tmpspecial[i][m]-1][j]; nspecial[i][2]++; } @@ -1471,19 +1481,19 @@ void Molecule::shakeflag_read(char *line) ValueTokenizer values(utils::trim_comment(line)); if (values.count() != 2) - error->one(FLERR,"Invalid Shake Flags section in molecule file"); + error->all(FLERR,"Invalid Shake Flags section in molecule file"); values.next_int(); shake_flag[i] = values.next_int(); } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid Shake Flags section in molecule file\n" + error->all(FLERR, fmt::format("Invalid Shake Flags section in molecule file\n" "{}", e.what())); } for (int i = 0; i < natoms; i++) if (shake_flag[i] < 0 || shake_flag[i] > 4) - error->one(FLERR,"Invalid shake flag in molecule file"); + error->all(FLERR,"Invalid shake flag in molecule file"); } /* ---------------------------------------------------------------------- @@ -1539,15 +1549,15 @@ void Molecule::shakeatom_read(char *line) break; default: - error->one(FLERR,"Invalid shake atom in molecule file"); + error->all(FLERR,"Invalid shake atom in molecule file"); } if (nmatch != nwant) - error->one(FLERR,"Invalid shake atom in molecule file"); + error->all(FLERR,"Invalid shake atom in molecule file"); } } catch (TokenizerException &e) { - error->one(FLERR,fmt::format("Invalid shake atom in molecule file\n" + error->all(FLERR,fmt::format("Invalid shake atom in molecule file\n" "{}", e.what())); } @@ -1556,7 +1566,7 @@ void Molecule::shakeatom_read(char *line) if (m == 1) m = 3; for (int j = 0; j < m; j++) if (shake_atom[i][j] <= 0 || shake_atom[i][j] > natoms) - error->one(FLERR,"Invalid shake atom in molecule file"); + error->all(FLERR,"Invalid shake atom in molecule file"); } } @@ -1610,14 +1620,14 @@ void Molecule::shaketype_read(char *line) break; default: - error->one(FLERR,"Invalid shake type data in molecule file"); + error->all(FLERR,"Invalid shake type data in molecule file"); } if (nmatch != nwant) - error->one(FLERR,"Invalid shake type data in molecule file"); + error->all(FLERR,"Invalid shake type data in molecule file"); } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid shake type data in molecule file\n", + error->all(FLERR, fmt::format("Invalid shake type data in molecule file\n", "{}", e.what())); } @@ -1626,10 +1636,10 @@ void Molecule::shaketype_read(char *line) if (m == 1) m = 3; for (int j = 0; j < m-1; j++) if (shake_type[i][j] <= 0) - error->one(FLERR,"Invalid shake bond type in molecule file"); + error->all(FLERR,"Invalid shake bond type in molecule file"); if (shake_flag[i] == 1) if (shake_type[i][2] <= 0) - error->one(FLERR,"Invalid shake angle type in molecule file"); + error->all(FLERR,"Invalid shake angle type in molecule file"); } } @@ -1653,9 +1663,9 @@ void Molecule::body(int flag, int pflag, char *line) int ncount = values.count(); if (ncount == 0) - error->one(FLERR,"Too few values in body section of molecule file"); + error->all(FLERR,"Too few values in body section of molecule file"); if (nword+ncount > nparam) - error->one(FLERR,"Too many values in body section of molecule file"); + error->all(FLERR,"Too many values in body section of molecule file"); if (flag) { if (pflag == 0) { @@ -1670,7 +1680,7 @@ void Molecule::body(int flag, int pflag, char *line) } else nword += ncount; } } catch (TokenizerException &e) { - error->one(FLERR, fmt::format("Invalid body params in molecule file\n", + error->all(FLERR, fmt::format("Invalid body params in molecule file\n", "{}", e.what())); } } From f455869de3d1c54ce0f69bb235d5a584d94b007c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 3 Mar 2021 13:43:54 -0500 Subject: [PATCH 22/22] update a few more error messages --- src/molecule.cpp | 73 +++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/molecule.cpp b/src/molecule.cpp index 6b6345315e..644e3ff79c 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -887,8 +887,6 @@ void Molecule::diameters(char *line) if (values.count() != 2) error->all(FLERR,fmt::format("Invalid line in Diameters section of " "molecule file: {}",line)); - - int iatom = values.next_int() - 1; if (iatom < 0 || iatom >= natoms) error->all(FLERR,"Invalid atom index in Diameters section of molecule file"); @@ -924,24 +922,29 @@ void Molecule::masses(char *line) readline(line); ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 2) error->all(FLERR,"Invalid Masses section in molecule file"); + if (values.count() != 2) + error->all(FLERR,fmt::format("Invalid line in Masses section of " + "molecule file: {}",line)); int iatom = values.next_int() - 1; - if (iatom < 0 || iatom >= natoms) error->all(FLERR,"Invalid Masses section in molecule file"); + if (iatom < 0 || iatom >= natoms) + error->all(FLERR,"Invalid atom index in Masses section of molecule file"); count[iatom]++; rmass[iatom] = values.next_double(); rmass[iatom] *= sizescale*sizescale*sizescale; } } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Masses section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Masses section of " + "molecule file: {}\n{}",e.what(),line)); } - for (int i = 0; i < natoms; i++) - if (count[i] == 0) error->all(FLERR,"Invalid Masses section in molecule file"); - - for (int i = 0; i < natoms; i++) - if (rmass[i] <= 0.0) error->all(FLERR,"Invalid atom mass in molecule file"); + for (int i = 0; i < natoms; i++) { + if (count[i] == 0) error->all(FLERR,fmt::format("Atom {} missing in Masses " + "section of molecule file",i+1)); + if (rmass[i] <= 0.0) + error->all(FLERR,fmt::format("Invalid atom mass {} for atom {} " + "in molecule file", radius[i], i+1)); + } } /* ---------------------------------------------------------------------- @@ -968,14 +971,16 @@ void Molecule::bonds(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 4) error->all(FLERR,"Invalid Bonds section in molecule file"); + if (values.count() != 4) + error->all(FLERR,fmt::format("Invalid line in Bonds section of " + "molecule file: {}",line)); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); atom2 = values.next_tagint(); } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Bonds section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Bonds section of " + "molecule file: {}\n{}",e.what(),line)); } itype += boffset; @@ -1036,15 +1041,17 @@ void Molecule::angles(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 5) error->all(FLERR,"Invalid Angles section in molecule file"); + if (values.count() != 5) + error->all(FLERR,fmt::format("Invalid line in Angles section of " + "molecule file: {}",line)); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); atom2 = values.next_tagint(); atom3 = values.next_tagint(); } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Angles section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Angles section of " + "molecule file: {}\n{}",e.what(),line)); } itype += aoffset; @@ -1120,7 +1127,10 @@ void Molecule::dihedrals(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 6) error->all(FLERR,"Invalid Dihedrals section in molecule file"); + if (values.count() != 6) + error->all(FLERR,fmt::format("Invalid line in Dihedrals section of " + "molecule file: {}",line)); + values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); @@ -1128,8 +1138,8 @@ void Molecule::dihedrals(int flag, char *line) atom3 = values.next_tagint(); atom4 = values.next_tagint(); } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Dihedrals section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Dihedrals section of " + "molecule file: {}\n{}",e.what(),line)); } itype += doffset; @@ -1219,7 +1229,9 @@ void Molecule::impropers(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 6) error->all(FLERR,"Invalid Impropers section in molecule file"); + if (values.count() != 6) + error->all(FLERR,fmt::format("Invalid line in Impropers section of " + "molecule file: {}",line)); values.next_int(); itype = values.next_int(); atom1 = values.next_tagint(); @@ -1227,8 +1239,8 @@ void Molecule::impropers(int flag, char *line) atom3 = values.next_tagint(); atom4 = values.next_tagint(); } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Impropers section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Impropers section of " + "molecule file: {}\n{}",e.what(),line)); } itype += ioffset; @@ -1312,14 +1324,16 @@ void Molecule::nspecial_read(int flag, char *line) try { ValueTokenizer values(utils::trim_comment(line)); - if (values.count() != 4) error->all(FLERR,"Invalid Special Bond Counts section in molecule file"); + if (values.count() != 4) + error->all(FLERR,fmt::format("Invalid line in Special Bond Counts section of " + "molecule file: {}",line)); values.next_int(); c1 = values.next_tagint(); c2 = values.next_tagint(); c3 = values.next_tagint(); } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Special Bond Counts section in molecule file\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Special Bond Counts section of " + "molecule file: {}\n{}",e.what(),line)); } if (flag) { @@ -1353,12 +1367,13 @@ void Molecule::special_read(char *line) special[i][m-1] = values.next_tagint(); if (special[i][m-1] <= 0 || special[i][m-1] > natoms || special[i][m-1] == i+1) - error->all(FLERR,"Invalid special atom index in molecule file"); + error->all(FLERR,"Invalid atom index in Special Bonds " + "section of molecule file"); } } } catch (TokenizerException &e) { - error->all(FLERR, fmt::format("Invalid Molecule file special list\n" - "{}", e.what())); + error->all(FLERR, fmt::format("Invalid line in Special Bonds section of " + "molecule file: {}\n{}",e.what(),line)); } }