Skip to content
Snippets Groups Projects
compute_pair.cpp 3.65 KiB
Newer Older
sjplimp's avatar
sjplimp committed
/* ----------------------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   http://lammps.sandia.gov, Sandia National Laboratories
   Steve Plimpton, sjplimp@sandia.gov

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

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

sjplimp's avatar
 
sjplimp committed
#include <mpi.h>
sjplimp's avatar
sjplimp committed
#include "compute_pair.h"
#include "update.h"
#include "force.h"
#include "pair.h"
#include "error.h"

using namespace LAMMPS_NS;

sjplimp's avatar
 
sjplimp committed
enum{EPAIR,EVDWL,ECOUL};

sjplimp's avatar
sjplimp committed
/* ---------------------------------------------------------------------- */

sjplimp's avatar
 
sjplimp committed
ComputePair::ComputePair(LAMMPS *lmp, int narg, char **arg) :
sjplimp's avatar
sjplimp committed
  Compute(lmp, narg, arg),
  pstyle(NULL), pair(NULL), one(NULL)
sjplimp's avatar
sjplimp committed
{
  if (narg < 4) error->all(FLERR,"Illegal compute pair command");
sjplimp's avatar
sjplimp committed

sjplimp's avatar
 
sjplimp committed
  scalar_flag = 1;
  extscalar = 1;
  peflag = 1;
  timeflag = 1;

sjplimp's avatar
 
sjplimp committed
  int n = strlen(arg[3]) + 1;
sjplimp's avatar
 
sjplimp committed
  if (lmp->suffix) n += strlen(lmp->suffix) + 1;
sjplimp's avatar
 
sjplimp committed
  pstyle = new char[n];
  strcpy(pstyle,arg[3]);

Axel Kohlmeyer's avatar
Axel Kohlmeyer committed
  evalue = EPAIR;

  if (narg > iarg) {
    if (isdigit(arg[iarg][0])) {
      nsub = force->inumeric(FLERR,arg[iarg]);
      ++iarg;
      if (nsub <= 0)
        error->all(FLERR,"Illegal compute pair command");
    }
  }
  if  (narg > iarg) {
    if (strcmp(arg[iarg],"epair") == 0) evalue = EPAIR;
    else if (strcmp(arg[iarg],"evdwl") == 0) evalue = EVDWL;
    else if (strcmp(arg[iarg],"ecoul") == 0) evalue = ECOUL;
    else error->all(FLERR, "Illegal compute pair command");
    ++iarg;
sjplimp's avatar
 
sjplimp committed

sjplimp's avatar
 
sjplimp committed
  // check if pair style with and without suffix exists

  pair = force->pair_match(pstyle,1,nsub);
sjplimp's avatar
 
sjplimp committed
  if (!pair && lmp->suffix) {
    strcat(pstyle,"/");
    strcat(pstyle,lmp->suffix);
    pair = force->pair_match(pstyle,1,nsub);
sjplimp's avatar
 
sjplimp committed
  }

sjplimp's avatar
 
sjplimp committed
  if (!pair)
sjplimp's avatar
 
sjplimp committed
    error->all(FLERR,"Unrecognized pair style in compute pair command");
sjplimp's avatar
sjplimp committed
  npair = pair->nextra;

sjplimp's avatar
 
sjplimp committed
  if (npair) {
    vector_flag = 1;
    size_vector = npair;
    extvector = 1;
    one = new double[npair];
    vector = new double[npair];
  } else one = vector = NULL;
sjplimp's avatar
sjplimp committed
}

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

ComputePair::~ComputePair()
{
sjplimp's avatar
 
sjplimp committed
  delete [] pstyle;
sjplimp's avatar
sjplimp committed
  delete [] one;
  delete [] vector;
}

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

sjplimp's avatar
 
sjplimp committed
void ComputePair::init()
{
  // recheck for pair style in case it has been deleted

  pair = force->pair_match(pstyle,1,nsub);
sjplimp's avatar
 
sjplimp committed
  if (!pair)
sjplimp's avatar
 
sjplimp committed
    error->all(FLERR,"Unrecognized pair style in compute pair command");
sjplimp's avatar
 
sjplimp committed
}

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

sjplimp's avatar
 
sjplimp committed
double ComputePair::compute_scalar()
{
  invoked_scalar = update->ntimestep;
  if (update->eflag_global != invoked_scalar)
sjplimp's avatar
 
sjplimp committed
    error->all(FLERR,"Energy was not tallied on needed timestep");
sjplimp's avatar
 
sjplimp committed

  double eng;
  if (evalue == EPAIR) eng = pair->eng_vdwl + pair->eng_coul;
  else if (evalue == EVDWL) eng = pair->eng_vdwl;
  else if (evalue == ECOUL) eng = pair->eng_coul;

  MPI_Allreduce(&eng,&scalar,1,MPI_DOUBLE,MPI_SUM,world);
  return scalar;
}

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

sjplimp's avatar
sjplimp committed
void ComputePair::compute_vector()
{
  invoked_vector = update->ntimestep;
  if (update->eflag_global != invoked_vector)
sjplimp's avatar
 
sjplimp committed
    error->all(FLERR,"Energy was not tallied on needed timestep");
sjplimp's avatar
sjplimp committed

  for (int i = 0; i < npair; i++)
    one[i] = pair->pvector[i];
  MPI_Allreduce(one,vector,npair,MPI_DOUBLE,MPI_SUM,world);
}