add link with more explanation to the illegal variable errors

This commit is contained in:
Axel Kohlmeyer
2024-11-20 11:15:12 -05:00
parent 8ba0d7bece
commit 4296db4991
2 changed files with 55 additions and 11 deletions

View File

@ -54,3 +54,26 @@ header of a data file (e.g. the number of atoms) is larger than the
number of lines provided (e.g. in the corresponding Atoms section)
and then LAMMPS will continue reading into the next section and that
would have a completely different format.
.. _err0003:
Illegal variable command: expected X arguments but found Y
----------------------------------------------------------
This error indicates that there are the wrong number of arguments for a
specific variable command, but a common reason for that is a variable
expression that has whitespace but is not enclosed in single or double
quotes.
To explain, the LAMMPS input parser reads and processes lines. The
resulting line is broken down into "words". Those are usually
individual commands, labels, names, values separated by whitespace (a
space or tab character). For "words" that may contain whitespace, they
have to be enclosed in single (') or double (") quotes. The parser will
then remove the outermost pair of quotes and then pass that string as
"word" to the variable command.
Thus missing quotes or accidental extra whitespace will lead to the
error shown in the header because the unquoted whitespace will result
in the text being broken into more "words", i.e. the variable expression
being split.

View File

@ -277,7 +277,8 @@ void Variable::set(int narg, char **arg)
copy(num[nvar],&arg[2],data[nvar]);
} else if (strcmp(arg[1],"uloop") == 0) {
if (narg < 3 || narg > 4)
error->all(FLERR,"Illegal variable command: expected 3 or 4 arguments but found {}", narg);
error->all(FLERR,"Illegal variable command: expected 3 or 4 arguments but found {}: {}",
narg, utils::errorurl(3));
if (narg == 4 && strcmp(arg[3],"pad") != 0)
error->all(FLERR, "Invalid variable uloop argument: {}", arg[3]);
if (find(arg[0]) >= 0) return;
@ -314,7 +315,9 @@ void Variable::set(int narg, char **arg)
// data = 1 value, string to eval
} else if (strcmp(arg[1],"string") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
int maxcopy = strlen(arg[2]) + 1;
int maxwork = maxcopy;
@ -348,7 +351,9 @@ void Variable::set(int narg, char **arg)
// data = 1 value, string to eval
} else if (strcmp(arg[1],"getenv") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
if (find(arg[0]) >= 0) {
if (style[find(arg[0])] != GETENV)
error->all(FLERR,"Cannot redefine variable as a different style");
@ -368,7 +373,9 @@ void Variable::set(int narg, char **arg)
// data = 1 value, string to eval
} else if (strcmp(arg[1],"file") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
if (find(arg[0]) >= 0) return;
if (nvar == maxvar) grow();
style[nvar] = SCALARFILE;
@ -387,7 +394,9 @@ void Variable::set(int narg, char **arg)
// data = nullptr
} else if (strcmp(arg[1],"atomfile") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
if (find(arg[0]) >= 0) return;
if (nvar == maxvar) grow();
style[nvar] = ATOMFILE;
@ -409,7 +418,9 @@ void Variable::set(int narg, char **arg)
} else if (strcmp(arg[1],"format") == 0) {
constexpr char validfmt[] = "^% ?-?[0-9]*\\.?[0-9]*[efgEFG]$";
if (narg != 4) error->all(FLERR,"Illegal variable command: expected 4 arguments but found {}", narg);
if (narg != 4)
error->all(FLERR,"Illegal variable command: expected 4 arguments but found {}{}",
narg, utils::errorurl(3));
int ivar = find(arg[0]);
int jvar = find(arg[2]);
if (jvar < 0)
@ -446,7 +457,9 @@ void Variable::set(int narg, char **arg)
// data = 2 values, 1st is string to eval, 2nd is filled on retrieval
} else if (strcmp(arg[1],"equal") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
int ivar = find(arg[0]);
if (ivar >= 0) {
if (style[ivar] != EQUAL)
@ -472,7 +485,9 @@ void Variable::set(int narg, char **arg)
// data = 1 value, string to eval
} else if (strcmp(arg[1],"atom") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
int ivar = find(arg[0]);
if (ivar >= 0) {
if (style[ivar] != ATOM)
@ -498,7 +513,9 @@ void Variable::set(int narg, char **arg)
// immediately store it as N-length vector and set dynamic flag to 0
} else if (strcmp(arg[1],"vector") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
int ivar = find(arg[0]);
if (ivar >= 0) {
if (style[ivar] != VECTOR)
@ -540,7 +557,9 @@ void Variable::set(int narg, char **arg)
// data = 2 values, 1st is Python func to invoke, 2nd is filled by invoke
} else if (strcmp(arg[1],"python") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
if (!python->is_enabled())
error->all(FLERR,"LAMMPS is not built with Python embedded");
int ivar = find(arg[0]);
@ -593,7 +612,9 @@ void Variable::set(int narg, char **arg)
// dvalue = numeric initialization from 2nd arg, reset by internal_set()
} else if (strcmp(arg[1],"internal") == 0) {
if (narg != 3) error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}", narg);
if (narg != 3)
error->all(FLERR,"Illegal variable command: expected 3 arguments but found {}{}",
narg, utils::errorurl(3));
int ivar = find(arg[0]);
if (ivar >= 0) {
if (style[ivar] != INTERNAL)