more local buffers removed and file error status added.

This commit is contained in:
Axel Kohlmeyer
2020-06-04 19:46:35 -04:00
parent 62ee8d41f3
commit 54a8b4e08b
15 changed files with 152 additions and 146 deletions

View File

@ -39,6 +39,8 @@
#include "imbalance_var.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -506,7 +508,9 @@ void Balance::options(int iarg, int narg, char **arg)
if (outflag && comm->me == 0) {
fp = fopen(arg[outarg],"w");
if (fp == NULL) error->one(FLERR,"Cannot open (fix) balance output file");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open (fix) balance output file {}: {}",
arg[outarg], utils::getsyserror()));
}
}

View File

@ -21,6 +21,8 @@
#include "atom_masks.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -254,7 +256,9 @@ void Bond::write_file(int narg, char **arg)
FILE *fp;
if (me == 0) {
fp = fopen(arg[4],"a");
if (fp == NULL) error->one(FLERR,"Cannot open bond_write file");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open bond_write file {}: {}",
arg[4], utils::getsyserror()));
}
// initialize potentials before evaluating bond potential

View File

@ -252,7 +252,8 @@ void Input::file(const char *filename)
infile = fopen(filename,"r");
if (infile == NULL)
error->one(FLERR,fmt::format("Cannot open input script {}",filename));
error->one(FLERR,fmt::format("Cannot open input script {}: {}",
filename, utils::getsyserror()));
infiles[nfile++] = infile;
}
@ -1036,7 +1037,8 @@ void Input::include()
infile = fopen(arg[0],"r");
if (infile == NULL)
error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));
error->one(FLERR,fmt::format("Cannot open input script {}: {}",
arg[0], utils::getsyserror()));
infiles[nfile++] = infile;
}
@ -1069,7 +1071,8 @@ void Input::jump()
if (infile && infile != stdin) fclose(infile);
infile = fopen(arg[0],"r");
if (infile == NULL)
error->one(FLERR,fmt::format("Cannot open input script {}",arg[0]));
error->one(FLERR,fmt::format("Cannot open input script {}: {}",
arg[0], utils::getsyserror()));
infiles[nfile-1] = infile;
}
@ -1112,7 +1115,8 @@ void Input::log()
else logfile = fopen(arg[0],"w");
if (logfile == NULL)
error->one(FLERR,fmt::format("Cannot open logfile {}",arg[0]));
error->one(FLERR,fmt::format("Cannot open logfile {}: {}",
arg[0], utils::getsyserror()));
}
if (universe->nworlds == 1) universe->ulogfile = logfile;
@ -1189,8 +1193,8 @@ void Input::print()
if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w");
else fp = fopen(arg[iarg+1],"a");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open print file {}",
arg[iarg+1]));
error->one(FLERR,fmt::format("Cannot open print file {}: {}",
arg[iarg+1], utils::getsyserror()));
}
iarg += 2;
} else if (strcmp(arg[iarg],"screen") == 0) {

View File

@ -400,20 +400,25 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
else {
universe->uscreen = fopen(arg[screenflag],"w");
if (universe->uscreen == NULL)
error->universe_one(FLERR,"Cannot open universe screen file");
error->universe_one(FLERR,fmt::format("Cannot open universe screen "
"file {}: {}",arg[screenflag],
utils::getsyserror()));
}
if (logflag == 0) {
if (helpflag == 0) {
universe->ulogfile = fopen("log.lammps","w");
if (universe->ulogfile == NULL)
error->universe_warn(FLERR,"Cannot open log.lammps for writing");
error->universe_warn(FLERR,"Cannot open log.lammps for writing: "
+ utils::getsyserror());
}
} else if (strcmp(arg[logflag],"none") == 0)
universe->ulogfile = NULL;
else {
universe->ulogfile = fopen(arg[logflag],"w");
if (universe->ulogfile == NULL)
error->universe_one(FLERR,"Cannot open universe log file");
error->universe_one(FLERR,fmt::format("Cannot open universe log "
"file {}: {}",arg[logflag],
utils::getsyserror()));
}
}
@ -437,8 +442,8 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
if (inflag == 0) infile = stdin;
else infile = fopen(arg[inflag],"r");
if (infile == NULL)
error->one(FLERR,fmt::format("Cannot open input script {}",
arg[inflag]));
error->one(FLERR,fmt::format("Cannot open input script {}: {}",
arg[inflag], utils::getsyserror()));
}
if ((universe->me == 0) && !helpflag)
@ -461,45 +466,58 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
if (screenflag == 0) {
str = fmt::format("screen.{}",universe->iworld);
screen = fopen(str.c_str(),"w");
if (screen == NULL) error->one(FLERR,"Cannot open screen file");
if (screen == NULL)
error->one(FLERR,fmt::format("Cannot open screen file {}: {}",
str,utils::getsyserror()));
} else if (strcmp(arg[screenflag],"none") == 0) {
screen = NULL;
} else {
str = fmt::format("{}.{}",arg[screenflag],universe->iworld);
screen = fopen(str.c_str(),"w");
if (screen == NULL) error->one(FLERR,"Cannot open screen file");
if (screen == NULL)
error->one(FLERR,fmt::format("Cannot open screen file {}: {}",
arg[screenflag],utils::getsyserror()));
}
} else if (strcmp(arg[partscreenflag],"none") == 0) {
screen = NULL;
} else {
str = fmt::format("{}.{}",arg[partscreenflag],universe->iworld);
screen = fopen(str.c_str(),"w");
if (screen == NULL) error->one(FLERR,"Cannot open screen file");
if (screen == NULL)
error->one(FLERR,fmt::format("Cannot open screen file {}: {}",
str,utils::getsyserror()));
}
if (partlogflag == 0) {
if (logflag == 0) {
str = fmt::format("log.lammps.{}",universe->iworld);
logfile = fopen(str.c_str(),"w");
if (logfile == NULL) error->one(FLERR,"Cannot open logfile");
if (logfile == NULL)
error->one(FLERR,fmt::format("Cannot open logfile {}: {}",
str, utils::getsyserror()));
} else if (strcmp(arg[logflag],"none") == 0) {
logfile = NULL;
} else {
str = fmt::format("{}.{}",arg[logflag],universe->iworld);
logfile = fopen(str.c_str(),"w");
if (logfile == NULL) error->one(FLERR,"Cannot open logfile");
if (logfile == NULL)
error->one(FLERR,fmt::format("Cannot open logfile {}: {}",
str, utils::getsyserror()));
}
} else if (strcmp(arg[partlogflag],"none") == 0) {
logfile = NULL;
} else {
str = fmt::format("{}.{}",arg[partlogflag],universe->iworld);
logfile = fopen(str.c_str(),"w");
if (logfile == NULL) error->one(FLERR,"Cannot open logfile");
if (logfile == NULL)
error->one(FLERR,fmt::format("Cannot open logfile {}: {}",
str, utils::getsyserror()));
}
infile = fopen(arg[inflag],"r");
if (infile == NULL)
error->one(FLERR,fmt::format("Cannot open input script {}",arg[inflag]));
error->one(FLERR,fmt::format("Cannot open input script {}: {}",
arg[inflag], utils::getsyserror()));
}
// screen and logfile messages for universe and world

View File

@ -1723,7 +1723,8 @@ void Molecule::open(char *file)
{
fp = fopen(file,"r");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open molecule file {}",file));
error->one(FLERR,fmt::format("Cannot open molecule file {}: {}",
file, utils::getsyserror()));
}
/* ----------------------------------------------------------------------

View File

@ -33,6 +33,8 @@
#include "memory.h"
#include "math_const.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
using namespace MathConst;
@ -1639,7 +1641,9 @@ void Pair::write_file(int narg, char **arg)
FILE *fp;
if (me == 0) {
fp = fopen(arg[6],"a");
if (fp == NULL) error->one(FLERR,"Cannot open pair_write file");
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open pair_write file {}: {}",
arg[6], utils::getsyserror()));
fprintf(fp,"# Pair potential %s for atom types %d %d: i,r,energy,force\n",
force->pair_style,itype,jtype);
if (style == RLINEAR)

View File

@ -38,8 +38,8 @@ PotentialFileReader::PotentialFileReader(LAMMPS *lmp,
fp = force->open_potential(filename.c_str());
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open {} potential file {}",
potential_name, filename));
error->one(FLERR,fmt::format("Cannot open {} potential file {}: {}",
potential_name, filename, utils::getsyserror()));
}
PotentialFileReader::~PotentialFileReader() {

View File

@ -18,6 +18,7 @@
#include "read_data.h"
#include <mpi.h>
#include <cstring>
#include <string>
#include <cctype>
#include "atom.h"
#include "atom_vec.h"
@ -42,6 +43,7 @@
#include "error.h"
#include "memory.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -709,11 +711,8 @@ void ReadData::command(int narg, char **arg)
if (firstpass) impropercoeffs(1);
else skip_lines(nimpropertypes);
} else {
char str[128];
snprintf(str,128,"Unknown identifier in data file: %s",keyword);
error->all(FLERR,str);
}
} else error->all(FLERR,fmt::format("Unknown identifier in data file: {}",
keyword));
parse_keyword(0);
}
@ -2013,25 +2012,21 @@ void ReadData::open(char *file)
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP
char gunzip[128];
snprintf(gunzip,128,"gzip -c -d %s",file);
std::string gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip,"rb");
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip,"r");
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror());
#endif
}
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open file {}: {}",
file, utils::getsyserror()));
}
/* ----------------------------------------------------------------------

View File

@ -113,11 +113,9 @@ void ReadRestart::command(int narg, char **arg)
*ptr = '%';
} else hfile = file;
fp = fopen(hfile,"rb");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open restart file %s",hfile);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
hfile, utils::getsyserror()));
if (multiproc) delete [] hfile;
}
@ -282,12 +280,9 @@ void ReadRestart::command(int narg, char **arg)
sprintf(procfile,"%s%d%s",file,iproc,ptr+1);
*ptr = '%';
fp = fopen(procfile,"rb");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open restart file %s",procfile);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
procfile, utils::getsyserror()));
utils::sfread(FLERR,&flag,sizeof(int),1,fp,NULL,error);
if (flag != PROCSPERFILE)
error->one(FLERR,"Invalid flag in peratom section of restart file");
@ -354,11 +349,9 @@ void ReadRestart::command(int narg, char **arg)
sprintf(procfile,"%s%d%s",file,icluster,ptr+1);
*ptr = '%';
fp = fopen(procfile,"rb");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open restart file %s",procfile);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open restart file {}: {}",
procfile, utils::getsyserror()));
delete [] procfile;
}
@ -718,12 +711,10 @@ void ReadRestart::header()
} else if (flag == NPROCS) {
nprocs_file = read_int();
if (nprocs_file != comm->nprocs && me == 0) {
char msg[128];
snprintf(msg,128,"Restart file used different # of processors: %d vs. %d",
nprocs_file,comm->nprocs);
error->warning(FLERR,msg);
}
if (nprocs_file != comm->nprocs && me == 0)
error->warning(FLERR,fmt::format("Restart file used different # of "
"processors: {} vs. {}",nprocs_file,
comm->nprocs));
// don't set procgrid, warn if different

View File

@ -14,6 +14,8 @@
#include "reader.h"
#include <cstring>
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -41,25 +43,21 @@ void Reader::open_file(const char *file)
if (!compressed) fp = fopen(file,"r");
else {
#ifdef LAMMPS_GZIP
char gunzip[1024];
snprintf(gunzip,1024,"gzip -c -d %s",file);
std::string gunzip = fmt::format("gzip -c -d {}",file);
#ifdef _WIN32
fp = _popen(gunzip,"rb");
fp = _popen(gunzip.c_str(),"rb");
#else
fp = popen(gunzip,"r");
fp = popen(gunzip.c_str(),"r");
#endif
#else
error->one(FLERR,"Cannot open gzipped file");
error->one(FLERR,"Cannot open gzipped file: " + utils::getsyserror());
#endif
}
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open file %s",file);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open file {}: {}",
file, utils::getsyserror()));
}
/* ----------------------------------------------------------------------

View File

@ -20,6 +20,8 @@
#include "error.h"
#include "force.h"
#include "memory.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -96,7 +98,10 @@ void Universe::reorder(char *style, char *arg)
if (me == 0) {
FILE *fp = fopen(arg,"r");
if (fp == NULL) error->universe_one(FLERR,"Cannot open -reorder file");
if (fp == NULL)
error->universe_one(FLERR,fmt::format("Cannot open -reorder "
"file {}: {}",arg,
utils::getsyserror()));
// skip header = blank and comment lines
@ -117,7 +122,8 @@ void Universe::reorder(char *style, char *arg)
rv = sscanf(line,"%d %d",&me_orig,&me_new);
if (me_orig < 0 || me_orig >= nprocs ||
me_new < 0 || me_new >= nprocs || rv != 2)
error->one(FLERR,"Invalid entry in -reorder file");
error->one(FLERR,fmt::format("Invalid entry '{} {}' in -reorder "
"file", me_orig, me_new));
uni2orig[me_new] = me_orig;
for (int i = 1; i < nprocs; i++) {
@ -126,7 +132,8 @@ void Universe::reorder(char *style, char *arg)
rv = sscanf(line,"%d %d",&me_orig,&me_new);
if (me_orig < 0 || me_orig >= nprocs ||
me_new < 0 || me_new >= nprocs || rv != 2)
error->one(FLERR,"Invalid entry in -reorder file");
error->one(FLERR,fmt::format("Invalid entry '{} {}' in -reorder "
"file", me_orig, me_new));
uni2orig[me_new] = me_orig;
}
fclose(fp);
@ -201,11 +208,9 @@ void Universe::add_world(char *str)
if (n < 1 || nper < 1) valid = false;
if (!valid) {
char msg[128];
snprintf(msg,128,"Invalid partition string '%s'",str);
error->universe_all(FLERR,msg);
}
if (!valid)
error->universe_all(FLERR,fmt::format("Invalid partition string '{}'",
str));
} else nper = nprocs;
memory->grow(procs_per_world,nworlds+n,"universe:procs_per_world");
@ -268,8 +273,7 @@ char *date2num(const char *version)
year = atoi(version);
}
char *ver = new char[64];
sprintf(ver,"%04d%02d%02d", year % 10000, month, day % 100);
char *ver = new char[12];
snprintf(ver,12,"%04d%02d%02d", year % 10000, month, day % 100);
return ver;
}

View File

@ -269,7 +269,8 @@ void Variable::set(int narg, char **arg)
if (universe->me == 0) {
FILE *fp = fopen("tmp.lammps.variable","w");
if (fp == NULL)
error->one(FLERR,"Cannot open temporary file for world counter.");
error->one(FLERR,"Cannot open temporary file for world counter: "
+ utils::getsyserror());
fprintf(fp,"%d\n",universe->nworlds);
fclose(fp);
fp = NULL;
@ -532,12 +533,10 @@ void Variable::set(int narg, char **arg)
strcpy(names[nvar],arg[0]);
for (int i = 0; i < n-1; i++)
if (!isalnum(names[nvar][i]) && names[nvar][i] != '_') {
char errmsg[128];
snprintf(errmsg,128,"Variable name '%s' must have only alphanumeric "
"characters or underscore",names[nvar]);
error->all(FLERR,errmsg);
}
if (!isalnum(names[nvar][i]) && names[nvar][i] != '_')
error->all(FLERR,fmt::format("Variable name '{}' must have only "
"alphanumeric characters or underscores",
names[nvar]));
nvar++;
}
@ -590,11 +589,9 @@ int Variable::next(int narg, char **arg)
for (int iarg = 0; iarg < narg; iarg++) {
ivar = find(arg[iarg]);
if (ivar < 0) {
char errmsg[128];
snprintf(errmsg,128,"Invalid variable '%s' in next command",arg[iarg]);
error->all(FLERR,errmsg);
}
if (ivar < 0)
error->all(FLERR,fmt::format("Invalid variable '{}' in next command",
arg[iarg]));
if (style[ivar] == ULOOP && style[find(arg[0])] == UNIVERSE) continue;
else if (style[ivar] == UNIVERSE && style[find(arg[0])] == ULOOP) continue;
else if (style[ivar] != style[find(arg[0])])
@ -919,11 +916,9 @@ char *Variable::retrieve(char *name)
str = data[ivar][1];
} else if (style[ivar] == PYTHON) {
int ifunc = python->variable_match(data[ivar][0],name,0);
if (ifunc < 0) {
char errmsg[128];
snprintf(errmsg,128,"Python variable '%s' does not match Python function",name);
error->all(FLERR,errmsg);
}
if (ifunc < 0)
error->all(FLERR,fmt::format("Python variable '{}' does not match "
"Python function", name));
python->invoke_function(ifunc,data[ivar][1]);
str = data[ivar][1];
// if Python func returns a string longer than VALUELENGTH
@ -1638,12 +1633,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
if (word[0] == 'F') lowercase = 0;
int ifix = modify->find_fix(word+2);
if (ifix < 0) {
std::string mesg = "Invalid fix ID '";
mesg += (word+2);
mesg += "' in variable formula";
print_var_error(FLERR,mesg,ivar);
}
if (ifix < 0)
print_var_error(FLERR,fmt::format("Invalid fix ID '{}' in variable"
" formula",word+2),ivar);
Fix *fix = modify->fix[ifix];
// parse zero or one or two trailing brackets
@ -2074,12 +2066,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
argstack,nargstack,ivar));
else if (special_function(word,contents,tree,treestack,ntreestack,
argstack,nargstack,ivar));
else {
char msg[128];
snprintf(msg,128,"Invalid math/group/special function '%s()'"
"in variable formula", word);
print_var_error(FLERR,msg,ivar);
}
else print_var_error(FLERR,fmt::format("Invalid math/group/special "
"function '{}()'in variable "
"formula", word),ivar);
delete [] contents;
// ----------------
@ -2134,11 +2123,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
"simulation box is defined",ivar);
int flag = output->thermo->evaluate_keyword(word,&value1);
if (flag) {
char msg[128];
snprintf(msg,128,"Invalid thermo keyword '%s' in variable formula",word);
print_var_error(FLERR,msg,ivar);
}
if (flag)
print_var_error(FLERR,fmt::format("Invalid thermo keyword '{}' in "
"variable formula",word),ivar);
if (tree) {
Tree *newtree = new Tree();
newtree->type = VALUE;
@ -5071,11 +5058,9 @@ VarReader::VarReader(LAMMPS *lmp, char *name, char *file, int flag) :
if (me == 0) {
fp = fopen(file,"r");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open file variable file %s",file);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open file variable file {}: {}",
file, utils::getsyserror()));
}
// if atomfile-style variable, must store per-atom values read from file

View File

@ -27,6 +27,7 @@
#include "error.h"
#include "domain.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -56,10 +57,9 @@ void WriteCoeff::command(int narg, char **arg)
char str[256], coeff[256];
FILE *one = fopen(file,"wb+");
if (one == NULL) {
snprintf(str,256,"Cannot open coeff file %s",file);
error->one(FLERR,str);
}
if (one == NULL)
error->one(FLERR,fmt::format("Cannot open coeff file {}: {}",
file, utils::getsyserror()));
if (force->pair && force->pair->writedata) {
fprintf(one,"# pair_style %s\npair_coeff\n",force->pair_style);
@ -91,10 +91,10 @@ void WriteCoeff::command(int narg, char **arg)
rewind(one);
FILE *two = fopen(file+4,"w");
if (two == NULL) {
snprintf(str,256,"Cannot open coeff file %s",file+4);
error->one(FLERR,str);
}
if (two == NULL)
error->one(FLERR,fmt::format("Cannot open coeff file {}: {}",
file+4, utils::getsyserror()));
fprintf(two,"# LAMMPS coeff file via write_coeff, version %s\n",
universe->version);

View File

@ -32,6 +32,8 @@
#include "thermo.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
using namespace LAMMPS_NS;
@ -191,11 +193,9 @@ void WriteData::write(char *file)
if (me == 0) {
fp = fopen(file,"w");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open data file %s",file);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR,fmt::format("Cannot open data file {}: {}",
file, utils::getsyserror()));
}
// proc 0 writes header, ntype-length arrays, force fields

View File

@ -35,6 +35,8 @@
#include "mpiio.h"
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
#include "lmprestart.h"
@ -242,11 +244,9 @@ void WriteRestart::write(char *file)
*ptr = '%';
} else hfile = file;
fp = fopen(hfile,"wb");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open restart file %s",hfile);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR, fmt::format("Cannot open restart file {}: {}",
hfile, utils::getsyserror()));
if (multiproc) delete [] hfile;
}
@ -310,11 +310,9 @@ void WriteRestart::write(char *file)
if (filewriter) {
fp = fopen(multiname,"wb");
if (fp == NULL) {
char str[128];
snprintf(str,128,"Cannot open restart file %s",multiname);
error->one(FLERR,str);
}
if (fp == NULL)
error->one(FLERR, fmt::format("Cannot open restart file {}: {}",
multiname, utils::getsyserror()));
write_int(PROCSPERFILE,nclusterprocs);
}