Commit c42b2698 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

convert several more table styles to use utils::sfgets()

parent e00757e1
Loading
Loading
Loading
Loading
+21 −10
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include "math_const.h"
#include "memory.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;
using namespace MathConst;
@@ -382,16 +383,17 @@ void AngleTable::read_table(Table *tb, char *file, char *keyword)
    if (line[0] == '#') continue;                          // comment
    char *word = strtok(line," \t\n\r");
    if (strcmp(word,keyword) == 0) break;            // matching keyword
    fgets(line,MAXLINE,fp);                         // no match, skip section
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error); // no match, skip section
    param_extract(tb,line);
    fgets(line,MAXLINE,fp);
    for (int i = 0; i < tb->ninput; i++) fgets(line,MAXLINE,fp);
    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

  fgets(line,MAXLINE,fp);
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  param_extract(tb,line);
  memory->create(tb->afile,tb->ninput,"angle:afile");
  memory->create(tb->efile,tb->ninput,"angle:efile");
@@ -399,15 +401,24 @@ void AngleTable::read_table(Table *tb, char *file, char *keyword)

  // read a,e,f table values from file

  int itmp;
  fgets(line,MAXLINE,fp);
  int cerror = 0;
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  for (int i = 0; i < tb->ninput; i++) {
    fgets(line,MAXLINE,fp);
    sscanf(line,"%d %lg %lg %lg",
      &itmp,&tb->afile[i],&tb->efile[i],&tb->ffile[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;
  }

  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);
  }
}

/* ----------------------------------------------------------------------
+10 −11
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include "force.h"
#include "memory.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;

@@ -311,16 +312,17 @@ void BondTable::read_table(Table *tb, char *file, char *keyword)
    if (line[0] == '#') continue;                          // comment
    char *word = strtok(line," \t\n\r");
    if (strcmp(word,keyword) == 0) break;            // matching keyword
    fgets(line,MAXLINE,fp);                         // no match, skip section
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error); // no match, skip section
    param_extract(tb,line);
    fgets(line,MAXLINE,fp);
    for (int i = 0; i < tb->ninput; i++) fgets(line,MAXLINE,fp);
    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

  fgets(line,MAXLINE,fp);
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  param_extract(tb,line);
  memory->create(tb->rfile,tb->ninput,"bond:rfile");
  memory->create(tb->efile,tb->ninput,"bond:efile");
@@ -328,16 +330,15 @@ void BondTable::read_table(Table *tb, char *file, char *keyword)

  // read r,e,f table values from file

  int itmp;
  int cerror = 0;
  int r0idx = -1;

  fgets(line,MAXLINE,fp);
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  for (int i = 0; i < tb->ninput; i++) {
    if (NULL == fgets(line,MAXLINE,fp))
      error->one(FLERR,"Premature end of file in bond table");
    if (4 != sscanf(line,"%d %lg %lg %lg",
                    &itmp,&tb->rfile[i],&tb->efile[i],&tb->ffile[i])) ++cerror;
    if (3 != sscanf(line,"%*d %lg %lg %lg",
                    &tb->rfile[i],&tb->efile[i],&tb->ffile[i])) ++cerror;
    if (tb->efile[i] < emin) {
      emin = tb->efile[i];
      r0idx = i;
@@ -369,8 +370,6 @@ void BondTable::read_table(Table *tb, char *file, char *keyword)
    fright = - (enext-e) / (rnext-r);
    if (f < fleft && f < fright) ferror++;
    if (f > fleft && f > fright) ferror++;
    //printf("Values %d: %g %g %g\n",i,r,e,f);
    //printf("  secant %d %d %g: %g %g %g\n",i,ferror,r,fleft,fright,f);
  }

  if (ferror) {
+11 −17
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@
#include "memory.h"
#include "error.h"
#include "dihedral_table.h"
#include "utils.h"

#include "math_const.h"
#include "math_extra.h"
@@ -51,7 +52,7 @@ using namespace MathExtra;
// ------------------------------------------------------------------------

// -------------------------------------------------------------------
// ---------    The function was stolen verbatim from the    ---------
// ---------    The function was taken verbatim from the    ---------
// ---------    GNU Scientific Library (GSL, version 1.15)   ---------
// -------------------------------------------------------------------

@@ -1091,17 +1092,17 @@ void DihedralTable::read_table(Table *tb, char *file, char *keyword)
    if (line[0] == '#') continue;                          // comment
    char *word = strtok(line," \t\n\r");
    if (strcmp(word,keyword) == 0) break;            // matching keyword
    fgets(line,MAXLINE,fp);                         // no match, skip section
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error); // no match, skip section
    param_extract(tb,line);
    fgets(line,MAXLINE,fp);
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
    for (int i = 0; i < tb->ninput; i++)
      fgets(line,MAXLINE,fp);
      utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  }

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

  fgets(line,MAXLINE,fp);
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  param_extract(tb,line);
  memory->create(tb->phifile,tb->ninput,"dihedral:phifile");
  memory->create(tb->efile,tb->ninput,"dihedral:efile");
@@ -1111,9 +1112,8 @@ void DihedralTable::read_table(Table *tb, char *file, char *keyword)

  int itmp;
  for (int i = 0; i < tb->ninput; i++) {
    // Read the next line.  Make sure the file is long enough.
    if (! fgets(line,MAXLINE,fp))
      error->one(FLERR, "Dihedral table does not contain enough entries.");
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);

    // Skip blank lines and delete text following a '#' character
    char *pe = strchr(line, '#');
    if (pe != NULL) *pe = '\0'; //terminate string at '#' character
@@ -1123,15 +1123,10 @@ void DihedralTable::read_table(Table *tb, char *file, char *keyword)
    if (*pc != '\0') { //If line is not a blank line
      stringstream line_ss(line);
      if (tb->f_unspecified) {
        //sscanf(line,"%d %lg %lg",
        //       &itmp,&tb->phifile[i],&tb->efile[i]);
        line_ss >> itmp;
        line_ss >> tb->phifile[i];
        line_ss >> tb->efile[i];
      }
      else {
        //sscanf(line,"%d %lg %lg %lg",
        //       &itmp,&tb->phifile[i],&tb->efile[i],&tb->ffile[i]);
      } else {
        line_ss >> itmp;
        line_ss >> tb->phifile[i];
        line_ss >> tb->efile[i];
@@ -1145,8 +1140,7 @@ void DihedralTable::read_table(Table *tb, char *file, char *keyword)
          err_msg << "\n   (This sometimes occurs if users forget to specify the \"NOF\" option.)\n";
        error->one(FLERR, err_msg.str().c_str());
      }
    }
    else //if it is a blank line, then skip it.
    } else //if it is a blank line, then skip it.
      i--;
  } //for (int i = 0; (i < tb->ninput) && fp; i++) {

+11 −16
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
#include "math_extra.h"
#include "memory.h"
#include "error.h"
#include "utils.h"

using namespace LAMMPS_NS;
using namespace MathConst;
@@ -45,7 +46,7 @@ using namespace MathExtra;


static const char cite_dihedral_tablecut[] =
  "dihedral_style  tablecut  command:\n\n"
  "dihedral_style  table/cut  command:\n\n"
  "@Article{Salerno17,\n"
  " author =  {K. M. Salerno and N. Bernstein},\n"
  " title =   {Persistence Length, End-to-End Distance, and Structure of Coarse-Grained Polymers},\n"
@@ -66,7 +67,7 @@ static const char cite_dihedral_tablecut[] =
// ------------------------------------------------------------------------

// -------------------------------------------------------------------
// ---------    The function was stolen verbatim from the    ---------
// ---------    The function was taken verbatim from the    ---------
// ---------    GNU Scientific Library (GSL, version 1.15)   ---------
// -------------------------------------------------------------------

@@ -1151,17 +1152,17 @@ void DihedralTableCut::read_table(Table *tb, char *file, char *keyword)
    if (line[0] == '#') continue;                          // comment
    char *word = strtok(line," \t\n\r");
    if (strcmp(word,keyword) == 0) break;           // matching keyword
    fgets(line,MAXLINE,fp);                         // no match, skip section
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);                         // no match, skip section
    param_extract(tb,line);
    fgets(line,MAXLINE,fp);
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
    for (int i = 0; i < tb->ninput; i++)
      fgets(line,MAXLINE,fp);
      utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  }

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

  fgets(line,MAXLINE,fp);
  utils::sfgets(FLERR,line,MAXLINE,fp,file,error);
  param_extract(tb,line);
  memory->create(tb->phifile,tb->ninput,"dihedral:phifile");
  memory->create(tb->efile,tb->ninput,"dihedral:efile");
@@ -1172,8 +1173,8 @@ void DihedralTableCut::read_table(Table *tb, char *file, char *keyword)
  int itmp;
  for (int i = 0; i < tb->ninput; i++) {
    // Read the next line.  Make sure the file is long enough.
    if (! fgets(line,MAXLINE,fp))
      error->one(FLERR, "Dihedral table does not contain enough entries.");
    utils::sfgets(FLERR,line,MAXLINE,fp,file,error);

    // Skip blank lines and delete text following a '#' character
    char *pe = strchr(line, '#');
    if (pe != NULL) *pe = '\0'; //terminate string at '#' character
@@ -1183,15 +1184,10 @@ void DihedralTableCut::read_table(Table *tb, char *file, char *keyword)
    if (*pc != '\0') { //If line is not a blank line
      stringstream line_ss(line);
      if (tb->f_unspecified) {
        //sscanf(line,"%d %lg %lg",
        //       &itmp,&tb->phifile[i],&tb->efile[i]);
        line_ss >> itmp;
        line_ss >> tb->phifile[i];
        line_ss >> tb->efile[i];
      }
      else {
        //sscanf(line,"%d %lg %lg %lg",
        //       &itmp,&tb->phifile[i],&tb->efile[i],&tb->ffile[i]);
      } else {
        line_ss >> itmp;
        line_ss >> tb->phifile[i];
        line_ss >> tb->efile[i];
@@ -1205,8 +1201,7 @@ void DihedralTableCut::read_table(Table *tb, char *file, char *keyword)
          err_msg << "\n   (This sometimes occurs if users forget to specify the \"NOF\" option.)\n";
        error->one(FLERR, err_msg.str().c_str());
      }
    }
    else //if it is a blank line, then skip it.
    } else //if it is a blank line, then skip it.
      i--;
  } //for (int i = 0; (i < tb->ninput) && fp; i++) {