If statement parsing non-error

At the risk of generating another non-bug bug report for the second time in as many days…

The parsing on the boolean expression in the “if” command seems to return false for anything other than a numerical value (or a suitable expression that evaluates to a numerical value). For example, I could make an input file that says,

if "hello" then "print 'hello'"
if "1" then "print 1"
print "got to the end"

I might expect the output to be

LAMMPS ([version])
[error message]

or perhaps

LAMMPS ([version])
hello
1
got to the end
Total wall time: 0:00:00

(if it evaluated “hello” as being non-zero and thus true), but instead it gives

LAMMPS (23 Jun 2022)
1
got to the end
Total wall time: 0:00:00

This behavior changed from an earlier version of LAMMPS:

LAMMPS (3 Mar 2020)
hello
1
got to the end
Total wall time: 0:00:00

An old version does give an error:

LAMMPS (16 Aug 2013)
ERROR: Invalid Boolean syntax in if command (../variable.cpp:3695)

If you’re wondering, I tripped this by reading the documentation a bit too literally: I saw it said “evaluates to TRUE”, so I tried the constant TRUE out of curiosity:

if TRUE then "print 'yes'"

(I read it again and realized it needed to be 1 in place of TRUE). However, I was surprised that it didn’t give me an error AND evaluated to 0 (given that either “TRUE” shouldn’t evaluate to a number or it should be evaluated as something other than zero.)

This is a valid bug report for your LAMMPS version, but the bug has been found and fixed about a month ago. With the 3 Aug 2022 version, that was released today, the output is now:

LAMMPS (3 Aug 2022)
ERROR: If command boolean cannot be single string (src/variable.cpp:4877)
Last command: hello

Unlike equal style variable expressions, the if command allows string comparisons, but the special case of just a single string had been overlooked.

Well, had you used "$(true)" or "$(yes)" or "$(on)" then this would have worked since true, false, yes, no, on, off are valid keywords for equal style variables. They do evaluate to either 0 or 1. In logical expressions non-zero is true and zero is false.