Unverified Commit 392a4f4e authored by Richard Berger's avatar Richard Berger
Browse files

Update AngleTable

parent 7658d05c
Loading
Loading
Loading
Loading
+47 −59
Original line number Diff line number Diff line
@@ -29,14 +29,16 @@
#include "memory.h"
#include "error.h"
#include "utils.h"
#include "utils.h"
#include "tokenizer.h"
#include "text_file_reader.h"
#include "table_file_reader.h"
#include "fmt/format.h"

using namespace LAMMPS_NS;
using namespace MathConst;

enum{LINEAR,SPLINE};

#define MAXLINE 1024
#define SMALL 0.001
#define TINY  1.E-10

@@ -381,37 +383,18 @@ void AngleTable::free_table(Table *tb)

void AngleTable::read_table(Table *tb, char *file, char *keyword)
{
  char line[MAXLINE];

  // open file
  TableFileReader reader(lmp, file);

  FILE *fp = force->open_potential(file);
  if (fp == NULL) {
    char str[128];
    snprintf(str,128,"Cannot open file %s",file);
    error->one(FLERR,str);
  }
  char * line = reader.find_section_start(keyword);

  // loop until section found with matching keyword

  while (1) {
    if (fgets(line,MAXLINE,fp) == NULL)
  if (!line) {
    error->one(FLERR,"Did not find keyword in table file");
    if (strspn(line," \t\n") == strlen(line)) continue;    // blank line
    if (line[0] == '#') continue;                          // comment
    char *word = strtok(line," \t\n\r");
    if (strcmp(word,keyword) == 0) break;            // matching keyword
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error); // no match, skip section
    param_extract(tb,line);
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
    for (int i = 0; i < tb->ninput; i++)
      utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  }

  // read args on 2nd line of section
  // allocate table arrays for file values

  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  line = reader.next_line();
  param_extract(tb, line);
  memory->create(tb->afile, tb->ninput, "angle:afile");
  memory->create(tb->efile, tb->ninput, "angle:efile");
@@ -420,22 +403,25 @@ void AngleTable::read_table(Table *tb, char *file, char *keyword)
  // read a,e,f table values from file

  int cerror = 0;
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  reader.skip_line();
  for (int i = 0; i < tb->ninput; i++) {
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
    if (3 != sscanf(line,"%*d %lg %lg %lg",
                    &tb->afile[i],&tb->efile[i],&tb->ffile[i])) ++cerror;
    line = reader.next_line(4);
    try {
      ValueTokenizer values(line);
      values.next_int();
      tb->afile[i] = values.next_double();
      tb->efile[i] = values.next_double();
      tb->ffile[i] = values.next_double();
    } catch (TokenizerException & e) {
      ++cerror;
    }
  }

  fclose(fp);

  // warn if data was read incompletely, e.g. columns were missing

  if (cerror) {
    char str[128];
    sprintf(str,"%d of %d lines in table were incomplete or could not be"
            " parsed completely",cerror,tb->ninput);
    error->warning(FLERR,str);
    std::string str = fmt::format("{} of {} lines in table were incomplete or could not be parsed completely", cerror, tb->ninput);
    error->warning(FLERR,str.c_str());
  }
}

@@ -521,26 +507,28 @@ void AngleTable::param_extract(Table *tb, char *line)
  tb->fpflag = 0;
  tb->theta0 = MY_PI;

  char *word = strtok(line," \t\n\r\f");
  while (word) {
    if (strcmp(word,"N") == 0) {
      word = strtok(NULL," \t\n\r\f");
      tb->ninput = atoi(word);
    } else if (strcmp(word,"FP") == 0) {
  ValueTokenizer values(line);

  try {
    while (values.has_next()) {
      std::string word = values.next_string();

      if (word == "N") {
        tb->ninput = values.next_int();
      } else if (word == "FP") {
        tb->fpflag = 1;
      word = strtok(NULL," \t\n\r\f");
      tb->fplo = atof(word);
      word = strtok(NULL," \t\n\r\f");
      tb->fphi = atof(word);
        tb->fplo = values.next_double();
        tb->fphi = values.next_double();
        tb->fplo *= (180.0/MY_PI)*(180.0/MY_PI);
        tb->fphi *= (180.0/MY_PI)*(180.0/MY_PI);
    } else if (strcmp(word,"EQ") == 0) {
      word = strtok(NULL," \t\n\r\f");
      tb->theta0 = atof(word)/180.0*MY_PI;
      } else if (word == "EQ") {
        tb->theta0 = values.next_double()/180.0*MY_PI;
      } else {
        error->one(FLERR,"Invalid keyword in angle table parameters");
      }
    word = strtok(NULL," \t\n\r\f");
    }
  } catch(TokenizerException & e) {
    error->one(FLERR, e.what());
  }

  if (tb->ninput == 0) error->one(FLERR,"Angle table parameters did not set N");