Access neighbors list to use in a C library

Hi All,

I am trying to build a new subroutine from scratch in LAMMPS and I need to access the neighbours list for a given atom for the same. Can anyone tell me how can I get the list of neighbour atoms to a given atom in the C code? I know the index of the atom whose neighbors I want.

You need to provide more information to get suitable help.

It does not make sense to “add a subroutine” to LAMMPS. If you add a feature you would add a new class, which will translate into a “style”.

As for the neighbor list, there is not the neighbor list, but potentially multiple and LAMMPS will automatically optimize how to build them in the most efficient way.
It depends on what you plan to do and and which kind of style is the best choice for your needs how to proceed. Typically the way to get access to a neighbor list is to create a neighbor list request object, set its properties (half/full, perpetual/occasional, etc.) and then either add the required method that will provide the suitable pointer or explicitly request a build.

Thanks a lot akohlmey! I am extremely new here, will learn posing questions in a better way.

Let me describe my problem in a bit more detail:
There is a fix atom/swap command, which choses any two particles at random and exchanges their particle types restricted to a metropolis acceptance. What I want instead is to alter the code such that it choses one particle and then choses the other particle in the neighbourhood of the first particle and then exchange their particle types restricted to a metropolis acceptance. For that purpose, I wished to know if I can access the neighbor list of the first particle and then choose a particle randomly from that list.

Currently I am trying to modify the atom/swap code itself such that it can serve my purpose.

Then I suggest you carefully study the (new) LAMMPS paper and read through this part of the manual: 4. Information for Developers — LAMMPS documentation

As already mentioned, you would need to create a neighbor list request and then process it accordingly.
The best way to learn how to do this is to study similar parts of LAMMPS that already use a neighbor list. For example fix bond/create or fix bond/swap.


I also suggest to either make a copy of the fix under a new name or - if possible - implement your modification as a derived class (probably not beneficial in this case).

I have made a copy of the atom/swap code with a different name.

I am much more proficient in python than at C++. Would you suggest that using a python wrapper would be easier?
I am thinking of the following in my case:
Run the background simulation for N steps and then at every Nth step, call the neighbors list, choose a random particle and its neighbour, swap them and then input that back to lammps.

My code is not that heavy, so currently efficiency is not the biggest concern.

Not as such for two reasons.

  1. While you can access existing neighbor lists from Python, there is no interface currently to request one of you own. Existing neighbor lists heavily depend on the specifics of the simulation, e.g. the pair style in use and may not be complete (if you have a complex model).
  2. you still need to understand the C++ data structures and flow of control.

That is not supposed to mean that it cannot be done, but in my experience the skill of understanding the flow of control and the code design is often underestimated while the importance of the familiarity with a specific programming language is overestimated. In other words, it is probably easier to improve your skills in C++ programming than to figure out what you have to do, and the latter will be much easier to realize for a setup like yours in C++.

Thanks a lot! Understood your point.