[lammps-users] Accessing one fix from another

Hello,

I am coding a custom fix, which requires an operation to extract a vector from another custom fix.

fixtarget = … (stuck on how to obtain the correct reference)

if(fixtarget){

input_vector = (int *) fixtarget->extract(“int_vector”,itmp);

}

However, as shown above, I am stuck on how to generate the “fixtarget” reference to access the desired fix. The closest example that I could find was the referencing of fix_drude in pair_thole (key syntax used as an example is pasted below). I tried to recreate a similar procedure in my custom fix, but it required importing the other fix’s header file. It appeared that doing so resulted in errors due to re-declaration of the common fix functions.

If there is any advice on how to correctly reference one fix from another (to perform an extract), I would greatly appreciate it.

Thank you,
Anne

from pair_thole.h:

protected:

class FixDrude * fix_drude;

from pair_thole.cpp:

#include “fix_drude.h”

void PairThole::init_style()
{
if (!atom->q_flag)
error->all(FLERR,“Pair style thole requires atom attribute q”);
int ifix;
for (ifix = 0; ifix < modify->nfix; ifix++)
if (strcmp(modify->fix[ifix]->style,“drude”) == 0) break;
if (ifix == modify->nfix) error->all(FLERR, “Pair thole requires fix drude”);
fix_drude = (FixDrude *) modify->fix[ifix];

neighbor->request(this,instance_me);
}

Hello,

I am coding a custom fix, which requires an operation to extract a vector from another custom fix.

fixtarget = … (stuck on how to obtain the correct reference)

if(fixtarget){

input_vector = (int *) fixtarget->extract(“int_vector”,itmp);

}

However, as shown above, I am stuck on how to generate the “fixtarget” reference to access the desired fix.

there are plenty of examples for this in the code. I am counting 74 files in the current LAMMPS distribution.
the basic operation is:

Fix *fixtarget = modify->find_file(“target_id”);

The fix instance is uniquely identified by its fix ID and since Fix::extract() is defined in the Fix base class as a virtual function, there is no need to import a specific fix header or cast the pointer to that specific fix. that would only be required if you would want to access data members of a specific kind of fix directly (i.e. without going through Fix::extract()).

The closest example that I could find was the referencing of fix_drude in pair_thole (key syntax used as an example is pasted below).

[…]

I tried to recreate a similar procedure in my custom fix, but it required importing the other fix’s header file. It appeared that doing so resulted in errors due to re-declaration of the common fix functions.

that doesn’t make sense. there must be something that you have done incorrectly or there is a bug in the header file you imported.

please see above for an example of how to do it.

Axel.

Thanks Axel,

I ended up getting this to work:

int ifix = modify->find_fix(“fix_target_id”);
Fix *fixtarget = modify->fix[ifix];

Anne