Update mliap_model_nn.cpp

This commit is contained in:
Pedro Antonio Santos Flórez
2021-02-16 14:07:16 -03:00
parent 96643cc5ec
commit 7bdbdad271

View File

@ -92,9 +92,6 @@ void MLIAPModelNN::read_coeffs(char *coefffilename)
// strip comment, skip line if blank
if ((ptr = strchr(line,'#'))) *ptr = '\0';
if ((ptr = strstr(line,"nn"))) {
*ptr = '\0';
}
nwords = utils::count_words(line);
}
if (nwords != 2)
@ -116,93 +113,93 @@ void MLIAPModelNN::read_coeffs(char *coefffilename)
memory->create(coeffelem,nelements,nparams,"mliap_snap_model:coeffelem");
int stats = 0;
int ielem = 0;
int l = 0;
int stats = 0;
int ielem = 0;
int l = 0;
while (1) {
if (comm->me == 0) {
ptr = fgets(line,MAXLINE,fpcoeff);
if (ptr == nullptr) {
eof = 1;
fclose(fpcoeff);
} 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::trim_and_count_words(line);
if (nwords == 0) continue;
if (stats == 0) { // Header NET
tstr = strtok(line,"' \t\n\r\f");
if (strncmp(tstr, "NET", 3) != 0) error->all(FLERR,"Incorrect format in NET coefficient file");
ndescriptors = atoi(strtok(nullptr,"' \t\n\r\f"));
nlayers = atoi(strtok(nullptr,"' \t\n\r\f"));
memory->create(activation,nlayers,"mliap_model:activation");
memory->create(nnodes,nlayers,"mliap_model:nnodes");
memory->create(scale,nelements,2,ndescriptors,"mliap_model:scale");
for (int ilayer = 0; ilayer < nlayers; ilayer++) {
tstr = strtok(NULL,"' \t\n\r\f");
nnodes[ilayer] = atoi(strtok(NULL,"' \t\n\r\f"));
if (strncmp(tstr, "linear", 6) == 0) activation[ilayer] = 0;
else if (strncmp(tstr, "sigmoid", 7) == 0) activation[ilayer] = 1;
else if (strncmp(tstr, "tanh", 4) == 0) activation[ilayer] = 2;
else if (strncmp(tstr, "relu", 4) == 0) activation[ilayer] = 3;
else activation[ilayer] = 4;
}
stats = 1;
} else if (stats == 1) {
scale[ielem][0][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
scale[ielem][0][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == ndescriptors) {
stats = 2;
l = 0;
}
} else if (stats == 2) {
scale[ielem][1][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
scale[ielem][1][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == ndescriptors) {
stats = 3;
l = 0;
}
// set up coeff lists
} else if (stats == 3) {
// if (nwords > 30) error->all(FLERR,"Wrong number of items per line, max 30");
coeffelem[ielem][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
coeffelem[ielem][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == nparams) {
stats = 0;
ielem++;
}
}
while (1) {
if (comm->me == 0) {
ptr = fgets(line,MAXLINE,fpcoeff);
if (ptr == nullptr) {
eof = 1;
fclose(fpcoeff);
} else n = strlen(line) + 1;
}
if (comm->me == 0) fclose(fpcoeff);
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::trim_and_count_words(line);
if (nwords == 0) continue;
if (stats == 0) { // Header NET
tstr = strtok(line,"' \t\n\r\f");
if (strncmp(tstr, "NET", 3) != 0) error->all(FLERR,"Incorrect format in MLIAPModel coefficient file");
ndescriptors = atoi(strtok(nullptr,"' \t\n\r\f"));
nlayers = atoi(strtok(nullptr,"' \t\n\r\f"));
memory->create(activation,nlayers,"mliap_model:activation");
memory->create(nnodes,nlayers,"mliap_model:nnodes");
memory->create(scale,nelements,2,ndescriptors,"mliap_model:scale");
for (int ilayer = 0; ilayer < nlayers; ilayer++) {
tstr = strtok(NULL,"' \t\n\r\f");
nnodes[ilayer] = atoi(strtok(NULL,"' \t\n\r\f"));
if (strncmp(tstr, "linear", 6) == 0) activation[ilayer] = 0;
else if (strncmp(tstr, "sigmoid", 7) == 0) activation[ilayer] = 1;
else if (strncmp(tstr, "tanh", 4) == 0) activation[ilayer] = 2;
else if (strncmp(tstr, "relu", 4) == 0) activation[ilayer] = 3;
else activation[ilayer] = 4;
}
stats = 1;
} else if (stats == 1) {
scale[ielem][0][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
scale[ielem][0][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == ndescriptors) {
stats = 2;
l = 0;
}
} else if (stats == 2) {
scale[ielem][1][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
scale[ielem][1][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == ndescriptors) {
stats = 3;
l = 0;
}
// set up coeff lists
} else if (stats == 3) {
if (nwords > 30) error->all(FLERR,"Incorrect format in MLIAPModel coefficient file");
coeffelem[ielem][l] = atof(strtok(line,"' \t\n\r\f"));
for (int icoeff = 1; icoeff < nwords; icoeff++) {
coeffelem[ielem][l+icoeff] = atof(strtok(nullptr,"' \t\n\r\f"));
}
l += nwords;
if (l == nparams) {
stats = 0;
ielem++;
}
}
}
if (comm->me == 0) fclose(fpcoeff);
}