help with library.cpp

Hi,

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.

Best,
Yumeng

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)
     numptr[m]=0;
   }

   if(nn>ndihedrals)
   {
    lmp->atom->ndihedrals=nn;
lmp->memory->grow(lmp->atom->dihedral_atom1,nn,maxd,"atom:dihedral_atom1");
lmp->memory->grow(lmp->atom->dihedral_atom2,nn,maxd,"atom:dihedral_atom2");
lmp->memory->grow(lmp->atom->dihedral_atom3,nn,maxd,"atom:dihedral_atom3");
lmp->memory->grow(lmp->atom->dihedral_atom4,nn,maxd,"atom:dihedral_atom4");
lmp->memory->grow(lmp->atom->dihedral_type,nn,maxd,"atom:dihedral_type");
    }
   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++){
         atom1=dptr[i][0];
         atom2=dptr[i][1];
         atom3=dptr[i][2];
         atom4=dptr[i][3];
         itype=dptr[i][4];
         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;

          numptr[m]++;
          }

         }

}

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.

Steve

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.

Best,
Yumeng

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.

Steve

Hi, Steve,

I got it work. Thank you very much.

Best,
Yumeng Li