reformat with clang-format

This commit is contained in:
Axel Kohlmeyer
2021-07-20 18:31:52 -04:00
parent b9cc8c8d24
commit c19d37990d
2 changed files with 71 additions and 67 deletions

View File

@ -1,4 +1,3 @@
// clang-format off
/* -*- c++ -*- ---------------------------------------------------------- /* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories https://www.lammps.org/, Sandia National Laboratories
@ -45,26 +44,20 @@ using namespace LAMMPS_NS;
* \param auto_convert Bitmask of supported unit conversions * \param auto_convert Bitmask of supported unit conversions
*/ */
PotentialFileReader::PotentialFileReader(LAMMPS *lmp, PotentialFileReader::PotentialFileReader(LAMMPS *lmp, const std::string &filename,
const std::string &filename,
const std::string &potential_name, const std::string &potential_name,
const std::string &name_suffix, const std::string &name_suffix, const int auto_convert) :
const int auto_convert) : Pointers(lmp),
Pointers(lmp), reader(nullptr), filename(filename), filetype(potential_name + name_suffix),
reader(nullptr), unit_convert(auto_convert)
filename(filename),
filetype(potential_name + name_suffix),
unit_convert(auto_convert)
{ {
if (comm->me != 0) { if (comm->me != 0) { error->one(FLERR, "FileReader should only be called by proc 0!"); }
error->one(FLERR, "FileReader should only be called by proc 0!");
}
try { try {
reader = open_potential(filename); reader = open_potential(filename);
if (!reader) { if (!reader) {
error->one(FLERR, "cannot open {} potential file {}: {}", error->one(FLERR, "cannot open {} potential file {}: {}", potential_name, filename,
potential_name, filename, utils::getsyserror()); utils::getsyserror());
} }
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -77,30 +70,32 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp,
* \param potential_name Name of potential style for error messages * \param potential_name Name of potential style for error messages
* \param auto_convert Bitmask of supported unit conversions * \param auto_convert Bitmask of supported unit conversions
*/ */
PotentialFileReader::PotentialFileReader(LAMMPS *lmp, PotentialFileReader::PotentialFileReader(LAMMPS *lmp, const std::string &filename,
const std::string &filename,
const std::string &potential_name, const std::string &potential_name,
const int auto_convert) : const int auto_convert) :
PotentialFileReader(lmp, filename, potential_name, " potential", auto_convert) PotentialFileReader(lmp, filename, potential_name, " potential", auto_convert)
{ {
} }
/** Closes the file */ /** Closes the file */
PotentialFileReader::~PotentialFileReader() { PotentialFileReader::~PotentialFileReader()
{
delete reader; delete reader;
} }
/** Set comment (= text after '#') handling preference for the file to be read /** Set comment (= text after '#') handling preference for the file to be read
* *
* \param value Comment text is ignored if true, or not if false */ * \param value Comment text is ignored if true, or not if false */
void PotentialFileReader::ignore_comments(bool value) { void PotentialFileReader::ignore_comments(bool value)
{
reader->ignore_comments = value; reader->ignore_comments = value;
} }
/** Read a line but ignore its content */ /** Read a line but ignore its content */
void PotentialFileReader::skip_line() { void PotentialFileReader::skip_line()
{
try { try {
reader->skip_line(); reader->skip_line();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
@ -119,7 +114,8 @@ void PotentialFileReader::skip_line() {
* \param nparams Number of words that must be read. Default: 0 * \param nparams Number of words that must be read. Default: 0
* \return String with the concatenated text */ * \return String with the concatenated text */
char *PotentialFileReader::next_line(int nparams) { char *PotentialFileReader::next_line(int nparams)
{
try { try {
return reader->next_line(nparams); return reader->next_line(nparams);
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
@ -137,7 +133,8 @@ char *PotentialFileReader::next_line(int nparams) {
* \param list Pointer to array with suitable storage for *n* doubles * \param list Pointer to array with suitable storage for *n* doubles
* \param n Number of doubles to be read */ * \param n Number of doubles to be read */
void PotentialFileReader::next_dvector(double * list, int n) { void PotentialFileReader::next_dvector(double *list, int n)
{
try { try {
return reader->next_dvector(list, n); return reader->next_dvector(list, n);
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
@ -155,7 +152,8 @@ void PotentialFileReader::next_dvector(double * list, int n) {
* \param separators String with list of separators. * \param separators String with list of separators.
* \return ValueTokenizer object for read in text */ * \return ValueTokenizer object for read in text */
ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string &separators) { ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string &separators)
{
try { try {
return reader->next_values(nparams, separators); return reader->next_values(nparams, separators);
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
@ -168,9 +166,10 @@ ValueTokenizer PotentialFileReader::next_values(int nparams, const std::string &
* *
* \return Value of first word in line as double */ * \return Value of first word in line as double */
double PotentialFileReader::next_double() { double PotentialFileReader::next_double()
{
try { try {
char * line = reader->next_line(1); char *line = reader->next_line(1);
return ValueTokenizer(line).next_double(); return ValueTokenizer(line).next_double();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -182,9 +181,10 @@ double PotentialFileReader::next_double() {
* *
* \return Value of first word in line as int */ * \return Value of first word in line as int */
int PotentialFileReader::next_int() { int PotentialFileReader::next_int()
{
try { try {
char * line = reader->next_line(1); char *line = reader->next_line(1);
return ValueTokenizer(line).next_int(); return ValueTokenizer(line).next_int();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -196,9 +196,10 @@ int PotentialFileReader::next_int() {
* *
* \return Value of first word in line as tagint */ * \return Value of first word in line as tagint */
tagint PotentialFileReader::next_tagint() { tagint PotentialFileReader::next_tagint()
{
try { try {
char * line = reader->next_line(1); char *line = reader->next_line(1);
return ValueTokenizer(line).next_tagint(); return ValueTokenizer(line).next_tagint();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -210,9 +211,10 @@ tagint PotentialFileReader::next_tagint() {
* *
* \return Value of first word in line as bigint */ * \return Value of first word in line as bigint */
bigint PotentialFileReader::next_bigint() { bigint PotentialFileReader::next_bigint()
{
try { try {
char * line = reader->next_line(1); char *line = reader->next_line(1);
return ValueTokenizer(line).next_bigint(); return ValueTokenizer(line).next_bigint();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -224,9 +226,10 @@ bigint PotentialFileReader::next_bigint() {
* *
* \return First word of read in line */ * \return First word of read in line */
std::string PotentialFileReader::next_string() { std::string PotentialFileReader::next_string()
{
try { try {
char * line = reader->next_line(1); char *line = reader->next_line(1);
return ValueTokenizer(line).next_string(); return ValueTokenizer(line).next_string();
} catch (FileReaderException &e) { } catch (FileReaderException &e) {
error->one(FLERR, e.what()); error->one(FLERR, e.what());
@ -246,17 +249,17 @@ std::string PotentialFileReader::next_string() {
* \param path Path of the potential file to open * \param path Path of the potential file to open
* \return Pointer to TextFileReader object created */ * \return Pointer to TextFileReader object created */
TextFileReader *PotentialFileReader::open_potential(const std::string &path) { TextFileReader *PotentialFileReader::open_potential(const std::string &path)
{
std::string filepath = utils::get_potential_file_path(path); std::string filepath = utils::get_potential_file_path(path);
if (!filepath.empty()) { if (!filepath.empty()) {
std::string unit_style = lmp->update->unit_style; std::string unit_style = lmp->update->unit_style;
std::string date = utils::get_potential_date(filepath, filetype); std::string date = utils::get_potential_date(filepath, filetype);
std::string units = utils::get_potential_units(filepath, filetype); std::string units = utils::get_potential_units(filepath, filetype);
if (!date.empty()) if (!date.empty())
utils::logmesg(lmp,"Reading {} file {} with DATE: {}\n", utils::logmesg(lmp, "Reading {} file {} with DATE: {}\n", filetype, filename, date);
filetype, filename, date);
if (units.empty()) { if (units.empty()) {
unit_convert = utils::NOCONVERT; unit_convert = utils::NOCONVERT;
@ -266,18 +269,18 @@ TextFileReader *PotentialFileReader::open_potential(const std::string &path) {
} else { } else {
if ((units == "metal") && (unit_style == "real") && (unit_convert & utils::METAL2REAL)) { if ((units == "metal") && (unit_style == "real") && (unit_convert & utils::METAL2REAL)) {
unit_convert = utils::METAL2REAL; unit_convert = utils::METAL2REAL;
} else if ((units == "real") && (unit_style == "metal") && (unit_convert & utils::REAL2METAL)) { } else if ((units == "real") && (unit_style == "metal") &&
(unit_convert & utils::REAL2METAL)) {
unit_convert = utils::REAL2METAL; unit_convert = utils::REAL2METAL;
} else { } else {
lmp->error->one(FLERR, "{} file {} requires {} units " lmp->error->one(FLERR, "{} file {} requires {} units but {} units are in use", filetype,
"but {} units are in use", filetype, filename, units, unit_style);
filename, units, unit_style);
} }
} }
} }
if (unit_convert != utils::NOCONVERT) if (unit_convert != utils::NOCONVERT)
lmp->error->warning(FLERR, "Converting {} in {} units to {} units", lmp->error->warning(FLERR, "Converting {} in {} units to {} units", filetype, units,
filetype, units, unit_style); unit_style);
return new TextFileReader(filepath, filetype); return new TextFileReader(filepath, filetype);
} }
return nullptr; return nullptr;

View File

@ -1,4 +1,3 @@
// clang-format off
/* -*- c++ -*- ---------------------------------------------------------- /* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories https://www.lammps.org/, Sandia National Laboratories
@ -41,14 +40,14 @@ using namespace LAMMPS_NS;
* \param filename Name of file to be read * \param filename Name of file to be read
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) TextFileReader::TextFileReader(const std::string &filename, const std::string &filetype) :
: filetype(filetype), closefp(true), ignore_comments(true) filetype(filetype), closefp(true), ignore_comments(true)
{ {
fp = fopen(filename.c_str(), "r"); fp = fopen(filename.c_str(), "r");
if (fp == nullptr) { if (fp == nullptr) {
throw FileReaderException(fmt::format("cannot open {} file {}: {}", throw FileReaderException(
filetype, filename, utils::getsyserror())); fmt::format("cannot open {} file {}: {}", filetype, filename, utils::getsyserror()));
} }
} }
@ -69,21 +68,23 @@ This function is useful in combination with :cpp:func:`utils::open_potential`.
* \param fp File descriptor of the already opened file * \param fp File descriptor of the already opened file
* \param filetype Description of file type for error messages */ * \param filetype Description of file type for error messages */
TextFileReader::TextFileReader(FILE *fp, const std::string &filetype) TextFileReader::TextFileReader(FILE *fp, const std::string &filetype) :
: filetype(filetype), closefp(false), fp(fp), ignore_comments(true) filetype(filetype), closefp(false), fp(fp), ignore_comments(true)
{ {
if (fp == nullptr) throw FileReaderException("Invalid file descriptor"); if (fp == nullptr) throw FileReaderException("Invalid file descriptor");
} }
/** Closes the file */ /** Closes the file */
TextFileReader::~TextFileReader() { TextFileReader::~TextFileReader()
{
if (closefp) fclose(fp); if (closefp) fclose(fp);
} }
/** Read the next line and ignore it */ /** Read the next line and ignore it */
void TextFileReader::skip_line() { void TextFileReader::skip_line()
{
char *ptr = fgets(line, MAXLINE, fp); char *ptr = fgets(line, MAXLINE, fp);
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
@ -105,7 +106,8 @@ void TextFileReader::skip_line() {
* \param nparams Number of words that must be read. Default: 0 * \param nparams Number of words that must be read. Default: 0
* \return String with the concatenated text */ * \return String with the concatenated text */
char *TextFileReader::next_line(int nparams) { char *TextFileReader::next_line(int nparams)
{
// concatenate lines until have nparams words // concatenate lines until have nparams words
int n = 0; int n = 0;
int nwords = 0; int nwords = 0;
@ -129,7 +131,8 @@ char *TextFileReader::next_line(int nparams) {
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
if (nwords > 0 && nwords < nparams) { if (nwords > 0 && nwords < nparams) {
throw EOFException(fmt::format("Incorrect format in {} file! {}/{} parameters", filetype, nwords, nparams)); throw EOFException(fmt::format("Incorrect format in {} file! {}/{} parameters", filetype,
nwords, nparams));
} }
return nullptr; return nullptr;
} }
@ -140,9 +143,7 @@ char *TextFileReader::next_line(int nparams) {
nwords += utils::count_words(&line[n]); nwords += utils::count_words(&line[n]);
// skip line if blank // skip line if blank
if (nwords > 0) { if (nwords > 0) { n = strlen(line); }
n = strlen(line);
}
} }
return line; return line;
@ -157,7 +158,8 @@ char *TextFileReader::next_line(int nparams) {
* \param list Pointer to array with suitable storage for *n* doubles * \param list Pointer to array with suitable storage for *n* doubles
* \param n Number of doubles to be read */ * \param n Number of doubles to be read */
void TextFileReader::next_dvector(double * list, int n) { void TextFileReader::next_dvector(double *list, int n)
{
int i = 0; int i = 0;
while (i < n) { while (i < n) {
char *ptr = next_line(); char *ptr = next_line();
@ -165,14 +167,13 @@ void TextFileReader::next_dvector(double * list, int n) {
if (ptr == nullptr) { if (ptr == nullptr) {
// EOF // EOF
if (i < n) { if (i < n) {
throw FileReaderException(fmt::format("Incorrect format in {} file! {}/{} values", filetype, i, n)); throw FileReaderException(
fmt::format("Incorrect format in {} file! {}/{} values", filetype, i, n));
} }
} }
ValueTokenizer values(line); ValueTokenizer values(line);
while (values.has_next()) { while (values.has_next()) { list[i++] = values.next_double(); }
list[i++] = values.next_double();
}
} }
} }
@ -186,9 +187,9 @@ void TextFileReader::next_dvector(double * list, int n) {
* \param separators String with list of separators. * \param separators String with list of separators.
* \return ValueTokenizer object for read in text */ * \return ValueTokenizer object for read in text */
ValueTokenizer TextFileReader::next_values(int nparams, const std::string &separators) { ValueTokenizer TextFileReader::next_values(int nparams, const std::string &separators)
{
char *ptr = next_line(nparams); char *ptr = next_line(nparams);
if (ptr == nullptr) if (ptr == nullptr) throw EOFException(fmt::format("Missing line in {} file!", filetype));
throw EOFException(fmt::format("Missing line in {} file!", filetype));
return ValueTokenizer(line, separators); return ValueTokenizer(line, separators);
} }