I am trying to terminate my LAMMPS simulation once the fluctuation of the potential energy is small. What’s in my mind is to compute an averaged value of the potential energy for a given time window and compare the adjacent ones. If the difference is smaller than some value, then terminate the simulation. Does anyone have any idea how can I do so? Or if you have better way to do so, could you share it with me, please?

What you describe is not monitoring the fluctuation of the potential energy but the convergence toward a constant value.

You can compute a sliding window average of a global property like the total potential energy using fix ave/time. You can then regularly store those values with fix vector. and finally, you can then monitor the slope of the vector points using the slope() function on that vector with an equal style variable. Ideally, one would modify the slope() function implementation for that purpose to take the vector length used as additional argument, so that one would look at the slope for the N most recent data points.

To monitor the fluctuations you would compute a sliding window average as well, but then determine (with an equal style variable) the squared difference of the instantaneous energy from the averaged energy and from that determine the width of the distribution of the energies.

Hi Akohlmey, I have prepared the following script according to your suggestions for this:

variable instantpe equal pe
fix 3 all ave/time 1 500 500 v_instantpe file aver_pe.txt
fix 4 all vector 500 v_instantpe
variable sloppe equal slope(v_instantpe)
fix 5 all halt 500 v_sloppe < 10
run 20000

But it gave me an error saying that “ERROR: Variable instantpe: Mis-matched special function variable in variable formula”. Is there anything that I am doing wrong here?

Plus, when I calculate the slope of the vector, is there any way to only calculate the slop of the last several elements in the vector instead of the entire data set in the vector?

variable pe equal pe
fix long_ave all ave/time 10 100 1000 v_pe ave window 5
fix short_ave all ave/time 10 100 1000 v_pe
run 5000 post no
variable diffpe equal abs(f_long_ave-f_short_ave)
fix halt_if_converged all halt 1000 v_diffpe <= 10
run 10000000

This runs your simulation for 5 windows, each of length 1000 steps, to establish the first 5-window average, and then checks every new window if the average PE is within 10 units of the last 5-window average and stops if the condition is met. Please see documentation for each command for details about how the keywords work.

It will take a little while until this change can make it into the LAMMPS distribution, since we currently have a feature freeze for the next stable release. However, you could download a snapshot of the code from GitHub and compile this as your LAMMPS version: https://github.com/akohlmey/lammps/archive/refs/heads/fix-vector-nmax.tar.gz