[lammps-users] patch: 'jump' forward in current file

Hello,

For common tasks, I tend to write generic input scripts, which
requires frequent use of "if". Multi-line "if" statements can be ugly,
but one learns to cope.

Recently, however, the command string I wanted to conditionally
execute required quotes. But the "if" statement requires the command
string to be in quotes. To the best of my knowledge and brief
experimentation, LAMMPS doesn't handle nested quotes.

To get around this, I conceptually replaced 'if condition then "do
this" "and this"' with 'if not-condition then "jump
over-these-things"'.

Workable... except "jump" requires a script name and my generic
scripts don't generally know their final names, plus stdin redirection
(i.e. '<') would never work.

By specifying a file name of "NULL", the one-line change to
"input.cpp" shown below allows "jump" to jump ahead to a label in the
current script without closing and subsequently reopening the script
file. (Disclaimer: I'm not aware of any side-effects, but...) This
solves the limitations described above. For me the desired usage looks
like:

if not-condition then "jump NULL not_today"
...
(assorted lammps commands, maybe with "quotes")
...
label not_today

As an added bonus, the enclosed command block is easier to write,
read, and debug.

Regards,
Craig

--- make/lammps-23Sep10/src/input.cpp 2010-09-08 20:01:34.000000000 -0400
+++ dev/input.cpp 2010-09-21 22:58:07.000000000 -0400
@@ -619,13 +619,13 @@

   if (jump_skip) {
     jump_skip = 0;
     return;
   }

- if (me == 0) {
+ if (me == 0 && strcmp(arg[0],"NULL")) {
     if (infile != stdin) fclose(infile);
     infile = fopen(arg[0],"r");
     if (infile == NULL) {
       char str[128];
       sprintf(str,"Cannot open input script %s",arg[0]);
       error->one(str);

Workable... except "jump" requires a script name and my generic
scripts don't generally know their final names, plus stdin redirection
(i.e. '<') would never work.

This change seems clever, but I assume it wouldn't work when
jumping to a previous line. I don't want the jump command to have
multiple behaviors.

How can your script not know its name? The command that
launched it could always pass the name of the script as
a variable into the script (-var command line switch), so the
script could use that as the jump arg.

Steve

This change seems clever, but I assume it wouldn't work when
jumping to a previous line. I don't want the jump command to have
multiple behaviors.

Good point about consistency. My motivation was the same: I wanted to
avoid having to remember when a particular script requires special
treatment (e.g. -var).

To make this proposed extension behave consistently when the label is
on a previous line, how about adding 'else rewind(infile)'? (I'm not
sure what will happen if the script is fed to lammps via stdin. Info
found from a quick Google search suggests it might, or might not, work
as desired.)

Steve,

This augmented segment of input.cpp code for the jump command allows
jumping forward and back within the current script by specifying
"NULL" as the filename. On my machine it works for scripts fed via
'-in' or '<'. For jumping within a single file, this improves script
generality, because renaming a script no longer requires updating the
script or feeding the script its name via the '-var' switch.

  if (me == 0) {
    if (strcmp(arg[0],"NULL") == 0) {
      rewind(infile);
    }
    else {
      if (infile != stdin) fclose(infile);
      infile = fopen(arg[0],"r");
      if (infile == NULL) {
        char str[128];
        sprintf(str,"Cannot open input script %s",arg[0]);
        error->one(str);
      }
      infiles[nfile-1] = infile;
    }
  }

Thanks,
Craig

PS Here's the script I used for testing:

label start
print starting
#jump NULL end
label middle
print middling
#jump NULL start
label end
print ending

Posted a 28 Sep 10 patch to add a SELF option for this.

Thanks,
Steve