Move common potentials opening code to utils

This commit is contained in:
Richard Berger
2020-06-04 17:18:28 -04:00
parent 7ab65c05da
commit 4a6d3bc0e0
6 changed files with 85 additions and 151 deletions

View File

@ -1037,102 +1037,18 @@ tagint Force::tnumeric(const char *file, int line, char *str)
FILE *Force::open_potential(const char *name)
{
FILE *fp;
std::string filepath = utils::get_potential_file_path(name);
std::string date;
if (name == NULL) return NULL;
if(!filepath.empty()) {
date = utils::get_potential_date(filepath, "potential");
// attempt to open file directly
// if successful, return ptr
fp = fopen(name,"r");
if (fp) {
if (comm->me == 0) potential_date(fp,name);
rewind(fp);
return fp;
}
// try the environment variable directory
const char *path = getenv("LAMMPS_POTENTIALS");
if (path == NULL) return NULL;
const char *pot = potential_name(name);
if (pot == NULL) return NULL;
size_t len1 = strlen(path);
size_t len2 = strlen(pot);
char *newpath = new char[len1+len2+2];
strcpy(newpath,path);
#if defined(_WIN32)
newpath[len1] = '\\';
newpath[len1+1] = 0;
#else
newpath[len1] = '/';
newpath[len1+1] = 0;
#endif
strcat(newpath,pot);
fp = fopen(newpath,"r");
if (fp) {
if (comm->me == 0) potential_date(fp,name);
rewind(fp);
}
delete [] newpath;
return fp;
}
/* ----------------------------------------------------------------------
strip off leading part of path, return just the filename
------------------------------------------------------------------------- */
const char *Force::potential_name(const char *path)
{
const char *pot;
if (path == NULL) return NULL;
#if defined(_WIN32)
// skip over the disk drive part of windows pathnames
if (isalpha(path[0]) && path[1] == ':')
path += 2;
#endif
for (pot = path; *path != '\0'; ++path) {
#if defined(_WIN32)
if ((*path == '\\') || (*path == '/')) pot = path + 1;
#else
if (*path == '/') pot = path + 1;
#endif
}
return pot;
}
/* ----------------------------------------------------------------------
read first line of potential file
if has DATE field, print following word
------------------------------------------------------------------------- */
void Force::potential_date(FILE *fp, const char *name)
{
char line[MAXLINE];
char *ptr = fgets(line,MAXLINE,fp);
if (ptr == NULL) return;
char *word;
word = strtok(line," \t\n\r\f");
while (word) {
if (strcmp(word,"DATE:") == 0) {
word = strtok(NULL," \t\n\r\f");
if (word == NULL) return;
utils::logmesg(lmp,fmt::format("Reading potential "
"file {} with DATE: {}",name,word));
return;
if(!date.empty()) {
utils::logmesg(lmp, fmt::format("Reading potential file {} with DATE: {}", name, date));
}
word = strtok(NULL," \t\n\r\f");
return fopen(filepath.c_str(), "r");
}
return nullptr;
}
/* ----------------------------------------------------------------------

View File

@ -136,8 +136,6 @@ class Force : protected Pointers {
tagint tnumeric(const char *, int, char *);
FILE *open_potential(const char *);
const char *potential_name(const char *);
void potential_date(FILE *, const char *);
bigint memory_usage();

View File

@ -142,66 +142,17 @@ std::string PotentialFileReader::next_string() {
return "";
}
/* ----------------------------------------------------------------------
open a potential file as specified by name
if fails, search in dir specified by env variable LAMMPS_POTENTIALS
------------------------------------------------------------------------- */
TextFileReader * PotentialFileReader::open_potential(const std::string& path) {
// attempt to open file directly
// if successful, return filename
std::string filepath = path;
std::string filename = utils::path_basename(path);
std::string filepath = utils::get_potential_file_path(path);
std::string date;
if(utils::file_is_readable(filepath)) {
date = get_potential_date(filepath);
} else {
// try the environment variable directory
const char *path = getenv("LAMMPS_POTENTIALS");
if(!filepath.empty()) {
date = utils::get_potential_date(filepath, filetype);
if (path != nullptr){
std::string pot = utils::path_basename(filepath);
filepath = utils::path_join(path, pot);
if (utils::file_is_readable(filepath)) {
date = get_potential_date(filepath);
} else {
return nullptr;
}
} else {
return nullptr;
if(!date.empty()) {
utils::logmesg(lmp, fmt::format("Reading potential file {} with DATE: {}", filename, date));
}
return new TextFileReader(filepath, filetype);
}
if(!date.empty()) {
utils::logmesg(lmp, fmt::format("Reading potential file {} with DATE: {}", filename, date));
}
return new TextFileReader(filepath, filetype);
}
/* ----------------------------------------------------------------------
read first line of potential file
if has DATE field, print following word
------------------------------------------------------------------------- */
std::string PotentialFileReader::get_potential_date(const std::string & path) {
TextFileReader reader(path, filetype);
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 == "DATE:") {
if (values.has_next()) {
std::string date = values.next_string();
return date;
}
}
}
}
return "";
return nullptr;
}

View File

@ -33,7 +33,6 @@ namespace LAMMPS_NS
std::string filetype;
TextFileReader * open_potential(const std::string& path);
std::string get_potential_date(const std::string & path);
public:
PotentialFileReader(class LAMMPS *lmp, const std::string &filename, const std::string &potential_name);

View File

@ -17,6 +17,7 @@
#include "lammps.h"
#include "error.h"
#include "tokenizer.h"
#include "text_file_reader.h"
#include "fmt/format.h"
#if defined(__linux__)
@ -450,6 +451,59 @@ bool utils::file_is_readable(const std::string & path) {
return false;
}
/* ----------------------------------------------------------------------
try to find potential file as specified by name
search current directory and the LAMMPS_POTENTIALS directory if
specified
------------------------------------------------------------------------- */
std::string utils::get_potential_file_path(const std::string& path) {
std::string filepath = path;
std::string filename = utils::path_basename(path);
if(utils::file_is_readable(filepath)) {
return filepath;
} else {
// try the environment variable directory
const char *path = getenv("LAMMPS_POTENTIALS");
if (path != nullptr){
std::string pot = utils::path_basename(filepath);
filepath = utils::path_join(path, pot);
if (utils::file_is_readable(filepath)) {
return filepath;
}
}
}
return "";
}
/* ----------------------------------------------------------------------
read first line of potential file
if has DATE field, print following word
------------------------------------------------------------------------- */
std::string utils::get_potential_date(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 == "DATE:") {
if (values.has_next()) {
std::string date = values.next_string();
return date;
}
}
}
}
return "";
}
/* ------------------------------------------------------------------ */
extern "C" {

View File

@ -193,6 +193,22 @@ namespace LAMMPS_NS {
* \return true if file exists and is readable
*/
bool file_is_readable(const std::string & path);
/**
* \brief Determine full path of potential file
* If file is not found in current directory, search LAMMPS_POTENTIALS folder
* \param path file path
* \return full path to potential file
*/
std::string get_potential_file_path(const std::string& path);
/**
* \brief Read potential file and return DATE field if it is present
* \param path file path
* \param potential_name name of potential that is being read
* \return DATE field if present
*/
std::string get_potential_date(const std::string & path, const std::string & potential_name);
}
}