Unverified Commit 944ac44b authored by Richard Berger's avatar Richard Berger
Browse files

Update pair coul streitz

parent 2ffb29f6
Loading
Loading
Loading
Loading
+44 −81
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@
#include "error.h"
#include "utils.h"
#include "fmt/format.h"
#include "potential_file_reader.h"

using namespace LAMMPS_NS;
using namespace MathConst;
@@ -237,83 +238,27 @@ double PairCoulStreitz::init_one(int i, int j)

void PairCoulStreitz::read_file(char *file)
{
  int params_per_line = 6;
  char **words = new char*[params_per_line+1];

  memory->sfree(params);
  params = NULL;
  params = nullptr;
  nparams = 0;
  maxparam = 0;

  // open file on proc 0

  FILE *fp;
  if (comm->me == 0) {
    fp = fopen(file,"r");
    if (fp == NULL)
      error->one(FLERR,fmt::format("Cannot open coul/streitz potential "
                                   "file {}",file));
  }

  // read each line out of file, skipping blank lines or leading '#'
  // store line of params if all 3 element tags are in element list

  int n,nwords,ielement;
  char line[MAXLINE],*ptr;
  int eof = 0;

  while (1) {
    if (comm->me == 0) {
      ptr = fgets(line,MAXLINE,fp);
      if (ptr == NULL) {
        eof = 1;
        fclose(fp);
      } else n = strlen(line) + 1;
    }
    MPI_Bcast(&eof,1,MPI_INT,0,world);
    if (eof) break;
    MPI_Bcast(&n,1,MPI_INT,0,world);
    MPI_Bcast(line,n,MPI_CHAR,0,world);

    // strip comment, skip line if blank

    if ((ptr = strchr(line,'#'))) *ptr = '\0';
    nwords = utils::count_words(line);
    if (nwords == 0) continue;

    // concatenate additional lines until have params_per_line words

    while (nwords < params_per_line) {
      n = strlen(line);
  if (comm->me == 0) {
        ptr = fgets(&line[n],MAXLINE-n,fp);
        if (ptr == NULL) {
          eof = 1;
          fclose(fp);
        } else n = strlen(line) + 1;
      }
      MPI_Bcast(&eof,1,MPI_INT,0,world);
      if (eof) break;
      MPI_Bcast(&n,1,MPI_INT,0,world);
      MPI_Bcast(line,n,MPI_CHAR,0,world);
      if ((ptr = strchr(line,'#'))) *ptr = '\0';
      nwords = utils::count_words(line);
    }

    if (nwords != params_per_line)
      error->all(FLERR,"Incorrect format in coul/streitz potential file");
    PotentialFileReader reader(lmp, file, "coul/streitz");
    char * line;

    // words = ptrs to all words in line
    while((line = reader.next_line(NPARAMS_PER_LINE))) {
      try {
        ValueTokenizer values(line);

    nwords = 0;
    words[nwords++] = strtok(line," \t\n\r\f");
    while ((words[nwords++] = strtok(NULL," \t\n\r\f"))) continue;
        std::string iname = values.next_string();

        // ielement = 1st args
        int ielement;

        for (ielement = 0; ielement < nelements; ielement++)
      if (strcmp(words[0],elements[ielement]) == 0) break;
    if (ielement == nelements) continue;
          if (iname == elements[ielement]) break;

        // load up parameter settings and error check their values

@@ -321,25 +266,43 @@ void PairCoulStreitz::read_file(char *file)
          maxparam += DELTA;
          params = (Param *) memory->srealloc(params,maxparam*sizeof(Param),
                                              "pair:params");

          // make certain all addional allocated storage is initialized
          // to avoid false positives when checking with valgrind

          memset(params + nparams, 0, DELTA*sizeof(Param));
        }


        params[nparams].ielement = ielement;
    params[nparams].chi = atof(words[1]);
    params[nparams].eta = atof(words[2]);
    params[nparams].gamma = atof(words[3]);
    params[nparams].zeta = atof(words[4]);
    params[nparams].zcore = atof(words[5]);
        params[nparams].chi = values.next_double();
        params[nparams].eta = values.next_double();
        params[nparams].gamma = values.next_double();
        params[nparams].zeta = values.next_double();
        params[nparams].zcore = values.next_double();

      } catch (TokenizerException & e) {
        error->one(FLERR, e.what());
      }

      // parameter sanity check

      if (params[nparams].eta < 0.0 || params[nparams].zeta < 0.0 ||
          params[nparams].zcore < 0.0 || params[nparams].gamma != 0.0 )
      error->all(FLERR,"Illegal coul/streitz parameter");
        error->one(FLERR,"Illegal coul/streitz parameter");

      nparams++;
    }
  }

  MPI_Bcast(&nparams, 1, MPI_INT, 0, world);
  MPI_Bcast(&maxparam, 1, MPI_INT, 0, world);

  if(comm->me != 0) {
    params = (Param *) memory->srealloc(params,maxparam*sizeof(Param), "pair:params");
  }

  delete [] words;
  MPI_Bcast(params, maxparam*sizeof(Param), MPI_BYTE, 0, world);
}

/* ---------------------------------------------------------------------- */
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ class PairCoulStreitz : public Pair {
  double memory_usage();
  virtual void *extract(const char *, int &);

  static const int NPARAMS_PER_LINE = 6;

 protected:
  struct Param {
    double chi, eta, gamma, zeta, zcore;