sync with current develop
This commit is contained in:
269
src/input.cpp
269
src/input.cpp
@ -27,9 +27,11 @@
|
||||
#include "dihedral.h"
|
||||
#include "domain.h"
|
||||
#include "error.h"
|
||||
#include "fix.h"
|
||||
#include "force.h"
|
||||
#include "group.h"
|
||||
#include "improper.h"
|
||||
#include "integrate.h"
|
||||
#include "kspace.h"
|
||||
#include "memory.h"
|
||||
#include "min.h"
|
||||
@ -38,7 +40,7 @@
|
||||
#include "output.h"
|
||||
#include "pair.h"
|
||||
#include "special.h"
|
||||
#include "style_command.h"
|
||||
#include "style_command.h" // IWYU pragma: keep
|
||||
#include "thermo.h"
|
||||
#include "timer.h"
|
||||
#include "universe.h"
|
||||
@ -46,20 +48,23 @@
|
||||
#include "variable.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <errno.h>
|
||||
#include <cerrno>
|
||||
#include <cctype>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
#define DELTALINE 256
|
||||
#define DELTA 4
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
one instance per command in style_command.h
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
template <typename T> static Command *command_creator(LAMMPS *lmp)
|
||||
{
|
||||
return new T(lmp);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
/** \class LAMMPS_NS::Input
|
||||
@ -126,7 +131,7 @@ Input::Input(LAMMPS *lmp, int argc, char **argv) : Pointers(lmp)
|
||||
#define COMMAND_CLASS
|
||||
#define CommandStyle(key,Class) \
|
||||
(*command_map)[#key] = &command_creator<Class>;
|
||||
#include "style_command.h"
|
||||
#include "style_command.h" // IWYU pragma: keep
|
||||
#undef CommandStyle
|
||||
#undef COMMAND_CLASS
|
||||
|
||||
@ -163,7 +168,7 @@ Input::~Input()
|
||||
memory->sfree(line);
|
||||
memory->sfree(copy);
|
||||
memory->sfree(work);
|
||||
if (labelstr) delete[] labelstr;
|
||||
delete[] labelstr;
|
||||
memory->sfree(arg);
|
||||
delete[] infiles;
|
||||
delete variable;
|
||||
@ -185,7 +190,7 @@ void Input::file()
|
||||
{
|
||||
int m,n;
|
||||
|
||||
while (1) {
|
||||
while (true) {
|
||||
|
||||
// read a line from input script
|
||||
// n = length of line including str terminator, 0 if end of file
|
||||
@ -194,7 +199,7 @@ void Input::file()
|
||||
if (me == 0) {
|
||||
|
||||
m = 0;
|
||||
while (1) {
|
||||
while (true) {
|
||||
|
||||
if (infile == nullptr) {
|
||||
n = 0;
|
||||
@ -614,8 +619,7 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag)
|
||||
}
|
||||
|
||||
if (value == nullptr)
|
||||
error->one(FLERR,"Substitution for illegal "
|
||||
"variable {}",var);
|
||||
error->one(FLERR,"Substitution for illegal variable {}",var);
|
||||
|
||||
// check if storage in str2 needs to be expanded
|
||||
// re-initialize ptr and ptr2 to the point beyond the variable.
|
||||
@ -788,9 +792,21 @@ int Input::execute_command()
|
||||
if (flag) return 0;
|
||||
|
||||
// invoke commands added via style_command.h
|
||||
// try suffixed version first
|
||||
|
||||
if (command_map->find(command) != command_map->end()) {
|
||||
CommandCreator &command_creator = (*command_map)[command];
|
||||
std::string mycmd = command;
|
||||
if (lmp->suffix_enable) {
|
||||
mycmd = command + std::string("/") + lmp->suffix;
|
||||
if (command_map->find(mycmd) == command_map->end()) {
|
||||
if (lmp->suffix2) {
|
||||
mycmd = command + std::string("/") + lmp->suffix2;
|
||||
if (command_map->find(mycmd) == command_map->end())
|
||||
mycmd = command;
|
||||
} else mycmd = command;
|
||||
}
|
||||
}
|
||||
if (command_map->find(mycmd) != command_map->end()) {
|
||||
CommandCreator &command_creator = (*command_map)[mycmd];
|
||||
Command *cmd = command_creator(lmp);
|
||||
cmd->command(narg,arg);
|
||||
delete cmd;
|
||||
@ -802,16 +818,6 @@ int Input::execute_command()
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
one instance per command in style_command.h
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
template <typename T>
|
||||
Command *Input::command_creator(LAMMPS *lmp)
|
||||
{
|
||||
return new T(lmp);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* ---------------------------------------------------------------------- */
|
||||
@ -885,7 +891,7 @@ void Input::ifthenelse()
|
||||
int ncommands = last-first + 1;
|
||||
if (ncommands <= 0) error->all(FLERR,"Illegal if command");
|
||||
|
||||
char **commands = new char*[ncommands];
|
||||
auto commands = new char*[ncommands];
|
||||
ncommands = 0;
|
||||
for (int i = first; i <= last; i++) {
|
||||
n = strlen(arg[i]) + 1;
|
||||
@ -938,7 +944,7 @@ void Input::ifthenelse()
|
||||
int ncommands = last-first + 1;
|
||||
if (ncommands <= 0) error->all(FLERR,"Illegal if command");
|
||||
|
||||
char **commands = new char*[ncommands];
|
||||
auto commands = new char*[ncommands];
|
||||
ncommands = 0;
|
||||
for (int i = first; i <= last; i++) {
|
||||
n = strlen(arg[i]) + 1;
|
||||
@ -970,10 +976,15 @@ void Input::include()
|
||||
if (nfile == maxfile)
|
||||
error->one(FLERR,"Too many nested levels of input scripts");
|
||||
|
||||
infile = fopen(arg[0],"r");
|
||||
// expand variables
|
||||
int n = strlen(arg[0]) + 1;
|
||||
if (n > maxline) reallocate(line,maxline,n);
|
||||
strcpy(line,arg[0]);
|
||||
substitute(line,work,maxline,maxwork,0);
|
||||
|
||||
infile = fopen(line,"r");
|
||||
if (infile == nullptr)
|
||||
error->one(FLERR,"Cannot open input script {}: {}",
|
||||
arg[0], utils::getsyserror());
|
||||
error->one(FLERR,"Cannot open input script {}: {}", line, utils::getsyserror());
|
||||
|
||||
infiles[nfile++] = infile;
|
||||
}
|
||||
@ -1015,7 +1026,7 @@ void Input::jump()
|
||||
|
||||
if (narg == 2) {
|
||||
label_active = 1;
|
||||
if (labelstr) delete[] labelstr;
|
||||
delete[] labelstr;
|
||||
labelstr = utils::strdup(arg[1]);
|
||||
}
|
||||
}
|
||||
@ -1069,19 +1080,14 @@ void Input::partition()
|
||||
{
|
||||
if (narg < 3) error->all(FLERR,"Illegal partition command");
|
||||
|
||||
int yesflag = 0;
|
||||
if (strcmp(arg[0],"yes") == 0) yesflag = 1;
|
||||
else if (strcmp(arg[0],"no") == 0) yesflag = 0;
|
||||
else error->all(FLERR,"Illegal partition command");
|
||||
|
||||
int ilo,ihi;
|
||||
int yesflag = utils::logical(FLERR,arg[0],false,lmp);
|
||||
utils::bounds(FLERR,arg[1],1,universe->nworlds,ilo,ihi,error);
|
||||
|
||||
// new command starts at the 3rd argument,
|
||||
// which must not be another partition command
|
||||
|
||||
if (strcmp(arg[2],"partition") == 0)
|
||||
error->all(FLERR,"Illegal partition command");
|
||||
if (strcmp(arg[2],"partition") == 0) error->all(FLERR,"Illegal partition command");
|
||||
|
||||
char *cmd = strstr(line,arg[2]);
|
||||
|
||||
@ -1124,21 +1130,16 @@ void Input::print()
|
||||
if (strcmp(arg[iarg],"file") == 0) fp = fopen(arg[iarg+1],"w");
|
||||
else fp = fopen(arg[iarg+1],"a");
|
||||
if (fp == nullptr)
|
||||
error->one(FLERR,"Cannot open print file {}: {}",
|
||||
arg[iarg+1], utils::getsyserror());
|
||||
error->one(FLERR,"Cannot open print file {}: {}", arg[iarg+1], utils::getsyserror());
|
||||
}
|
||||
iarg += 2;
|
||||
} else if (strcmp(arg[iarg],"screen") == 0) {
|
||||
if (iarg+2 > narg) error->all(FLERR,"Illegal print command");
|
||||
if (strcmp(arg[iarg+1],"yes") == 0) screenflag = 1;
|
||||
else if (strcmp(arg[iarg+1],"no") == 0) screenflag = 0;
|
||||
else error->all(FLERR,"Illegal print command");
|
||||
screenflag = utils::logical(FLERR,arg[iarg+1],false,lmp);
|
||||
iarg += 2;
|
||||
} else if (strcmp(arg[iarg],"universe") == 0) {
|
||||
if (iarg+2 > narg) error->all(FLERR,"Illegal print command");
|
||||
if (strcmp(arg[iarg+1],"yes") == 0) universeflag = 1;
|
||||
else if (strcmp(arg[iarg+1],"no") == 0) universeflag = 0;
|
||||
else error->all(FLERR,"Illegal print command");
|
||||
universeflag = utils::logical(FLERR,arg[iarg+1],false,lmp);
|
||||
iarg += 2;
|
||||
} else error->all(FLERR,"Illegal print command");
|
||||
}
|
||||
@ -1175,15 +1176,6 @@ void Input::quit()
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
char *shell_failed_message(const char* cmd, int errnum)
|
||||
{
|
||||
std::string errmsg = fmt::format("Shell command '{}' failed with error '{}'",
|
||||
cmd, strerror(errnum));
|
||||
char *msg = new char[errmsg.size()+1];
|
||||
strcpy(msg, errmsg.c_str());
|
||||
return msg;
|
||||
}
|
||||
|
||||
void Input::shell()
|
||||
{
|
||||
int rv,err;
|
||||
@ -1192,106 +1184,88 @@ void Input::shell()
|
||||
|
||||
if (strcmp(arg[0],"cd") == 0) {
|
||||
if (narg != 2) error->all(FLERR,"Illegal shell cd command");
|
||||
rv = (chdir(arg[1]) < 0) ? errno : 0;
|
||||
rv = (platform::chdir(arg[1]) < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
errno = err;
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("cd",err);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
error->warning(FLERR, "Shell command 'cd {}' failed with error '{}'", arg[1], utils::getsyserror());
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"mkdir") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell mkdir command");
|
||||
if (me == 0)
|
||||
if (me == 0) {
|
||||
for (int i = 1; i < narg; i++) {
|
||||
#if defined(_WIN32)
|
||||
rv = _mkdir(arg[i]);
|
||||
#else
|
||||
rv = mkdir(arg[i], S_IRWXU | S_IRGRP | S_IXGRP);
|
||||
#endif
|
||||
if (rv < 0) {
|
||||
char *message = shell_failed_message("mkdir",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
}
|
||||
rv = (platform::mkdir(arg[i]) < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
errno = err;
|
||||
if (err != 0)
|
||||
error->warning(FLERR, "Shell command 'mkdir {}' failed with error '{}'",
|
||||
arg[i],utils::getsyserror());
|
||||
}
|
||||
|
||||
}
|
||||
} else if (strcmp(arg[0],"mv") == 0) {
|
||||
if (narg != 3) error->all(FLERR,"Illegal shell mv command");
|
||||
rv = (rename(arg[1],arg[2]) < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("mv",err);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
if (me == 0) {
|
||||
if (platform::path_is_directory(arg[2])) {
|
||||
if (system(fmt::format("mv {} {}", arg[1], arg[2]).c_str()))
|
||||
error->warning(FLERR,"Shell command 'mv {} {}' returned with non-zero status", arg[1], arg[2]);
|
||||
} else {
|
||||
if (rename(arg[1],arg[2]) < 0) {
|
||||
error->warning(FLERR, "Shell command 'mv {} {}' failed with error '{}'",
|
||||
arg[1],arg[2],utils::getsyserror());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (strcmp(arg[0],"rm") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell rm command");
|
||||
if (me == 0)
|
||||
for (int i = 1; i < narg; i++) {
|
||||
if (unlink(arg[i]) < 0) {
|
||||
char *message = shell_failed_message("rm",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
}
|
||||
if (me == 0) {
|
||||
int i = 1;
|
||||
bool warn = true;
|
||||
if (strcmp(arg[i], "-f") == 0) {
|
||||
warn = false;
|
||||
++i;
|
||||
}
|
||||
|
||||
for (;i < narg; i++) {
|
||||
if (platform::unlink(arg[i]) < 0)
|
||||
if (warn)
|
||||
error->warning(FLERR, "Shell command 'rm {}' failed with error '{}'",
|
||||
arg[i], utils::getsyserror());
|
||||
}
|
||||
}
|
||||
} else if (strcmp(arg[0],"rmdir") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell rmdir command");
|
||||
if (me == 0)
|
||||
if (me == 0) {
|
||||
for (int i = 1; i < narg; i++) {
|
||||
if (rmdir(arg[i]) < 0) {
|
||||
char *message = shell_failed_message("rmdir",errno);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
}
|
||||
if (platform::rmdir(arg[i]) < 0)
|
||||
error->warning(FLERR, "Shell command 'rmdir {}' failed with error '{}'",
|
||||
arg[i], utils::getsyserror());
|
||||
}
|
||||
|
||||
}
|
||||
} else if (strcmp(arg[0],"putenv") == 0) {
|
||||
if (narg < 2) error->all(FLERR,"Illegal shell putenv command");
|
||||
for (int i = 1; i < narg; i++) {
|
||||
rv = 0;
|
||||
#ifdef _WIN32
|
||||
if (arg[i]) rv = _putenv(utils::strdup(arg[i]));
|
||||
#else
|
||||
if (arg[i]) {
|
||||
std::string vardef(arg[i]);
|
||||
auto found = vardef.find_first_of("=");
|
||||
if (found == std::string::npos) {
|
||||
rv = setenv(vardef.c_str(),"",1);
|
||||
} else {
|
||||
rv = setenv(vardef.substr(0,found).c_str(),
|
||||
vardef.substr(found+1).c_str(),1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (arg[i]) rv = platform::putenv(arg[i]);
|
||||
rv = (rv < 0) ? errno : 0;
|
||||
MPI_Reduce(&rv,&err,1,MPI_INT,MPI_MAX,0,world);
|
||||
if (me == 0 && err != 0) {
|
||||
char *message = shell_failed_message("putenv",err);
|
||||
error->warning(FLERR,message);
|
||||
delete[] message;
|
||||
}
|
||||
errno = err;
|
||||
if (me == 0 && err != 0)
|
||||
error->warning(FLERR, "Shell command 'putenv {}' failed with error '{}'",
|
||||
arg[i], utils::getsyserror());
|
||||
}
|
||||
|
||||
// use work string to concat args back into one string separated by spaces
|
||||
// invoke string in shell via system()
|
||||
// concat arguments and invoke string in shell via system()
|
||||
|
||||
} else {
|
||||
int n = 0;
|
||||
for (int i = 0; i < narg; i++) n += strlen(arg[i]) + 1;
|
||||
if (n > maxwork) reallocate(work,maxwork,n);
|
||||
if (me == 0) {
|
||||
std::string cmd = arg[0];
|
||||
for (int i = 1; i < narg; i++) {
|
||||
cmd += " ";
|
||||
cmd += arg[i];
|
||||
}
|
||||
|
||||
strcpy(work,arg[0]);
|
||||
for (int i = 1; i < narg; i++) {
|
||||
strcat(work," ");
|
||||
strcat(work,arg[i]);
|
||||
if (system(cmd.c_str()) != 0)
|
||||
error->warning(FLERR,"Shell command {} returned with non-zero status", cmd);
|
||||
}
|
||||
|
||||
if (me == 0)
|
||||
if (system(work) != 0)
|
||||
error->warning(FLERR,"Shell command returned with non-zero status");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1633,16 +1607,10 @@ void Input::newton()
|
||||
int newton_pair=1,newton_bond=1;
|
||||
|
||||
if (narg == 1) {
|
||||
if (strcmp(arg[0],"off") == 0) newton_pair = newton_bond = 0;
|
||||
else if (strcmp(arg[0],"on") == 0) newton_pair = newton_bond = 1;
|
||||
else error->all(FLERR,"Illegal newton command");
|
||||
newton_pair = newton_bond = utils::logical(FLERR,arg[0],false,lmp);
|
||||
} else if (narg == 2) {
|
||||
if (strcmp(arg[0],"off") == 0) newton_pair = 0;
|
||||
else if (strcmp(arg[0],"on") == 0) newton_pair= 1;
|
||||
else error->all(FLERR,"Illegal newton command");
|
||||
if (strcmp(arg[1],"off") == 0) newton_bond = 0;
|
||||
else if (strcmp(arg[1],"on") == 0) newton_bond = 1;
|
||||
else error->all(FLERR,"Illegal newton command");
|
||||
newton_pair = utils::logical(FLERR,arg[0],false,lmp);
|
||||
newton_bond = utils::logical(FLERR,arg[1],false,lmp);
|
||||
} else error->all(FLERR,"Illegal newton command");
|
||||
|
||||
force->newton_pair = newton_pair;
|
||||
@ -1841,11 +1809,13 @@ void Input::suffix()
|
||||
{
|
||||
if (narg < 1) error->all(FLERR,"Illegal suffix command");
|
||||
|
||||
if (strcmp(arg[0],"off") == 0) lmp->suffix_enable = 0;
|
||||
else if (strcmp(arg[0],"on") == 0) {
|
||||
if (!lmp->suffix)
|
||||
error->all(FLERR,"May only enable suffixes after defining one");
|
||||
const std::string firstarg = arg[0];
|
||||
|
||||
if ((firstarg == "off") || (firstarg == "no") || (firstarg == "false")) {
|
||||
lmp->suffix_enable = 0;
|
||||
} else if ((firstarg == "on") || (firstarg == "yes") || (firstarg == "true")) {
|
||||
lmp->suffix_enable = 1;
|
||||
if (!lmp->suffix) error->all(FLERR,"May only enable suffixes after defining one");
|
||||
} else {
|
||||
lmp->suffix_enable = 1;
|
||||
|
||||
@ -1853,7 +1823,7 @@ void Input::suffix()
|
||||
delete[] lmp->suffix2;
|
||||
lmp->suffix = lmp->suffix2 = nullptr;
|
||||
|
||||
if (strcmp(arg[0],"hybrid") == 0) {
|
||||
if (firstarg == "hybrid") {
|
||||
if (narg != 3) error->all(FLERR,"Illegal suffix command");
|
||||
lmp->suffix = utils::strdup(arg[1]);
|
||||
lmp->suffix2 = utils::strdup(arg[2]);
|
||||
@ -1897,8 +1867,25 @@ void Input::timer_command()
|
||||
void Input::timestep()
|
||||
{
|
||||
if (narg != 1) error->all(FLERR,"Illegal timestep command");
|
||||
|
||||
update->update_time();
|
||||
update->dt = utils::numeric(FLERR,arg[0],false,lmp);
|
||||
update->dt_default = 0;
|
||||
|
||||
// timestep command can be invoked between runs or by run every
|
||||
// calls to other classes that need to know timestep size changed
|
||||
// similar logic is in FixDtReset::end_of_step()
|
||||
// only need to do this if a run has already occurred
|
||||
|
||||
if (update->first_update == 0) return;
|
||||
|
||||
int respaflag = 0;
|
||||
if (utils::strmatch(update->integrate_style, "^respa")) respaflag = 1;
|
||||
if (respaflag) update->integrate->reset_dt();
|
||||
|
||||
if (force->pair) force->pair->reset_dt();
|
||||
for (auto &ifix : modify->get_fix_list()) ifix->reset_dt();
|
||||
output->reset_dt();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user