[lammps-users] FCM command

Dear LAMMPS Users,
I want to print force acting on a group of atoms in a direction into a file. I tried to play with fcm, compute, dump and variable commands. I was not able to do it. Does anyone know how to do it?
Thank you for your time.
Rutuparna

Rutuparna,

You could write a fix to do that. Use ‘group’ to identify which atoms you want, then use ‘fix id group myfix freq …’ to output the results to a file. Use a fix like fix_rdf.cpp as a step off point.

Pieter

Hi Rutupama.

Here is a hack which I used to use. It also dumps position and velocity data in addition to average force. (I think I was using this to get a wall velocity and wall force at one point). I’m not positive that it will work with the current LAMMPS release, but I would guess it will. Usage should be obvious… you need to provide a filename in the “fix” line in the input file.

–Craig
-------------------------------------------- fix_sumforce.cpp -----------------------
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
www.cs.sandia.gov/~sjplimp/lammps.html
Steve Plimpton, [email protected], Sandia National Laboratories

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.

Based on “fix_ave_force.cpp” and “fix_centro.cpp”
by cmaloney@…35… 8 May 06

------------------------------------------------------------------------- */

#include “mpi.h”
#include “string.h”
#include “stdlib.h”
#include “fix_sumforce.h”
#include “atom.h”
#include “update.h”
#include “respa.h”
#include “error.h”

/* ---------------------------------------------------------------------- */

FixSumforce::FixSumforce(int narg, char **arg) : Fix(narg, arg)
{
MPI_Comm_rank(world,&me);
char strTmp[1024];
sprintf(strTmp,“Illegal fix sumforce command… nargs=%d”,narg);
if (narg != 5) error->all(strTmp);
nevery=atoi(arg[3]);
line = new char[MAXLINE];

if (me==0) outfile = fopen(arg[4],“w”);
}

FixSumforce::~FixSumforce(){
if(me==0) fclose(outfile);
delete[] line;
}

/* ---------------------------------------------------------------------- */

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

/* ---------------------------------------------------------------------- */

void FixSumforce::init()
{
// ncount = total # of atoms in group

int *mask = atom->mask;
int nlocal = atom->nlocal;

int count = 0;
for (int i = 0; i < nlocal; i++)
if (mask[i] & groupbit) count++;
MPI_Allreduce(&count,&ncount,1,MPI_INT,MPI_SUM,world);
}

/* ---------------------------------------------------------------------- */

void FixSumforce::post_force(int vflag)
{
// sum forces on participating atoms
if (update->ntimestep % nevery !=0) return;
double **f = atom->f;
double **v = atom->v;
double **x = atom->x;
int *mask = atom->mask;
int nlocal = atom->nlocal;

double fsum[3],vsum[3],xsum[3];
fsum[0] = fsum[1] = fsum[2] = 0.0;
vsum[0] = vsum[1] = vsum[2] = 0.0;
xsum[0] = xsum[1] = xsum[2] = 0.0;

for (int i = 0; i < nlocal; i++){
if (mask[i] & groupbit) {
fsum[0] += f[i][0];
fsum[1] += f[i][1];
fsum[2] += f[i][2];
vsum[0] += v[i][0];
vsum[1] += v[i][1];
vsum[2] += v[i][2];
xsum[0] += x[i][0];
xsum[1] += x[i][1];
xsum[2] += x[i][2];
}
}

// average the force on participating atoms
// add in requested amount

double fsumall[3],vsumall[3],xsumall[3];
MPI_Allreduce(fsum,fsumall,3,MPI_DOUBLE,MPI_SUM,world);
MPI_Allreduce(vsum,vsumall,3,MPI_DOUBLE,MPI_SUM,world);
MPI_Allreduce(xsum,xsumall,3,MPI_DOUBLE,MPI_SUM,world);

fsumall[0] = fsumall[0]/ncount;
fsumall[1] = fsumall[1]/ncount;
fsumall[2] = fsumall[2]/ncount;
vsumall[0] = vsumall[0]/ncount;
vsumall[1] = vsumall[1]/ncount;
vsumall[2] = vsumall[2]/ncount;
xsumall[0] = xsumall[0]/ncount;
xsumall[1] = xsumall[1]/ncount;
xsumall[2] = xsumall[2]/ncount;

sprintf(line,"%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",
update->ntimestep,
fsumall[0],
fsumall[1],
fsumall[2],
vsumall[0],
vsumall[1],
vsumall[2],
xsumall[0],
xsumall[1],
xsumall[2]);

// If I am proc zero, write to output file
if(me==0){
fprintf(outfile,line);
fflush(outfile);
}
}

----------------------------------------------- fix_sumforce.h -----------------------------
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
www.cs.sandia.gov/~sjplimp/lammps.html
Steve Plimpton, [email protected], Sandia National Laboratories

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.
------------------------------------------------------------------------- */

#ifndef FIX_SUMFORCE_H
#define FIX_SUMFORCE_H
#define MAXLINE 1024

#include “fix.h”

class FixSumforce : public Fix {
public:
FixSumforce(int, char **);
~FixSumforce();
int setmask();
void init();
void post_force(int);

private:
FILE* outfile;
char* line;
int ncount;
int nevery;
int me;
};

#endif