I add one function to library.cpp as below. I tried to create a ragged matrix and get the information about bonds like atoms for each bonds and types of bonds. But I got two problems.

First one is I got **copy right but not **data. The total number of items of **data is 123. The first 61 items in **data are good. But after that I got “58 -1961300264” repeated for the rest of the items. I have no idea what happened.

The second problem is to free **copy. I first followed other functions in library.cpp and used lmp->memory->destroy(copy). But somehow it has influence on the transfer of **data back to the main C code which means when printing **data in main code it is full of negative numbers or very big numbers. But if I printed out the **copy and **data in the function itself, at least half of items are right as I stated in the first problem. I also tried



Still the same problem. The reason why I think the transfer problem may result from lmp->memory->destroy(copy) is because I got the same **data printed from the main code as printed from the function when I accidently delete lmp->memory->destroy(copy).

/* ----------------------------------------------------------------------

gather the bonds across all processors

type=1 bond_type[m][num_bond[m]]

type=2 bond_atom[m][num_bond[m]]

------------------------------------------------------------------------- */

void lammps_gather_ragged_bonds(void *ptr,int type,int *num, int *data, FILE fpp)


LAMMPS *lmp = (LAMMPS *) ptr;

int natoms = static_cast (lmp->atom->natoms);

int nbonds = static_cast (lmp->atom->nbonds);

int i,j,offset;

int **tptr = lmp->atom->bond_type;

int **aptr = lmp->atom->bond_atom;

int **array;

if (type==1) {

array = (int **)tptr;

}else {

array = (int **)aptr;


int **copy,*numbond;


for (i = 0; i < natoms; i++)

{for (j=0;j<num[i];j++)

copy[i][j] = 0;


int *tag = lmp->atom->tag;

int nlocal = lmp->atom->nlocal;

for (i = 0; i < nlocal; i++) {

offset = tag[i]-1;

for (j = 0; j < num[offset]; j++){

copy[offset][j] = array[i][j];




fprintf(fpp,“hahah: %d \n”,nbonds);

for (i = 0; i < natoms; i++) {

for (j = 0; j < num[i]; j++) {

fprintf(fpp,"%d %d \n",data[i][j],copy[i][j]);






I will really appreciated if you can help me with this.




This will not work since copy and data are 2d arrays. I think
you want &copy[0][0] and &data[0][0] to point at the contiguous
values in the 2d arrays since that is what the MPI function works on.