[lammps-users] Tracking distance between two atoms

Dear Users,
I am trying to track the distance between two atoms during a simulation but having some problems with it.

Basically, I define
variable a1 equal x[1]-x[2]

variable a2 equal y[1]-y[2]
variable a3 equal z[1]-z[2]

variable dist_sq equal {a1}*{a1}+{a2}*{a2}+{a3}*{a3}

So, when I print the info of v_dist_sq using thermo_style, I get the same number each time. However, v_a1, v_a2, v_a3 keeps updating. So, I expect that the dist_sq should also change. But I did not see that happen.

I also try using fix print option but in that case too, dist_sq gave the same value as the first one while the values of {a1}, {a2} and {a3} kept changing.

I will appreciate if some could help me out here or let me know a way to get around it.



Try this:

variable dist_sq equal v_a1v_a1+v_a2v_a2+v_a3*v_a3

instead of:

variable dist_sq equal {a1}*{a1}+{a2}*{a2}+{a3}*{a3}
Also, please see:


Specifically the following, which I’ll cut and paste from that page:

"Using a $x, the value of the include variable is substituted for immediately when the line is read from the input script, just as it would be in other input script command. This could be the desired behavior if a static value is desired. Or it could be the desired behavior for an equal-style variable if the variable command appears in a loop (see the jump and next commands), since the substitution will be performed anew each time thru the loop as the command is re-read. Note that if the variable formula is enclosed in double quotes, this prevents variable substitution and thus an error will be generated when the variable formula is evaluated.

Using a v_x, the value of the included variable will not be accessed until the variable formula is evaluated. Thus the value may change each time the evaluation is performed. This may also be desired behavior.

As an example, if the current simulation box volume is 1000.0, then these lines:

variable x equal vol
variable y equal 2*$x 

will associate the equation string “2*1000.0” with variable y.

By contrast, these lines:

variable x equal vol
variable y equal 2*v_x 

will associate the equation string “2*v_x” with variable y.

Thus if the variable y were evaluated periodically during a run where the box volume changed, the resulting value would always be 2000.0 for the first case, but would change dynamically for the second case."