Python gather_bonds() error

Using the unit test lammps/unittest/python/python-gather-scatter.py PythonGather.testGatherBond_newton_off() as an example, if I change the following lines in
the data.fourmol file:

5c5
< 24 bonds
---
> 2 bonds
117,138d116
< 3 2 3 4
< 4 2 3 5
< 5 1 3 6
< 6 3 6 8
< 7 4 6 7
< 8 5 8 9
< 9 3 8 10
< 10 2 10 11
< 11 1 10 12
< 12 1 10 16
< 13 2 12 13
< 14 2 12 14
< 15 2 12 15
< 16 4 16 17
< 17 5 18 19
< 18 5 18 20
< 19 5 21 22
< 20 5 21 23
< 21 5 24 25
< 22 5 24 26
< 23 5 27 28
< 24 5 27 29 

To remove all but the first two bonds in the system, and then run the unit test:

from lammps import lammps

lmp=lammps(cmdargs=['-nocite',
                    '-log','none',
                    '-echo','screen'])

lmp.command("newton off off")
lmp.file("in.fourmol")
lmp.command("run 0 post no")
nbonds, bonds = lmp.gather_bonds()

It works when running on one processor, however, when using more than one processor I get segmentation faults. I believe the problem happens when there are bonds on some processors, but none on other processors.

1 Like

@blake Thanks for your report. Please try to apply the following patch to address this issue.

  commit 7c98d4dba3b56407ff2209e3249c32df9092a36c (HEAD -> collected-small-fixes)
  Author: Axel Kohlmeyer <[email protected]>
  Date:   Wed May 17 10:26:33 2023 -0400
  
      avoid null pointer dereferences by allocating a buffer for at least 1 item
  
  diff --git a/src/library.cpp b/src/library.cpp
  index cc4e748f57..33f49a4e53 100644
  --- a/src/library.cpp
  +++ b/src/library.cpp
  @@ -3159,7 +3159,8 @@ void lammps_gather_bonds(void *handle, void *data)
       }
   
       tagint **bonds;
  -    lmp->memory->create(bonds, localbonds, 3, "library:gather_bonds:localbonds");
  +    // add 1 to localbonds, so "bonds" does not become a NULL pointer
  +    lmp->memory->create(bonds, localbonds+1, 3, "library:gather_bonds:localbonds");
       lmp->atom->avec->pack_bond(bonds);
       MPI_Allgatherv(&bonds[0][0], 3*localbonds, MPI_LMP_TAGINT, data, bufsizes,
                      bufoffsets, MPI_LMP_TAGINT, lmp->world);
  @@ -3269,7 +3270,8 @@ void lammps_gather_angles(void *handle, void *data)
       }
   
       tagint **angles;
  -    lmp->memory->create(angles, localangles, 4, "library:gather_angles:localangles");
  +    // add 1 to localangles, so "angles" does not become a NULL pointer
  +    lmp->memory->create(angles, localangles+1, 4, "library:gather_angles:localangles");
       lmp->atom->avec->pack_angle(angles);
       MPI_Allgatherv(&angles[0][0], 4*localangles, MPI_LMP_TAGINT, data, bufsizes,
                      bufoffsets, MPI_LMP_TAGINT, lmp->world);
  @@ -3380,7 +3382,8 @@ void lammps_gather_dihedrals(void *handle, void *data)
       }
   
       tagint **dihedrals;
  -    lmp->memory->create(dihedrals, localdihedrals, 5, "library:gather_dihedrals:localdihedrals");
  +    // add 1 to localdihedrals, so "dihedrals" does not become a NULL pointer
  +    lmp->memory->create(dihedrals, localdihedrals+1, 5, "library:gather_dihedrals:localdihedrals");
       lmp->atom->avec->pack_dihedral(dihedrals);
       MPI_Allgatherv(&dihedrals[0][0], 5*localdihedrals, MPI_LMP_TAGINT, data, bufsizes,
                      bufoffsets, MPI_LMP_TAGINT, lmp->world);
  @@ -3491,7 +3494,8 @@ void lammps_gather_impropers(void *handle, void *data)
       }
   
       tagint **impropers;
  -    lmp->memory->create(impropers, localimpropers, 5, "library:gather_impropers:localimpropers");
  +    // add 1 to localimpropers, so "impropers" does not become a NULL pointer
  +    lmp->memory->create(impropers, localimpropers+1, 5, "library:gather_impropers:localimpropers");
       lmp->atom->avec->pack_improper(impropers);
       MPI_Allgatherv(&impropers[0][0], 5*localimpropers, MPI_LMP_TAGINT, data, bufsizes,
                      bufoffsets, MPI_LMP_TAGINT, lmp->world);

Works perfectly now, cheers.