7
#include "colvarscript.h"
10
colvarscript::colvarscript(colvarproxy *p)
17
/// Run method based on given arguments
18
int colvarscript::run(int argc, char const *argv[]) {
23
cvm::log("Called script run with " + cvm::to_str(argc) + " args");
24
for (int i = 0; i < argc; i++) { cvm::log(argv[i]); }
28
result = "usage: "+std::string(argv[0])+" <subcommand> [args...]\n\
30
Managing the colvars module:\n\
31
configfile <file name> -- read configuration from a file\n\
32
config <string> -- read configuration from the given string\n\
33
reset -- delete all internal configuration\n\
34
delete -- delete this colvars module instance\n\
35
version -- return version of colvars code\n\
38
list -- return a list of all variables\n\
39
list biases -- return a list of all biases\n\
40
load <file name> -- load a state file (requires configuration)\n\
41
update -- recalculate colvars and biases based\n\
42
printframe -- return a summary of the current frame\n\
43
printframelabels -- return labels to annotate printframe's output\n";
45
if (proxy->frame() != COLVARS_NOT_IMPLEMENTED) {
47
frame -- return current frame number\n\
48
frame <new_frame> -- set frame number\n";
52
Accessing collective variables:\n\
53
colvar <name> value -- return the current value of colvar <name>\n\
54
colvar <name> update -- recalculate colvar <name>\n\
55
colvar <name> type -- return the type of colvar <name>\n\
56
colvar <name> delete -- delete colvar <name>\n\
57
colvar <name> addforce <F> -- apply given force on colvar <name>\n\
58
colvar <name> getconfig -- return config string of colvar <name>\n\
61
bias <name> energy -- return the current energy of bias <name>\n\
62
bias <name> update -- recalculate bias <name>\n\
63
bias <name> delete -- delete bias <name>\n\
64
bias <name> getconfig -- return config string of bias <name>\n\
67
return COLVARSCRIPT_OK;
70
std::string cmd = argv[1];
72
if (cmd == "colvar") {
73
return proc_colvar(argc-1, &(argv[1]));
77
return proc_bias(argc-1, &(argv[1]));
80
if (cmd == "version") {
81
result = COLVARS_VERSION;
82
return COLVARSCRIPT_OK;
86
/// Delete every child object
88
return COLVARSCRIPT_OK;
91
if (cmd == "delete") {
93
// Note: the delete bit may be ignored by some backends
94
// it is mostly useful in VMD
95
colvars->set_error_bits(DELETE_COLVARS);
96
return COLVARSCRIPT_OK;
99
if (cmd == "update") {
101
return COLVARSCRIPT_OK;
106
for (std::vector<colvar *>::iterator cvi = colvars->colvars.begin();
107
cvi != colvars->colvars.end();
109
result += (cvi == colvars->colvars.begin() ? "" : " ") + (*cvi)->name;
111
return COLVARSCRIPT_OK;
112
} else if (argc == 3 && !strcmp(argv[2], "biases")) {
113
for (std::vector<colvarbias *>::iterator bi = colvars->biases.begin();
114
bi != colvars->biases.end();
116
result += (bi == colvars->biases.begin() ? "" : " ") + (*bi)->name;
118
return COLVARSCRIPT_OK;
120
result = "Wrong arguments to command \"list\"";
121
return COLVARSCRIPT_ERROR;
125
/// Parse config from file
126
if (cmd == "configfile") {
128
result = "Missing arguments";
129
return COLVARSCRIPT_ERROR;
131
if (colvars->read_config_file(argv[2]) == COLVARS_OK) {
132
return COLVARSCRIPT_OK;
134
return COLVARSCRIPT_ERROR;
138
/// Parse config from string
139
if (cmd == "config") {
141
result = "Missing arguments";
142
return COLVARSCRIPT_ERROR;
144
std::string conf = argv[2];
145
if (colvars->read_config_string(conf) == COLVARS_OK) {
146
return COLVARSCRIPT_OK;
148
return COLVARSCRIPT_ERROR;
152
/// Load an input state file
155
result = "Missing arguments";
156
return COLVARSCRIPT_ERROR;
158
proxy->input_prefix_str = argv[2];
159
if (colvars->setup_input() == COLVARS_OK) {
160
return COLVARSCRIPT_OK;
162
return COLVARSCRIPT_ERROR;
166
/// TODO Write an output state file? (Useful for testing)
168
/// Print the values that would go on colvars.traj
169
if (cmd == "printframelabels") {
170
std::ostringstream os;
171
colvars->write_traj_label(os);
173
return COLVARSCRIPT_OK;
175
if (cmd == "printframe") {
176
std::ostringstream os;
177
colvars->write_traj(os);
179
return COLVARSCRIPT_OK;
182
if (cmd == "frame") {
184
int f = proxy->frame();
186
result = cvm::to_str(f);
187
return COLVARSCRIPT_OK;
189
result = "Frame number is not available";
190
return COLVARSCRIPT_ERROR;
192
} else if (argc == 3) {
193
// Failure of this function does not trigger an error, but
194
// returns the plain result to let scripts detect available frames
195
long int f = proxy->frame(strtol(argv[2], NULL, 10));
196
colvars->it = proxy->frame();
197
result = cvm::to_str(f);
198
return COLVARSCRIPT_OK;
200
result = "Wrong arguments to command \"frame\"";
201
return COLVARSCRIPT_ERROR;
205
result = "Syntax error";
206
return COLVARSCRIPT_ERROR;
210
int colvarscript::proc_colvar(int argc, char const *argv[]) {
211
std::string name = argv[1];
212
colvar *cv = cvm::colvar_by_name(name);
214
result = "Colvar not found: " + name;
215
return COLVARSCRIPT_ERROR;
218
result = "Missing parameters";
219
return COLVARSCRIPT_ERROR;
221
std::string subcmd = argv[2];
223
if (subcmd == "value") {
224
result = (cv->value()).to_simple_string();
225
return COLVARSCRIPT_OK;
228
if (subcmd == "width") {
229
result = cvm::to_str(cv->width, 0, cvm::cv_prec);
230
return COLVARSCRIPT_OK;
233
if (subcmd == "type") {
234
result = cv->value().type_desc(cv->value().value_type);
235
return COLVARSCRIPT_OK;
238
if (subcmd == "update") {
241
result = (cv->value()).to_simple_string();
242
return COLVARSCRIPT_OK;
245
if (subcmd == "delete") {
246
if (cv->biases.size() > 0) {
247
result = "Cannot delete a colvar currently used by biases, delete those biases first";
248
return COLVARSCRIPT_ERROR;
250
// colvar destructor is tasked with the cleanup
252
// TODO this could be done by the destructors
253
colvars->write_traj_label(colvars->cv_traj_os);
254
return COLVARSCRIPT_OK;
257
if (subcmd == "getconfig") {
258
result = cv->get_config();
259
return COLVARSCRIPT_OK;
262
if (subcmd == "addforce") {
264
result = "addforce: missing parameter: force value";
265
return COLVARSCRIPT_ERROR;
267
std::string f_str = argv[3];
268
std::istringstream is(f_str);
269
is.width(cvm::cv_width);
270
is.precision(cvm::cv_prec);
271
colvarvalue force(cv->value());
272
force.is_derivative();
273
if (force.from_simple_string(is.str()) != COLVARS_OK) {
274
result = "addforce : error parsing force value";
275
return COLVARSCRIPT_ERROR;
277
cv->add_bias_force(force);
278
result = force.to_simple_string();
279
return COLVARSCRIPT_OK;
282
result = "Syntax error";
283
return COLVARSCRIPT_ERROR;
287
int colvarscript::proc_bias(int argc, char const *argv[]) {
288
std::string name = argv[1];
289
colvarbias *b = cvm::bias_by_name(name);
291
result = "Bias not found: " + name;
292
return COLVARSCRIPT_ERROR;
296
result = "Missing parameters";
297
return COLVARSCRIPT_ERROR;
299
std::string subcmd = argv[2];
301
if (subcmd == "energy") {
302
result = cvm::to_str(b->get_energy());
303
return COLVARSCRIPT_OK;
306
if (subcmd == "update") {
308
result = cvm::to_str(b->get_energy());
309
return COLVARSCRIPT_OK;
312
if (subcmd == "getconfig") {
313
result = b->get_config();
314
return COLVARSCRIPT_OK;
317
// Subcommands for MW ABF
318
if (subcmd == "bin") {
319
int r = b->current_bin();
320
result = cvm::to_str(r);
321
return COLVARSCRIPT_OK;
324
if (subcmd == "binnum") {
325
int r = b->bin_num();
327
result = "Error: calling bin_num() for bias " + b->name;
328
return COLVARSCRIPT_ERROR;
330
result = cvm::to_str(r);
331
return COLVARSCRIPT_OK;
334
if (subcmd == "share") {
335
int r = b->replica_share();
337
result = "Error: calling replica_share() for bias " + b->name;
338
return COLVARSCRIPT_ERROR;
340
result = cvm::to_str(r);
341
return COLVARSCRIPT_OK;
343
// End commands for MW ABF
345
if (subcmd == "delete") {
346
// the bias destructor takes care of the cleanup at cvm level
348
// TODO this could be done by the destructors
349
colvars->write_traj_label(colvars->cv_traj_os);
350
return COLVARSCRIPT_OK;
354
std::string param = argv[3];
355
if (subcmd == "count") {
357
if (!(std::istringstream(param) >> index)) {
358
result = "bin_count: error parsing bin index";
359
return COLVARSCRIPT_ERROR;
361
result = cvm::to_str(b->bin_count(index));
362
return COLVARSCRIPT_OK;
365
result = "Syntax error";
366
return COLVARSCRIPT_ERROR;
368
result = "Syntax error";
369
return COLVARSCRIPT_ERROR;