Using if command in Lammps

Dear lammps users,

I would like to count the number of atoms when velocity of particles meets the certain conditions. I used below command to accheive it.

compute avejVztip3p tip3p reduce/region interest ave v_jVzAtom
fix trap_v all ave/time 10 1000 10000 c_avejVztip3p
variable trap_velocity equal f_trap_v
if “${trap_velocity}< 35” then “variable num_na equal count(trapped_ion)” &"fix 16 all ave/time 10 1000 10000 v_num_na "

I want to pass the time averaged velocity to a variable, however, the “c_avejVztip3p” is not equal to “v_trap_velocity” in the output, so it seems I did something wrong. What is the problem?

Also, I wonder if the count atom action happends at the same time step as those time step when the veloicy meets the certain condition.

The compute does its calculation every step, whereas the fix is returning a new average every 10000 steps. It makes sense that they would be two different numbers.

I suspect you are mistaken how LAMMPS will evaluate this. LAMMPS processes each line of input as it encounters it. So the if statement is going to be evaluated once with the current value at the time (i.e. before the run and either define the fix or does not define the fix (which will then be triggered at regular intervals, and not when the if condition is true during a run).

To do what you describe you would need to write a custom fix.