compute_pe_atom called used in WriteDump

Hello,
I am using version 12Dec2018.

My group has written code that finds local minima. It then dumps the atomic positions of those minima by using
char** dumparg = new char*[8];

dumparg[0] = (char *) “all”;

dumparg[1] = (char *) “atom”;

dumparg[2] = (char *) “TLS.pos”;

dumparg[3] = (char *) “modify”;

dumparg[4] = (char *) “append”;

dumparg[5] = (char *) “yes”;

dumparg[6] = (char *) “scale”;

dumparg[7] = (char *) “no”;

WriteDump* pDump = new WriteDump(lmp);

update->reset_timestep(0);

CopyAtoms(atom->x,pTLS1);

CopyLatToBox(lTLS1);

UpdateMapping();

pDump->command(8,dumparg);

I was hoping to add the energy of each individual atom to this dump output by making the following changes

char** dumparg = new char*[10];

dumparg[0] = (char *) “all”;

dumparg[1] = (char *) “custom”;

dumparg[2] = (char *) “TLS.pos”;

dumparg[3] = (char *) “id”;

dumparg[4] = (char *) “mass”;

dumparg[5] = (char *) “type”;

dumparg[6] = (char *) “x”;

dumparg[7] = (char *) “y”;

dumparg[8] = (char *) “z”;

dumparg[9] = (char *) “c_peratom”;

WriteDump* pDump = new WriteDump(lmp);

update->reset_timestep(0);

CopyAtoms(atom->x,pTLS1);

CopyLatToBox(lTLS1);

UpdateMapping();

#define INVOKED_PERATOM 8

char *pearg = new char[5];

pearg[0] = (char *) “peratom”;

pearg[1] = (char *) “all”;

pearg[2] = (char *) “pe/atom”;

modify->add_compute(3,pearg);

delete [] pearg;

int ipe = modify->find_compute(“peratom”);

Compute *c_pe;

c_pe = modify->compute[ipe];

update->integrate->run(0);

c_pe->invoked_peratom = update->ntimestep;

update->eflag_atom = update->ntimestep;

c_pe->compute_peratom();

c_pe->invoked_flag |= INVOKED_PERATOM;

pDump->command(8,dumparg);

I get the following error:
Caught signal 11 (Segmentation fault: address not mapped to object at address (nil))

This seems to occur in compute_pe_atom.cpp at the following line

if (pairflag && force->pair) {

double *eatom = force->pair->eatom;

for (i = 0; i < npair; i++) energy[i] += eatom[i];

}

Thank you for the help,
Alec

Hello,

[…]

c_pe->invoked_peratom = update->ntimestep;

update->eflag_atom = update->ntimestep;

you get what you deserve because you are messing with the internal state of classes where you essentially lie about what has been done, which indeed has not been done (e.g. allocate data for storing per-atom energies).
Without these you would get the - correct - error message that the compute is not up-to-date. This is because this kind of compute is actually not doing any computation, but rather providing access to data that is collected during a previous run. However, to make LAMMPS to collect this information, you need to consume a compute (which will then automatically update the flags that you are - incorrectly - overriding). a simple way to define a “consumer” would be to define a fix ave/atom 1 1 1 c_peratom
routing a compute through an averaging fix is often a good idea, since the fix will act as a cache and keep its data, even if you otherwise make changes, that would invalidate the state of the compute.

Axel.

[…]