# Accessing internal forces array before fixes are applied.

Dear LAMMPS users,

When writing a new fix for LAMMPS in the source code, one sets a mask for the fix which defines at what point in the simulation it’s called, and then writes a method for the fix with the same name, e.g. setting the mask as

then defining a method post_force which is called at the post_force stage.

I would like to know if there is any way to have a fix called after the forces from bonds, pairs, dihedrals and so on are set - but BEFORE contributions from any other fixes, such as fix NVT.

In other words, I would like to have LAMMPS call my method in my fix when the contents of the f[][] array has only the forces due to the gradient of the potential and nothing else such as thermostatting, langevin dynamics, and so on.

If not, is there any other way to get my hands on this information in each timestep?

Thanks

Gil Rutter

University of Warwick

Dear LAMMPS users,

When writing a new fix for LAMMPS in the source code, one sets a mask for
the fix which defines at what point in the simulation it's called, and then
writes a method for the fix with the same name, e.g. setting the mask as

then defining a method post_force which is called at the post_force stage.

I would like to know if there is any way to have a fix called after the
forces from bonds, pairs, dihedrals and so on are set - but BEFORE
contributions from any other fixes, such as fix NVT.

In other words, I would like to have LAMMPS call my method in my fix when
the contents of the f[][] array has only the forces due to the gradient of
the potential and nothing else such as thermostatting, langevin dynamics,
and so on.

fixes are executed in the order they are defined. so all you'd have to
do is to make sure that your fix is defined first.

axel.

What Axel says is correct, within the categories of points at
which fixes can be called. The post-force catetgory is the point
in time at which only bond/pair/etc forces are present, so if your
fix is the first post-force fix defined, it will have the untouched forces.

You can also use fix store/force to store those untouched forces,
which your fix could then access, even if your fix operated later in
the timestep.

Steve

Thanks for your replies. I need to access and manipulate the forces that the velocity Verlet algorithm will use to advance, and to do this before the thermostat alters them. I have made my fix be set in the input script before the thermostat. Would post_force be the correct category for my fix to work in?

I have noticed that if I ask my fix to print out the forces from f[][] on the very first time step, they are different depending on if I set NVT or NVE. They are the same if I use initial_integrate instead, but this gives the wrong answer to a test case so it’s obviously not doing what I intend.

Gil

Thanks for your replies. I need to access and manipulate the forces that the
velocity Verlet algorithm will use to advance, and to do this before the
thermostat alters them. I have made my fix be set in the input script before
the thermostat. Would post_force be the correct category for my fix to work
in?

yes. have a look at Verlet::run()

the modify class calls all post_force fixes right after all forces are
computed and the reverse communication for newton's 3rd law across
domains is done.

I have noticed that if I ask my fix to print out the forces from f[][] on
the very first time step, they are different depending on if I set NVT or
NVE. They are the same if I use initial_integrate instead, but this gives
the wrong answer to a test case so it's obviously not doing what I intend.

please recall how the velocity verlet algorithm works and how it is
implemented in LAMMPS:
a) you start from initial positions and velocities and forces.
b) then update the velocities for a half step from the forces
c) then update the position for a full step from the velocities
d) then recompute the forces
e) then update the velocities for another half step based on the new forces
f) continue from step b) until done

step a) is done during the "setup" phase, where coordinates and
velocities are read in (or set) and then the initial forces computed.
step b) and c) is handled in the "initial integrate" phase of a fix
step d) after that (and after a potential neighborlist rebuild)
step e) at the "final integrate" phase
and "post force" is right between d) and e)

so the reason that you see a different between nve and nvt is not that
you didn't get to access the forces properly, but rather due to the
fact that fix nvt modified the forces of the *previous* step, i.e.
those computed during the setup phase. if you insert your fix before
the integrator in "initial integrate", then - of course - you will
still see the original initial forces.

axel.
{"ft_lang_label":"__label__en","ft_lang_prob":0.930541,"math_prob":0.7655257,"size":4689,"snap":"2022-40-2023-06","text_gpt3_token_len":1106,"char_repetition_ratio":0.12251867,"word_repetition_ratio":0.5525114,"special_character_ratio":0.2217957,"punctuation_ratio":0.08020833,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9585809,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-07T12:31:57Z\",\"WARC-Record-ID\":\"<urn:uuid:da950448-2c6e-4948-b325-2a77763600cd>\",\"Content-Length\":\"28380\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:84a5946c-09c4-4085-9918-8b74e20dc547>\",\"WARC-Concurrent-To\":\"<urn:uuid:f115af97-a0bf-460d-a30e-01102009fffd>\",\"WARC-IP-Address\":\"104.21.77.20\",\"WARC-Target-URI\":\"https://matsci.org/t/accessing-internal-forces-array-before-fixes-are-applied/17608\",\"WARC-Payload-Digest\":\"sha1:7BDWOQ5FZUVN5HCH4LQNV5ABGI4OUPHB\",\"WARC-Block-Digest\":\"sha1:XZC5TQSFNGLYF4LT2KLT6PFUFGOIL7ZE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500456.61_warc_CC-MAIN-20230207102930-20230207132930-00296.warc.gz\"}"}