diff --git a/src/atom_vec_hybrid.cpp b/src/atom_vec_hybrid.cpp index 9ba2b6f468..6db27601a4 100644 --- a/src/atom_vec_hybrid.cpp +++ b/src/atom_vec_hybrid.cpp @@ -18,6 +18,7 @@ #include "memory.h" #include "error.h" #include "tokenizer.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -215,12 +216,9 @@ void AtomVecHybrid::process_args(int narg, char **arg) for (int idup = 0; idup < ndupfield; idup++) { char *dup = (char *) dupfield[idup]; ptr = strstr(concat_grow,dup); - if (ptr && strstr(ptr+1,dup)) { - char str[128]; - sprintf(str,"Peratom %s is in multiple sub-styles - " - "must be used consistently",dup); - if (comm->me == 0) error->warning(FLERR,str); - } + if ((ptr && strstr(ptr+1,dup)) && (comm->me == 0)) + error->warning(FLERR,fmt::format("Peratom {} is in multiple sub-styles " + "- must be used consistently",dup)); } delete [] concat_grow; diff --git a/src/compute_cna_atom.cpp b/src/compute_cna_atom.cpp index 5f0f047958..7ab9e8adf2 100644 --- a/src/compute_cna_atom.cpp +++ b/src/compute_cna_atom.cpp @@ -30,6 +30,7 @@ #include "comm.h" #include "memory.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -184,11 +185,9 @@ void ComputeCNAAtom::compute_peratom() int nerrorall; MPI_Allreduce(&nerror,&nerrorall,1,MPI_INT,MPI_SUM,world); - if (nerrorall && comm->me == 0) { - char str[128]; - sprintf(str,"Too many neighbors in CNA for %d atoms",nerrorall); - error->warning(FLERR,str,0); - } + if (nerrorall && comm->me == 0) + error->warning(FLERR,fmt::format("Too many neighbors in CNA for {} " + "atoms",nerrorall),0); // compute CNA for each atom in group // only performed if # of nearest neighbors = 12 or 14 (fcc,hcp) @@ -345,11 +344,9 @@ void ComputeCNAAtom::compute_peratom() // warning message MPI_Allreduce(&nerror,&nerrorall,1,MPI_INT,MPI_SUM,world); - if (nerrorall && comm->me == 0) { - char str[128]; - sprintf(str,"Too many common neighbors in CNA %d times",nerrorall); - error->warning(FLERR,str); - } + if (nerrorall && comm->me == 0) + error->warning(FLERR,fmt::format("Too many common neighbors in CNA {} " + "times", nerrorall)); } /* ---------------------------------------------------------------------- diff --git a/src/compute_group_group.cpp b/src/compute_group_group.cpp index c64db19fc1..c8876daeef 100644 --- a/src/compute_group_group.cpp +++ b/src/compute_group_group.cpp @@ -147,12 +147,9 @@ void ComputeGroupGroup::init() if (kspaceflag) { kspace_correction(); - if (fabs(e_correction) > SMALL && comm->me == 0) { - char str[128]; - sprintf(str,"Both groups in compute group/group have a net charge; " - "the Kspace boundary correction to energy will be non-zero"); - error->warning(FLERR,str); - } + if ((fabs(e_correction) > SMALL) && (comm->me == 0)) + error->warning(FLERR,"Both groups in compute group/group have a net charge; " + "the Kspace boundary correction to energy will be non-zero"); } // recheck that group 2 has not been deleted diff --git a/src/compute_orientorder_atom.cpp b/src/compute_orientorder_atom.cpp index 979935b12e..34d11f9ac1 100644 --- a/src/compute_orientorder_atom.cpp +++ b/src/compute_orientorder_atom.cpp @@ -32,6 +32,7 @@ #include "memory.h" #include "error.h" #include "math_const.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace MathConst; @@ -715,11 +716,8 @@ void ComputeOrientOrderAtom::init_clebsch_gordan() double ComputeOrientOrderAtom::factorial(int n) { - if (n < 0 || n > nmaxfactorial) { - char str[128]; - sprintf(str, "Invalid argument to factorial %d", n); - error->all(FLERR, str); - } + if (n < 0 || n > nmaxfactorial) + error->all(FLERR,fmt::format("Invalid argument to factorial {}", n)); return nfac_table[n]; } diff --git a/src/dump_movie.cpp b/src/dump_movie.cpp index ea67320d4a..68a77c8f63 100644 --- a/src/dump_movie.cpp +++ b/src/dump_movie.cpp @@ -20,6 +20,7 @@ #include "comm.h" #include "force.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; @@ -58,11 +59,9 @@ void DumpMovie::openfile() fp = popen(moviecmd,"w"); #endif - if (fp == NULL) { - char str[128]; - sprintf(str,"Failed to open FFmpeg pipeline to file %s",filename); - error->one(FLERR,str); - } + if (fp == NULL) + error->one(FLERR,fmt::format("Failed to open FFmpeg pipeline to " + "file {}",filename)); } } /* ---------------------------------------------------------------------- */ diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index 42b63c69f1..c5960dfe13 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "update.h" #include "force.h" #include "input.h" @@ -25,6 +26,7 @@ #include "comm.h" #include "timer.h" #include "error.h" +#include "fmt/format.h" using namespace LAMMPS_NS; using namespace FixConst; @@ -224,15 +226,13 @@ void FixHalt::end_of_step() // soft/continue halt -> trigger timer to break from run loop // print message with ID of fix halt in case multiple instances - char str[128]; - sprintf(str,"Fix halt condition for fix-id %s met on step " - BIGINT_FORMAT " with value %g", - id, update->ntimestep, attvalue); - + std::string message = fmt::format("Fix halt condition for fix-id {} met on " + "step {} with value {}", + id, update->ntimestep, attvalue); if (eflag == HARD) { - error->all(FLERR,str); + error->all(FLERR,message); } else if (eflag == SOFT || eflag == CONTINUE) { - if (comm->me == 0 && msgflag == YESMSG) error->message(FLERR,str); + if (comm->me == 0 && msgflag == YESMSG) error->message(FLERR,message); timer->force_timeout(); } } diff --git a/src/group.cpp b/src/group.cpp index 93cd668194..4b42223646 100644 --- a/src/group.cpp +++ b/src/group.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "domain.h" #include "atom.h" @@ -32,6 +33,7 @@ #include "memory.h" #include "error.h" #include "utils.h" +#include "fmt/format.h" #include @@ -123,11 +125,9 @@ void Group::assign(int narg, char **arg) for (i = 0; i < nlocal; i++) mask[i] &= bits; if (dynamic[igroup]) { - int n = strlen("GROUP_") + strlen(names[igroup]) + 1; - char *fixID = new char[n]; - sprintf(fixID,"GROUP_%s",names[igroup]); - modify->delete_fix(fixID); - delete [] fixID; + std::string fixID = "GROUP_"; + fixID += names[igroup]; + modify->delete_fix(fixID.c_str()); } delete [] names[igroup]; @@ -492,27 +492,23 @@ void Group::assign(int narg, char **arg) // if group is already dynamic, delete existing FixGroup if (dynamic[igroup]) { - int n = strlen("GROUP_") + strlen(names[igroup]) + 1; - char *fixID = new char[n]; - sprintf(fixID,"GROUP_%s",names[igroup]); - modify->delete_fix(fixID); - delete [] fixID; + std::string fixID = "GROUP_"; + fixID += names[igroup]; + modify->delete_fix(fixID.c_str()); } dynamic[igroup] = 1; - int n = strlen("GROUP_") + strlen(names[igroup]) + 1; - char *fixID = new char[n]; - sprintf(fixID,"GROUP_%s",names[igroup]); + std::string fixID = "GROUP_"; + fixID += names[igroup]; char **newarg = new char*[narg]; - newarg[0] = fixID; + newarg[0] = (char *)fixID.c_str(); newarg[1] = arg[2]; newarg[2] = (char *) "GROUP"; for (int i = 3; i < narg; i++) newarg[i] = arg[i]; modify->add_fix(narg,newarg); delete [] newarg; - delete [] fixID; // style = static // remove dynamic FixGroup if necessary @@ -522,11 +518,9 @@ void Group::assign(int narg, char **arg) if (narg != 2) error->all(FLERR,"Illegal group command"); if (dynamic[igroup]) { - int n = strlen("GROUP_") + strlen(names[igroup]) + 1; - char *fixID = new char[n]; - sprintf(fixID,"GROUP_%s",names[igroup]); - modify->delete_fix(fixID); - delete [] fixID; + std::string fixID = "GROUP_"; + fixID += names[igroup]; + modify->delete_fix(fixID.c_str()); } dynamic[igroup] = 0; @@ -546,15 +540,10 @@ void Group::assign(int narg, char **arg) MPI_Allreduce(&rlocal,&all,1,MPI_DOUBLE,MPI_SUM,world); if (me == 0) { - if (dynamic[igroup]) { - if (screen) fprintf(screen,"dynamic group %s defined\n",names[igroup]); - if (logfile) fprintf(logfile,"dynamic group %s defined\n",names[igroup]); - } else { - if (screen) - fprintf(screen,"%.15g atoms in group %s\n",all,names[igroup]); - if (logfile) - fprintf(logfile,"%.15g atoms in group %s\n",all,names[igroup]); - } + if (dynamic[igroup]) + utils::logmesg(lmp,fmt::format("dynamic group {} defined\n",names[igroup])); + else + utils::logmesg(lmp,fmt::format("{:.15g} atoms in group {}\n",all,names[igroup])); } }