more use of fmtlib and std::string

This commit is contained in:
Axel Kohlmeyer
2020-06-25 21:32:30 -04:00
parent 4816c5c7cc
commit e4a3a518f7
12 changed files with 94 additions and 162 deletions

View File

@ -848,16 +848,12 @@ bool Info::is_active(const char *category, const char *name)
if (!match && lmp->suffix_enable) {
if (lmp->suffix) {
char *name_w_suffix = new char [len + 2 + strlen(lmp->suffix)];
sprintf(name_w_suffix,"%s/%s",name,lmp->suffix);
if (strcmp(style,name_w_suffix) == 0) match = 1;
delete[] name_w_suffix;
std::string name_w_suffix = name + std::string("/") + lmp->suffix;
if (name_w_suffix == style) match = 1;
}
if (!match && lmp->suffix2) {
char *name_w_suffix = new char [len + 2 + strlen(lmp->suffix2)];
sprintf(name_w_suffix,"%s/%s",name,lmp->suffix2);
if (strcmp(style,name_w_suffix) == 0) match = 1;
delete[] name_w_suffix;
std::string name_w_suffix = name + std::string("/") + lmp->suffix2;
if (name_w_suffix == style) match = 1;
}
}
return match ? true : false;

View File

@ -227,11 +227,8 @@ void Input::file()
// execute the command
if (execute_command()) {
char *str = new char[maxline+32];
sprintf(str,"Unknown command: %s",line);
error->all(FLERR,str);
}
if (execute_command())
error->all(FLERR,fmt::format("Unknown command: {}",line));
}
}
@ -1249,7 +1246,7 @@ char *shell_failed_message(const char* cmd, int errnum)
const char *errmsg = strerror(errnum);
int len = strlen(cmd)+strlen(errmsg)+64;
char *msg = new char[len];
sprintf(msg,"Shell command '%s' failed with error '%s'", cmd, errmsg);
snprintf(msg, len, "Shell command '%s' failed with error '%s'", cmd, errmsg);
return msg;
}

View File

@ -16,6 +16,7 @@
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include "atom.h"
#include "comm.h"
#include "force.h"
@ -25,6 +26,7 @@
#include "error.h"
#include "suffix.h"
#include "domain.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -307,10 +309,10 @@ void KSpace::qsum_qsq(int warning_flag)
// so issue warning or error
if (fabs(qsum) > SMALL) {
char str[128];
sprintf(str,"System is not charge neutral, net charge = %g",qsum);
if (!warn_nonneutral) error->all(FLERR,str);
if (warn_nonneutral == 1 && comm->me == 0) error->warning(FLERR,str);
std::string message = fmt::format("System is not charge neutral, net "
"charge = {}",qsum);
if (!warn_nonneutral) error->all(FLERR,message);
if (warn_nonneutral == 1 && comm->me == 0) error->warning(FLERR,message);
warn_nonneutral = 2;
}
}
@ -324,12 +326,10 @@ double KSpace::estimate_table_accuracy(double q2_over_sqrt, double spr)
double table_accuracy = 0.0;
int nctb = force->pair->ncoultablebits;
if (comm->me == 0) {
char str[128];
if (nctb)
sprintf(str," using %d-bit tables for long-range coulomb",nctb);
error->message(FLERR,fmt::format(" using {}-bit tables for long-range coulomb",nctb));
else
sprintf(str," using polynomial approximation for long-range coulomb");
error->message(FLERR,str);
error->message(FLERR," using polynomial approximation for long-range coulomb");
}
if (nctb) {

View File

@ -667,15 +667,12 @@ LAMMPS::~LAMMPS()
double totalclock = MPI_Wtime() - initclock;
if ((me == 0) && (screen || logfile)) {
char outtime[128];
int seconds = fmod(totalclock,60.0);
totalclock = (totalclock - seconds) / 60.0;
int minutes = fmod(totalclock,60.0);
int hours = (totalclock - minutes) / 60.0;
sprintf(outtime,"Total wall time: "
"%d:%02d:%02d\n", hours, minutes, seconds);
if (screen) fputs(outtime,screen);
if (logfile) fputs(outtime,logfile);
utils::logmesg(this,fmt::format("Total wall time: {}:{:02d}:{:02d}\n",
hours, minutes, seconds));
}
if (universe->nworlds == 1) {

View File

@ -13,6 +13,7 @@
#include "modify.h"
#include <cstring>
#include <string>
#include "style_compute.h"
#include "style_fix.h"
#include "atom.h"
@ -836,18 +837,17 @@ void Modify::add_fix(int narg, char **arg, int trysuffix)
int match = 0;
if (strcmp(arg[2],fix[ifix]->style) == 0) match = 1;
if (!match && trysuffix && lmp->suffix_enable) {
char estyle[256];
if (lmp->suffix) {
sprintf(estyle,"%s/%s",arg[2],lmp->suffix);
if (strcmp(estyle,fix[ifix]->style) == 0) match = 1;
std::string estyle = arg[2] + std::string("/") + lmp->suffix;
if (estyle == fix[ifix]->style) match = 1;
}
if (lmp->suffix2) {
sprintf(estyle,"%s/%s",arg[2],lmp->suffix2);
if (strcmp(estyle,fix[ifix]->style) == 0) match = 1;
std::string estyle = arg[2] + std::string("/") + lmp->suffix2;
if (estyle == fix[ifix]->style) match = 1;
}
}
if (!match) error->all(FLERR,
"Replacing a fix, but new style != old style");
if (!match)
error->all(FLERR,"Replacing a fix, but new style != old style");
if (fix[ifix]->igroup != igroup && comm->me == 0)
error->warning(FLERR,"Replacing a fix, but new group != old group");
@ -870,26 +870,24 @@ void Modify::add_fix(int narg, char **arg, int trysuffix)
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
int n = strlen(arg[2])+strlen(lmp->suffix)+2;
char *estyle = new char[n];
sprintf(estyle,"%s/%s",arg[2],lmp->suffix);
std::string estyle = arg[2] + std::string("/") + lmp->suffix;
if (fix_map->find(estyle) != fix_map->end()) {
FixCreator fix_creator = (*fix_map)[estyle];
fix[ifix] = fix_creator(lmp,narg,arg);
delete[] fix[ifix]->style;
fix[ifix]->style = estyle;
} else delete[] estyle;
fix[ifix]->style = new char[estyle.size()+1];
strcpy(fix[ifix]->style,estyle.c_str());
}
}
if (fix[ifix] == NULL && lmp->suffix2) {
int n = strlen(arg[2])+strlen(lmp->suffix2)+2;
char *estyle = new char[n];
sprintf(estyle,"%s/%s",arg[2],lmp->suffix2);
std::string estyle = arg[2] + std::string("/") + lmp->suffix2;
if (fix_map->find(estyle) != fix_map->end()) {
FixCreator fix_creator = (*fix_map)[estyle];
fix[ifix] = fix_creator(lmp,narg,arg);
delete[] fix[ifix]->style;
fix[ifix]->style = estyle;
} else delete[] estyle;
fix[ifix]->style = new char[estyle.size()+1];
strcpy(fix[ifix]->style,estyle.c_str());
}
}
}
@ -1164,7 +1162,7 @@ int Modify::check_rigid_list_overlap(int *select)
int n = 0;
for (int ifix = 0; ifix < nfix; ifix++) {
if (strncmp("rigid",fix[ifix]->style,5) == 0) {
if (utils::strmatch(fix[ifix]->style,"^rigid")) {
const int * const body = (const int *)fix[ifix]->extract("body",dim);
if ((body == NULL) || (dim != 1)) break;
@ -1209,26 +1207,24 @@ void Modify::add_compute(int narg, char **arg, int trysuffix)
if (trysuffix && lmp->suffix_enable) {
if (lmp->suffix) {
int n = strlen(arg[2])+strlen(lmp->suffix)+2;
char *estyle = new char[n];
sprintf(estyle,"%s/%s",arg[2],lmp->suffix);
std::string estyle = arg[2] + std::string("/") + lmp->suffix;
if (compute_map->find(estyle) != compute_map->end()) {
ComputeCreator compute_creator = (*compute_map)[estyle];
compute[ncompute] = compute_creator(lmp,narg,arg);
delete[] compute[ncompute]->style;
compute[ncompute]->style = estyle;
} else delete[] estyle;
compute[ncompute]->style = new char[estyle.size()+1];
strcpy(compute[ncompute]->style,estyle.c_str());
}
}
if (compute[ncompute] == NULL && lmp->suffix2) {
int n = strlen(arg[2])+strlen(lmp->suffix2)+2;
char *estyle = new char[n];
sprintf(estyle,"%s/%s",arg[2],lmp->suffix2);
std::string estyle = arg[2] + std::string("/") + lmp->suffix2;
if (compute_map->find(estyle) != compute_map->end()) {
ComputeCreator compute_creator = (*compute_map)[estyle];
compute[ncompute] = compute_creator(lmp,narg,arg);
delete[] compute[ncompute]->style;
compute[ncompute]->style = estyle;
} else delete[] estyle;
compute[ncompute]->style = new char[estyle.size()+1];
strcpy(compute[ncompute]->style,estyle.c_str());
}
}
}

View File

@ -97,10 +97,7 @@ void NTopoImproperPartial::build()
int all;
MPI_Allreduce(&nmissing,&all,1,MPI_INT,MPI_SUM,world);
if (all) {
char str[128];
sprintf(str,
"Improper atoms missing at step " BIGINT_FORMAT,update->ntimestep);
if (me == 0) error->warning(FLERR,str);
}
if (all && me == 0)
error->warning(FLERR,fmt::format("Improper atoms missing at step {}",
update->ntimestep));
}

View File

@ -116,37 +116,28 @@ void PairTable::compute(int eflag, int vflag)
if (rsq < cutsq[itype][jtype]) {
tb = &tables[tabindex[itype][jtype]];
if (rsq < tb->innersq) {
sprintf(estr,"Pair distance < table inner cutoff: "
"ijtype %d %d dist %g",itype,jtype,sqrt(rsq));
error->one(FLERR,estr);
}
if (rsq < tb->innersq)
error->one(FLERR,fmt::format("Pair distance < table inner cutoff: "
"ijtype {} {} dist {}",itype,jtype,sqrt(rsq)));
if (tabstyle == LOOKUP) {
itable = static_cast<int> ((rsq - tb->innersq) * tb->invdelta);
if (itable >= tlm1) {
sprintf(estr,"Pair distance > table outer cutoff: "
"ijtype %d %d dist %g",itype,jtype,sqrt(rsq));
error->one(FLERR,estr);
}
if (itable >= tlm1)
error->one(FLERR,fmt::format("Pair distance > table outer cutoff: "
"ijtype {} {} dist {}",itype,jtype,sqrt(rsq)));
fpair = factor_lj * tb->f[itable];
} else if (tabstyle == LINEAR) {
itable = static_cast<int> ((rsq - tb->innersq) * tb->invdelta);
if (itable >= tlm1) {
sprintf(estr,"Pair distance > table outer cutoff: "
"ijtype %d %d dist %g",itype,jtype,sqrt(rsq));
error->one(FLERR,estr);
}
if (itable >= tlm1)
error->one(FLERR,fmt::format("Pair distance > table outer cutoff: "
"ijtype {} {} dist {}",itype,jtype,sqrt(rsq)));
fraction = (rsq - tb->rsq[itable]) * tb->invdelta;
value = tb->f[itable] + fraction*tb->df[itable];
fpair = factor_lj * value;
} else if (tabstyle == SPLINE) {
itable = static_cast<int> ((rsq - tb->innersq) * tb->invdelta);
if (itable >= tlm1) {
sprintf(estr,"Pair distance > table outer cutoff: "
"ijtype %d %d dist %g",itype,jtype,sqrt(rsq));
error->one(FLERR,estr);
}
if (itable >= tlm1)
error->one(FLERR,fmt::format("Pair distance > table outer cutoff: "
"ijtype {} {} dist {}",itype,jtype,sqrt(rsq)));
b = (rsq - tb->rsq[itable]) * tb->invdelta;
a = 1.0 - b;
value = a * tb->f[itable] + b * tb->f[itable+1] +

View File

@ -1157,11 +1157,8 @@ void ReadData::header(int firstpass)
parse_keyword(1);
for (n = 0; n < NSECTIONS; n++)
if (strcmp(keyword,section_keywords[n]) == 0) break;
if (n == NSECTIONS) {
char str[128];
sprintf(str,"Unknown identifier in data file: %s",keyword);
error->all(FLERR,str);
}
if (n == NSECTIONS)
error->all(FLERR,fmt::format("Unknown identifier in data file: {}",keyword));
// error checks on header values
// must be consistent with atom style and other header values

View File

@ -289,13 +289,9 @@ bigint ReadDump::seek(bigint nrequest, int exact)
for (ifile = 0; ifile < nfile; ifile++) {
ntimestep = -1;
if (multiproc) {
char *ptr = strchr(files[ifile],'%');
char *multiname = new char[strlen(files[ifile]) + 16];
*ptr = '\0';
sprintf(multiname,"%s%d%s",files[ifile],0,ptr+1);
*ptr = '%';
readers[0]->open_file(multiname);
delete [] multiname;
std::string multiname = files[ifile];
multiname.replace(multiname.find("%"),1,"0");
readers[0]->open_file(multiname.c_str());
} else readers[0]->open_file(files[ifile]);
while (1) {
@ -337,13 +333,9 @@ bigint ReadDump::seek(bigint nrequest, int exact)
if (multiproc && filereader) {
for (int i = 0; i < nreader; i++) {
if (me == 0 && i == 0) continue; // proc 0, reader 0 already found it
char *ptr = strchr(files[currentfile],'%');
char *multiname = new char[strlen(files[currentfile]) + 16];
*ptr = '\0';
sprintf(multiname,"%s%d%s",files[currentfile],firstfile+i,ptr+1);
*ptr = '%';
readers[i]->open_file(multiname);
delete [] multiname;
std::string multiname = files[currentfile];
multiname.replace(multiname.find("%"),1,fmt::format("{}",firstfile+i));
readers[i]->open_file(multiname.c_str());
bigint step;
while (1) {
@ -389,13 +381,9 @@ bigint ReadDump::next(bigint ncurrent, bigint nlast, int nevery, int nskip)
ntimestep = -1;
if (ifile != currentfile) {
if (multiproc) {
char *ptr = strchr(files[ifile],'%');
char *multiname = new char[strlen(files[ifile]) + 16];
*ptr = '\0';
sprintf(multiname,"%s%d%s",files[ifile],0,ptr+1);
*ptr = '%';
readers[0]->open_file(multiname);
delete [] multiname;
std::string multiname = files[ifile];
multiname.replace(multiname.find("%"),1,"0");
readers[0]->open_file(multiname.c_str());
} else readers[0]->open_file(files[ifile]);
}
@ -447,13 +435,9 @@ bigint ReadDump::next(bigint ncurrent, bigint nlast, int nevery, int nskip)
if (multiproc && filereader) {
for (int i = 0; i < nreader; i++) {
if (me == 0 && i == 0) continue;
char *ptr = strchr(files[currentfile],'%');
char *multiname = new char[strlen(files[currentfile]) + 16];
*ptr = '\0';
sprintf(multiname,"%s%d%s",files[currentfile],firstfile+i,ptr+1);
*ptr = '%';
readers[i]->open_file(multiname);
delete [] multiname;
std::string multiname = files[currentfile];
multiname.replace(multiname.find("%"),1,fmt::format("{}",firstfile+i));
readers[i]->open_file(multiname.c_str());
bigint step;
while (1) {

View File

@ -104,19 +104,14 @@ void ReadRestart::command(int narg, char **arg)
if (me == 0) {
if (screen) fprintf(screen,"Reading restart file ...\n");
char *hfile;
std::string hfile = file;
if (multiproc) {
hfile = new char[strlen(file) + 16];
char *ptr = strchr(file,'%');
*ptr = '\0';
sprintf(hfile,"%s%s%s",file,"base",ptr+1);
*ptr = '%';
} else hfile = file;
fp = fopen(hfile,"rb");
hfile.replace(hfile.find("%"),1,"base");
}
fp = fopen(hfile.c_str(),"rb");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
hfile, utils::getsyserror()));
if (multiproc) delete [] hfile;
}
// read magic string, endian flag, format revision
@ -272,14 +267,10 @@ void ReadRestart::command(int narg, char **arg)
else if (nprocs <= multiproc_file) {
char *procfile = new char[strlen(file) + 16];
char *ptr = strchr(file,'%');
for (int iproc = me; iproc < multiproc_file; iproc += nprocs) {
*ptr = '\0';
sprintf(procfile,"%s%d%s",file,iproc,ptr+1);
*ptr = '%';
fp = fopen(procfile,"rb");
std::string procfile = file;
procfile.replace(procfile.find("%"),1,fmt::format("{}",iproc));
fp = fopen(procfile.c_str(),"rb");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
procfile, utils::getsyserror()));
@ -309,8 +300,6 @@ void ReadRestart::command(int narg, char **arg)
fclose(fp);
fp = NULL;
}
delete [] procfile;
}
// input of multiple native files with procs > files
@ -343,16 +332,12 @@ void ReadRestart::command(int narg, char **arg)
MPI_Comm_split(world,icluster,0,&clustercomm);
if (filereader) {
char *procfile = new char[strlen(file) + 16];
char *ptr = strchr(file,'%');
*ptr = '\0';
sprintf(procfile,"%s%d%s",file,icluster,ptr+1);
*ptr = '%';
fp = fopen(procfile,"rb");
std::string procfile = file;
procfile.replace(procfile.find("%"),1,fmt::format("{}",icluster));
fp = fopen(procfile.c_str(),"rb");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
procfile, utils::getsyserror()));
delete [] procfile;
}
int flag,procsperfile;
@ -618,10 +603,9 @@ void ReadRestart::file_search(char *inpfile, char *outfile)
// create outfile with maxint substituted for "*"
// use original inpfile, not pattern, since need to retain "%" in filename
ptr = strchr(inpfile,'*');
*ptr = '\0';
sprintf(outfile,"%s" BIGINT_FORMAT "%s",inpfile,maxnum,ptr+1);
*ptr = '*';
std::string newoutfile = inpfile;
newoutfile.replace(newoutfile.find("*"),1,fmt::format("{}",maxnum));
strcpy(outfile,newoutfile.c_str());
// clean up

View File

@ -24,6 +24,7 @@
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -252,12 +253,9 @@ void ResetIDs::command(int narg, char **arg)
int all;
MPI_Allreduce(&badcount,&all,1,MPI_INT,MPI_SUM,world);
if (all) {
char str[128];
sprintf(str,"Reset_ids missing %d bond topology atom IDs - "
"use comm_modify cutoff",all);
error->all(FLERR,str);
}
if (all)
error->all(FLERR,fmt::format("Reset_ids missing {} bond topology atom IDs - "
"use comm_modify cutoff",all));
// reset IDs and atom map for owned atoms

View File

@ -45,6 +45,7 @@
#include "error.h"
#include "math_const.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
using namespace MathConst;
@ -423,21 +424,15 @@ bigint Thermo::lost_check()
// error message
if (lostflag == Thermo::ERROR) {
char str[64];
sprintf(str,
"Lost atoms: original " BIGINT_FORMAT " current " BIGINT_FORMAT,
atom->natoms,ntotal);
error->all(FLERR,str);
}
if (lostflag == Thermo::ERROR)
error->all(FLERR,fmt::format("Lost atoms: original {} current {}",
atom->natoms,ntotal));
// warning message
char str[64];
sprintf(str,
"Lost atoms: original " BIGINT_FORMAT " current " BIGINT_FORMAT,
atom->natoms,ntotal);
if (me == 0) error->warning(FLERR,str,0);
if (me == 0)
error->warning(FLERR,fmt::format("Lost atoms: original {} current {}",
atom->natoms,ntotal),0);
// reset total atom count