Accessing the velocities of atom at previous timestep

I am writing a fix which a force need to be added to the atoms which depends upon the average velocities of last 100 time steps.Any Idea how that can be done ?

Look at fix ave/atom for inspiration. I’m not sure if you’ll be able to use its output directly or if you’ll have to do the averaging in your fix, but the source code should offer some guidance.

Essentially, you need to keep track of the average velocity per atom from timestep to timestep, where the main challenge is to ensure that the velocities are communicated together with the atom when an atom crosses the processor boundary. Look at the pack/unpack exchange routines, where you’ll only be communicating three numbers per atom.

Additionally, you need to tell LAMMPS to call the grow/copy arrays methods in your class whenever reallocations occur or atoms get sorted by adding a callback.

In the “bookkeeping” methods (communication, sorting, reallocation, etc.), the array of size values.size() from fix ave/atom should just be replaced with your average velocity of size 3

Note: For forces, you may also need the average velocities of the ghost atoms, in which case you’ll need to add methods for packing/unpacking forward communication.

Edit: On second thought, I guess you need more than just the average velocities to do the moving average, but that should be a straightforward extension.

1 Like

Yes, that is possible. You can create an instance of a fix internally with modify->add_fix().
Several compute commands do this. For fixes it is more common to create a compute internally.

Normally, this fix would be created in the constructor, but you cannot create a new fix while creating a fix, so this has to be done in a Fix::post_constructor() function. See fix adapt as an example which uses fix STORE/ATOM to provide raw per-atom storage. With a fix computing a per-atom property like fix ave/atom, you can access the computed data by accessing Fix::vector_atom or Fix::array_atom depending on whether you define one or multiple properties per atom (e.g. vx, vy, and vz).

Fix ave/atom knows how to do a sliding window average but can do other kinds of averaging as well.

1 Like