help with library.cpp


I wrote the function below to scatter dihedrals back to lammps. Because the number of dihedrals changes, I increase the length of the lists for dihedrals. However, the code crashed or gave me the errors dihedral atoms missing on processers. Could you give me some advices? Thank you very much.


void lammps_scatter_dihedrals(void *ptr, int **data,int nn,int maxd,FILE* fp)
   LAMMPS *lmp = (LAMMPS *) ptr;

   int natoms = static_cast<int> (lmp->atom->natoms);
   int ndihedrals = static_cast<int> (lmp->atom->ndihedrals);
   int ndihedraltypes = static_cast<int> (lmp->atom->ndihedraltypes);

   int i,j,m,atom1,atom2,atom3,atom4,itype;

   int *numptr= lmp->atom->num_dihedral;

   int **dptr = (int **) data;

   for(i=0;i<natoms;i++) {
   if ((m = lmp->atom->map(i+1)) >= 0)

   int **vptr1 = lmp->atom->dihedral_atom1;
   int **vptr2 = lmp->atom->dihedral_atom2;
   int **vptr3 = lmp->atom->dihedral_atom3;
   int **vptr4 = lmp->atom->dihedral_atom4;
   int **vvptr = lmp->atom->dihedral_type;

     for (i = 0; i < nn; i++){
         if (itype <= 0 || itype > ndihedraltypes)
           lmp->error->one(FLERR,"Invalid dihedral type in Dihedrals section of data file");
         if ((m = lmp->atom->map(atom2)) >= 0) {
           vvptr[m][numptr[m]] = itype;
           vptr1[m][numptr[m]] = atom1;
           vptr2[m][numptr[m]] = atom2;
           vptr3[m][numptr[m]] = atom3;
           vptr4[m][numptr[m]] = atom4;




I don't understand what you are trying to do.
An array like dihedral_atom1, which is owned
by LAMMPS, cannot be "grown" except in
the first dimension, and LAMMPS takes care
of that whenever the # of atoms on a processor
changes (e.g. at reneighboring).

If you are trying to grow the 2nd or 3rd dims,
you can't do it that way.


Hi, Steve,

Thank you for your reply. Actually I want to grow the second dimension because the number of dihedrals will change and dihedral list will be different during crosslinking procedure. If I can not grow the second dimension of lists for dihedral_atom, does this mean I can not change dihedrals by updating the lists of dihedral_atom. Should I directly updated the dihedrallist to change the dihedrals inside LAMMPS? But as my understanding if I do not make change to lists of dihedral_atom, reneighboring will cause problem because it will update the dihedrallist according to dihedral_atom.

Thank you.


You cannot change the 2nd dim b/c the array is layed
out in contiguous memory, and changing it would wipe
out all the values in the array. What you would need to
do is know in advance the max # of dihedrals for any atom.
LAMMPS computes this by scanning a data file before
it reads the values and stores it. When LAMMPS needs
to add bonds later, there is a setting in the data file
for "extra bond per atom" that allows the user to augment
the 2nd dim on the bond arrays. If you added an "extra
angle per atom" and "extra dihedral per atom" you could
do the same for the angle and dihedral arrays. Then
never need to change it in the lib interface.


Hi, Steve,

I got it work. Thank you very much.

Yumeng Li