Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor input file processing flow of control #1537

Merged
merged 4 commits into from Jul 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 31 additions & 26 deletions src/input.cpp
Expand Up @@ -85,8 +85,9 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp)
ifthenelse_flag = 0;

if (me == 0) {
nfile = maxfile = 1;
infiles = (FILE **) memory->smalloc(sizeof(FILE *),"input:infiles");
nfile = 1;
maxfile = 16;
infiles = new FILE *[maxfile];
infiles[0] = infile;
} else infiles = NULL;

Expand Down Expand Up @@ -138,7 +139,7 @@ Input::~Input()
memory->sfree(work);
if (labelstr) delete [] labelstr;
memory->sfree(arg);
memory->sfree(infiles);
delete [] infiles;
delete variable;

delete command_map;
Expand Down Expand Up @@ -206,17 +207,7 @@ void Input::file()
MPI_Bcast(&n,1,MPI_INT,0,world);
if (n == 0) {
if (label_active) error->all(FLERR,"Label wasn't found in input script");
if (me == 0) {
if (infile != stdin) {
fclose(infile);
infile = NULL;
}
nfile--;
}
MPI_Bcast(&nfile,1,MPI_INT,0,world);
if (nfile == 0) break;
if (me == 0) infile = infiles[nfile-1];
continue;
break;
}

if (n > maxline) reallocate(line,maxline,n);
Expand Down Expand Up @@ -250,8 +241,8 @@ void Input::file()
}

/* ----------------------------------------------------------------------
process all input from filename
called from library interface
process all input from file at filename
mostly called from library interface
------------------------------------------------------------------------- */

void Input::file(const char *filename)
Expand All @@ -261,21 +252,27 @@ void Input::file(const char *filename)
// call to file() will close filename and decrement nfile

if (me == 0) {
if (nfile > 1)
error->one(FLERR,"Invalid use of library file() function");
if (nfile == maxfile)
error->one(FLERR,"Too many nested levels of input scripts");

if (infile && infile != stdin) fclose(infile);
infile = fopen(filename,"r");
if (infile == NULL) {
char str[128];
snprintf(str,128,"Cannot open input script %s",filename);
error->one(FLERR,str);
}
infiles[0] = infile;
nfile = 1;
infiles[nfile++] = infile;
}

// process contents of file

file();

if (me == 0) {
fclose(infile);
nfile--;
infile = infiles[nfile-1];
}
}

/* ----------------------------------------------------------------------
Expand Down Expand Up @@ -1045,11 +1042,9 @@ void Input::include()
error->all(FLERR,"Cannot use include command within an if command");

if (me == 0) {
if (nfile == maxfile) {
maxfile++;
infiles = (FILE **)
memory->srealloc(infiles,maxfile*sizeof(FILE *),"input:infiles");
}
if (nfile == maxfile)
error->one(FLERR,"Too many nested levels of input scripts");

infile = fopen(arg[0],"r");
if (infile == NULL) {
char str[128];
Expand All @@ -1058,6 +1053,16 @@ void Input::include()
}
infiles[nfile++] = infile;
}

// process contents of file

file();

if (me == 0) {
fclose(infile);
nfile--;
infile = infiles[nfile-1];
}
}

/* ---------------------------------------------------------------------- */
Expand Down