Thanks Axel. I looked more closely into library.cpp and found “lammps_extract_compute”. According to the file:
void *lammps_extract_compute(void *ptr, char *id, int style, int type)
Where
id = compute ID
---------------------> I am assuming this is “pe” for potential energy
wrong. it is the ID of the compute you created. in your posted input example case it would be “peratom”. see the docs for the compute command.
style = 0 for global data, 1 for per-atom data, 2 for local data
----------------------> assuming “1” for per-atom data
correct.
type = 0 for scalar, 1 for vector, 2 for arr
----------------------------> assuming “1” for vector
correct.
Using this, I added the lammps_extract_compute line in the following block of code in the original simple.c example:
// extract force on single atom two different ways
if (lammps == 1) {
double **f = (double **) lammps_extract_atom(lmp,“f”);
printf(“Force on 1 atom via extract_atom: %g\n”,f[0][0]);
double *fx = (double *) lammps_extract_variable(lmp,“fx”,“all”);
printf(“Force on 1 atom via extract_variable: %g\n”,fx[0]);
double *ptr = (double ) lammps_extract_compute(lmp,(char ) “pe”,1,1);
**printf(“POTENTIAL ENERGY on 1 atom via extract_variable: %f~~~~~~~~~\n”,ptr[1]);
}
unfortunately, it is not as simple as this. for efficiency reasons, computations of the potential energy are not performed explicitly, but done as part of the force computation. but for that to be triggered, there has to be a “consumer” for the compute defined (e.g. a dump style or a fix), so that LAMMPS can determine that it needs to accumulate per-atom potential energy. so to access the data of a compute between runs, you need to do something like this:
if (lammps == 1) {
int i,j;
int *nlocal;
double *pe;
int *id = lammps_extract_atom(lmp,“id”);
lammps_command(lmp,“fix xxx all ave/atom 1 1 1 c_peratom”);
lammps_command(lmp,“run 0”);
nlocal = lammps_extract_global(lmp,“nlocal”);
pe = lammps_extract_compute(lmp,“peratom”,1,1);
for (i = 0; i < nprocs_lammps; ++i) {
MPI_Barrier(comm_lammps);
if (i == me) {
for (j = 0; j < *nlocal; ++j)
printf(“PE of atom %d: %g\n”,id[j],pe[j]);
}
}
lammps_command(lmp,“unfix xxx”);
}
please also note, that per-atom data is distributed across processors (hence the loops over nlocal and mpi ranks). only for a single MPI rank, do you have “nlocal == natoms”.
axel.