fix issues with potential file parser

- use Force::open_potential()
- replace ftell()/fseek() with rewind()/fgets() which is safer on windows and other platforms with automatic CR/LF to LF conversion on text files
- make parser use properly NULL terminated strings through using strtok()
This commit is contained in:
Axel Kohlmeyer
2017-05-09 15:35:48 -04:00
parent 42531389df
commit 9cd994f57c

View File

@ -45,9 +45,6 @@
#include "neigh_request.h" #include "neigh_request.h"
#include "memory.h" #include "memory.h"
#include "error.h" #include "error.h"
#include <iostream>
using namespace std;
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -449,7 +446,7 @@ void PairMEAMSpline::read_file(const char* filename)
int nmultichoose2; // = (n+1)*n/2; int nmultichoose2; // = (n+1)*n/2;
if(comm->me == 0) { if(comm->me == 0) {
FILE *fp = fopen(filename, "r"); FILE *fp = force->open_potential(filename);
if(fp == NULL) { if(fp == NULL) {
char str[1024]; char str[1024];
sprintf(str,"Cannot open spline MEAM potential file %s", filename); sprintf(str,"Cannot open spline MEAM potential file %s", filename);
@ -458,31 +455,35 @@ void PairMEAMSpline::read_file(const char* filename)
// Skip first line of file. It's a comment. // Skip first line of file. It's a comment.
char line[MAXLINE]; char line[MAXLINE];
char *ptr;
fgets(line, MAXLINE, fp); fgets(line, MAXLINE, fp);
// Second line holds potential type ("meam/spline") in new potential format. // Second line holds potential type ("meam/spline")
bool isNewFormat; // in new potential format.
long loc = ftell(fp);
bool isNewFormat = false;
fgets(line, MAXLINE, fp); fgets(line, MAXLINE, fp);
if (strncmp(line, "meam/spline", 11) == 0) { ptr = strtok(line, " \t\n\r\f");
if (strcmp(ptr, "meam/spline") == 0) {
isNewFormat = true; isNewFormat = true;
// parse the rest of the line! // parse the rest of the line!
char *linep = line+12, *word; ptr = strtok(NULL," \t\n\r\f");
const char *sep = " ,;:-\t\n"; // overkill, but safe if (ptr == NULL)
word = strsep(&linep, sep); error->one(FLERR,"Need to include number of atomic species on"
if (! *word) " meam/spline line in multi-element potential file");
error->one(FLERR, "Need to include number of atomic species on meam/spline line in potential file"); nelements = atoi(ptr);
int n = atoi(word); if (nelements < 1)
if (n<1) error->one(FLERR, "Invalid number of atomic species on"
error->one(FLERR, "Invalid number of atomic species on meam/spline line in potential file"); " meam/spline line in potential file");
nelements = n; elements = new char*[nelements];
elements = new char*[n]; for (int i=0; i<nelements; ++i) {
for (int i=0; i<n; ++i) { ptr = strtok(NULL," \t\n\r\f");
word = strsep(&linep, sep); if (ptr == NULL)
if (! *word) error->one(FLERR, "Not enough atomic species in meam/spline"
error->one(FLERR, "Not enough atomic species in meam/spline\n"); " line of multi-element potential file");
elements[i] = new char[strlen(word)+1]; elements[i] = new char[strlen(ptr)+1];
strcpy(elements[i], word); strcpy(elements[i], ptr);
} }
} else { } else {
isNewFormat = false; isNewFormat = false;
@ -490,7 +491,8 @@ void PairMEAMSpline::read_file(const char* filename)
elements = new char*[1]; elements = new char*[1];
elements[0] = new char[1]; elements[0] = new char[1];
strcpy(elements[0], ""); strcpy(elements[0], "");
fseek(fp, loc, SEEK_SET); rewind(fp);
fgets(line, MAXLINE, fp);
} }
nmultichoose2 = ((nelements+1)*nelements)/2; nmultichoose2 = ((nelements+1)*nelements)/2;