Unverified Commit eaf2ca9c authored by Axel Kohlmeyer's avatar Axel Kohlmeyer Committed by GitHub
Browse files

Merge pull request #1272 from lammps/utility-functions

Add library of utility functions to make LAMMPS code simpler and more consistent.

This is the first part of multiple pull requests that will add some more of these functions and will apply them to more of LAMMPS. 
parents a645278d d7c2ecad
Loading
Loading
Loading
Loading
+7 −12
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "neighbor.h"
#include "citeme.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;
using namespace FixConst;
@@ -221,12 +222,11 @@ void FixGPU::init()
  // hybrid cannot be used with force/neigh option

  if (_gpu_mode == GPU_NEIGH || _gpu_mode == GPU_HYB_NEIGH)
    if (force->pair_match("hybrid",1) != NULL ||
        force->pair_match("hybrid/overlay",1) != NULL)
    if (force->pair_match("^hybrid",0) != NULL)
      error->all(FLERR,"Cannot use pair hybrid with GPU neighbor list builds");

  if (_particle_split < 0)
    if (force->pair_match("hybrid",1) != NULL ||
        force->pair_match("hybrid/overlay",1) != NULL)
    if (force->pair_match("^hybrid",0) != NULL)
      error->all(FLERR,"GPU split param must be positive "
                 "for hybrid pair styles");

@@ -243,21 +243,16 @@ void FixGPU::init()

  // make sure fdotr virial is not accumulated multiple times

  if (force->pair_match("hybrid",1) != NULL) {
  if (force->pair_match("^hybrid",0) != NULL) {
    PairHybrid *hybrid = (PairHybrid *) force->pair;
    for (int i = 0; i < hybrid->nstyles; i++)
      if (strstr(hybrid->keywords[i],"/gpu")==NULL)
        force->pair->no_virial_fdotr_compute = 1;
  } else if (force->pair_match("hybrid/overlay",1) != NULL) {
    PairHybridOverlay *hybrid = (PairHybridOverlay *) force->pair;
    for (int i = 0; i < hybrid->nstyles; i++)
      if (strstr(hybrid->keywords[i],"/gpu")==NULL)
      if (!utils::strmatch(hybrid->keywords[i],"/gpu$"))
        force->pair->no_virial_fdotr_compute = 1;
  }

  // rRESPA support

  if (strstr(update->integrate_style,"respa"))
  if (utils::strmatch(update->integrate_style,"^respa"))
    _nlevels_respa = ((Respa *) update->integrate)->nlevels;
}

+8 −6
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@
#include "neigh_request.h"
#include "gpu_extra.h"
#include "domain.h"
#include "utils.h"


using namespace LAMMPS_NS;

@@ -374,10 +376,10 @@ void PairEAMAlloyGPU::read_file(char *filename)

  int n;
  if (me == 0) {
    fgets(line,MAXLINE,fptr);
    fgets(line,MAXLINE,fptr);
    fgets(line,MAXLINE,fptr);
    fgets(line,MAXLINE,fptr);
    utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
    n = strlen(line) + 1;
  }
  MPI_Bcast(&n,1,MPI_INT,0,world);
@@ -402,7 +404,7 @@ void PairEAMAlloyGPU::read_file(char *filename)
  delete [] words;

  if (me == 0) {
    fgets(line,MAXLINE,fptr);
    utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
    sscanf(line,"%d %lg %d %lg %lg",
           &file->nrho,&file->drho,&file->nr,&file->dr,&file->cut);
  }
@@ -422,7 +424,7 @@ void PairEAMAlloyGPU::read_file(char *filename)
  int i,j,tmp;
  for (i = 0; i < file->nelements; i++) {
    if (me == 0) {
      fgets(line,MAXLINE,fptr);
      utils::sfgets(FLERR,line,MAXLINE,fptr,filename,error);
      sscanf(line,"%d %lg",&tmp,&file->mass[i]);
    }
    MPI_Bcast(&file->mass[i],1,MPI_DOUBLE,0,world);
+5 −3
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include "update.h"
#include "memory.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;
using namespace FixConst;

@@ -121,12 +123,12 @@ void FixQEQComb::init()
  if (!atom->q_flag)
    error->all(FLERR,"Fix qeq/comb requires atom attribute q");

  comb = (PairComb *) force->pair_match("comb",1);
  comb3 = (PairComb3 *) force->pair_match("comb3",1);
  comb = (PairComb *) force->pair_match("^comb",0);
  comb3 = (PairComb3 *) force->pair_match("^comb3",0);
  if (comb == NULL && comb3 == NULL)
    error->all(FLERR,"Must use pair_style comb or comb3 with fix qeq/comb");

  if (strstr(update->integrate_style,"respa")) {
  if (utils::strmatch(update->integrate_style,"^respa")) {
    ilevel_respa = ((Respa *) update->integrate)->nlevels-1;
    if (respa_level >= 0) ilevel_respa = MIN(respa_level,ilevel_respa);
  }
+14 −13
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "neigh_list.h"
#include "memory.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;

@@ -561,10 +562,10 @@ void PairADP::read_file(char *filename)

  int n;
  if (me == 0) {
    fgets(line,MAXLINE,fp);
    fgets(line,MAXLINE,fp);
    fgets(line,MAXLINE,fp);
    fgets(line,MAXLINE,fp);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    n = strlen(line) + 1;
  }
  MPI_Bcast(&n,1,MPI_INT,0,world);
@@ -589,7 +590,7 @@ void PairADP::read_file(char *filename)
  delete [] words;

  if (me == 0) {
    fgets(line,MAXLINE,fp);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    sscanf(line,"%d %lg %d %lg %lg",
           &file->nrho,&file->drho,&file->nr,&file->dr,&file->cut);
  }
@@ -613,32 +614,32 @@ void PairADP::read_file(char *filename)
  int i,j,tmp;
  for (i = 0; i < file->nelements; i++) {
    if (me == 0) {
      fgets(line,MAXLINE,fp);
      utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
      sscanf(line,"%d %lg",&tmp,&file->mass[i]);
    }
    MPI_Bcast(&file->mass[i],1,MPI_DOUBLE,0,world);

    if (me == 0) grab(fp,file->nrho,&file->frho[i][1]);
    if (me == 0) grab(fp,filename,file->nrho,&file->frho[i][1]);
    MPI_Bcast(&file->frho[i][1],file->nrho,MPI_DOUBLE,0,world);
    if (me == 0) grab(fp,file->nr,&file->rhor[i][1]);
    if (me == 0) grab(fp,filename,file->nr,&file->rhor[i][1]);
    MPI_Bcast(&file->rhor[i][1],file->nr,MPI_DOUBLE,0,world);
  }

  for (i = 0; i < file->nelements; i++)
    for (j = 0; j <= i; j++) {
      if (me == 0) grab(fp,file->nr,&file->z2r[i][j][1]);
      if (me == 0) grab(fp,filename,file->nr,&file->z2r[i][j][1]);
      MPI_Bcast(&file->z2r[i][j][1],file->nr,MPI_DOUBLE,0,world);
    }

  for (i = 0; i < file->nelements; i++)
    for (j = 0; j <= i; j++) {
      if (me == 0) grab(fp,file->nr,&file->u2r[i][j][1]);
      if (me == 0) grab(fp,filename,file->nr,&file->u2r[i][j][1]);
      MPI_Bcast(&file->u2r[i][j][1],file->nr,MPI_DOUBLE,0,world);
    }

  for (i = 0; i < file->nelements; i++)
    for (j = 0; j <= i; j++) {
      if (me == 0) grab(fp,file->nr,&file->w2r[i][j][1]);
      if (me == 0) grab(fp,filename,file->nr,&file->w2r[i][j][1]);
      MPI_Bcast(&file->w2r[i][j][1],file->nr,MPI_DOUBLE,0,world);
    }

@@ -918,14 +919,14 @@ void PairADP::interpolate(int n, double delta, double *f, double **spline)
   only called by proc 0
------------------------------------------------------------------------- */

void PairADP::grab(FILE *fp, int n, double *list)
void PairADP::grab(FILE *fp, char *filename, int n, double *list)
{
  char *ptr;
  char line[MAXLINE];

  int i = 0;
  while (i < n) {
    fgets(line,MAXLINE,fp);
    utils::sfgets(FLERR,line,MAXLINE,fp,filename,error);
    ptr = strtok(line," \t\n\r\f");
    list[i++] = atof(ptr);
    while ((ptr = strtok(NULL," \t\n\r\f"))) list[i++] = atof(ptr);
+1 −1
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ class PairADP : public Pair {
  void allocate();
  void array2spline();
  void interpolate(int, double, double *, double **);
  void grab(FILE *, int, double *);
  void grab(FILE *, char *, int, double *);

  void read_file(char *);
  void file2array();
Loading