Unverified Commit 12b379df authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

improve MEAM file detection in pair_coeff command, get rid of ERRFMT()

parent a7e2cc33
Loading
Loading
Loading
Loading
+54 −18
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@
#include <mpi.h>
#include <mpi.h>
#include <cstdlib>
#include <cstdlib>
#include <cstring>
#include <cstring>
#include <string>
#include "meam.h"
#include "meam.h"
#include "atom.h"
#include "atom.h"
#include "force.h"
#include "force.h"
@@ -29,11 +30,11 @@
#include "memory.h"
#include "memory.h"
#include "error.h"
#include "error.h"
#include "utils.h"
#include "utils.h"
#include "fmt/format.h"


using namespace LAMMPS_NS;
using namespace LAMMPS_NS;


#define MAXLINE 1024
#define MAXLINE 1024
#define ERRFMT(errfn,format,...) do { char _strbuf[128]; snprintf(_strbuf,sizeof(_strbuf),format,__VA_ARGS__); errfn(FLERR,_strbuf); } while (0)


static const int nkeywords = 22;
static const int nkeywords = 22;
static const char *keywords[] = {
static const char *keywords[] = {
@@ -148,9 +149,8 @@ void PairMEAMC::compute(int eflag, int vflag)


  meam_inst->meam_dens_final(nlocal,eflag_either,eflag_global,eflag_atom,
  meam_inst->meam_dens_final(nlocal,eflag_either,eflag_global,eflag_atom,
                   &eng_vdwl,eatom,ntype,type,map,scale,errorflag);
                   &eng_vdwl,eatom,ntype,type,map,scale,errorflag);
  if (errorflag) {
  if (errorflag)
    ERRFMT(error->one,"MEAM library error %d",errorflag);
    error->one(FLERR,fmt::format("MEAM library error {}",errorflag));
  }


  comm->forward_comm_pair(this);
  comm->forward_comm_pair(this);


@@ -216,7 +216,35 @@ void PairMEAMC::coeff(int narg, char **arg)
  if (strcmp(arg[0],"*") != 0 || strcmp(arg[1],"*") != 0)
  if (strcmp(arg[0],"*") != 0 || strcmp(arg[1],"*") != 0)
    error->all(FLERR,"Incorrect args for pair coefficients");
    error->all(FLERR,"Incorrect args for pair coefficients");


  // read MEAM element names between 2 filenames
  // check for presence of first meam file

  std::string lib_file = utils::get_potential_file_path(arg[2]);
  if (lib_file.empty())
    error->all(FLERR,fmt::format("Cannot open MEAM library file {}",lib_file));

  // find meam parameter file in arguments:
  // first word that is a file or "NULL" after the MEAM library file
  // we need to extract at least one element, so start from index 4

  int paridx=-1;
  std::string par_file;
  for (int i = 4; i < narg; ++i) {
    if (strcmp(arg[i],"NULL") == 0) {
      par_file = "NULL";
      paridx = i;
      break;
    }
    par_file = utils::get_potential_file_path(arg[i]);
    if (!par_file.empty()) {
      paridx=i;
      break;
    }
  }
  if (paridx < 0) error->all(FLERR,"No MEAM parameter file in pair coefficients");
  if ((narg - paridx - 1) != atom->ntypes)
    error->all(FLERR,"Incorrect args for pair coefficients");

  // MEAM element names between 2 filenames
  // nelements = # of MEAM elements
  // nelements = # of MEAM elements
  // elements = list of unique element names
  // elements = list of unique element names


@@ -225,11 +253,13 @@ void PairMEAMC::coeff(int narg, char **arg)
    delete [] elements;
    delete [] elements;
    delete [] mass;
    delete [] mass;
  }
  }
  nelements = narg - 4 - atom->ntypes;

  nelements = paridx - 3;
  if (nelements < 1) error->all(FLERR,"Incorrect args for pair coefficients");
  if (nelements < 1) error->all(FLERR,"Incorrect args for pair coefficients");
  if (nelements > maxelt)
  if (nelements > maxelt)
    ERRFMT(error->all, "Too many elements extracted from MEAM library (current limit: %d)."
    error->all(FLERR,fmt::format("Too many elements extracted from MEAM "
                       " Increase 'maxelt' in meam.h and recompile.", maxelt);
                                 "library (current limit: {}). Increase "
                                 "'maxelt' in meam.h and recompile.", maxelt));
  elements = new char*[nelements];
  elements = new char*[nelements];
  mass = new double[nelements];
  mass = new double[nelements];


@@ -243,7 +273,7 @@ void PairMEAMC::coeff(int narg, char **arg)
  // pass all parameters to MEAM package
  // pass all parameters to MEAM package
  // tell MEAM package that setup is done
  // tell MEAM package that setup is done


  read_files(arg[2],arg[2+nelements+1]);
  read_files(lib_file,par_file);
  meam_inst->meam_setup_done(&cutmax);
  meam_inst->meam_setup_done(&cutmax);


  // read args that map atom types to MEAM elements
  // read args that map atom types to MEAM elements
@@ -327,15 +357,17 @@ double PairMEAMC::init_one(int i, int j)


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


void PairMEAMC::read_files(char *globalfile, char *userfile)
void PairMEAMC::read_files(const std::string &globalfile,
                           const std::string &userfile)
{
{
  // open global meamf file on proc 0
  // open global meamf file on proc 0


  FILE *fp;
  FILE *fp;
  if (comm->me == 0) {
  if (comm->me == 0) {
    fp = force->open_potential(globalfile);
    fp = force->open_potential(globalfile.c_str());
    if (fp == NULL)
    if (fp == NULL)
      ERRFMT(error->one, "Cannot open MEAM potential file %s", globalfile);
      error->one(FLERR,fmt::format("Cannot open MEAM potential file {}",
                                   globalfile));
  }
  }


  // allocate parameter arrays
  // allocate parameter arrays
@@ -425,7 +457,8 @@ void PairMEAMC::read_files(char *globalfile, char *userfile)
      // map lat string to an integer
      // map lat string to an integer


      if (!MEAM::str_to_lat(words[1], true, lat[index]))
      if (!MEAM::str_to_lat(words[1], true, lat[index]))
        ERRFMT(error->one, "Unrecognized lattice type in MEAM library file: %s", words[1]);
        error->one(FLERR,fmt::format("Unrecognized lattice type in MEAM "
                                     "library file: {}", words[1]));


      // store parameters
      // store parameters


@@ -525,14 +558,15 @@ void PairMEAMC::read_files(char *globalfile, char *userfile)


  // done if user param file is NULL
  // done if user param file is NULL


  if (strcmp(userfile,"NULL") == 0) return;
  if (userfile == "NULL") return;


  // open user param file on proc 0
  // open user param file on proc 0


  if (comm->me == 0) {
  if (comm->me == 0) {
    fp = force->open_potential(userfile);
    fp = force->open_potential(userfile.c_str());
    if (fp == NULL)
    if (fp == NULL)
      ERRFMT(error->one, "Cannot open MEAM potential file %s", userfile);
      error->one(FLERR,fmt::format("Cannot open MEAM potential file {}",
                                   userfile));
  }
  }


  // read settings
  // read settings
@@ -576,7 +610,8 @@ void PairMEAMC::read_files(char *globalfile, char *userfile)
    for (which = 0; which < nkeywords; which++)
    for (which = 0; which < nkeywords; which++)
      if (strcmp(params[0],keywords[which]) == 0) break;
      if (strcmp(params[0],keywords[which]) == 0) break;
    if (which == nkeywords)
    if (which == nkeywords)
      ERRFMT(error->all, "Keyword %s in MEAM parameter file not recognized", params[0]);
      error->all(FLERR,fmt::format("Keyword {} in MEAM parameter file not "
                                   "recognized", params[0]));


    nindex = nparams - 2;
    nindex = nparams - 2;
    for (int i = 0; i < nindex; i++) index[i] = atoi(params[i+1]) - 1;
    for (int i = 0; i < nindex; i++) index[i] = atoi(params[i+1]) - 1;
@@ -586,7 +621,8 @@ void PairMEAMC::read_files(char *globalfile, char *userfile)
    if (which == 4) {
    if (which == 4) {
      lattice_t latt;
      lattice_t latt;
      if (!MEAM::str_to_lat(params[nparams-1], false, latt))
      if (!MEAM::str_to_lat(params[nparams-1], false, latt))
        ERRFMT(error->all, "Unrecognized lattice type in MEAM parameter file: %s", params[nparams-1]);
        error->all(FLERR, fmt::format("Unrecognized lattice type in MEAM "
                                      "parameter file: {}", params[nparams-1]));
      value = latt;
      value = latt;
    }
    }
    else value = atof(params[nparams-1]);
    else value = atof(params[nparams-1]);
+2 −1
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ PairStyle(meam,PairMEAMC)
#define LMP_PAIR_MEAMC_H
#define LMP_PAIR_MEAMC_H


#include "pair.h"
#include "pair.h"
#include <string>


namespace LAMMPS_NS {
namespace LAMMPS_NS {


@@ -54,7 +55,7 @@ class PairMEAMC : public Pair {
  double **scale;               // scaling factor for adapt
  double **scale;               // scaling factor for adapt


  void allocate();
  void allocate();
  void read_files(char *, char *);
  void read_files(const std::string &, const std::string &);
  void neigh_strip(int, int *, int *, int **);
  void neigh_strip(int, int *, int *, int **);
};
};