# Terminate LAMMPS simulation once potential energy fluctuation is small

Dear all,

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?

Lesheng Li

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.

Thank you for the suggestions, I will try and keep this topic updated. Cheers!

Also see `fix halt` which can be used to terminate LAMMPS due to some criteria: fix halt command — LAMMPS documentation

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?

Thanks,
Lesheng

The slope function requires a vector, but the instantpe variable is a scalar.
You probably want to feed it the vector from fix 4 instead.

Hi Akohlmey, what do you mean by “feed it the vector from fix 4 instead”? Sorry I didn’t understand it well.

-Lesheng

Use `f_4` instead of `v_instantpe` in the slope command

Ohh I see. Thank you!

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?

-Lesheng

``````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.

This requires a modification to the fix vector command. I have just implemented such a modification and posted it for inclusion into LAMMPS on GitHub: Add nmax keyword to fix vector to restrict vector length by akohlmey · Pull Request #3867 · lammps/lammps · GitHub

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

Thank you all for the suggestions!

Thank you Akohlmey!