# How to add the force computation

Dear Lammps user,

Recently, I added an compute command, and it needs recompute the forces of each atom.
What I did is shown here

int eflag = 0, vflag = 4;

int n_pre_force = modify->n_pre_force;
int n_post_force = modify->n_post_force;

// set the flag true
calling_at_end =1;

// force computations
force_clear();

if (n_pre_force) modify->pre_force(vflag);

if (force->pair) force->pair->compute(eflag,vflag);

if (atom->molecular)
{
if (force->bond) force->bond->compute(eflag,vflag);
if (force->angle) force->angle->compute(eflag,vflag);
if (force->dihedral) force->dihedral->compute(eflag,vflag);
if (force->improper) force->improper->compute(eflag,vflag);
}

if (force->kspace) force->kspace->compute(eflag,vflag);

// reverse communication of forces
if (force->newton) comm->reverse_comm(); // for the total force f

if (n_post_force) modify->post_force(vflag);

calling_at_end = 0; // set the flag flase

But the efficiency is rather low. It seems that every CPU core has calculated the forces between all the atoms.
So how to calculate the forces of local atoms in each CPU core ? That would be much more efficient.
Thank you very much.

Dear Lammps user,

Recently, I added an compute command, and it needs recompute the forces
of each atom.
What I did is shown here

int eflag = 0, vflag = 4;

int n_pre_force = modify->n_pre_force;
int n_post_force = modify->n_post_force;

// set the flag true
calling_at_end =1;

// force computations
force_clear();

if (n_pre_force) modify->pre_force(vflag);

if (force->pair) force->pair->compute(eflag,vflag);

if (atom->molecular)
{
if (force->bond) force->bond->compute(eflag,vflag);
if (force->angle) force->angle->compute(eflag,vflag);
if (force->dihedral) force->dihedral->compute(eflag,vflag);
if (force->improper) force->improper->compute(eflag,vflag);
}

if (force->kspace) force->kspace->compute(eflag,vflag);

// reverse communication of forces
if (force->newton) comm->reverse_comm(); // for the total force
f

if (n_post_force) modify->post_force(vflag);

calling_at_end = 0; // set the flag flase

But the efficiency is rather low. It seems that every CPU core has
calculated the forces between all the atoms.

what makes you think that?

you just repeat the entire force calculation, which is the by far most
time consuming step of a simple MD.

So how to calculate the forces of local atoms in each CPU core ? That

unless you doing something that isn't kosher, you *do* run in parallel.

what is more of a concern is that you "destroy" all current force
information which may keep the regular time integration for working
correctly.

axel

Dear Axel

I won’t destroy the force calculation. I backup them before my calculation and will recover them after that.
The MD simulation is running in parallel. But I just donot know how to calculate the force in parallel.

Dear Axel

I won't destroy the force calculation. I backup them before my
calculation and will recover them after that.
The MD simulation is running in parallel. But I just donot know how to
calculate the force in parallel.

you do. please provide evidence to the contrary, if you disagree.

axel.

Dear Axel,

I donot know. Just found the calculation is really slow.Even add the code in compute_stress_atom, it’s still in parallel?

*you* are writing the code. not me. you need to know what your code
does and what not. i can't read minds and i don't have a crystal ball.
how should i know.

perhaps you should spend a little time learning how to profile an
application. it doesn't take too much effort, but can be very
enlightening.

axel.