diff --git a/src/fix_print.cpp b/src/fix_print.cpp index 023b9355cd..ccef03c3ae 100644 --- a/src/fix_print.cpp +++ b/src/fix_print.cpp @@ -1,4 +1,3 @@ -// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -14,6 +13,7 @@ #include "fix_print.h" +#include "comm.h" #include "error.h" #include "input.h" #include "memory.h" @@ -29,24 +29,22 @@ using namespace FixConst; /* ---------------------------------------------------------------------- */ FixPrint::FixPrint(LAMMPS *lmp, int narg, char **arg) : - Fix(lmp, narg, arg), - fp(nullptr), text(nullptr), copy(nullptr), work(nullptr), var_print(nullptr) + Fix(lmp, narg, arg), fp(nullptr), text(nullptr), copy(nullptr), work(nullptr), + var_print(nullptr) { - if (narg < 5) error->all(FLERR,"Illegal fix print command"); - if (utils::strmatch(arg[3],"^v_")) { - var_print = utils::strdup(arg[3]+2); + if (narg < 5) utils::missing_cmd_args(FLERR, "fix print", error); + if (utils::strmatch(arg[3], "^v_")) { + var_print = utils::strdup(arg[3] + 2); nevery = 1; } else { - nevery = utils::inumeric(FLERR,arg[3],false,lmp); - if (nevery <= 0) error->all(FLERR,"Illegal fix print command"); + nevery = utils::inumeric(FLERR, arg[3], false, lmp); + if (nevery <= 0) error->all(FLERR, "Illegal fix print nevery value {}; must be > 0", nevery); } - MPI_Comm_rank(world,&me); - text = utils::strdup(arg[4]); - int n = strlen(text)+1; - copy = (char *) memory->smalloc(n*sizeof(char),"fix/print:copy"); - work = (char *) memory->smalloc(n*sizeof(char),"fix/print:work"); + int n = strlen(text) + 1; + copy = (char *) memory->smalloc(n * sizeof(char), "fix/print:copy"); + work = (char *) memory->smalloc(n * sizeof(char), "fix/print:work"); maxcopy = maxwork = n; // parse optional args @@ -57,48 +55,54 @@ FixPrint::FixPrint(LAMMPS *lmp, int narg, char **arg) : int iarg = 5; while (iarg < narg) { - if (strcmp(arg[iarg],"file") == 0 || strcmp(arg[iarg],"append") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix print command"); - if (me == 0) { - if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w"); - else fp = fopen(arg[iarg+1],"a"); + if ((strcmp(arg[iarg], "file") == 0) || (strcmp(arg[iarg], "append") == 0)) { + if (iarg + 2 > narg) + utils::missing_cmd_args(FLERR, std::string("fix print ") + arg[iarg], error); + if (comm->me == 0) { + if (strcmp(arg[iarg], "file") == 0) + fp = fopen(arg[iarg + 1], "w"); + else + fp = fopen(arg[iarg + 1], "a"); if (fp == nullptr) - error->one(FLERR,"Cannot open fix print file {}: {}", - arg[iarg+1], utils::getsyserror()); + error->one(FLERR, "Cannot open fix print file {}: {}", arg[iarg + 1], + utils::getsyserror()); } iarg += 2; - } else if (strcmp(arg[iarg],"screen") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix print command"); - screenflag = utils::logical(FLERR,arg[iarg+1],false,lmp); + } else if (strcmp(arg[iarg], "screen") == 0) { + if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "fix print screen", error); + screenflag = utils::logical(FLERR, arg[iarg + 1], false, lmp); iarg += 2; - } else if (strcmp(arg[iarg],"title") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix print command"); - delete [] title; - title = utils::strdup(arg[iarg+1]); + } else if (strcmp(arg[iarg], "title") == 0) { + if (iarg + 2 > narg) utils::missing_cmd_args(FLERR, "fix print title", error); + delete[] title; + title = utils::strdup(arg[iarg + 1]); iarg += 2; - } else error->all(FLERR,"Illegal fix print command"); + } else + error->all(FLERR, "Unknown fix print keyword: {}", arg[iarg]); } // print file comment line - if (fp && me == 0) { - if (title) fprintf(fp,"%s\n",title); - else fprintf(fp,"# Fix print output for fix %s\n",id); + if (fp && (comm->me == 0)) { + if (title) + fprintf(fp, "%s\n", title); + else + fprintf(fp, "# Fix print output for fix %s\n", id); } - delete [] title; + delete[] title; } /* ---------------------------------------------------------------------- */ FixPrint::~FixPrint() { - delete [] text; - delete [] var_print; + delete[] text; + delete[] var_print; memory->sfree(copy); memory->sfree(work); - if (fp && me == 0) fclose(fp); + if (fp && (comm->me == 0)) fclose(fp); } /* ---------------------------------------------------------------------- */ @@ -117,16 +121,16 @@ void FixPrint::init() if (var_print) { ivar_print = input->variable->find(var_print); if (ivar_print < 0) - error->all(FLERR,"Variable name for fix print timestep does not exist"); + error->all(FLERR, "Variable {} for fix print timestep does not exist", var_print); if (!input->variable->equalstyle(ivar_print)) - error->all(FLERR,"Variable for fix print timestep is invalid style"); - next_print = static_cast - (input->variable->compute_equal(ivar_print)); + error->all(FLERR, "Variable {} for fix print timestep is invalid style", var_print); + next_print = static_cast(input->variable->compute_equal(ivar_print)); if (next_print <= update->ntimestep) - error->all(FLERR,"Fix print timestep variable returned a bad timestep"); + error->all(FLERR, "Fix print timestep variable {} returned a bad timestep: {}", var_print, + next_print); } else { if (update->ntimestep % nevery) - next_print = (update->ntimestep/nevery)*nevery + nevery; + next_print = (update->ntimestep / nevery) * nevery + nevery; else next_print = update->ntimestep; } @@ -158,24 +162,23 @@ void FixPrint::end_of_step() modify->clearstep_compute(); - strncpy(copy,text,maxcopy); - input->substitute(copy,work,maxcopy,maxwork,0); + strncpy(copy, text, maxcopy); + input->substitute(copy, work, maxcopy, maxwork, 0); if (var_print) { - next_print = static_cast - (input->variable->compute_equal(ivar_print)); + next_print = static_cast(input->variable->compute_equal(ivar_print)); if (next_print <= update->ntimestep) - error->all(FLERR,"Fix print timestep variable returned a bad timestep"); + error->all(FLERR, "Fix print timestep variable returned a bad timestep: {}", next_print); } else { - next_print = (update->ntimestep/nevery)*nevery + nevery; + next_print = (update->ntimestep / nevery) * nevery + nevery; } modify->addstep_compute(next_print); - if (me == 0) { - if (screenflag) utils::logmesg(lmp,std::string(copy) + "\n"); + if (comm->me == 0) { + if (screenflag) utils::logmesg(lmp, std::string(copy) + "\n"); if (fp) { - fmt::print(fp,"{}\n",copy); + fmt::print(fp, "{}\n", copy); fflush(fp); } } diff --git a/src/fix_print.h b/src/fix_print.h index 48eda897b5..9e699e22ba 100644 --- a/src/fix_print.h +++ b/src/fix_print.h @@ -34,7 +34,7 @@ class FixPrint : public Fix { void end_of_step() override; private: - int me, screenflag; + int screenflag; FILE *fp; char *text, *copy, *work; int maxcopy, maxwork;