From 05ff352021d6a29fd29046e37a5286aef6546eee Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 29 Aug 2020 22:08:16 -0400 Subject: [PATCH] add utils::open_potential() function to utils namespace --- src/utils.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 18 +++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/utils.cpp b/src/utils.cpp index 46a369c02d..b6a6df9311 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -921,6 +921,62 @@ double utils::get_conversion_factor(const int property, const int conversion) return 0.0; } +/* ---------------------------------------------------------------------- + open a potential file as specified by name + if fails, search in dir specified by env variable LAMMPS_POTENTIALS +------------------------------------------------------------------------- */ + +FILE *utils::open_potential(const char *name, LAMMPS *lmp, int *auto_convert) +{ + auto error = lmp->error; + auto me = lmp->comm->me; + + std::string filepath = get_potential_file_path(name); + + if(!filepath.empty()) { + std::string unit_style = lmp->update->unit_style; + std::string date = get_potential_date(filepath, "potential"); + std::string units = get_potential_units(filepath, "potential"); + + if(!date.empty() && (me == 0)) { + logmesg(lmp, fmt::format("Reading potential file {} " + "with DATE: {}\n", name, date)); + } + + if (auto_convert == nullptr) { + if (!units.empty() && (units != unit_style) && (me == 0)) { + error->one(FLERR, fmt::format("Potential file {} requires {} units " + "but {} units are in use", name, units, + unit_style)); + return nullptr; + } + } else { + if (units.empty() || units == unit_style) { + *auto_convert = NOCONVERT; + } else { + if ((units == "metal") && (unit_style == "real") + && (*auto_convert & METAL2REAL)) { + *auto_convert = METAL2REAL; + } else if ((units == "real") && (unit_style == "metal") + && (*auto_convert & REAL2METAL)) { + *auto_convert = REAL2METAL; + } else { + error->one(FLERR, fmt::format("Potential file {} requires {} units " + "but {} units are in use", name, + units, unit_style)); + return nullptr; + } + } + if ((*auto_convert != NOCONVERT) && (me == 0)) + error->warning(FLERR, fmt::format("Converting potential file in " + "{} units to {} units", + units, unit_style)); + } + return fopen(filepath.c_str(), "r"); + } + return nullptr; +} + /* ---------------------------------------------------------------------- convert a timespec ([[HH:]MM:]SS) to seconds the strings "off" and "unlimited" result in -1.0; diff --git a/src/utils.h b/src/utils.h index 2fb3c839a5..2b647cfd87 100644 --- a/src/utils.h +++ b/src/utils.h @@ -367,6 +367,24 @@ and *nhi* according to the following five cases: */ double get_conversion_factor(const int property, const int conversion); + /** Open a potential file as specified by *name* + * +\verbatim embed:rst +If opening the file fails, it will search for it in the folder(s) +pointed to by the environment variable LAMMPS_POTENTIALS. + +If the potential file has a ``UNITS`` tag in the first line, its +value is compared to the current :doc:`unit style ` setting. +\endverbatim + * + * \param name file- or pathname of the potential file + * \param lmp pointer to top-level LAMMPS class instance + * \param auto_convert pointer to automatic unit conversion bitmask + * \return FILE pointer of the opened potential file or NULL + */ + FILE *open_potential(const char *name, LAMMPS *lmp, + int *auto_convert=nullptr); + /** Convert a time string to seconds * * The strings "off" and "unlimited" result in -1