From 974e1eb022534ee62afe8eabbfa623a9e12fca52 Mon Sep 17 00:00:00 2001 From: sjplimp Date: Mon, 6 Apr 2015 15:14:53 +0000 Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@13396 f3b2605a-c512-4ea7-a41b-209d697bcdaa --- lib/colvars/colvarmodule.cpp | 21 ++++-- lib/colvars/colvarmodule.h | 2 +- lib/colvars/colvarscript.cpp | 127 +++++++++++++++++++---------------- lib/colvars/colvarscript.h | 4 ++ 4 files changed, 89 insertions(+), 65 deletions(-) diff --git a/lib/colvars/colvarmodule.cpp b/lib/colvars/colvarmodule.cpp index 372cc3b402..e91ed1e6cc 100644 --- a/lib/colvars/colvarmodule.cpp +++ b/lib/colvars/colvarmodule.cpp @@ -598,7 +598,7 @@ int colvarmodule::calc() { write_traj(cv_traj_os); } - if (restart_out_freq) { + if (restart_out_freq && cv_traj_os.is_open()) { // flush the trajectory file if we are at the restart frequency if ( (cvm::step_relative() > 0) && ((cvm::step_absolute() % restart_out_freq) == 0) ) { @@ -683,8 +683,10 @@ int colvarmodule::reset() index_groups.clear(); index_group_names.clear(); - // Do not close file here, as we might not be done with it yet. - cv_traj_os.flush(); + if (cv_traj_os.is_open()) { + // Do not close file here, as we might not be done with it yet. + cv_traj_os.flush(); + } return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK); } @@ -826,8 +828,11 @@ int colvarmodule::write_output_files() } cvm::decrease_depth(); - // do not close to avoid problems with multiple NAMD runs - cv_traj_os.flush(); + if (cv_traj_os.is_open()) { + // do not close to avoid problems with multiple NAMD runs + cv_traj_os.flush(); + } + return (cvm::get_error() ? COLVARS_ERROR : COLVARS_OK); } @@ -985,8 +990,9 @@ std::ostream & colvarmodule::write_traj_label(std::ostream &os) (*bi)->write_traj_label(os); } os << "\n"; - if (cvm::debug()) + if (cvm::debug()) { os.flush(); + } cvm::decrease_depth(); return os; } @@ -1010,8 +1016,9 @@ std::ostream & colvarmodule::write_traj(std::ostream &os) (*bi)->write_traj(os); } os << "\n"; - if (cvm::debug()) + if (cvm::debug()) { os.flush(); + } cvm::decrease_depth(); return os; } diff --git a/lib/colvars/colvarmodule.h b/lib/colvars/colvarmodule.h index d2fcb998de..07134c2db5 100644 --- a/lib/colvars/colvarmodule.h +++ b/lib/colvars/colvarmodule.h @@ -4,7 +4,7 @@ #define COLVARMODULE_H #ifndef COLVARS_VERSION -#define COLVARS_VERSION "2015-02-27" +#define COLVARS_VERSION "2015-03-15" #endif #ifndef COLVARS_DEBUG diff --git a/lib/colvars/colvarscript.cpp b/lib/colvars/colvarscript.cpp index 037f284b90..b985ea5d2d 100644 --- a/lib/colvars/colvarscript.cpp +++ b/lib/colvars/colvarscript.cpp @@ -25,45 +25,7 @@ int colvarscript::run(int argc, char const *argv[]) { } if (argc < 2) { - result = "usage: "+std::string(argv[0])+" [args...]\n\ -\n\ -Managing the colvars module:\n\ - configfile -- read configuration from a file\n\ - config -- read configuration from the given string\n\ - reset -- delete all internal configuration\n\ - delete -- delete this colvars module instance\n\ - version -- return version of colvars code\n\ - \n\ -Input and output:\n\ - list -- return a list of all variables\n\ - list biases -- return a list of all biases\n\ - load -- load a state file (requires configuration)\n\ - update -- recalculate colvars and biases based\n\ - printframe -- return a summary of the current frame\n\ - printframelabels -- return labels to annotate printframe's output\n"; - - if (proxy->frame() != COLVARS_NOT_IMPLEMENTED) { - result += "\ - frame -- return current frame number\n\ - frame -- set frame number\n"; - } - - result += "\n\ -Accessing collective variables:\n\ - colvar value -- return the current value of colvar \n\ - colvar update -- recalculate colvar \n\ - colvar type -- return the type of colvar \n\ - colvar delete -- delete colvar \n\ - colvar addforce -- apply given force on colvar \n\ - colvar getconfig -- return config string of colvar \n\ -\n\ -Accessing biases:\n\ - bias energy -- return the current energy of bias \n\ - bias update -- recalculate bias \n\ - bias delete -- delete bias \n\ - bias getconfig -- return config string of bias \n\ -\n\ -"; + result = help_string(); return COLVARSCRIPT_OK; } @@ -117,7 +79,7 @@ Accessing biases:\n\ } return COLVARSCRIPT_OK; } else { - result = "Wrong arguments to command \"list\""; + result = "Wrong arguments to command \"list\"\n" + help_string(); return COLVARSCRIPT_ERROR; } } @@ -125,12 +87,13 @@ Accessing biases:\n\ /// Parse config from file if (cmd == "configfile") { if (argc < 3) { - result = "Missing arguments"; + result = "Missing arguments\n" + help_string(); return COLVARSCRIPT_ERROR; } if (colvars->read_config_file(argv[2]) == COLVARS_OK) { return COLVARSCRIPT_OK; } else { + result = "Error parsing configuration file"; return COLVARSCRIPT_ERROR; } } @@ -138,13 +101,14 @@ Accessing biases:\n\ /// Parse config from string if (cmd == "config") { if (argc < 3) { - result = "Missing arguments"; + result = "Missing arguments\n" + help_string(); return COLVARSCRIPT_ERROR; } std::string conf = argv[2]; if (colvars->read_config_string(conf) == COLVARS_OK) { return COLVARSCRIPT_OK; } else { + result = "Error parsing configuration string"; return COLVARSCRIPT_ERROR; } } @@ -152,13 +116,14 @@ Accessing biases:\n\ /// Load an input state file if (cmd == "load") { if (argc < 3) { - result = "Missing arguments"; + result = "Missing arguments\n" + help_string(); return COLVARSCRIPT_ERROR; } proxy->input_prefix_str = argv[2]; if (colvars->setup_input() == COLVARS_OK) { return COLVARSCRIPT_OK; } else { + result = "Error loading state file"; return COLVARSCRIPT_ERROR; } } @@ -197,27 +162,28 @@ Accessing biases:\n\ result = cvm::to_str(f); return COLVARSCRIPT_OK; } else { - result = "Wrong arguments to command \"frame\""; + result = "Wrong arguments to command \"frame\"\n" + help_string(); return COLVARSCRIPT_ERROR; } } - result = "Syntax error"; + result = "Syntax error\n" + help_string(); return COLVARSCRIPT_ERROR; } int colvarscript::proc_colvar(int argc, char const *argv[]) { + if (argc < 3) { + result = "Missing parameters\n" + help_string(); + return COLVARSCRIPT_ERROR; + } + std::string name = argv[1]; colvar *cv = cvm::colvar_by_name(name); if (cv == NULL) { result = "Colvar not found: " + name; return COLVARSCRIPT_ERROR; } - if (argc < 3) { - result = "Missing parameters"; - return COLVARSCRIPT_ERROR; - } std::string subcmd = argv[2]; if (subcmd == "value") { @@ -261,7 +227,7 @@ int colvarscript::proc_colvar(int argc, char const *argv[]) { if (subcmd == "addforce") { if (argc < 4) { - result = "addforce: missing parameter: force value"; + result = "addforce: missing parameter: force value\n" + help_string(); return COLVARSCRIPT_ERROR; } std::string f_str = argv[3]; @@ -279,12 +245,17 @@ int colvarscript::proc_colvar(int argc, char const *argv[]) { return COLVARSCRIPT_OK; } - result = "Syntax error"; + result = "Syntax error\n" + help_string(); return COLVARSCRIPT_ERROR; } int colvarscript::proc_bias(int argc, char const *argv[]) { + if (argc < 3) { + result = "Missing parameters\n" + help_string(); + return COLVARSCRIPT_ERROR; + } + std::string name = argv[1]; colvarbias *b = cvm::bias_by_name(name); if (b == NULL) { @@ -292,10 +263,6 @@ int colvarscript::proc_bias(int argc, char const *argv[]) { return COLVARSCRIPT_ERROR; } - if (argc < 3) { - result = "Missing parameters"; - return COLVARSCRIPT_ERROR; - } std::string subcmd = argv[2]; if (subcmd == "energy") { @@ -362,9 +329,55 @@ int colvarscript::proc_bias(int argc, char const *argv[]) { return COLVARSCRIPT_OK; } - result = "Syntax error"; + result = "Syntax error\n" + help_string(); return COLVARSCRIPT_ERROR; } - result = "Syntax error"; + + result = "Syntax error\n" + help_string(); return COLVARSCRIPT_ERROR; } + + +std::string colvarscript::help_string() +{ + std::string buf; + buf = "Usage: cv [args...]\n\ +\n\ +Managing the colvars module:\n\ + configfile -- read configuration from a file\n\ + config -- read configuration from the given string\n\ + reset -- delete all internal configuration\n\ + delete -- delete this colvars module instance\n\ + version -- return version of colvars code\n\ + \n\ +Input and output:\n\ + list -- return a list of all variables\n\ + list biases -- return a list of all biases\n\ + load -- load a state file (requires configuration)\n\ + update -- recalculate colvars and biases based\n\ + printframe -- return a summary of the current frame\n\ + printframelabels -- return labels to annotate printframe's output\n"; + + if (proxy->frame() != COLVARS_NOT_IMPLEMENTED) { + buf += "\ + frame -- return current frame number\n\ + frame -- set frame number\n"; + } + + buf += "\n\ +Accessing collective variables:\n\ + colvar value -- return the current value of colvar \n\ + colvar update -- recalculate colvar \n\ + colvar type -- return the type of colvar \n\ + colvar delete -- delete colvar \n\ + colvar addforce -- apply given force on colvar \n\ + colvar getconfig -- return config string of colvar \n\ +\n\ +Accessing biases:\n\ + bias energy -- return the current energy of bias \n\ + bias update -- recalculate bias \n\ + bias delete -- delete bias \n\ + bias getconfig -- return config string of bias \n"; + + return buf; +} diff --git a/lib/colvars/colvarscript.h b/lib/colvars/colvarscript.h index 8057802edd..745914c76b 100644 --- a/lib/colvars/colvarscript.h +++ b/lib/colvars/colvarscript.h @@ -36,11 +36,15 @@ public: /// Run script command with given positional arguments int run(int argc, char const *argv[]); +private: /// Run subcommands on colvar int proc_colvar(int argc, char const *argv[]); /// Run subcommands on bias int proc_bias(int argc, char const *argv[]); + + /// Builds and return a short help + std::string help_string(void); };