diff --git a/src/input.cpp b/src/input.cpp index 050b8112a9..5d4760149b 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -251,11 +251,9 @@ void Input::file(const char *filename) error->one(FLERR,"Too many nested levels of input scripts"); infile = fopen(filename,"r"); - if (infile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open input script %s",filename); - error->one(FLERR,str); - } + if (infile == NULL) + error->one(FLERR,fmt::format("Cannot open input script {}",filename)); + infiles[nfile++] = infile; } @@ -545,11 +543,10 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag) value = variable->retrieve(var); } - if (value == NULL) { - char str[128]; - snprintf(str,128,"Substitution for illegal variable %s",var); - error->one(FLERR,str); - } + if (value == NULL) + error->one(FLERR,fmt::format("Substitution for illegal " + "variable {}",var)); + // check if storage in str2 needs to be expanded // re-initialize ptr and ptr2 to the point beyond the variable. @@ -1038,11 +1035,9 @@ void Input::include() error->one(FLERR,"Too many nested levels of input scripts"); infile = fopen(arg[0],"r"); - if (infile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open input script %s",arg[0]); - error->one(FLERR,str); - } + if (infile == NULL) + error->one(FLERR,fmt::format("Cannot open input script {}",arg[0])); + infiles[nfile++] = infile; } @@ -1073,11 +1068,9 @@ void Input::jump() else { if (infile && infile != stdin) fclose(infile); infile = fopen(arg[0],"r"); - if (infile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open input script %s",arg[0]); - error->one(FLERR,str); - } + if (infile == NULL) + error->one(FLERR,fmt::format("Cannot open input script {}",arg[0])); + infiles[nfile-1] = infile; } } @@ -1118,11 +1111,9 @@ void Input::log() if (appendflag) logfile = fopen(arg[0],"a"); else logfile = fopen(arg[0],"w"); - if (logfile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open logfile %s",arg[0]); - error->one(FLERR,str); - } + if (logfile == NULL) + error->one(FLERR,fmt::format("Cannot open logfile {}",arg[0])); + } if (universe->nworlds == 1) universe->ulogfile = logfile; } @@ -1197,11 +1188,9 @@ void Input::print() if (fp != NULL) fclose(fp); if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w"); else fp = fopen(arg[iarg+1],"a"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open print file %s",arg[iarg+1]); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open print file {}", + arg[iarg+1])); } iarg += 2; } else if (strcmp(arg[iarg],"screen") == 0) { @@ -1810,18 +1799,15 @@ void Input::pair_style() { if (narg < 1) error->all(FLERR,"Illegal pair_style command"); if (force->pair) { + std::string style = arg[0]; int match = 0; - if (strcmp(arg[0],force->pair_style) == 0) match = 1; + if (style == force->pair_style) match = 1; if (!match && lmp->suffix_enable) { - char estyle[256]; - if (lmp->suffix) { - snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix); - if (strcmp(estyle,force->pair_style) == 0) match = 1; - } - if (lmp->suffix2) { - snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2); - if (strcmp(estyle,force->pair_style) == 0) match = 1; - } + if (lmp->suffix) + if (style + "/" + lmp->suffix == force->pair_style) match = 1; + + if (lmp->suffix2) + if (style + "/" + lmp->suffix2 == force->pair_style) match = 1; } if (match) { force->pair->settings(narg-1,&arg[1]); diff --git a/src/lammps.cpp b/src/lammps.cpp index f2edaef2a4..67c4d7aea6 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -436,11 +436,9 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : if (universe->me == 0) { if (inflag == 0) infile = stdin; else infile = fopen(arg[inflag],"r"); - if (infile == NULL) { - char str[128]; - snprintf(str,128,"Cannot open input script %s",arg[inflag]); - error->one(FLERR,str); - } + if (infile == NULL) + error->one(FLERR,fmt::format("Cannot open input script {}", + arg[inflag])); } if ((universe->me == 0) && !helpflag) diff --git a/src/library.cpp b/src/library.cpp index 3ba8cd0092..d815afdbb5 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -41,6 +41,7 @@ #include "neighbor.h" #include "neigh_list.h" #include "neigh_request.h" +#include "fmt/format.h" #if defined(LAMMPS_EXCEPTIONS) #include "exceptions.h" @@ -1582,14 +1583,11 @@ void lammps_create_atoms(void *ptr, int n, tagint *id, int *type, // warn if new natoms is not correct - if (lmp->atom->natoms != natoms_prev + n) { - char str[128]; - snprintf(str, 128, "Library warning in lammps_create_atoms, " - "invalid total atoms " BIGINT_FORMAT " " BIGINT_FORMAT, - lmp->atom->natoms,natoms_prev+n); - if (lmp->comm->me == 0) - lmp->error->warning(FLERR,str); - } + if ((lmp->atom->natoms != natoms_prev + n) && (lmp->comm->me == 0)) + lmp->error->warning(FLERR,fmt::format("Library warning in " + "lammps_create_atoms: " + "invalid total atoms {} vs. {}", + lmp->atom->natoms,natoms_prev+n)); } END_CAPTURE } @@ -1607,19 +1605,13 @@ void lammps_set_fix_external_callback(void *ptr, char *id, FixExternalFnPtr call BEGIN_CAPTURE { int ifix = lmp->modify->find_fix(id); - if (ifix < 0) { - char str[128]; - snprintf(str, 128, "Can not find fix with ID '%s'!", id); - lmp->error->all(FLERR,str); - } + if (ifix < 0) + lmp->error->all(FLERR,fmt::format("Can not find fix with ID '{}'!", id)); Fix *fix = lmp->modify->fix[ifix]; - if (strcmp("external",fix->style) != 0){ - char str[128]; - snprintf(str, 128, "Fix '%s' is not of style external!", id); - lmp->error->all(FLERR,str); - } + if (strcmp("external",fix->style) != 0) + lmp->error->all(FLERR,fmt::format("Fix '{}' is not of style external!", id)); FixExternal * fext = (FixExternal*) fix; fext->set_callback(callback, caller); diff --git a/src/memory.cpp b/src/memory.cpp index 4374882efe..873bc754c0 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -14,6 +14,7 @@ #include "memory.h" #include #include "error.h" +#include "fmt/format.h" #if defined(LMP_USER_INTEL) && defined(__INTEL_COMPILER) #ifndef LMP_INTEL_NO_TBB @@ -56,12 +57,9 @@ void *Memory::smalloc(bigint nbytes, const char *name) #else void *ptr = malloc(nbytes); #endif - if (ptr == NULL) { - char str[128]; - sprintf(str,"Failed to allocate " BIGINT_FORMAT " bytes for array %s", - nbytes,name); - error->one(FLERR,str); - } + if (ptr == NULL) + error->one(FLERR,fmt::format("Failed to allocate {} bytes for array {}", + nbytes,name)); return ptr; } @@ -92,12 +90,9 @@ void *Memory::srealloc(void *ptr, bigint nbytes, const char *name) #else ptr = realloc(ptr,nbytes); #endif - if (ptr == NULL) { - char str[128]; - sprintf(str,"Failed to reallocate " BIGINT_FORMAT " bytes for array %s", - nbytes,name); - error->one(FLERR,str); - } + if (ptr == NULL) + error->one(FLERR,fmt::format("Failed to reallocate {} bytes for array {}", + nbytes,name)); return ptr; } @@ -121,8 +116,6 @@ void Memory::sfree(void *ptr) void Memory::fail(const char *name) { - char str[128]; - snprintf(str,128, - "Cannot create/grow a vector/array of pointers for %s",name); - error->one(FLERR,str); + error->one(FLERR,fmt::format("Cannot create/grow a vector/array of " + "pointers for {}",name)); } diff --git a/src/modify.cpp b/src/modify.cpp index b230cabc17..f367def0f4 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -28,6 +28,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace FixConst; @@ -245,21 +246,15 @@ void Modify::init() // error if any fix or compute is using a dynamic group when not allowed for (i = 0; i < nfix; i++) - if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup]) { - char str[128]; - snprintf(str,128, - "Fix %s does not allow use of dynamic group",fix[i]->id); - error->all(FLERR,str); - } + if (!fix[i]->dynamic_group_allow && group->dynamic[fix[i]->igroup]) + error->all(FLERR,fmt::format("Fix {} does not allow use with a " + "dynamic group",fix[i]->id)); for (i = 0; i < ncompute; i++) if (!compute[i]->dynamic_group_allow && - group->dynamic[compute[i]->igroup]) { - char str[128]; - snprintf(str,128,"Compute %s does not allow use of dynamic group", - fix[i]->id); - error->all(FLERR,str); - } + group->dynamic[compute[i]->igroup]) + error->all(FLERR,fmt::format("Compute {} does not allow use with a " + "dynamic group",compute[i]->id)); // warn if any particle is time integrated more than once diff --git a/src/molecule.cpp b/src/molecule.cpp index 1f0a9e1601..50ef344ea9 100644 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -26,6 +26,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -1721,11 +1722,8 @@ void Molecule::deallocate() void Molecule::open(char *file) { fp = fopen(file,"r"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open molecule file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open molecule file {}",file)); } /* ---------------------------------------------------------------------- diff --git a/src/pair_coul_streitz.cpp b/src/pair_coul_streitz.cpp index 4511b34c24..e79d712f32 100644 --- a/src/pair_coul_streitz.cpp +++ b/src/pair_coul_streitz.cpp @@ -31,6 +31,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -249,11 +250,9 @@ void PairCoulStreitz::read_file(char *file) FILE *fp; if (comm->me == 0) { fp = fopen(file,"r"); - if (fp == NULL) { - char str[128]; - snprintf(str,128,"Cannot open coul/streitz potential file %s",file); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open coul/streitz potential " + "file {}",file)); } // read each line out of file, skipping blank lines or leading '#' diff --git a/src/potential_file_reader.cpp b/src/potential_file_reader.cpp index bf886c5f97..ea949ac984 100644 --- a/src/potential_file_reader.cpp +++ b/src/potential_file_reader.cpp @@ -22,6 +22,7 @@ #include "potential_file_reader.h" #include "utils.h" #include "tokenizer.h" +#include "fmt/format.h" #include @@ -36,11 +37,9 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp, fp = force->open_potential(filename.c_str()); - if (fp == NULL) { - char str[128]; - snprintf(str, 128, "cannot open %s potential file %s", potential_name.c_str(), filename.c_str()); - error->one(FLERR, str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Cannot open {} potential file {}", + potential_name, filename)); } PotentialFileReader::~PotentialFileReader() { @@ -49,12 +48,9 @@ PotentialFileReader::~PotentialFileReader() { void PotentialFileReader::skip_line() { char *ptr = fgets(line, MAXLINE, fp); - if (ptr == nullptr) { - // EOF - char str[128]; - snprintf(str, 128, "Missing line in %s potential file!", potential_name.c_str()); - error->one(FLERR, str); - } + if (ptr == nullptr) + error->one(FLERR,fmt::format("Missing line in {} potential file!", + potential_name)); } char *PotentialFileReader::next_line(int nparams) { @@ -83,16 +79,13 @@ char *PotentialFileReader::next_line(int nparams) { if (ptr == nullptr) { // EOF - if (nwords > 0 && nwords < nparams) { - char str[128]; - snprintf(str, 128, "Incorrect format in %s potential file! %d/%d parameters", potential_name.c_str(), nwords, nparams); - error->one(FLERR, str); - } - + if (nwords > 0 && nwords < nparams) + error->one(FLERR,fmt::format("Incorrect format in {} potential " + "file! {}/{} parameters", + potential_name, nwords, nparams)); return nullptr; } - // strip comment if ((ptr = strchr(line, '#'))) *ptr = '\0'; @@ -114,11 +107,9 @@ void PotentialFileReader::next_dvector(int n, double * list) { if (ptr == nullptr) { // EOF - if (i < n) { - char str[128]; - snprintf(str, 128, "Incorrect format in %s potential file! %d/%d values", potential_name.c_str(), i, n); - error->one(FLERR, str); - } + if (i < n) + error->one(FLERR,fmt::format("Incorrect format in {} potential file! " + "{}/{} values", potential_name, i, n)); } ValueTokenizer values(line);