Feature request/Bug fix: allow full-line comments in data files

Dear all:

In the current stable version of LAMMPS, comments are only allowed to be trailing: a line can’t start with a “#” once the masses have been input. Comments before that are OK, but anything afterward causes an error, because it appears to short-circuit reading of the file, and an error such as “No atoms in data file” or “Needed molecular topology not in data file” is generated.

This seems like a logic bug or an unintended “feature.” Is there any way to fix this behavior?

(See the attached input files for a simple example that replicates the necessary behavior. All comment lines below “Masses” need to be deleted for the file to be read properly.)

Thanks,

–AEI

ethane.input (1.66 KB)

ethane.script (981 Bytes)

Dear all:

In the current stable version of LAMMPS, comments are only allowed to be
trailing: a line can't start with a "#" once the masses have been input.
Comments before that are OK, but anything afterward causes an error, because
it appears to short-circuit reading of the file, and an error such as "No
atoms in data file" or "Needed molecular topology not in data file" is
generated.

This seems like a logic bug or an unintended "feature." Is there any way to
fix this behavior?

support for comments was so far primarily added to allow for
consistency checks and store information in support for VMD/topotools.
e.g. you can indicate the desired atom style as "full" by adding a
comment " # full" to the Atoms keyword and then read_data will print a
warning, when the atom style differs. same for all kinds of XXXCoeff
statements.

adding support for handling comments *between* sections seems
possible, since there is already logic in place to skip over
whitespace-only lines. with a small change like the following
read_data should be able to handle this (it works for the provided
example).

diff --git a/src/read_data.cpp b/src/read_data.cpp
index 0fb388f..17b7f37 100644
--- a/src/read_data.cpp
+++ b/src/read_data.cpp
@@ -1566,6 +1566,7 @@ void ReadData::open(char *file)
void ReadData::parse_keyword(int first)
{
   int eof = 0;
+ int done = 0;

   // proc 0 reads upto non-blank line plus 1 following line
   // eof is set to 1 if any read hits end-of-file
@@ -1574,8 +1575,11 @@ void ReadData::parse_keyword(int first)
     if (!first) {
       if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
     }
- while (eof == 0 && strspn(line," \t\n\r") == strlen(line)) {
- if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
+ while (eof == 0 && done == 0) {
+ int blank = strspn(line," \t\n\r");
+ if ((blank == strlen(line)) || (line[blank] == '#')) {
+ if (fgets(line,MAXLINE,fp) == NULL) eof = 1;
+ } else done = 1;
     }
     if (fgets(buffer,MAXLINE,fp) == NULL) eof = 1;
   }

comments *inside* of sections should already be handled.

please let us know if this works to your satisfaction.

cheers,
     axel.