Variable evaluation error, post clear


I am attempting to `read_restart`s in a loop, as follows:

  variable NUMS equal atoms
  variable SIZE format NUMS %05.0f

  write_restart restart_${SIZE}.bin

  variable FRUIT loop 2

    label CRUNCH

    read_restart restart_\{SIZE\}\.bin     \.     \.     write\_restart restart\_{SIZE}.bin
    jump SELF CRUNCH

(where dots indicate other operations)

But post `clear`, it seems like the variable 'NUMS' is no longer defined,
because I get the following message:

  ERROR: Variable nums: Variable evaluation before simulation box is defined (src/variable.cpp:2096)
  Last command: read_restart restart_${SIZE}.bin

How might I get around this?


Just define all the variables after “read_restart” command and you will be there.

how can this work? your variable depends on the number of atoms in the system, but after clear, there are no more atoms. equal style variables are always evaluated fresh when expanded. you would have to define a different style of variable with the value of “SIZE” (e.g. index style) where the definition uses the immediate expansion (${name}), so that the evaluation of your equal style variable is done when the other variable is defined, not when it is referenced.

LAMMPS is doing the right thing here. your thinking is wrong.


Yes, that is the issue. My actual restart file also has other parameters in
the name, all of which are 'index' style variables, and they cause no

I tried the following, and it works!

  variable NUMS equal atoms
  variable SIZE format NUMS %05.0f
  variable PART index ${SIZE}

And then, I use \{PART\} instead of {SIZE} in the file names.