add utility function to get the units tag value from a potential file

This commit is contained in:
Axel Kohlmeyer 2020-06-14 06:22:47 -04:00
parent d84b4a3fff
commit 0481184862
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
3 changed files with 53 additions and 9 deletions

View File

@ -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" {

View File

@ -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);
}
}

View File

@ -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");
}