Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarscript.h
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARSCRIPT_H
11#define COLVARSCRIPT_H
12
13#include <string>
14#include <vector>
15#include <map>
16
17#include "colvarmodule.h"
18#include "colvarvalue.h"
19#include "colvarproxy.h"
20
21
22// Only these error values are part of the scripting interface
23#define COLVARSCRIPT_ERROR -1
24#define COLVARSCRIPT_OK 0
25
26#define COLVARSCRIPT_MAGIC 0x5349474E
27
28class colvardeps;
29
31
32public:
34 uint32_t const magic = COLVARSCRIPT_MAGIC;
35
38 static bool is_valid(const colvarscript* script_ptr) {
39 if (!script_ptr) return false;
40 // if address is inaccessible, this will sefgault
41 return script_ptr->magic == COLVARSCRIPT_MAGIC;
42 }
43
44private:
45
46 colvarproxy *proxy_;
47 colvarmodule *cvmodule;
48
49 inline colvarscript() {} // no-argument construction forbidden
50
51public:
52
53 friend class colvarproxy;
54
56
58
60 std::string str_result_;
61
63 int run(int objc, unsigned char *const objv[]);
64
66 inline std::string const &str_result() const
67 {
68 return str_result_;
69 }
70
72 inline std::string &modify_str_result()
73 {
74 return str_result_;
75 }
76
78 int set_result_str(std::string const &s);
79
81 int clear_str_result();
82
84 void add_error_msg(std::string const &s);
85
87 enum command {
88#define CVSCRIPT_ENUM_COMM(COMM) COMM,
89#undef CVSCRIPT
90#define CVSCRIPT(COMM,HELP,N_ARGS_MIN,N_ARGS_MAX,ARGS,FN_BODY) \
91 CVSCRIPT_ENUM_COMM(COMM)
92#ifdef COLVARSCRIPT_COMMANDS_H
93#undef COLVARSCRIPT_COMMANDS_H
94#endif
95#include "colvarscript_commands.h"
96#undef COLVARSCRIPT_COMMANDS_H
97#undef CVSCRIPT
98#undef CVSCRIPT_ENUM_COMM
99 cv_n_commands
100 };
101
104 use_module,
105 use_colvar,
106 use_bias
107 };
108
110 std::string get_cmd_prefix(Object_type t);
111
113 template<Object_type T>
114 unsigned char *get_cmd_arg(int iarg, int objc, unsigned char *const objv[]);
115
117 unsigned char *get_module_cmd_arg(int iarg, int objc,
118 unsigned char *const objv[]);
119
121 unsigned char *get_colvar_cmd_arg(int iarg, int objc,
122 unsigned char *const objv[]);
123
125 unsigned char *get_bias_cmd_arg(int iarg, int objc,
126 unsigned char *const objv[]);
127
129 template<Object_type T>
130 int check_cmd_nargs(char const *cmd, int objc,
131 int n_args_min, int n_args_max);
132
134 int check_module_cmd_nargs(char const *cmd, int objc,
135 int n_args_min, int n_args_max);
136
138 int check_colvar_cmd_nargs(char const *cmd, int objc,
139 int n_args_min, int n_args_max);
140
142 int check_bias_cmd_nargs(char const *cmd, int objc,
143 int n_args_min, int n_args_max);
144
146 template<colvarscript::Object_type T>
147 int cmd_arg_shift();
148
150 inline char const **get_command_names() const
151 {
152 return cmd_names;
153 }
154
157 char const *get_command_help(char const *cmd);
158
161 char const *get_command_rethelp(char const *cmd);
162
168 char const *get_command_arghelp(char const *cmd, int i);
169
172 int get_command_n_args_min(char const *cmd);
173
176 int get_command_n_args_max(char const *cmd);
177
179 inline void set_cmdline_main_cmd(std::string const &cmd) {
180 cmdline_main_cmd_ = cmd;
181 }
182
185 char const *get_command_full_help(char const *cmd);
186
190
195
199 std::string get_command_cmdline_help(Object_type t, std::string const &cmd);
200
202 int unsupported_op();
203
206 {
207 return this->cvmodule;
208 }
209
212 {
213 return this->proxy_;
214 }
215
216 // Input functions - get the string reps of script argument objects
217
219 char *obj_to_str(unsigned char *obj);
220
222 std::vector<std::string> obj_to_str_vector(unsigned char *obj);
223
224
225 // Output functions - convert internal objects to representations suitable
226 // for use in the scripting language. At the moment only conversion to C
227 // strings is supported, and obj is assumed to be a char * pointer.
228
230 int set_result_int(int const &x, unsigned char *obj = NULL);
231
233 int set_result_int_vec(std::vector<int> const &x, unsigned char *obj = NULL);
234
236 int set_result_long_int(long int const &x, unsigned char *obj = NULL);
237
239 int set_result_long_int_vec(std::vector<long int> const &x,
240 unsigned char *obj = NULL);
241
243 int set_result_real(cvm::real const &x, unsigned char *obj = NULL);
244
246 template <typename T>
247 int set_result_real_vec(T const &x,
248 unsigned char *obj = NULL);
249
251 int set_result_rvector(cvm::rvector const &x, unsigned char *obj = NULL);
252
254 template <typename T>
256 unsigned char *obj = NULL);
257
259 int set_result_colvarvalue(colvarvalue const &x, unsigned char *obj = NULL);
260
262 int set_result_colvarvalue_vec(std::vector<colvarvalue> const &x,
263 unsigned char *obj = NULL);
264
265private:
266
268 int init_commands();
269
271 int init_command(colvarscript::command const &comm,
272 char const *name, char const *help,
273 int n_args_min, int n_args_max, char const *arghelp,
274 int (*fn)(void *, int, unsigned char * const *));
275
276public: // TODO this function will be removed soon
277
279 int proc_features(colvardeps *obj,
280 int argc, unsigned char *const argv[]);
281
282private: // TODO
283
285 std::map<std::string, command> cmd_str_map;
286
288 std::string cmdline_main_cmd_;
289
291 char const **cmd_names;
292
294 std::vector<std::string> cmd_help;
295
297 std::vector<std::string> cmd_rethelp;
298
300 std::vector<size_t> cmd_n_args_min;
301
303 std::vector<size_t> cmd_n_args_max;
304
306 std::vector< std::vector<std::string> > cmd_arghelp;
307
309 std::vector<std::string> cmd_full_help;
310
312 std::vector<int (*)(void *, int, unsigned char * const *)> cmd_fns;
313
315 inline int (*get_cmd_fn(std::string const &cmd_key))(void *,
316 int,
317 unsigned char * const *)
318 {
319 if (cmd_str_map.count(cmd_key) > 0) {
320 return cmd_fns[cmd_str_map[cmd_key]];
321 }
322 return NULL;
323 }
324
326 template <typename T>
327 int set_result_text(T const &x, unsigned char *obj);
328
330 template <typename T>
331 int pack_vector_elements_text(T const &x, std::string &x_str);
332
334 int set_result_text_from_str(std::string const &x_str, unsigned char *obj);
335
336
337};
338
339
341inline static colvarscript *colvarscript_obj(void *pobj)
342{
343 return reinterpret_cast<colvarscript *>(pobj);
344}
345
347inline static colvarscript *colvarscript_obj()
348{
349 return cvm::main()->proxy->script;
350}
351
353inline static colvar *colvar_obj(void *pobj)
354{
355 return reinterpret_cast<colvar *>(pobj);
356}
357
358
360inline static colvarbias *colvarbias_obj(void *pobj)
361{
362 return reinterpret_cast<colvarbias *>(pobj);
363}
364
365
366
367template<colvarscript::Object_type T>
368unsigned char *colvarscript::get_cmd_arg(int iarg,
369 int objc,
370 unsigned char *const objv[])
371{
372 int const shift = cmd_arg_shift<T>();
373 return (shift+iarg < objc) ? objv[shift+iarg] : NULL;
374}
375
376
377inline unsigned char *colvarscript::get_module_cmd_arg(int iarg, int objc,
378 unsigned char *const objv[])
379{
380 return get_cmd_arg<use_module>(iarg, objc, objv);
381}
382
383
384inline unsigned char *colvarscript::get_colvar_cmd_arg(int iarg, int objc,
385 unsigned char *const objv[])
386{
387 return get_cmd_arg<use_colvar>(iarg, objc, objv);
388}
389
390
391inline unsigned char *colvarscript::get_bias_cmd_arg(int iarg, int objc,
392 unsigned char *const objv[])
393{
394 return get_cmd_arg<use_bias>(iarg, objc, objv);
395}
396
397
398template<colvarscript::Object_type T>
400 int objc,
401 int n_args_min,
402 int n_args_max)
403{
404 int const shift = cmd_arg_shift<T>();
405 if (objc < shift+n_args_min) {
406 add_error_msg("Insufficient number of arguments ("+cvm::to_str(objc)+
407 ") for script function \""+std::string(cmd)+
408 "\":\n"+get_command_full_help(cmd));
409 return COLVARSCRIPT_ERROR;
410 }
411 if (objc > shift+n_args_max) {
412 add_error_msg("Too many arguments ("+cvm::to_str(objc)+
413 ") for script function \""+std::string(cmd)+
414 "\":\n"+get_command_full_help(cmd));
415 return COLVARSCRIPT_ERROR;
416 }
417 return COLVARSCRIPT_OK;
418}
419
420
421inline int colvarscript::check_module_cmd_nargs(char const *cmd,
422 int objc,
423 int n_args_min,
424 int n_args_max)
425{
426 return check_cmd_nargs<use_module>(cmd, objc, n_args_min, n_args_max);
427}
428
429
430inline int colvarscript::check_colvar_cmd_nargs(char const *cmd,
431 int objc,
432 int n_args_min,
433 int n_args_max)
434{
435 return check_cmd_nargs<use_colvar>(cmd, objc, n_args_min, n_args_max);
436}
437
438
439inline int colvarscript::check_bias_cmd_nargs(char const *cmd,
440 int objc,
441 int n_args_min,
442 int n_args_max)
443{
444 return check_cmd_nargs<use_bias>(cmd, objc, n_args_min, n_args_max);
445}
446
447
448template<colvarscript::Object_type T>
450{
451 int shift = 0;
452 if (T == use_module) {
453 // "cv" and "COMMAND" are 1st and 2nd argument, and shift is equal to 2
454 shift = 2;
455 } else if (T == use_colvar) {
456 // Same as above with additional arguments "colvar" and "NAME"
457 shift = 4;
458 } else if (T == use_bias) {
459 shift = 4;
460 }
461 return shift;
462}
463
464
465extern "C" {
466
468 // New: requires pointer to colvarscript object - TODO - pass it in ctypes Python example
469 int run_colvarscript_command(colvarscript *script, int objc, unsigned char *const objv[]);
470
472 const char * get_colvarscript_result();
473
474}
475
476#endif // #ifndef COLVARSCRIPT_H
A collective variable (main class); to be defined, it needs at least one object of a derived class of...
Definition: colvar.h:53
Collective variable bias, base class.
Definition: colvarbias.h:23
Parent class for a member object of a bias, cv or cvc etc. containing features and their dependencies...
Definition: colvardeps.h:34
vector of real numbers with three components
Definition: colvartypes.h:728
Collective variables module (main class)
Definition: colvarmodule.h:72
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
static colvarmodule * main()
Access the main instance of the Colvars module.
Definition: colvarmodule.cpp:205
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2541
colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program.
Definition: colvarmodule.h:957
colvarscript * script
Definition: colvarproxy.h:532
Definition: colvarproxy.h:564
Definition: colvarscript.h:30
int clear_str_result()
Clear the string result.
Definition: colvarscript.cpp:611
int set_result_long_int(long int const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:921
int set_result_real(cvm::real const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:931
char const * get_command_arghelp(char const *cmd, int i)
Definition: colvarscript.cpp:212
int proc_features(colvardeps *obj, int argc, unsigned char *const argv[])
Run subcommands on base colvardeps object (colvar, bias, ...)
Definition: colvarscript.cpp:511
void add_error_msg(std::string const &s)
Add the given string to the error message of the script interface.
Definition: colvarscript.cpp:601
int run(int objc, unsigned char *const objv[])
Run a script command with space-separated positional arguments (objects)
Definition: colvarscript.cpp:348
int set_result_str(std::string const &s)
Set the return value to the given string.
Definition: colvarscript.cpp:589
std::string & modify_str_result()
Modify the string result of the current scripting call.
Definition: colvarscript.h:72
char const * get_command_rethelp(char const *cmd)
Definition: colvarscript.cpp:200
std::string const & str_result() const
Get the string result of the current scripting call.
Definition: colvarscript.h:66
int set_result_long_int_vec(std::vector< long int > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:925
void set_cmdline_main_cmd(std::string const &cmd)
Set the main command for the CLI, when it is not "cv" (e.g. LAMMPS)
Definition: colvarscript.h:179
int set_result_text(T const &x, unsigned char *obj)
Set obj equal to x, using its string representation.
Definition: colvarscript.cpp:797
command
Commands available.
Definition: colvarscript.h:87
std::vector< std::string > obj_to_str_vector(unsigned char *obj)
Get a list of strings from an object (does not work with a simple cast)
Definition: colvarscript.cpp:468
uint32_t const magic
This magic number is used to validate colvarscript pointers.
Definition: colvarscript.h:34
int set_result_colvarvalue(colvarvalue const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:965
int unsupported_op()
Set error code for unsupported script operation.
Definition: colvarscript.cpp:582
std::map< std::string, command > cmd_str_map
Internal identifiers of command strings.
Definition: colvarscript.h:285
int set_result_text_from_str(std::string const &x_str, unsigned char *obj)
Code reused by all instances of set_result_text()
Definition: colvarscript.cpp:784
int set_result_real_vec(T const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
int set_result_int(int const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:911
int check_module_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of check_cmd_nargs<> for module-level commands.
Definition: colvarscript.h:421
char * obj_to_str(unsigned char *obj)
Get the string representation of an object (by default, a simple cast)
Definition: colvarscript.cpp:457
int set_result_rvector_vec(T const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
static bool is_valid(const colvarscript *script_ptr)
Definition: colvarscript.h:38
char const * get_command_help(char const *cmd)
Definition: colvarscript.cpp:188
int init_command(colvarscript::command const &comm, char const *name, char const *help, int n_args_min, int n_args_max, char const *arghelp, int(*fn)(void *, int, unsigned char *const *))
Set up a single script API function.
Definition: colvarscript.cpp:105
int check_colvar_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of check_cmd_nargs<> for colvar-level commands.
Definition: colvarscript.h:430
int cmd_arg_shift()
Number of positional arguments to shift for each object type.
Definition: colvarscript.h:449
std::vector< size_t > cmd_n_args_max
Maximum number of arguments for each command.
Definition: colvarscript.h:303
char const * get_command_full_help(char const *cmd)
Definition: colvarscript.cpp:248
int get_command_n_args_min(char const *cmd)
Definition: colvarscript.cpp:224
unsigned char * get_colvar_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for colvar-level commands.
Definition: colvarscript.h:384
int pack_vector_elements_text(T const &x, std::string &x_str)
Code reused by instances of set_result_text()
Definition: colvarscript.cpp:804
colvarmodule * module()
Pointer to the Colvars main object.
Definition: colvarscript.h:205
std::vector< std::string > cmd_rethelp
Description of the return values of each command (may be empty)
Definition: colvarscript.h:297
int check_bias_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of get_cmd_arg<> for bias-level commands.
Definition: colvarscript.h:439
char const ** get_command_names() const
Get names of all commands.
Definition: colvarscript.h:150
std::vector< size_t > cmd_n_args_min
Minimum number of arguments for each command.
Definition: colvarscript.h:300
unsigned char * get_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Get a pointer to the i-th argument of the command (NULL if not given)
Definition: colvarscript.h:368
char const ** cmd_names
Inverse of cmd_str_map (to be exported outside this class)
Definition: colvarscript.h:291
int set_result_colvarvalue_vec(std::vector< colvarvalue > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:970
int set_result_rvector(cvm::rvector const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:941
unsigned char * get_bias_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for bias-level commands.
Definition: colvarscript.h:391
Object_type
Type of object handling a script command.
Definition: colvarscript.h:103
int get_command_n_args_max(char const *cmd)
Definition: colvarscript.cpp:236
colvarproxy * proxy()
Pointer to the colvarproxy object (interface with host engine)
Definition: colvarscript.h:211
unsigned char * get_module_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for module-level commands.
Definition: colvarscript.h:377
int set_result_int_vec(std::vector< int > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:915
int init_commands()
Set up all script API functions.
Definition: colvarscript.cpp:66
std::string get_command_cmdline_syntax(Object_type t, command c)
Definition: colvarscript.cpp:260
std::vector< std::vector< std::string > > cmd_arghelp
Help strings for each command argument.
Definition: colvarscript.h:306
int(*)(void *, int, unsigned char *const *) get_cmd_fn(std::string const &cmd_key)
Get a pointer to the implementation of the given command.
Definition: colvarscript.h:315
std::string cmdline_main_cmd_
Main command used in command line ("cv" by default)
Definition: colvarscript.h:288
std::string get_cmdline_help_summary(Object_type t)
Definition: colvarscript.cpp:302
int check_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Check the argument count of the command.
Definition: colvarscript.h:399
std::vector< std::string > cmd_help
Help strings for each command.
Definition: colvarscript.h:294
std::string get_cmd_prefix(Object_type t)
Return the prefix of the individual command for each object function.
Definition: colvarscript.cpp:171
std::string get_command_cmdline_help(Object_type t, std::string const &cmd)
Definition: colvarscript.cpp:334
std::vector< std::string > cmd_full_help
Full help strings for each command.
Definition: colvarscript.h:309
std::string str_result_
String representation of the result of a script call.
Definition: colvarscript.h:60
std::vector< int(*)(void *, int, unsigned char *const *)> cmd_fns
Implementations of each command.
Definition: colvarscript.h:312
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
Collective variables main module.
Colvars proxy classes.