From 36b3ee32a4abd1ff96bfbfb6728d6a8d4dc4fda5 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 15 Sep 2021 16:46:33 -0400 Subject: [PATCH] simplify --- src/EXTRA-FIX/fix_npt_cauchy.cpp | 42 ++++++++------------------------ src/FEP/fix_adapt_fep.cpp | 10 +++----- src/PLUMED/fix_plumed.cpp | 6 +---- src/fix_adapt.cpp | 24 +++++------------- src/input.cpp | 3 +-- 5 files changed, 21 insertions(+), 64 deletions(-) diff --git a/src/EXTRA-FIX/fix_npt_cauchy.cpp b/src/EXTRA-FIX/fix_npt_cauchy.cpp index e58632635f..afb06c895d 100644 --- a/src/EXTRA-FIX/fix_npt_cauchy.cpp +++ b/src/EXTRA-FIX/fix_npt_cauchy.cpp @@ -2460,30 +2460,17 @@ double FixNPTCauchy::memory_usage() void FixNPTCauchy::CauchyStat_init() { if (comm->me == 0) { - if (screen) { - fprintf(screen,"Using fix npt/cauchy with alpha=%f\n",alpha); - if (restartPK==1) { - fprintf(screen," (this is a continuation run)\n"); - } else { - fprintf(screen," (this is NOT a continuation run)\n"); - } - } - if (logfile) { - fprintf(logfile,"Using fix npt/cauchy with alpha=%f\n",alpha); - if (restartPK==1) { - fprintf(logfile," this is a continuation run\n"); - } else { - fprintf(logfile," this is NOT a continuation run\n"); - } + std::string mesg = fmt::format("Using fix npt/cauchy with alpha={:f.8}\n",alpha); + if (restartPK==1) { + mesg += " (this is a continuation run)\n"; + } else { + mesg += " (this is NOT a continuation run)\n"; } + utils::logmesg(lmp, mesg); } - if (!id_store) { - int n = strlen(id) + 14; - id_store = new char[n]; - strcpy(id_store,id); - strcat(id_store,"_FIX_NH_STORE"); - } + if (!id_store) + id_store = utils::strdup(std::string(id) + "_FIX_NH_STORE"); restart_stored = modify->find_fix(id_store); if (restartPK==1 && restart_stored < 0) @@ -2491,19 +2478,10 @@ void FixNPTCauchy::CauchyStat_init() " must follow a previously equilibrated npt/cauchy run"); if (alpha<=0.0) - error->all(FLERR,"Illegal fix npt/cauchy command: " - " Alpha cannot be zero or negative."); + error->all(FLERR,"Illegal fix npt/cauchy command: Alpha cannot be zero or negative."); if (restart_stored < 0) { - char **newarg = new char *[6]; - newarg[0] = id_store; - newarg[1] = (char *) "all"; - newarg[2] = (char *) "STORE"; - newarg[3] = (char *) "global"; - newarg[4] = (char *) "1"; - newarg[5] = (char *) "6"; - modify->add_fix(6,newarg); - delete[] newarg; + modify->add_fix(std::string(id_store) + "all STORE global 1 6"); restart_stored = modify->find_fix(id_store); } init_store = (FixStore *)modify->fix[restart_stored]; diff --git a/src/FEP/fix_adapt_fep.cpp b/src/FEP/fix_adapt_fep.cpp index 3b4290f2c9..5b3c565198 100644 --- a/src/FEP/fix_adapt_fep.cpp +++ b/src/FEP/fix_adapt_fep.cpp @@ -282,13 +282,9 @@ void FixAdaptFEP::init() anypair = 1; Pair *pair = nullptr; - if (lmp->suffix_enable) { - char psuffix[128]; - strcpy(psuffix,ad->pstyle); - strcat(psuffix,"/"); - strcat(psuffix,lmp->suffix); - pair = force->pair_match(psuffix,1); - } + if (lmp->suffix_enable) + pair = force->pair_match(std::string(pstyle)+"/"+lmp->suffix,1); + if (pair == nullptr) pair = force->pair_match(ad->pstyle,1); if (pair == nullptr) error->all(FLERR, "Fix adapt/fep pair style does not exist"); diff --git a/src/PLUMED/fix_plumed.cpp b/src/PLUMED/fix_plumed.cpp index c34afe6d3f..b38e745b7a 100644 --- a/src/PLUMED/fix_plumed.cpp +++ b/src/PLUMED/fix_plumed.cpp @@ -176,11 +176,7 @@ FixPlumed::FixPlumed(LAMMPS *lmp, int narg, char **arg) : if (universe->existflag == 1) { // Each replica writes an independent log file // with suffix equal to the replica id - char str_num[32], logFile[1024]; - sprintf(str_num,".%d",universe->iworld); - strncpy(logFile,arg[i],1024-32); - strcat(logFile,str_num); - p->cmd("setLogFile",logFile); + p->cmd("setLogFile",fmt::format("{}.{}",arg[i],universe->iworld).c_str()); next=0; } else { // partition option not used diff --git a/src/fix_adapt.cpp b/src/fix_adapt.cpp index 5efa70859a..358cf8045f 100644 --- a/src/fix_adapt.cpp +++ b/src/fix_adapt.cpp @@ -331,15 +331,9 @@ void FixAdapt::init() nsub = utils::inumeric(FLERR,cptr+1,false,lmp); } - if (lmp->suffix_enable) { - int len = 2 + strlen(pstyle) + strlen(lmp->suffix); - char *psuffix = new char[len]; - strcpy(psuffix,pstyle); - strcat(psuffix,"/"); - strcat(psuffix,lmp->suffix); - ad->pair = force->pair_match(psuffix,1,nsub); - delete[] psuffix; - } + if (lmp->suffix_enable) + ad->pair = force->pair_match(fmt::format("{}/{}",pstyle,lmp->suffix),1,nsub); + if (ad->pair == nullptr) ad->pair = force->pair_match(pstyle,1,nsub); if (ad->pair == nullptr) error->all(FLERR,"Fix adapt pair style does not exist"); @@ -374,15 +368,9 @@ void FixAdapt::init() anybond = 1; char *bstyle = utils::strdup(ad->bstyle); - if (lmp->suffix_enable) { - int len = 2 + strlen(bstyle) + strlen(lmp->suffix); - char *bsuffix = new char[len]; - strcpy(bsuffix,bstyle); - strcat(bsuffix,"/"); - strcat(bsuffix,lmp->suffix); - ad->bond = force->bond_match(bsuffix); - delete [] bsuffix; - } + if (lmp->suffix_enable) + ad->bond = force->bond_match(fmt::format("{}/{}",bstyle,lmp->suffix)); + if (ad->bond == nullptr) ad->bond = force->bond_match(bstyle); if (ad->bond == nullptr ) error->all(FLERR,"Fix adapt bond style does not exist"); diff --git a/src/input.cpp b/src/input.cpp index 97757fd1f6..3a17b3f533 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -614,8 +614,7 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag) } if (value == nullptr) - error->one(FLERR,"Substitution for illegal " - "variable {}",var); + error->one(FLERR,"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.