add support for input file line numbers

This commit is contained in:
Axel Kohlmeyer
2023-08-25 16:11:46 -04:00
parent c61aaa81d2
commit c5da0581e6
3 changed files with 30 additions and 13 deletions

View File

@ -57,6 +57,9 @@ using namespace LAMMPS_NS;
#define DELTALINE 256 #define DELTALINE 256
#define DELTA 4 #define DELTA 4
// maximum nesting level of input files
static constexpr int LMP_MAXFILE = 16;
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
one instance per command in style_command.h one instance per command in style_command.h
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
@ -118,9 +121,9 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp)
if (me == 0) { if (me == 0) {
nfile = 1; nfile = 1;
maxfile = 16; infiles = new FILE *[LMP_MAXFILE];
infiles = new FILE *[maxfile];
infiles[0] = infile; infiles[0] = infile;
inlines = new int[LMP_MAXFILE];
} else infiles = nullptr; } else infiles = nullptr;
variable = new Variable(lmp); variable = new Variable(lmp);
@ -190,6 +193,7 @@ of the file is reached. The *infile* pointer will usually point to
void Input::file() void Input::file()
{ {
int m,n,mstart,ntriple,endfile; int m,n,mstart,ntriple,endfile;
int nline = *output->thermo->get_line();
while (true) { while (true) {
@ -245,9 +249,12 @@ void Input::file()
m--; m--;
while (m >= 0 && isspace(line[m])) m--; while (m >= 0 && isspace(line[m])) m--;
// continue reading if final printable char is "&" // continue reading if final printable char is "&", count line
if (m >= 0 && line[m] == '&') continue; if (m >= 0 && line[m] == '&') {
++nline;
continue;
}
// continue reading if odd number of triple quotes // continue reading if odd number of triple quotes
@ -264,6 +271,7 @@ void Input::file()
break; break;
} }
} }
output->thermo->set_line(++nline);
// bcast the line // bcast the line
// if n = 0, end-of-file // if n = 0, end-of-file
@ -301,6 +309,7 @@ void Input::file()
if (execute_command() && line) if (execute_command() && line)
error->all(FLERR,"Unknown command: {}",line); error->all(FLERR,"Unknown command: {}",line);
nline = *output->thermo->get_line();
} }
} }
@ -327,12 +336,14 @@ void Input::file(const char *filename)
// call to file() will close filename and decrement nfile // call to file() will close filename and decrement nfile
if (me == 0) { if (me == 0) {
if (nfile == maxfile) error->one(FLERR,"Too many nested levels of input scripts"); if (nfile == LMP_MAXFILE) error->one(FLERR,"Too many nested levels of input scripts");
if (filename) { if (filename) {
infile = fopen(filename,"r"); infile = fopen(filename,"r");
if (infile == nullptr) if (infile == nullptr)
error->one(FLERR,"Cannot open input script {}: {}", filename, utils::getsyserror()); error->one(FLERR,"Cannot open input script {}: {}", filename, utils::getsyserror());
if (nfile > 0) inlines[nfile - 1] = *output->thermo->get_line();
inlines[nfile] = -1;
infiles[nfile++] = infile; infiles[nfile++] = infile;
} }
} }
@ -346,6 +357,7 @@ void Input::file(const char *filename)
fclose(infile); fclose(infile);
nfile--; nfile--;
infile = infiles[nfile-1]; infile = infiles[nfile-1];
output->thermo->set_line(inlines[nfile-1]);
} }
} }
} }
@ -869,6 +881,7 @@ int Input::execute_command()
void Input::clear() void Input::clear()
{ {
if (narg > 0) error->all(FLERR,"Illegal clear command: unexpected arguments but found {}", narg); if (narg > 0) error->all(FLERR,"Illegal clear command: unexpected arguments but found {}", narg);
output->thermo->set_line(-1);
lmp->destroy(); lmp->destroy();
lmp->create(); lmp->create();
lmp->post_create(); lmp->post_create();
@ -1015,7 +1028,7 @@ void Input::include()
if (narg != 1) error->all(FLERR,"Illegal include command"); if (narg != 1) error->all(FLERR,"Illegal include command");
if (me == 0) { if (me == 0) {
if (nfile == maxfile) if (nfile == LMP_MAXFILE)
error->one(FLERR,"Too many nested levels of input scripts"); error->one(FLERR,"Too many nested levels of input scripts");
// expand variables // expand variables
@ -1054,14 +1067,15 @@ void Input::jump()
} }
if (me == 0) { if (me == 0) {
if (strcmp(arg[0],"SELF") == 0) rewind(infile); output->thermo->set_line(-1);
else { if (strcmp(arg[0],"SELF") == 0) {
rewind(infile);
} else {
if (infile && infile != stdin) fclose(infile); if (infile && infile != stdin) fclose(infile);
infile = fopen(arg[0],"r"); infile = fopen(arg[0],"r");
if (infile == nullptr) if (infile == nullptr)
error->one(FLERR,"Cannot open input script {}: {}", error->one(FLERR,"Cannot open input script {}: {}", arg[0], utils::getsyserror());
arg[0], utils::getsyserror()); inlines[nfile-1] = -1;
infiles[nfile-1] = infile; infiles[nfile-1] = infile;
} }
} }
@ -1914,7 +1928,9 @@ void Input::thermo_modify()
void Input::thermo_style() void Input::thermo_style()
{ {
int nline = *output->thermo->get_line();
output->create_thermo(narg,arg); output->create_thermo(narg,arg);
output->thermo->set_line(nline);
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

@ -50,13 +50,14 @@ class Input : protected Pointers {
int maxarg; // max # of args in arg int maxarg; // max # of args in arg
char *line, *copy, *work; // input line & copy and work string char *line, *copy, *work; // input line & copy and work string
int maxline, maxcopy, maxwork; // max lengths of char strings int maxline, maxcopy, maxwork; // max lengths of char strings
int nfile, maxfile; // current # and max # of open input files int nfile; // current # of open input files
int label_active; // 0 = no label, 1 = looking for label int label_active; // 0 = no label, 1 = looking for label
char *labelstr; // label string being looked for char *labelstr; // label string being looked for
int jump_skip; // 1 if skipping next jump, 0 otherwise int jump_skip; // 1 if skipping next jump, 0 otherwise
bool utf8_warn; // true if need to warn about UTF-8 chars bool utf8_warn; // true if need to warn about UTF-8 chars
FILE **infiles; // list of open input files FILE **infiles; // list of open input files
int *inlines; // list of saved line numbers of open input files
public: public:
typedef Command *(*CommandCreator)(LAMMPS *); typedef Command *(*CommandCreator)(LAMMPS *);

View File

@ -112,7 +112,7 @@ Thermo::Thermo(LAMMPS *_lmp, int narg, char **arg) :
lostbefore = warnbefore = 0; lostbefore = warnbefore = 0;
flushflag = 0; flushflag = 0;
ntimestep = -1; ntimestep = -1;
nline = 0; nline = -1;
// set style and corresponding lineflag // set style and corresponding lineflag
// custom style builds its own line of keywords, including wildcard expansion // custom style builds its own line of keywords, including wildcard expansion