replace non-portable file globbing with LAMMPS utility functions

This commit is contained in:
Axel Kohlmeyer 2022-11-30 12:01:30 -05:00
parent fdeeb3fdbc
commit b543c4caa3
No known key found for this signature in database
GPG Key ID: D9B44E93BF0C375A
2 changed files with 12 additions and 18 deletions

View File

@ -34,7 +34,6 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <glob.h>
#include <random> #include <random>
#include <string> #include <string>
#include <vector> #include <vector>
@ -43,18 +42,6 @@ using namespace LAMMPS_NS;
#define MAXLINE 1024 #define MAXLINE 1024
std::vector<std::string> static globVector(const std::string& pattern, std::vector<std::string> & files)
{
glob_t glob_result;
glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
std::string s = std::string(glob_result.gl_pathv[i]);
files.push_back(s);
}
globfree(&glob_result);
return files;
}
void CFITPOD::command(int narg, char **arg) void CFITPOD::command(int narg, char **arg)
{ {
if (narg < 2) utils::missing_cmd_args(FLERR, "fitpod", error); if (narg < 2) utils::missing_cmd_args(FLERR, "fitpod", error);
@ -236,21 +223,26 @@ void CFITPOD::read_data_file(double *fitting_weights, std::string &file_format,
} }
} }
void CFITPOD::get_exyz_files(std::vector<std::string>& files, std::string datapath, std::string extension) void CFITPOD::get_exyz_files(std::vector<std::string>& files, const std::string &datapath,
const std::string &extension)
{ {
std::vector<std::string> res = globVector(datapath + "/*." + extension, files); auto allfiles = platform::list_directory(datapath);
for (auto fname : allfiles) {
if (utils::strmatch(fname, fmt::format(".*\\.{}$", extension)))
files.push_back(datapath + platform::filepathsep + fname);
}
} }
int CFITPOD::get_number_atom_exyz(std::vector<int>& num_atom, int& num_atom_sum, std::string file) int CFITPOD::get_number_atom_exyz(std::vector<int>& num_atom, int& num_atom_sum, std::string file)
{ {
std::string filename = file; std::string filename = file;
FILE *fp; FILE *fp;
//if (comm->me == 0){ if (comm->me == 0) {
fp = utils::open_potential(filename,lmp,nullptr); fp = utils::open_potential(filename,lmp,nullptr);
if (fp == nullptr) if (fp == nullptr)
error->one(FLERR,"Cannot open POD coefficient file {}: ", filename, utils::getsyserror()); error->one(FLERR,"Cannot open POD coefficient file {}: ", filename, utils::getsyserror());
//} }
char line[MAXLINE],*ptr; char line[MAXLINE],*ptr;
int eof = 0; int eof = 0;
@ -481,6 +473,8 @@ void CFITPOD::get_data(datastruct &data, std::vector<std::string> species)
utils::logmesg(lmp, "number of atoms in all files: {}\n", data.num_atom_sum); utils::logmesg(lmp, "number of atoms in all files: {}\n", data.num_atom_sum);
} }
if (data.data_files.size() < 1) error->all(FLERR, "Cannot fit potential without data files");
int n = data.num_config_sum; int n = data.num_config_sum;
memory->create(data.lattice, 9*n, "fitpod:lattice"); memory->create(data.lattice, 9*n, "fitpod:lattice");
memory->create(data.stress, 9*n, "fitpod:stress"); memory->create(data.stress, 9*n, "fitpod:stress");

View File

@ -121,7 +121,7 @@ public:
void command(int, char **) override; void command(int, char **) override;
void read_data_file(double *fitting_weights, std::string &file_format, std::string &file_extension, void read_data_file(double *fitting_weights, std::string &file_format, std::string &file_extension,
std::string &test_path, std::string &training_path, const std::string &data_file); std::string &test_path, std::string &training_path, const std::string &data_file);
void get_exyz_files(std::vector<std::string>& files, std::string datapath, std::string extension); void get_exyz_files(std::vector<std::string> &, const std::string &, const std::string &);
int get_number_atom_exyz(std::vector<int>& num_atom, int& num_atom_sum, std::string file); int get_number_atom_exyz(std::vector<int>& num_atom, int& num_atom_sum, std::string file);
int get_number_atoms(std::vector<int>& num_atom, std::vector<int> &num_atom_sum, std::vector<int>& num_config, std::vector<std::string> training_files); int get_number_atoms(std::vector<int>& num_atom, std::vector<int> &num_atom_sum, std::vector<int>& num_config, std::vector<std::string> training_files);
void read_exyz_file(double *lattice, double *stress, double *energy, double *pos, double *forces, void read_exyz_file(double *lattice, double *stress, double *energy, double *pos, double *forces,