diff --git a/src/EXTRA-FIX/fix_ttm.cpp b/src/EXTRA-FIX/fix_ttm.cpp index 4c0d2e9cd5..de4b0a4c7e 100644 --- a/src/EXTRA-FIX/fix_ttm.cpp +++ b/src/EXTRA-FIX/fix_ttm.cpp @@ -28,7 +28,6 @@ #include "random_mars.h" #include "respa.h" #include "potential_file_reader.h" -#include "tokenizer.h" #include "update.h" #include @@ -46,18 +45,6 @@ using namespace FixConst; static constexpr int OFFSET = 16384; static constexpr double SHIFT = 0.0; -// helper class - -namespace { - class parser_error : public std::exception { - std::string message; - - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - /* ---------------------------------------------------------------------- */ FixTTM::FixTTM(LAMMPS *lmp, int narg, char **arg) : @@ -503,10 +490,10 @@ void FixTTM::read_electron_temperatures(const std::string &filename) // check correctness of input data if ((ix < 0) || (ix >= nxgrid) || (iy < 0) || (iy >= nygrid) || (iz < 0) || (iz >= nzgrid)) - throw parser_error("Fix ttm invalid grid index in fix ttm grid file"); + throw TokenizerException("Fix ttm invalid grid index in fix ttm grid file",""); if (T_tmp < 0.0) - throw parser_error("Fix ttm electron temperatures must be > 0.0"); + throw TokenizerException("Fix ttm electron temperatures must be > 0.0",""); T_electron[iz][iy][ix] = T_tmp; T_initial_set[iz][iy][ix] = 1; diff --git a/src/EXTRA-FIX/fix_ttm_grid.cpp b/src/EXTRA-FIX/fix_ttm_grid.cpp index b42b876a3b..8890750818 100644 --- a/src/EXTRA-FIX/fix_ttm_grid.cpp +++ b/src/EXTRA-FIX/fix_ttm_grid.cpp @@ -41,18 +41,6 @@ static constexpr int MAXLINE = 256; static constexpr int CHUNK = 1024; static constexpr int OFFSET = 16384; -// helper class - -namespace { - class parser_error : public std::exception { - std::string message; - - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - /* ---------------------------------------------------------------------- */ FixTTMGrid::FixTTMGrid(LAMMPS *lmp, int narg, char **arg) : @@ -318,7 +306,7 @@ void FixTTMGrid::read_electron_temperatures(const std::string &filename) int iz = values.next_int(); if (ix < 0 || ix >= nxgrid || iy < 0 || iy >= nygrid || iz < 0 || iz >= nzgrid) - throw parser_error("Fix ttm/grid invalid grid index in input"); + throw TokenizerException("Fix ttm/grid invalid grid index in input",""); if (ix >= nxlo_in && ix <= nxhi_in && iy >= nylo_in && iy <= nyhi_in && iz >= nzlo_in && iz <= nzhi_in) { @@ -326,7 +314,7 @@ void FixTTMGrid::read_electron_temperatures(const std::string &filename) T_initial_set[iz][iy][ix] = 1; } } else { - throw parser_error("Incorrect format in fix ttm electron grid file"); + throw TokenizerException("Incorrect format in fix ttm electron grid file",""); } } catch (std::exception &e) { error->one(FLERR,e.what()); diff --git a/src/EXTRA-FIX/fix_ttm_mod.cpp b/src/EXTRA-FIX/fix_ttm_mod.cpp index d39d50c9e5..233f877e8c 100644 --- a/src/EXTRA-FIX/fix_ttm_mod.cpp +++ b/src/EXTRA-FIX/fix_ttm_mod.cpp @@ -41,18 +41,6 @@ using namespace LAMMPS_NS; using namespace FixConst; using namespace MathConst; -// helper class - -namespace { - class parser_error : public std::exception { - std::string message; - - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - // OFFSET avoids outside-of-box atoms being rounded to grid pts incorrectly // SHIFT = 0.0 assigns atoms to lower-left grid pt // SHIFT = 0.5 assigns atoms to nearest grid pt @@ -596,10 +584,10 @@ void FixTTMMod::read_electron_temperatures(const std::string &filename) // check correctness of input data if ((ix < 0) || (ix >= nxgrid) || (iy < 0) || (iy >= nygrid) || (iz < 0) || (iz >= nzgrid)) - throw parser_error("Fix ttm invalid grid index in fix ttm/mod grid file"); + throw TokenizerException("Fix ttm invalid grid index in fix ttm/mod grid file",""); if (T_tmp < 0.0) - throw parser_error("Fix ttm electron temperatures must be > 0.0"); + throw TokenizerException("Fix ttm electron temperatures must be > 0.0",""); T_electron[iz][iy][ix] = T_tmp; T_initial_set[iz][iy][ix] = 1; diff --git a/src/QEQ/fix_qeq.cpp b/src/QEQ/fix_qeq.cpp index b43afb00d6..5a4e4e4e7d 100644 --- a/src/QEQ/fix_qeq.cpp +++ b/src/QEQ/fix_qeq.cpp @@ -43,10 +43,10 @@ using namespace FixConst; #define MAXLINE 1024 namespace { - class parser_error : public std::exception { + class qeq_parser_error : public std::exception { std::string message; public: - parser_error(const std::string &mesg) { message = mesg; } + explicit qeq_parser_error(const std::string &mesg) { message = mesg; } const char *what() const noexcept { return message.c_str(); } }; } @@ -761,8 +761,8 @@ void FixQEq::read_file(char *file) FILE *fp = utils::open_potential(file,lmp,nullptr); if (fp == nullptr) - throw parser_error(fmt::format("Cannot open fix qeq parameter file {}:" - " {}", file,utils::getsyserror())); + throw qeq_parser_error(fmt::format("Cannot open fix qeq parameter file {}: {}", + file,utils::getsyserror())); TextFileReader reader(fp, "qeq parameter"); while (1) { @@ -770,12 +770,12 @@ void FixQEq::read_file(char *file) if (values.count() == 0) continue; if (values.count() < 6) - throw parser_error("Invalid qeq parameter file"); + throw qeq_parser_error("Invalid qeq parameter file"); auto word = values.next_string(); utils::bounds(FLERR,word,1,ntypes,nlo,nhi,nullptr); if ((nlo < 0) || (nhi < 0)) - throw parser_error("Invalid atom type range"); + throw qeq_parser_error(fmt::format("Invalid atom type range: {}",word)); val = values.next_double(); for (int n=nlo; n <= nhi; ++n) chi[n] = val; diff --git a/src/REAXFF/fix_acks2_reaxff.cpp b/src/REAXFF/fix_acks2_reaxff.cpp index ec14ed72b2..b6789b1b2e 100644 --- a/src/REAXFF/fix_acks2_reaxff.cpp +++ b/src/REAXFF/fix_acks2_reaxff.cpp @@ -43,15 +43,6 @@ using namespace LAMMPS_NS; using namespace FixConst; -namespace { - class parser_error : public std::exception { - std::string message; - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - static const char cite_fix_acks2_reax[] = "fix acks2/reaxff command:\n\n" "@Article{O'Hearn2020,\n" @@ -175,26 +166,27 @@ void FixACKS2ReaxFF::pertype_parameters(char *arg) const char *line = reader.next_line(); if (!line) - throw parser_error("Invalid parameter file for fix acks2/reaxff"); + throw TokenizerException("Invalid parameter file for fix acks2/reaxff",""); ValueTokenizer values(line); if (values.count() != 1) - throw parser_error("Fix acks2/reaxff: Incorrect format of parameter file"); + throw TokenizerException("Fix acks2/reaxff: Incorrect parameter file format",""); bond_softness = values.next_double(); for (int i = 1; i <= ntypes; i++) { const char *line = reader.next_line(); if (!line) - throw parser_error("Invalid parameter file for fix acks2/reaxff"); + throw TokenizerException("Fix acks2/reaxff: Incorrect parameter file format",""); ValueTokenizer values(line); if (values.count() != 5) - throw parser_error("Fix acks2/reaxff: Incorrect format of parameter file"); + throw TokenizerException("Fix acks2/reaxff: Incorrect parameter file format",""); int itype = values.next_int(); if ((itype < 1) || (itype > ntypes)) - throw parser_error("Fix acks2/reaxff: invalid atom type in parameter file"); + throw TokenizerException("Fix acks2/reaxff: invalid atom type in parameter file", + std::to_string(itype)); chi[itype] = values.next_double(); eta[itype] = values.next_double(); diff --git a/src/REAXFF/fix_qeq_reaxff.cpp b/src/REAXFF/fix_qeq_reaxff.cpp index 89ee3f338f..874a10bbae 100644 --- a/src/REAXFF/fix_qeq_reaxff.cpp +++ b/src/REAXFF/fix_qeq_reaxff.cpp @@ -45,19 +45,11 @@ #include #include #include +#include using namespace LAMMPS_NS; using namespace FixConst; -namespace { - class parser_error : public std::exception { - std::string message; - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - static constexpr double EV_TO_KCAL_PER_MOL = 14.4; static constexpr double SMALL = 1.0e-14; @@ -243,15 +235,16 @@ void FixQEqReaxFF::pertype_parameters(char *arg) for (int i = 1; i <= ntypes; i++) { const char *line = reader.next_line(); if (!line) - throw parser_error("Invalid param file for fix qeq/reaxff"); + throw TokenizerException("Fix qeq/reaxff: Invalid param file format",""); ValueTokenizer values(line); if (values.count() != 4) - throw parser_error("Fix qeq/reaxff: Incorrect format of param file"); + throw TokenizerException("Fix qeq/reaxff: Incorrect format of param file",""); int itype = values.next_int(); if ((itype < 1) || (itype > ntypes)) - throw parser_error("Fix qeq/reaxff: invalid atom type in param file"); + throw TokenizerException("Fix qeq/reaxff: invalid atom type in param file", + std::to_string(itype)); chi[itype] = values.next_double(); eta[itype] = values.next_double(); diff --git a/src/REAXFF/reaxff_control.cpp b/src/REAXFF/reaxff_control.cpp index 90f12b2031..a95ec1d4ac 100644 --- a/src/REAXFF/reaxff_control.cpp +++ b/src/REAXFF/reaxff_control.cpp @@ -40,15 +40,6 @@ using LAMMPS_NS::utils::sfgets; using LAMMPS_NS::utils::logmesg; using LAMMPS_NS::ValueTokenizer; -namespace { - class parser_error : public std::exception { - std::string message; - public: - parser_error(const std::string &mesg) { message = mesg; } - const char *what() const noexcept { return message.c_str(); } - }; -} - namespace ReaxFF { static std::unordered_set inactive_keywords = { "ensemble_type", "nsteps", "dt", "proc_by_dim", "random_vel", @@ -63,6 +54,15 @@ namespace ReaxFF { "energy_update_freq", "atom_info", "atom_velocities", "atom_forces", "bond_info", "angle_info" }; + class control_parser_error : public std::exception { + std::string message; + public: + explicit control_parser_error(const std::string &format, const std::string &keyword) { + message = fmt::format(format, keyword); + } + const char *what() const noexcept { return message.c_str(); } + }; + // NOTE: this function is run on MPI rank 0 only void Read_Control_File(const char *control_file, control_params *control) @@ -92,7 +92,7 @@ namespace ReaxFF { auto keyword = values.next_string(); if (!values.has_next()) - throw parser_error(fmt::format("No value(s) for control parameter: {}\n",keyword)); + throw control_parser_error("No value(s) for control parameter: {}\n", keyword); if (inactive_keywords.find(keyword) != inactive_keywords.end()) { error->warning(FLERR,fmt::format("Ignoring inactive control " @@ -114,8 +114,7 @@ namespace ReaxFF { error->warning(FLERR,"Support for writing native trajectories has " "been removed after LAMMPS version 8 April 2021"); } else { - throw parser_error(fmt::format("Unknown parameter {} in " - "control file", keyword)); + throw control_parser_error("Unknown parameter {} in control file", keyword); } } } catch (LAMMPS_NS::EOFException &) { diff --git a/src/REAXFF/reaxff_ffield.cpp b/src/REAXFF/reaxff_ffield.cpp index db4a63972e..34db1c232d 100644 --- a/src/REAXFF/reaxff_ffield.cpp +++ b/src/REAXFF/reaxff_ffield.cpp @@ -41,16 +41,14 @@ using LAMMPS_NS::utils::open_potential; using LAMMPS_NS::utils::getsyserror; using LAMMPS_NS::utils::uppercase; -namespace { - class parser_error : public std::exception { +namespace ReaxFF { + + class ffield_parser_error : public std::exception { std::string message; public: - parser_error(const std::string &mesg) { message = mesg; } + explicit ffield_parser_error(const std::string &mesg) { message = mesg; } const char *what() const noexcept { return message.c_str(); } }; -} - -namespace ReaxFF { void Read_Force_Field(const char *filename, reax_interaction *reax, control_params *control, MPI_Comm world) @@ -63,7 +61,7 @@ namespace ReaxFF { // read and parse the force field only on rank 0 #define THROW_ERROR(txt) \ - throw parser_error(fmt::format("{}:{}: {}",filename,lineno,txt)) + throw ffield_parser_error(fmt::format("{}:{}: {}",filename,lineno,txt)) if (control->me == 0) { FILE *fp = LAMMPS_NS::utils::open_potential(filename, lmp, nullptr); @@ -165,7 +163,7 @@ namespace ReaxFF { // copy element symbol in uppercase and truncate stored element symbol if necessary auto element = uppercase(values.next_string()); - strncpy(sbp[i].name,element.c_str(),4); + strncpy(sbp[i].name,element.c_str(),3); sbp[i].name[3] = '\0'; sbp[i].r_s = values.next_double(); diff --git a/src/tokenizer.h b/src/tokenizer.h index 247afd0240..060e1bf6e3 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -60,7 +60,7 @@ class TokenizerException : public std::exception { * * \param msg String with error message * \param token String of the token/word that caused the error */ - TokenizerException(const std::string &msg, const std::string &token); + explicit TokenizerException(const std::string &msg, const std::string &token); ~TokenizerException() noexcept {} @@ -74,7 +74,7 @@ class InvalidIntegerException : public TokenizerException { /** Thrown during converting string to integer number * * \param token String of the token/word that caused the error */ - InvalidIntegerException(const std::string &token) : + explicit InvalidIntegerException(const std::string &token) : TokenizerException("Not a valid integer number", token) { } @@ -85,7 +85,7 @@ class InvalidFloatException : public TokenizerException { /** Thrown during converting string to floating point number * * \param token String of the token/word that caused the error */ - InvalidFloatException(const std::string &token) : + explicit InvalidFloatException(const std::string &token) : TokenizerException("Not a valid floating-point number", token) { }