Print timestep when two atoms cross x-position

Dear all,

I want to have the exact timestep at which two atoms interchange their order according to the x-direction. That is, initially atom1 has a bigger x-coordinate than atom2, how do I efficiently measure the timestep at which the x-coordinate of atom2 becomes larger than this of atom1. Printing the x-coordinate of the particles at every timestep does not seem an efficient solution and preferably I would also like the program to terminate when this event happens. Furthermore, both particles need to be able to move, so I cannot restrict the motion of one of them. As the program is written in such a way that atom1 likes to have a bigger x-coordinate than atom2, it is also true that once they cross along the x-direction, they probably immediately cross back, so I need the exact timestep as this is a rear event.

Thank you for your future answers!

Assuming that you want to monitor the position of two particular atoms, then you can leverage the fix halt command:

variable a1x equal x[1]
variable a2x equal x[2]

# Monitor the position of atoms every other timestep.
fix STOP all halt 2 v_a2x > v_a1x error continue

# Run the simulation.
fix NVT all nvt temp 300 300 $(100*dt)
run 10000000

# When the halt condition is triggered, the simulation stops and the remaining of the script is executed.
print '$(step) ${a1x} ${a2x}'

You can play with the variables to dynamically monitor atoms that are in a certain region. Check the documentation for the variable command if you need more complex definitions.

Dear hothello,

I have slightly changed it to a single variable

variable ax equal "x[2] - x[1]"

as I got an error when using two variables in the condition of the fix halt, but now your solution indeed solves my problem. Thank you so much for your excellent response!

My bad, in fact the halt condition requires a numerical input, as stated in the documentation :stuck_out_tongue:

Another workaround is to use an instant variable:

fix STOP all halt 2 v_a2x > $(v_a1x) error continue