version 1.9 | version 1.10 |
---|
| |
#include <string.h> | #include <string.h> |
| |
#include "colvarscript.h" | #include "colvarscript.h" |
| #include "colvarproxy.h" |
#include "colvardeps.h" | #include "colvardeps.h" |
| |
| |
| |
| |
// Generic hooks; NAMD and VMD have Tcl-specific versions in the respective proxies | // Generic hooks; NAMD and VMD have Tcl-specific versions in the respective proxies |
| |
int run_colvarscript_command(int argc, const char **argv) | int run_colvarscript_command(int objc, unsigned char *const objv[]) |
{ | { |
colvarproxy *cvp = cvm::proxy; | colvarproxy *cvp = cvm::proxy; |
if (!cvp) { | if (!cvp) { |
| |
cvm::error("Called run_colvarscript_command without a script object initialized.\n"); | cvm::error("Called run_colvarscript_command without a script object initialized.\n"); |
return -1; | return -1; |
} | } |
return cvp->script->run(argc, argv); | return cvp->script->run(objc, objv); |
} | } |
| |
const char * get_colvarscript_result() | const char * get_colvarscript_result() |
| |
| |
| |
/// Run method based on given arguments | /// Run method based on given arguments |
int colvarscript::run(int argc, char const *argv[]) { | int colvarscript::run(int objc, unsigned char *const objv[]) |
| { |
result = ""; | result.clear(); |
| |
if (cvm::debug()) { | if (cvm::debug()) { |
cvm::log("Called script run with " + cvm::to_str(argc) + " args"); | cvm::log("Called script run with " + cvm::to_str(objc) + " args:"); |
for (int i = 0; i < argc; i++) { cvm::log(argv[i]); } | for (int i = 0; i < objc; i++) { |
| cvm::log(obj_to_str(objv[i])); |
| } |
} | } |
| |
if (argc < 2) { | if (objc < 2) { |
result = help_string(); | result = help_string(); |
return COLVARS_OK; | return COLVARS_OK; |
} | } |
| |
std::string cmd = argv[1]; | std::string const cmd(obj_to_str(objv[1])); |
| |
int error_code = COLVARS_OK; | int error_code = COLVARS_OK; |
| |
if (cmd == "colvar") { | if (cmd == "colvar") { |
return proc_colvar(argc-1, &(argv[1])); | if (objc < 3) { |
| result = "Missing parameters\n" + help_string(); |
| return COLVARSCRIPT_ERROR; |
| } |
| std::string const name(obj_to_str(objv[2])); |
| colvar *cv = cvm::colvar_by_name(name); |
| if (cv == NULL) { |
| result = "Colvar not found: " + name; |
| return COLVARSCRIPT_ERROR; |
| } |
| return proc_colvar(cv, objc-1, &(objv[1])); |
} | } |
| |
if (cmd == "bias") { | if (cmd == "bias") { |
return proc_bias(argc-1, &(argv[1])); | if (objc < 3) { |
| result = "Missing parameters\n" + help_string(); |
| return COLVARSCRIPT_ERROR; |
| } |
| std::string const name(obj_to_str(objv[2])); |
| colvarbias *b = cvm::bias_by_name(name); |
| if (b == NULL) { |
| result = "Bias not found: " + name; |
| return COLVARSCRIPT_ERROR; |
| } |
| return proc_bias(b, objc-1, &(objv[1])); |
} | } |
| |
if (cmd == "version") { | if (cmd == "version") { |
| |
error_code |= colvars->calc(); | error_code |= colvars->calc(); |
error_code |= proxy->update_output(); | error_code |= proxy->update_output(); |
if (error_code) { | if (error_code) { |
result += "Error updating the colvars module.\n"; | result += "Error updating the Colvars module.\n"; |
} | } |
return error_code; | return error_code; |
} | } |
| |
if (cmd == "list") { | if (cmd == "list") { |
if (argc == 2) { | if (objc == 2) { |
for (std::vector<colvar *>::iterator cvi = colvars->colvars.begin(); | for (std::vector<colvar *>::iterator cvi = colvars->colvars.begin(); |
cvi != colvars->colvars.end(); | cvi != colvars->colvars.end(); |
++cvi) { | ++cvi) { |
result += (cvi == colvars->colvars.begin() ? "" : " ") + (*cvi)->name; | result += (cvi == colvars->colvars.begin() ? "" : " ") + (*cvi)->name; |
} | } |
return COLVARS_OK; | return COLVARS_OK; |
} else if (argc == 3 && !strcmp(argv[2], "biases")) { | } else if (objc == 3 && !strcmp(obj_to_str(objv[2]), "biases")) { |
for (std::vector<colvarbias *>::iterator bi = colvars->biases.begin(); | for (std::vector<colvarbias *>::iterator bi = colvars->biases.begin(); |
bi != colvars->biases.end(); | bi != colvars->biases.end(); |
++bi) { | ++bi) { |
| |
| |
/// Parse config from file | /// Parse config from file |
if (cmd == "configfile") { | if (cmd == "configfile") { |
if (argc < 3) { | if (objc < 3) { |
result = "Missing arguments\n" + help_string(); | result = "Missing arguments\n" + help_string(); |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
if (colvars->read_config_file(argv[2]) == COLVARS_OK) { | if (colvars->read_config_file(obj_to_str(objv[2])) == COLVARS_OK) { |
return COLVARS_OK; | return COLVARS_OK; |
} else { | } else { |
result = "Error parsing configuration file"; | result = "Error parsing configuration file"; |
| |
| |
/// Parse config from string | /// Parse config from string |
if (cmd == "config") { | if (cmd == "config") { |
if (argc < 3) { | if (objc < 3) { |
result = "Missing arguments\n" + help_string(); | result = "Missing arguments\n" + help_string(); |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
std::string conf = argv[2]; | std::string const conf(obj_to_str(objv[2])); |
if (colvars->read_config_string(conf) == COLVARS_OK) { | if (colvars->read_config_string(conf) == COLVARS_OK) { |
return COLVARS_OK; | return COLVARS_OK; |
} else { | } else { |
| |
| |
/// Load an input state file | /// Load an input state file |
if (cmd == "load") { | if (cmd == "load") { |
if (argc < 3) { | if (objc < 3) { |
result = "Missing arguments\n" + help_string(); | result = "Missing arguments\n" + help_string(); |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
proxy->input_prefix() = argv[2]; | proxy->input_prefix() = obj_to_str(objv[2]); |
if (colvars->setup_input() == COLVARS_OK) { | if (colvars->setup_input() == COLVARS_OK) { |
return COLVARS_OK; | return COLVARS_OK; |
} else { | } else { |
| |
| |
/// Save to an output state file | /// Save to an output state file |
if (cmd == "save") { | if (cmd == "save") { |
if (argc < 3) { | if (objc < 3) { |
result = "Missing arguments"; | result = "Missing arguments"; |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
proxy->output_prefix_str = argv[2]; | proxy->output_prefix_str = obj_to_str(objv[2]); |
int error = 0; | int error = 0; |
error |= colvars->setup_output(); | error |= colvars->setup_output(); |
error |= colvars->write_output_files(); | error |= colvars->write_output_files(); |
| |
} | } |
| |
if (cmd == "frame") { | if (cmd == "frame") { |
if (argc == 2) { | if (objc == 2) { |
long int f; | long int f; |
int error = proxy->get_frame(f); | int error = proxy->get_frame(f); |
if (error == COLVARS_OK) { | if (error == COLVARS_OK) { |
| |
result = "Frame number is not available"; | result = "Frame number is not available"; |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
} else if (argc == 3) { | } else if (objc == 3) { |
// Failure of this function does not trigger an error, but | // Failure of this function does not trigger an error, but |
// returns nonzero, to let scripts detect available frames | // returns nonzero, to let scripts detect available frames |
int error = proxy->set_frame(strtol(argv[2], NULL, 10)); | int error = proxy->set_frame(strtol(obj_to_str(objv[2]), NULL, 10)); |
result = cvm::to_str(error == COLVARS_OK ? 0 : -1); | result = cvm::to_str(error == COLVARS_OK ? 0 : -1); |
return COLVARS_OK; | return COLVARS_OK; |
} else { | } else { |
| |
} | } |
| |
if (cmd == "addenergy") { | if (cmd == "addenergy") { |
if (argc == 3) { | if (objc == 3) { |
colvars->total_bias_energy += strtod(argv[2], NULL); | colvars->total_bias_energy += strtod(obj_to_str(objv[2]), NULL); |
return COLVARS_OK; | return COLVARS_OK; |
} else { | } else { |
result = "Wrong arguments to command \"addenergy\"\n" + help_string(); | result = "Wrong arguments to command \"addenergy\"\n" + help_string(); |
| |
} | } |
| |
| |
int colvarscript::proc_colvar(int argc, char const *argv[]) { | int colvarscript::proc_colvar(colvar *cv, int objc, unsigned char *const objv[]) { |
if (argc < 3) { | |
result = "Missing parameters\n" + help_string(); | |
return COLVARSCRIPT_ERROR; | |
} | |
| |
std::string name = argv[1]; | std::string const subcmd(obj_to_str(objv[2])); |
colvar *cv = cvm::colvar_by_name(name); | |
if (cv == NULL) { | |
result = "Colvar not found: " + name; | |
return COLVARSCRIPT_ERROR; | |
} | |
std::string subcmd = argv[2]; | |
| |
if (subcmd == "value") { | if (subcmd == "value") { |
result = (cv->value()).to_simple_string(); | result = (cv->value()).to_simple_string(); |
| |
} | } |
| |
if (subcmd == "addforce") { | if (subcmd == "addforce") { |
if (argc < 4) { | if (objc < 4) { |
result = "addforce: missing parameter: force value\n" + help_string(); | result = "addforce: missing parameter: force value\n" + help_string(); |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
std::string f_str = argv[3]; | std::string const f_str(obj_to_str(objv[3])); |
std::istringstream is(f_str); | std::istringstream is(f_str); |
is.width(cvm::cv_width); | is.width(cvm::cv_width); |
is.precision(cvm::cv_prec); | is.precision(cvm::cv_prec); |
| |
} | } |
| |
if (subcmd == "cvcflags") { | if (subcmd == "cvcflags") { |
if (argc < 4) { | if (objc < 4) { |
result = "cvcflags: missing parameter: vector of flags"; | result = "cvcflags: missing parameter: vector of flags"; |
return COLVARSCRIPT_ERROR; | return COLVARSCRIPT_ERROR; |
} | } |
std::string flags_str = argv[3]; | std::string const flags_str(obj_to_str(objv[3])); |
std::istringstream is(flags_str); | std::istringstream is(flags_str); |
std::vector<bool> flags; | std::vector<bool> flags; |
| |
| |
} | } |
| |
if ((subcmd == "get") || (subcmd == "set") || (subcmd == "state")) { | if ((subcmd == "get") || (subcmd == "set") || (subcmd == "state")) { |
return proc_features(cv, argc, argv); | return proc_features(cv, objc, objv); |
} | } |
| |
result = "Syntax error\n" + help_string(); | result = "Syntax error\n" + help_string(); |
| |
} | } |
| |
| |
int colvarscript::proc_bias(int argc, char const *argv[]) { | int colvarscript::proc_bias(colvarbias *b, int objc, unsigned char *const objv[]) { |
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) { | |
result = "Bias not found: " + name; | |
return COLVARSCRIPT_ERROR; | |
} | |
| |
std::string subcmd = argv[2]; | std::string const key(obj_to_str(objv[0])); |
| std::string const subcmd(obj_to_str(objv[2])); |
| |
if (subcmd == "energy") { | if (subcmd == "energy") { |
result = cvm::to_str(b->get_energy()); | result = cvm::to_str(b->get_energy()); |
| |
} | } |
| |
if ((subcmd == "get") || (subcmd == "set") || (subcmd == "state")) { | if ((subcmd == "get") || (subcmd == "set") || (subcmd == "state")) { |
return proc_features(b, argc, argv); | return proc_features(b, objc, objv); |
} | } |
| |
if (argc >= 4) { | if (objc >= 4) { |
std::string param = argv[3]; | std::string const param(obj_to_str(objv[3])); |
if (subcmd == "count") { | if (subcmd == "count") { |
int index; | int index; |
if (!(std::istringstream(param) >> index)) { | if (!(std::istringstream(param) >> index)) { |
| |
| |
| |
int colvarscript::proc_features(colvardeps *obj, | int colvarscript::proc_features(colvardeps *obj, |
int argc, char const *argv[]) { | int objc, unsigned char *const objv[]) { |
// size was already checked before calling | // size was already checked before calling |
std::string subcmd = argv[2]; | std::string const subcmd(obj_to_str(objv[2])); |
| |
if (argc == 3) { | if (objc == 3) { |
if (subcmd == "state") { | if (subcmd == "state") { |
// TODO make this returned as result? | // TODO make this returned as result? |
obj->print_state(); | obj->print_state(); |
| |
| |
if ((subcmd == "get") || (subcmd == "set")) { | if ((subcmd == "get") || (subcmd == "set")) { |
std::vector<colvardeps::feature *> &features = obj->features(); | std::vector<colvardeps::feature *> &features = obj->features(); |
std::string const req_feature(argv[3]); | std::string const req_feature(obj_to_str(objv[3])); |
colvardeps::feature *f = NULL; | colvardeps::feature *f = NULL; |
int fid = 0; | int fid = 0; |
for (fid = 0; fid < int(features.size()); fid++) { | for (fid = 0; fid < int(features.size()); fid++) { |
| |
} | } |
| |
if (subcmd == "set") { | if (subcmd == "set") { |
if (argc == 5) { | if (objc == 5) { |
std::string const yesno = | std::string const yesno = |
colvarparse::to_lower_cppstr(std::string(argv[4])); | colvarparse::to_lower_cppstr(std::string(obj_to_str(objv[4]))); |
if ((yesno == std::string("yes")) || | if ((yesno == std::string("yes")) || |
(yesno == std::string("on")) || | (yesno == std::string("on")) || |
(yesno == std::string("1"))) { | (yesno == std::string("1"))) { |
| |
std::string buf; | std::string buf; |
buf = "Usage: cv <subcommand> [args...]\n\ | buf = "Usage: cv <subcommand> [args...]\n\ |
\n\ | \n\ |
Managing the colvars module:\n\ | Managing the Colvars module:\n\ |
configfile <file name> -- read configuration from a file\n\ | configfile <file name> -- read configuration from a file\n\ |
config <string> -- read configuration from the given string\n\ | config <string> -- read configuration from the given string\n\ |
reset -- delete all internal configuration\n\ | reset -- delete all internal configuration\n\ |
delete -- delete this colvars module instance\n\ | delete -- delete this Colvars module instance\n\ |
version -- return version of colvars code\n\ | version -- return version of colvars code\n\ |
\n\ | \n\ |
Input and output:\n\ | Input and output:\n\ |