10#ifndef COLVARSCRIPT_COMMANDS_H
11#define COLVARSCRIPT_COMMANDS_H
43#ifndef CVSCRIPT_COMM_FNAME
44#define CVSCRIPT_COMM_FNAME(COMM) cvscript_ ## COMM
50#define CVSCRIPT_COMM_PROTO(COMM) \
51 extern "C" int CVSCRIPT_COMM_FNAME(COMM)(void *, \
53 unsigned char *const *);
55#define CVSCRIPT(COMM,HELP,N_ARGS_MIN,N_ARGS_MAX,ARGS,FN_BODY) \
56 CVSCRIPT_COMM_PROTO(COMM)
63 int cvscript_n_commands();
66 char const ** cvscript_command_names();
70 char const *cvscript_command_help(
char const *cmd);
74 char const *cvscript_command_rethelp(
char const *cmd);
81 char const *cvscript_command_arghelp(
char const *cmd,
int i);
85 char const *cvscript_command_full_help(
char const *cmd);
89 int cvscript_command_n_args_min(
char const *cmd);
93 int cvscript_command_n_args_max(
char const *cmd);
100CVSCRIPT(cv_addenergy,
101 "Add an energy to the MD engine (no effect in VMD)",
103 "E : float - Amount of energy to add",
105 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
107 return cvm::get_error();
111 "Prefix for bias-specific commands",
119 "Prefix for colvar-specific commands",
127 "Read configuration from the given string",
129 "conf : string - Configuration string",
130 char const *conf_str =
131 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
132 std::string
const conf(conf_str);
133 script->proxy()->add_config(
"config", conf);
134 if (script->proxy()->engine_ready()) {
136 if ((script->proxy()->parse_module_config() |
137 script->proxy()->setup()) == COLVARS_OK) {
140 script->add_error_msg(
"Error parsing configuration string");
141 return COLVARSCRIPT_ERROR;
148CVSCRIPT(cv_configfile,
149 "Read configuration from a file",
151 "conf_file : string - Path to configuration file",
152 char const *conf_file_name =
153 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
154 script->proxy()->add_config(
"configfile", std::string(conf_file_name));
155 if (script->proxy()->engine_ready()) {
157 if ((script->proxy()->parse_module_config() |
158 script->proxy()->setup()) == COLVARS_OK) {
161 script->add_error_msg(
"Error parsing configuration file");
162 return COLVARSCRIPT_ERROR;
170 "Delete this Colvars module instance (VMD only)",
173 return script->proxy()->request_deletion();
176CVSCRIPT(cv_featurereport,
177 "Return a summary of Colvars features used so far and their citations\n"
178 "report : string - Feature report and citations",
181 return script->set_result_str(script->module()->feature_report());
185 "Get or set current frame number (VMD only)\n"
186 "frame : integer - Frame number",
188 "frame : integer - Frame number",
190 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
193 if (script->proxy()->get_frame(f) == COLVARS_OK) {
194 script->set_result_long_int(f);
197 script->add_error_msg(
"Frame number is not available");
198 return COLVARSCRIPT_ERROR;
201 int const f = strtol(
const_cast<char *
>(arg), NULL, 10);
202 int error_code = script->proxy()->set_frame(f);
203 if (error_code == COLVARS_NO_SUCH_FRAME) {
204 script->add_error_msg(
"Invalid frame number: \""+std::string(arg)+
212CVSCRIPT(cv_getatomappliedforces,
213 "Get the list of forces applied by Colvars to atoms\n"
214 "forces : array of arrays of floats - Atomic forces",
217 script->set_result_rvector_vec(*(script->proxy()->get_atom_applied_forces()));
221CVSCRIPT(cv_getatomappliedforcesmax,
222 "Get the maximum norm of forces applied by Colvars to atoms\n"
223 "force : float - Maximum atomic force",
226 script->set_result_real(script->proxy()->max_atoms_applied_force());
230CVSCRIPT(cv_getatomappliedforcesmaxid,
231 "Get the atom ID with the largest applied force\n"
232 "id : int - ID of the atom with the maximum atomic force",
235 script->set_result_int(script->proxy()->max_atoms_applied_force_id());
239CVSCRIPT(cv_getatomappliedforcesrms,
240 "Get the root-mean-square norm of forces applied by Colvars to atoms\n"
241 "force : float - RMS atomic force",
244 script->set_result_real(script->proxy()->rms_atoms_applied_force());
248CVSCRIPT(cv_resetatomappliedforces,
249 "Reset forces applied by Colvars to atoms",
253 std::vector<cvm::rvector> *f = script->proxy()->modify_atom_applied_forces();
254 for (i = 0; i < f->size(); i++) {
260CVSCRIPT(cv_getatomids,
261 "Get the list of indices of atoms used in Colvars\n"
262 "indices : array of ints - Atom indices",
265 script->set_result_int_vec(*(script->proxy()->get_atom_ids()));
269CVSCRIPT(cv_getatomcharges,
270 "Get the list of charges of atoms used in Colvars\n"
271 "charges : array of floats - Atomic charges",
274 script->set_result_real_vec(*(script->proxy()->get_atom_charges()));
278CVSCRIPT(cv_getatommasses,
279 "Get the list of masses of atoms used in Colvars\n"
280 "masses : array of floats - Atomic masses",
283 script->set_result_real_vec(*(script->proxy()->get_atom_masses()));
287CVSCRIPT(cv_getatompositions,
288 "Get the list of cached positions of atoms used in Colvars\n"
289 "positions : array of arrays of floats - Atomic positions",
292 script->set_result_rvector_vec(*(script->proxy()->get_atom_positions()));
296CVSCRIPT(cv_getatomtotalforces,
297 "Get the list of cached total forces of atoms used in Colvars\n"
298 "forces : array of arrays of floats - Atomic total foces",
301 script->set_result_rvector_vec(*(script->proxy()->get_atom_total_forces()));
305CVSCRIPT(cv_getconfig,
306 "Get the module's configuration string read so far\n"
307 "conf : string - Current configuration string",
314CVSCRIPT(cv_getenergy,
315 "Get the current Colvars energy\n"
316 "E : float - Amount of energy (internal units)",
323CVSCRIPT(cv_getnumactiveatomgroups,
324 "Get the number of atom groups that currently have positive ref counts\n"
325 "count : integer - Total number of atom groups",
328 script->set_result_int(
static_cast<int>(script->proxy()->get_num_active_atom_groups()));
332CVSCRIPT(cv_getnumactiveatoms,
333 "Get the number of atoms that currently have positive ref counts\n"
334 "count : integer - Total number of atoms",
337 script->set_result_int(
static_cast<int>(script->proxy()->get_num_active_atoms()));
341CVSCRIPT(cv_getnumatoms,
342 "Get the number of requested atoms, including those not in use now\n"
343 "count : integer - Total number of atoms",
346 script->set_result_int(
static_cast<int>(script->proxy()->get_atom_ids()->size()));
350CVSCRIPT(cv_getstepabsolute,
351 "Get the current step number of the simulation (including restarts)\n"
352 "step : int - Absolute step number",
359CVSCRIPT(cv_getsteprelative,
360 "Get the current step number from the start of this job\n"
361 "step : int - Relative step number",
369 "Get the help string of the Colvars scripting interface\n"
370 "help : string - Help string",
372 "command : string - Get the help string of this specific command",
373 unsigned char *
const cmdobj =
374 script->get_module_cmd_arg(0, objc, objv);
376 std::string
const cmdstr(script->obj_to_str(cmdobj));
378 if (cmdstr == std::string(
"colvar")) {
379 script->set_result_str(script->get_cmdline_help_summary(colvarscript::use_colvar));
380 }
else if (cmdstr == std::string(
"bias")) {
381 script->set_result_str(script->get_cmdline_help_summary(colvarscript::use_bias));
383 script->set_result_str(script->get_command_cmdline_help(colvarscript::use_module,
386 return cvm::get_error();
388 return COLVARSCRIPT_ERROR;
391 script->set_result_str(script->get_cmdline_help_summary(colvarscript::use_module));
396CVSCRIPT(cv_languageversion,
397 "Get the C++ language version number\n"
398 "version : string - C++ language version",
401 script->set_result_int(__cplusplus);
406 "Return a list of all variables or biases\n"
407 "list : sequence of strings - List of elements",
409 "param : string - \"colvars\" or \"biases\"; default is \"colvars\"",
411 unsigned char *
const kwarg = script->get_module_cmd_arg(0, objc, objv);
412 std::string
const kwstr = kwarg ? script->obj_to_str(kwarg) :
413 std::string(
"colvars");
414 if (kwstr ==
"colvars") {
415 for (std::vector<colvar *>::iterator cvi = script->module()->variables()->begin();
416 cvi != script->module()->variables()->end();
418 res += (cvi == script->module()->variables()->begin() ?
"" :
" ") + (*cvi)->name;
420 script->set_result_str(res);
422 }
else if (kwstr ==
"biases") {
423 for (std::vector<colvarbias *>::iterator bi = script->module()->biases.begin();
424 bi != script->module()->biases.end();
426 res += (bi == script->module()->biases.begin() ?
"" :
" ") + (*bi)->name;
428 script->set_result_str(res);
431 script->add_error_msg(
"Wrong arguments to command \"list\"\n");
432 return COLVARSCRIPT_ERROR;
436CVSCRIPT(cv_listcommands,
437 "Get the list of script functions, prefixed with \"cv_\", \"colvar_\" or \"bias_\"\n"
438 "list : sequence of strings - List of commands",
441 int const n_commands = cvscript_n_commands();
442 char const **command_names = cvscript_command_names();
444 for (
int i = 0; i < n_commands; i++) {
445 if (i > 0) result.append(1,
' ');
446 result.append(std::string(command_names[i]));
448 script->set_result_str(result);
452CVSCRIPT(cv_listindexfiles,
453 "Get a list of the index files loaded in this session\n"
454 "list : sequence of strings - List of index file names",
457 int const n_files = script->module()->index_file_names.size();
459 for (
int i = 0; i < n_files; i++) {
460 if (i > 0) result.append(1,
' ');
461 result.append(script->module()->index_file_names[i]);
463 script->set_result_str(result);
467CVSCRIPT(cv_listinputfiles,
468 "Get a list of all input/configuration files loaded in this session\n"
469 "list : sequence of strings - List of file names",
472 std::list<std::string>
const l =
473 script->proxy()->list_input_stream_names();
475 for (std::list<std::string>::const_iterator li = l.begin();
476 li != l.end(); li++) {
477 if (li != l.begin()) result.append(1,
' ');
480 script->set_result_str(result);
485 "Load data from a state file into all matching colvars and biases",
487 "prefix : string - Path to existing state file or input prefix",
489 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
492 error_code |= script->module()->setup_input();
493 if (error_code != COLVARS_OK) {
494 script->add_error_msg(
"Error loading state file");
499CVSCRIPT(cv_loadfromstring,
500 "Load state data from a string into all matching colvars and biases",
502 "buffer : string - String buffer containing the state information",
504 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
505 script->proxy()->input_stream_from_string(
"input state string",
507 if (script->module()->setup_input() == COLVARS_OK) {
510 script->add_error_msg(
"Error loading state string");
511 return COLVARSCRIPT_ERROR;
516 "Get or set the molecule ID on which Colvars is defined (VMD only)\n"
517 "molid : integer - Current molecule ID",
519 "molid : integer - New molecule ID; -1 means undefined",
521 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
524 script->proxy()->get_molid(molid);
525 script->set_result_int(molid);
528 script->add_error_msg(
"Error: To change the molecule ID in VMD, use cv delete first.");
529 return COLVARS_NOT_IMPLEMENTED;
533CVSCRIPT(cv_printframe,
534 "Return the values that would be written to colvars.traj\n"
535 "values : string - The values\n",
538 std::ostringstream os;
539 script->module()->write_traj(os);
540 script->set_result_str(os.str());
544CVSCRIPT(cv_patchversion,
545 "Get the Colvars patch version number (used for bugfixes only)\n"
546 "version : string - Colvars version",
553CVSCRIPT(cv_printframelabels,
554 "Return the labels that would be written to colvars.traj\n"
555 "Labels : string - The labels",
558 std::ostringstream os;
559 script->module()->write_traj_label(os);
560 script->set_result_str(os.str());
565 "Delete all internal configuration",
568 cvm::log(
"Resetting the Collective Variables module.");
569 return script->module()->reset();
572CVSCRIPT(cv_resetindexgroups,
573 "Clear the index groups loaded so far, allowing to replace them",
582 "Change the prefix of all output files and save them",
584 "prefix : string - Output prefix with trailing \".colvars.state\" gets removed)",
585 std::string
const prefix =
587 int error_code = script->proxy()->set_output_prefix(prefix);
588 error_code |= script->module()->setup_output();
589 error_code |= script->module()->write_restart_file(prefix+
591 error_code |= script->module()->write_output_files();
595CVSCRIPT(cv_savetostring,
596 "Write the Colvars state to a string and return it\n"
597 "state : string - The saved state",
600 return script->module()->write_restart_string(script->modify_str_result());
603CVSCRIPT(cv_targettemperature,
604 "Get/set target temperature, overriding internally what the MD engine reports\n"
605 "T : float - Current target temperature in K",
607 "T : float - New target temperature in K (internal use)",
609 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
611 return script->set_result_real(script->proxy()->target_temperature());
613 return script->proxy()->set_target_temperature(strtod(Targ, NULL));
618 "Get/set integration timestep, overriding internally what the MD engine reports\n"
619 "dt : float - Current integration timestep in MD engine units",
621 "dt : float - New integration timestep in MD engine units",
623 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
625 return script->set_result_real(script->proxy()->dt());
627 return script->proxy()->set_integration_timestep(strtod(arg, NULL));
632 "Get or set the current Colvars unit system\n"
633 "units : string - The current unit system",
635 "units : string - The new unit system",
637 script->obj_to_str(script->get_module_cmd_arg(0, objc, objv));
647 "Recalculate colvars and biases",
650 int error_code = script->proxy()->update_input();
652 script->add_error_msg(
"Error updating the Colvars module (input)");
655 error_code |= script->module()->calc();
657 script->add_error_msg(
"Error updating the Colvars module (calc)");
660 error_code |= script->proxy()->update_output();
662 script->add_error_msg(
"Error updating the Colvars module (output)");
668 "Get the Colvars version string\n"
669 "version : string - Colvars version",
672 script->set_result_str(COLVARS_VERSION);
678#ifndef COLVARSCRIPT_COMMANDS_GLOBAL
679#include
"colvarscript_commands_colvar.h"
680#include
"colvarscript_commands_bias.h"
static std::string state_file_prefix(char const *filename)
Strips .colvars.state from filename and checks that it is not empty.
Definition: colvarmodule.cpp:1524
std::vector< std::string > index_group_names
Names of groups from one or more Gromacs .ndx files.
Definition: colvarmodule.h:749
int patch_version_number() const
Get the patch version number (non-zero in patch releases of other packages)
Definition: colvarmodule.h:76
std::vector< std::vector< int > * > index_groups
Groups from one or more Gromacs .ndx files.
Definition: colvarmodule.h:752
real total_bias_energy
Energy of built-in and scripted biases, summed per time-step.
Definition: colvarmodule.h:314
static void log(std::string const &message, int min_log_level=10)
Definition: colvarmodule.cpp:1969
std::string const & get_config() const
Get the configuration string read so far (includes comments)
Definition: colvarmodule.cpp:358
static colvarmodule * main()
Access the one instance of the Colvars module.
Definition: colvarmodule.cpp:185
static step_number step_relative()
Return the current step number from the beginning of this run.
Definition: colvarmodule.h:233
static colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program; it is static in o...
Definition: colvarmodule.h:860
static step_number step_absolute()
Definition: colvarmodule.h:240
virtual int set_unit_system(std::string const &units, bool check_only)
Request to set the units used internally by Colvars.
Definition: colvarproxy_system.cpp:36