Add force to LAMMPS in fix_addforce

Dear LAMMPS users,

I am using the lammps_14Mar13. I am intending to add an additional force after the standard mechanical force calculation, then do energy minimization for the system. However, the calculation of this additional force requires the input of the coordinates of all the atoms in the system. The process should be like this:

  1. Use LAMMPS to calculate the standard mechanical force of the system.
  2. Based on the coordinates of the atoms, calculate the additional force and add this force to standard mechanical force in the fix_addforce.cpp code.
  3. Tell LAMMPS the new force values and ask LAMMPS to do energy minimization for one step to get new coordinates of atoms.
  4. go back to 1, repeat step 1,2,3 until the minimum energy value is reached.

What I am going to do is to extend the fix_addforce.cpp to add my additional force to the system. However, there is a problem that when I am calculating the additional force, I need not only current coordinates of the atoms but also the coordinates of the atoms in the past timesteps.

Then my question is first, whether it is possible to extend the fix_addforce.cpp code to add my additional force to the system and second whether I can define a global variable to save the coordinates of atoms every step so that I can use it directly when I am calculating the additional force (or whether there is another way to save the coordinates of atoms every step).

Looking forward to your reply.

Thank you so much,
Weiwei

Dear LAMMPS users,

I am using the lammps_14Mar13. I am intending to add an additional force
after the standard mechanical force calculation, then do energy minimization
for the system. However, the calculation of this additional force requires
the input of the coordinates of all the atoms in the system. The process
should be like this:
1. Use LAMMPS to calculate the standard mechanical force of the system.
2. Based on the coordinates of the atoms, calculate the additional force and
add this force to standard mechanical force in the fix_addforce.cpp code.
3. Tell LAMMPS the new force values and ask LAMMPS to do energy minimization
for one step to get new coordinates of atoms.
4. go back to 1, repeat step 1,2,3 until the minimum energy value is
reached.

What I am going to do is to extend the fix_addforce.cpp to add my additional
force to the system. However, there is a problem that when I am calculating
the additional force, I need not only current coordinates of the atoms but
also the coordinates of the atoms in the past timesteps.

Then my question is first, whether it is possible to extend the
fix_addforce.cpp code to add my additional force to the system and second
whether I can define a global variable to save the coordinates of atoms
every step so that I can use it directly when I am calculating the
additional force (or whether there is another way to save the coordinates of
atoms every step).

no, this is a bad idea. the very basic design of LAMMPS is based on
using a domain decomposition.
collecting *all* coordinates will massively reduce the parallel
performance. there is no simple way to have a "global" variable with
MPI parallelization. if you don't have any experience with MPI
programming, you should probably attend a parallel programming
tutorial. for what you want to do, you probably need more than just a
basic tutorial, too.

you should spend some time figuring out if there is a way to reduce
the amount of coordinate information that is needed.
this is done for example in the USER-COLVARS package, which interfaces
to the non-parallel colvars library.
for as long as the collective variables only affect a small number or
coordinates, the impact on the parallelization is very small.

axel.

Hi Axel and LAMMPS users,

Thanks for your reply. I am not familiar with MPI. I will attend a parallel programing tutorial and check the USER-COLVARS package as you suggested.

I am thinking that whether I can define a variable (three-dimensional as xxx[atoms][coordinates][steps]) in the atom class and record the atom coordinates of each step to that variable. My code can use the coordinates by calling that variable (atom->xxx).

Also, I wonder whether it is possible to extend the fix_addforce.cpp to add my force to the system or whether there is a better way to do it.

I am a beginner in LAMMPS. Your suggestion would be really helpful to me. Thanks again!!

Weiwei

Hi Axel and LAMMPS users,

Thanks for your reply. I am not familiar with MPI. I will attend a parallel
programing tutorial and check the USER-COLVARS package as you suggested.

I am thinking that whether I can define a variable (three-dimensional as
xxx[atoms][coordinates][steps]) in the atom class and record the atom
coordinates of each step to that variable. My code can use the coordinates
by calling that variable (atom->xxx).

like is said before. this cannot work. you have to learn how parallel
programming with MPI works and you will understand why.
also, even if you implement something like this, you will run out of
RAM quickly.

Also, I wonder whether it is possible to extend the fix_addforce.cpp to add
my force to the system or whether there is a better way to do it.

this doesn't make sense either. fix addforce already has everything
that is needed to add a force. this can be a fixed value or defined in
a variable, which in turn can utilize a compute.

I am a beginner in LAMMPS. Your suggestion would be really helpful to me.

it is not a good idea to start modifying LAMMPS before you even have a
basic understanding of its fundamental design.

axel.

Hi Axel and LAMMPS users,

Thanks for Axel’s reply! In my case, I don’t need to run LAMMPS in parallel. 1 CPU is fine for me. Is it still impossible to define a variable to collect the coordinates of the atoms of each step if I run LAMPPS in serial?

I have also considered using LAMMPS as a library and using fix_external.cpp via the library interface. However, since I need atoms’ coordinates to calculate my force, transferring the coordinates would be very time-consuming. Therefore I want to write my code of calculating the new force into the fix_addforce.cpp and stay in LAMMPS’s current class. Which method should be more promising or any other way to work it out?

Thank you so much!!!

Weiwei

Hi Axel and LAMMPS users,

Thanks for Axel's reply! In my case, I don't need to run LAMMPS in parallel.
1 CPU is fine for me. Is it still impossible to define a variable to collect
the coordinates of the atoms of each step if I run LAMPPS in serial?

for the third (and last) time: it is a bad idea. LAMMPS is designed to
be parallel throughout. that has implications even for the serial
case.

I have also considered using LAMMPS as a library and using fix_external.cpp
via the library interface. However, since I need atoms' coordinates to
calculate my force, transferring the coordinates would be very
time-consuming. Therefore I want to write my code of calculating the new

this makes no sense. again, you don't know enough LAMMPS and you don't
know enough parallel programming to understand what you are trying to
do. you put the carriage before the horse. learn the basics. see how
things work, and then this whole deliberation will become a non-issue
(and you'll understand the pointlessness of your questions). the
library interface gives you direct access to all kinds of atom based
properties.

force into the fix_addforce.cpp and stay in LAMMPS's current class. Which
method should be more promising or any other way to work it out?

it is pointless to discuss this before you have a sufficient
understanding of the inner workings of LAMMPS. we just go in circles
because you are making assumptions that are invalid and i am running
out of patience to explain them over and over again. sorry, there are
no shortcuts to doing the fundamentals.

axel.

Thanks for Axel’s reply. Sorry for asking naive questions. I am learning it and trying to figure out a way to solve my problem. Thank you all the same.

Weiwei

If you just want to run LAMMPS in serial, then the “x” array
of coordinates is all the atoms in the system.

You can write a new fix (or modify fix addforce, or whatever you like),
to accumulate coords in some new data structure at each timestep
and use the old info to add a new force.

As Axel said you could easily use a lot of memory if you’re not

careful how you do that, but nothing in LAMMPS would stop
you from doing that.

Steve

Hi Steve, Axel and LAMPPS users,

Thanks for your reply. I am now studying the COLVARS package and trying to figure out whether I can add my force through that package. Thanks for your help!

Weiwei

Hi Steve, Axel and LAMPPS users,

Thanks for your reply. I am now studying the COLVARS package and trying to
figure out whether I can add my force through that package. Thanks for your
help!

no, no, no, noooo!

please do yourself and us a favor and reign in your ambition and learn
LAMMPS first and worry about adding your hack to the code later.
considering the level of insight you have displayed so far, everything
else will lead to disaster.

i mentioned this package to you because it demonstrates how to apply
forces efficiently from a serial code to a small subset of atoms. you
do *not* want to hack into that. you probably will have a hard time to
understand the flow of control. i could have mentioned fix imd just as
well, as it does the same (and then some).

but this is all going to be a pointless exercise unless you start
approaching your problem in a more reasonable manner.

axel.