Nested if loops (if loops within if loops) in LAMMPS


Hello LAMMPS users,

I am using the LAMMPS version 29Oct2020. I am currently attempting to do a KMC simulation to calculate the diffusion coefficient of an Inconel-Ni alloy using LAMMPS. As a part of that, I am having to use nested if loops in order to decide which activation energy value to use. I have attached the screenshot of a part of my code.
Since if-else loops in LAMMPS uses the ‘&’ sign to define the continuity of the loop, and that indents do not make any difference, how do I ensure that my loop goes based on the indents that I have given, i.e. for example, if the ’ if “{NRAND}<={r1}” ’ condition is not satisfied, the code should then ideally execute the ’ elif “{NRAND}<={r2}” '. But I am not sure it works like that because of the ‘&’ signs and not having curly brackets like in C++ or the indentations like in Python to clearly define blocks of loops. Is there any way to circumnavigate this issue? Thank you.

Regards,
Rajesh

If you want to use conditionals to set variables (and not to choose execution paths), you can build equal-style variables using Boolean operators, which overload from false / true to 0.0 or 1.0 when used numerically.

So, for example:

variable branch equal "(${NRAND} >= ${r1}) + &
    (${NRAND} >= ${r2}) + (${NRAND} >= ${r3})"
variable counter equal "${countFe} * (${branch} == 0) + &
    ${countNi} * (${branch} == 1) + &
    ${countCr} * (${branch} == 2)"
variable jrandom equal "1 + ${counter} * random(0, 1, ${seed4})"
variable E equal "(${counter} != 0) * (${E_Fe} * (${branch} == 0) + &
    ${E_Ni} * (${branch} == 1) + &
    ${E_Cr} * (${branch} == 2) )"

Note that & is the general line-continuation character (5.2. Parsing rules for input scripts — LAMMPS documentation) and is not specially interpreted in a different way in the if command. Also note that only one level of nesting in quotes is allowed, so deeply nested if loops probably will not work in LAMMPS (or will require a cruel and unusual amount of careful jumping).