Segementation fault ,addres not mapped

LAMMPS_NS::FixArea::post_force (this=0x555555e1ce10) at …/fix_area.cpp:97
97 atml_tmp[i][molatom[k]] = k;
(gdb) (gdb) print molatom[k]
Undefined command: “”. Try “help”.
(gdb) print i
$1 = 1
(gdb) print molatom[k]
Cannot access memory at address 0x0
(gdb) backtrace
#0 LAMMPS_NS::FixArea::post_force (this=0x555555e1ce10) at …/fix_area.cpp:97
#1 0x0000555555699c56 in LAMMPS_NS::Modify::post_force (vflag=, this=)
at …/modify.cpp:461
#2 LAMMPS_NS::Modify::post_force (this=0x555555d62e10, vflag=0) at …/modify.cpp:454
#3 0x00005555557c4484 in LAMMPS_NS::Verlet::run (this=0x555555d5c9d0, n=10000) at …/verlet.cpp:347
#4 0x000055555574be58 in LAMMPS_NS::Run::command (this=0x555555d6b3a0, narg=, arg=0x555555fd0c40)
at …/run.cpp:176
#5 0x0000555555644d1c in LAMMPS_NS::Input::execute_command (this=0x555555d4d510) at …/input.cpp:868
#6 0x00005555556456ee in LAMMPS_NS::Input::file (this=0x555555d4d510) at …/input.cpp:313
#7 0x0000555555635fe9 in main (argc=, argv=) at …/main.cpp:77
(gdb) info locals
k = 0
i = 1
fx =
fy =
molecule = 0x555555dccd60
id_mol = 1
x = 0x555555dacd50
f = 0x7ffff78b0010
molatom = 0x0
image = 0x555555d9cd40
atml_tmp = 0x555555e214c0
(gdb)

I am encountering a segmentation fault in my simulation

If anyone can help with this

Hi @Deepak_Pradhan,

If you want relevant help, you should provide the LAMMPS version you’re using as well as some context on what you are trying to do with lammps, input file and potential modifications you implemented in the code. I’ve never heard of a FixArea command such as the one I can see in your gdb log. Is it from your research group or your own? That’s where the issue seems to come from. So people from the forum will definitely need some more context here.

/* ----------------------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   http://lammps.sandia.gov, Sandia National Laboratories
   Steve Plimpton, [email protected]

   Copyright (2003) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the GNU General Public License.

   See the README file in the top-level LAMMPS directory. **/


#include "fix_area.h"
#include <mpi.h>
#include <cstring>
#include <cstdlib>
#include "atom.h"
#include "atom_masks.h"
#include "comm.h"
#include "update.h"
#include "modify.h"
#include "domain.h"
#include "region.h"
#include "respa.h"
#include "input.h"
#include "variable.h"
#include "memory.h"
#include "error.h"
#include "force.h"
#include "atom_vec.h"
#include "molecule.h"


using namespace LAMMPS_NS;
using namespace FixConst;


FixArea::FixArea(LAMMPS *lmp, int narg, char **arg) :
  Fix(lmp, narg, arg),atml(nullptr),area(nullptr),potarea(nullptr)

{
  if (strcmp(style,"area") != 0 && narg < 5)
    error->all(FLERR,"Illegal fix dumbbell command: not enough args");
  A0 = utils::numeric(FLERR,arg[3],false,lmp);
  Xi =utils::numeric(FLERR,arg[4],false,lmp);
  n =utils::numeric(FLERR,arg[5],false,lmp); //number of atoms per molecule 

 memory->create(atml, n_mol +1, n , "FixArea:atml");
 memory->create(area,n_mol+1,"FixArea:area");
 memory->create(potarea,n_mol+1,"FixArea:potarea");
 


}





int FixArea::setmask()
{
  int mask = 0;
  mask |= POST_FORCE;
  
  return mask;
}

void FixArea::post_force(int /*vflag*/)
{
  
  double fx, fy ;
  
  
  tagint *molecule = atom->molecule;
  int id_mol = 0 ;
  
  double **x = atom->x;
  double **f = atom->f;
  int *molatom = atom->molatom;
  int molecular = atom->molecular;
  imageint *image = atom->image;
  int jatom;

  for (int i = 0; i < atom->nlocal; i++) {
    // molecule id starts from 1. max(id_mol) equals to the number of molecules in the system
    id_mol = std::max(id_mol, molecule[i]);

  }
  MPI_Allreduce(&id_mol, &n_mol, 1, MPI_LMP_TAGINT, MPI_MAX, world);
  
  int  **atml_tmp;
  memory->create(atml_tmp, n_mol +1, n , "FixArea:atml_tmp");
  memset(&atml_tmp[0][0], 0, sizeof(int) * n_mol +1 * n+1);
  
  
 if(molecular == 1){
  for (int i = 1; i < n_mol+1; ++i) {
    for (int k = 0; k < atom->nlocal; ++k) {
        if (molecule[k] == i) {
            jatom = molatom[k];
            atml_tmp[i][jatom] = k;
             
        }
    }
    

    
  
  }

 }
 else{
  error->all(FLERR,"not a molecular template system ");

 }
  

  memory->destroy(atml);
  memory->create(atml, n_mol +1, n +1, "FixArea:atml");
   


  MPI_Allreduce(atml_tmp,atml, (n_mol + 1)*(n+1), MPI_INT, MPI_SUM, world);

  memory->destroy(atml_tmp);
 
  memory->destroy(area);
  memory->create(area,n_mol+1,"FixArea:area");
  

  for(int i = 1 ; i< n_mol + 1;++i){
    double unwrap1[3];
    double unwrap2[3];
    double unwrap3[3];
    double unwrap4[3];
    for(int jj=1 ; jj< n+1; ++jj){
        domain->unmap(x[atml[i][jj]], image[atml[i][jj]], unwrap1);
        domain->unmap(x[atml[i][jj+1]], image[atml[i][jj+1]], unwrap2);

     
       area[i] += unwrap1[0]*unwrap2[1] - unwrap2[0] * unwrap1[0];
    
    }
    domain->unmap(x[atml[i][n]], image[atml[i][n]], unwrap3);
    domain->unmap(x[atml[i][1]], image[atml[i][1]], unwrap4);

    area[i] += unwrap3[0]*unwrap4[1]-unwrap3[1] * unwrap4[0];
  

    fprintf(screen, "area = %f \n", area[i]);

  }  
  memory->destroy(potarea);
  memory->create(potarea,n_mol+1,"FixArea:potarea");
  
   
  for (int i = 1 ; i< n_mol+1; ++i){
    potarea[i]= 0.5 * Xi *(1-(area[i]/A0))*(1-(area[i]/A0));
  }
  
  for(int i=1 ;i < n_mol+1; i++){
    for(int j=1 ; j< n+1;++j){
      if(j == 1){
        
        fx= Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][j+1]][1]-x[atml[i][n]][1]);
        fy= Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][n]][0]-x[atml[i][j+1]][0]);

           
        f[atml[i][j]][0] += fx;  // unit vector rotated CW
        f[atml[i][j]][1] += fy ;
      }
      if(j == n){
        
        fx= -Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][1]][1]-x[atml[i][j-1]][1]);
        fy=  -Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][j-1]][0]-x[atml[i][1]][0]);

        f[atml[i][j]][0] += fx;  
        f[atml[i][j]][1] += fy ;


      }
      else{
        
        fx= -Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][j+1]][1]-x[atml[i][j-1]][1]);
        fy= -Xi *A0 *(1-(area[molecule[i]]/A0))*(x[atml[i][j-1]][0]-x[atml[i][j +1]][0]);

        f[atml[i][j]][0] += fx;  
        f[atml[i][j]][1] += fy ;

      }
    }
    
        



 }
}
 

I am writing a custom fix whih calculate the potential energy of a ring molecule associated with its area ,and the corresponding force due to it ,

while compilation i get the above error .The above is my fix_area.cpp file

Please have a look at this thread for posting guidelines which will make your code readable. It is nearly impossible to help you as is.

As I suspected the error comes from code you added to LAMMPS and segfault indicates that this comes from reading of data that has not been allocated. In other libraries using the molatom properties some verification is made to insure that the atom style actually sets such properties (look for lines like atom->molecular == Atom::MOLECULAR). molatom is set to nullptr by default, I suspect that checks in your code are insufficient.

Also note that segfault is a run-time error, not a compile error. Knowing what settings your simulation/test-case use would be of great help to give more meaningful insights.

Hi Deepak,

It would have helped if you linked your previous thread, so that I knew the context of writing an area-dependent potential. The first obvious bug I see in your code is:

Please double-check that you truly intended sizeof(int), because I suspect you really want sizeof(tagint). More importantly, you are missing the brackets around n_mol + 1 and n + 1.

Both of these mistakes mean that your atml_tmp array has unzeroed memory in its last few entries. These previous in-memory values then “contaminate” the MPI sum that collects the atml_tmp arrays into the final atml array. Since each array entry is itself an array index, the “contamination” results in your code later trying to access another array out-of-bounds, leading to a segfault. (You are lucky that this triggers a clear error – if the code silently accesses the wrong index instead, you will get a simulation that runs with zero crashes but completely wrong forces.)

1 Like

Using Valgrind or AddressSanitizer can be very helpful with debugging segfaults.