diff --git a/src/utils.cpp b/src/utils.cpp index f3a5770f5d..90a197da03 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -539,7 +539,7 @@ std::string utils::get_potential_file_path(const std::string& path) { /* ---------------------------------------------------------------------- read first line of potential file - if has DATE field, print following word + if it has a DATE field, return the following word ------------------------------------------------------------------------- */ std::string utils::get_potential_date(const std::string & path, const std::string & potential_name) { @@ -562,6 +562,31 @@ std::string utils::get_potential_date(const std::string & path, const std::strin return ""; } +/* ---------------------------------------------------------------------- + read first line of potential file + if it has UNITS field, return following word +------------------------------------------------------------------------- */ + +std::string utils::get_potential_units(const std::string & path, const std::string & potential_name) { + TextFileReader reader(path, potential_name); + reader.ignore_comments = false; + char * line = nullptr; + + while ((line = reader.next_line())) { + ValueTokenizer values(line); + while (values.has_next()) { + std::string word = values.next_string(); + if (word == "UNITS:") { + if (values.has_next()) { + std::string units = values.next_string(); + return units; + } + } + } + } + return ""; +} + /* ------------------------------------------------------------------ */ extern "C" { diff --git a/src/utils.h b/src/utils.h index ef272087b7..91e5024121 100644 --- a/src/utils.h +++ b/src/utils.h @@ -142,7 +142,6 @@ namespace LAMMPS_NS { tagint tnumeric(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp); - /** * \brief Trim anything from '#' onward * \param line string that should be trimmed @@ -233,6 +232,14 @@ namespace LAMMPS_NS { * \return DATE field if present */ std::string get_potential_date(const std::string & path, const std::string & potential_name); + + /** + * \brief Read potential file and return UNITS field if it is present + * \param path file path + * \param potential_name name of potential that is being read + * \return UNITS field if present + */ + std::string get_potential_units(const std::string & path, const std::string & potential_name); } } diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 317c77cf3f..352cb29766 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -239,22 +239,34 @@ TEST(Utils, getsyserror) { TEST(Utils, potential_file) { FILE *fp; - fp = fopen("ctest.txt","w"); + fp = fopen("ctest1.txt","w"); ASSERT_NE(fp,nullptr); - fputs("# DATE: 2020-02-20 CONTRIBUTOR: Nessuno\n",fp); + fputs("# DATE: 2020-02-20 CONTRIBUTOR: Nessuno UNITS: real\n",fp); + fclose(fp); + fp = fopen("ctest2.txt","w"); + ASSERT_NE(fp,nullptr); + fputs("# CONTRIBUTOR: Pippo\n",fp); fclose(fp); - EXPECT_TRUE(utils::file_is_readable("ctest.txt")); - EXPECT_FALSE(utils::file_is_readable("no_such_file.txt")); + ASSERT_TRUE(utils::file_is_readable("ctest1.txt")); + ASSERT_TRUE(utils::file_is_readable("ctest2.txt")); + ASSERT_FALSE(utils::file_is_readable("no_such_file.txt")); + + ASSERT_THAT(utils::get_potential_file_path("ctest1.txt"),Eq("ctest1.txt")); + ASSERT_THAT(utils::get_potential_file_path("no_such_file.txt"),Eq("")); - EXPECT_THAT(utils::get_potential_file_path("ctest.txt"),Eq("ctest.txt")); const char *folder = getenv("LAMMPS_POTENTIALS"); if (folder != nullptr) { std::string path=utils::path_join(folder,"Cu_u3.eam"); EXPECT_THAT(utils::get_potential_file_path("Cu_u3.eam"),Eq(path)); + EXPECT_THAT(utils::get_potential_units(path,"EAM"),Eq("metal")); } - EXPECT_THAT(utils::get_potential_date("ctest.txt","Test"),Eq("2020-02-20")); + ASSERT_THAT(utils::get_potential_date("ctest1.txt","Test"),Eq("2020-02-20")); + ASSERT_THAT(utils::get_potential_units("ctest1.txt","Test"),Eq("real")); + ASSERT_THAT(utils::get_potential_date("ctest2.txt","Test"),Eq("")); + ASSERT_THAT(utils::get_potential_units("ctest2.txt","Test"),Eq("")); - remove("ctest.txt"); + remove("ctest1.txt"); + remove("ctest2.txt"); }