[lammps-users] Structure of if-sentences in LAMMPS and declaration of variables.

Dear all,

I have a few questions regarding the "if" command in LAMMPS, which makes it possible to write if - then - else structures like in regular programming. However, when the expressions become complicated, it is hard to make it work, and to understand how to write it in a correct manner.

In my input file, I want to find the polar coordinate of an atom with respect to the origin, which means I need to calculate the distance R and the angle THETA of each atom with respect to the origin. The distance is trivial, but to find the angle I use an expression including the function atan(f(x,y)). If f(x,y) = 0, which is the case with an atom placed in the origin, this function produce an error. I therefore want to use an if-expression to check if both x = y = 0, and then put THETA = 0 or THETA = function_value depending on the result. But how to do this in a correct way?

I have tried one version, which has this structure:
if $x == $y == 0 then &
   "variable THETA atom 0" &
else &
   "variable THETA atom "2*atan(y/(sqrt(x^2+y^2)+x))""

One problem here is how to properly define the expression to be evaluated - should it be enclosed in parentheses? Can one check two variables at the same time, like in the above code, or should I then use something like:
if $x == 0 then "if $y == 0 then "variable THETA atom 0" else "variable THETA atom atan(f(x,y)""

Another problem is that if one uses complicated expressions like above, there is quickly a need for several layers of quotation marks "", but how does LAMMPS handle this?

Also, will LAMMPS be able to define variables from inside an if-expression like above, or should I rather somehow use an if-sentence inside the variable declaration? How would the structure be then?

Thanks for any contributions to solve this problem.

Sincerely,
Christer H. Ersland.

The LAMMPS if-then-else is not a programming language.
It's just a way to conditionally execute two sets of commands
based on a simple test that has a simple syntax. None of
things you want to do are possible in a LAMMPS input
script without a considerably smarter parser.

You don't say whether you are computing this on one atom
or every atom, or what you want to do with the quantity, but
I would probably write a small compute or fix to do this
calculation.

Steve