Adding a new per atom variable in the source code

Dear Axel,

I’m trying out both the fix:array and the find_custom options you proposed.

I’m now able to define new variables through the lammps_command function and find them again through the code snippet below.

int tmp = -1;
int n = lmp->atom->find_custom(“d_xDuDt”,tmp);
double *dvector = lmp->atom->dvector[n];

How can I now write to individual atoms?

Given that I have nlocal atoms on my MPI rank and I know the index of the atom of interest “atomI”, I’d like to write a new double to dvector[atomI] or to lmp->atom->dvector[n][atomI]. If tried it in this manner but I get an error and gdb isn’t very informative about it (only gives me the line).

Thanks in advance,

Victor

Dear Axel,

I'm trying out both the fix:array and the find_custom options you proposed.

I'm now able to define new variables through the lammps_command function
and find them again through the code snippet below.

int tmp = -1;
int n = lmp->atom->find_custom("d_xDuDt",tmp);
double *dvector = lmp->atom->dvector[n];

How can I now write to individual atoms?

Given that I have nlocal atoms on my MPI rank and I know the index of the
atom of interest "atomI", I'd like to write a new double to dvector[atomI]
or to lmp->atom->dvector[n][atomI]. If tried it in this manner but I get an
error and gdb isn't very informative about it (only gives me the line).

​it is not clear what you are referring to as "index". atoms in LAMMPS have
an "atom id", which is stored in atom->tag[]. this "tag" value is the atom
id used in a data file or the "id" field in a dump file. these allow to
address atoms globally.

however, the per-atom storage, is associated with atoms and they are
distributed and in a different order. you have the "local" atoms​ (i.e.
atoms owned by a subdomain == MPI rank) between index 0 and nlocal and
"ghost" atoms (copies of atoms from neighboring subdomains up to the
communication cutoff) between nlocal and nlocal+nghost. to get the global
atom id from a local or ghost atom, you look at atom->tag[i] you will get
the global atom id of an atom, and the function atom->map(id) will return
the local atom index or -1 from a global atom id. index == -1 means, this
atom is not present on the local MPI rank.

if you are looping over atom indices from neighbor lists, be aware that
those are using local indices and in the jlist, atoms indices may be
"decorated" with extra bits, when an atom is connected via a bond or angle
or dihedral interaction and "special_bond" scaling factors are applied.

if you want gdb to be more helpful, please compile without optimization and
with -g. then gcc won't optimize away loop indices or other temporary
variables and you can look up their values as expected.

HTH,

axel.