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
27class colvardeps;
28
30
31private:
32
33 colvarproxy *proxy_;
34 colvarmodule *colvars;
35
36 inline colvarscript() {} // no-argument construction forbidden
37
38public:
39
40 friend class colvarproxy;
41
43
45
47 std::string str_result_;
48
50 int run(int objc, unsigned char *const objv[]);
51
53 inline std::string const &str_result() const
54 {
55 return str_result_;
56 }
57
59 inline std::string &modify_str_result()
60 {
61 return str_result_;
62 }
63
65 int set_result_str(std::string const &s);
66
68 int clear_str_result();
69
71 void add_error_msg(std::string const &s);
72
74 enum command {
75#define CVSCRIPT_ENUM_COMM(COMM) COMM,
76#undef CVSCRIPT
77#define CVSCRIPT(COMM,HELP,N_ARGS_MIN,N_ARGS_MAX,ARGS,FN_BODY) \
78 CVSCRIPT_ENUM_COMM(COMM)
79#ifdef COLVARSCRIPT_COMMANDS_H
80#undef COLVARSCRIPT_COMMANDS_H
81#endif
82#include "colvarscript_commands.h"
83#undef COLVARSCRIPT_COMMANDS_H
84#undef CVSCRIPT
85#undef CVSCRIPT_ENUM_COMM
86 cv_n_commands
87 };
88
91 use_module,
92 use_colvar,
93 use_bias
94 };
95
97 std::string get_cmd_prefix(Object_type t);
98
100 template<Object_type T>
101 unsigned char *get_cmd_arg(int iarg, int objc, unsigned char *const objv[]);
102
104 unsigned char *get_module_cmd_arg(int iarg, int objc,
105 unsigned char *const objv[]);
106
108 unsigned char *get_colvar_cmd_arg(int iarg, int objc,
109 unsigned char *const objv[]);
110
112 unsigned char *get_bias_cmd_arg(int iarg, int objc,
113 unsigned char *const objv[]);
114
116 template<Object_type T>
117 int check_cmd_nargs(char const *cmd, int objc,
118 int n_args_min, int n_args_max);
119
121 int check_module_cmd_nargs(char const *cmd, int objc,
122 int n_args_min, int n_args_max);
123
125 int check_colvar_cmd_nargs(char const *cmd, int objc,
126 int n_args_min, int n_args_max);
127
129 int check_bias_cmd_nargs(char const *cmd, int objc,
130 int n_args_min, int n_args_max);
131
133 template<colvarscript::Object_type T>
134 int cmd_arg_shift();
135
137 inline char const **get_command_names() const
138 {
139 return cmd_names;
140 }
141
144 char const *get_command_help(char const *cmd);
145
148 char const *get_command_rethelp(char const *cmd);
149
155 char const *get_command_arghelp(char const *cmd, int i);
156
159 int get_command_n_args_min(char const *cmd);
160
163 int get_command_n_args_max(char const *cmd);
164
166 inline void set_cmdline_main_cmd(std::string const &cmd) {
167 cmdline_main_cmd_ = cmd;
168 }
169
172 char const *get_command_full_help(char const *cmd);
173
177
182
186 std::string get_command_cmdline_help(Object_type t, std::string const &cmd);
187
189 int unsupported_op();
190
193 {
194 return this->colvars;
195 }
196
199 {
200 return this->proxy_;
201 }
202
203 // Input functions - get the string reps of script argument objects
204
206 char *obj_to_str(unsigned char *obj);
207
209 std::vector<std::string> obj_to_str_vector(unsigned char *obj);
210
211
212 // Output functions - convert internal objects to representations suitable
213 // for use in the scripting language. At the moment only conversion to C
214 // strings is supported, and obj is assumed to be a char * pointer.
215
217 int set_result_int(int const &x, unsigned char *obj = NULL);
218
220 int set_result_int_vec(std::vector<int> const &x, unsigned char *obj = NULL);
221
223 int set_result_long_int(long int const &x, unsigned char *obj = NULL);
224
226 int set_result_long_int_vec(std::vector<long int> const &x,
227 unsigned char *obj = NULL);
228
230 int set_result_real(cvm::real const &x, unsigned char *obj = NULL);
231
233 template <typename T>
234 int set_result_real_vec(T const &x,
235 unsigned char *obj = NULL);
236
238 int set_result_rvector(cvm::rvector const &x, unsigned char *obj = NULL);
239
241 template <typename T>
243 unsigned char *obj = NULL);
244
246 int set_result_colvarvalue(colvarvalue const &x, unsigned char *obj = NULL);
247
249 int set_result_colvarvalue_vec(std::vector<colvarvalue> const &x,
250 unsigned char *obj = NULL);
251
252private:
253
255 int init_commands();
256
258 int init_command(colvarscript::command const &comm,
259 char const *name, char const *help,
260 int n_args_min, int n_args_max, char const *arghelp,
261 int (*fn)(void *, int, unsigned char * const *));
262
263public: // TODO this function will be removed soon
264
266 int proc_features(colvardeps *obj,
267 int argc, unsigned char *const argv[]);
268
269private: // TODO
270
272 std::map<std::string, command> cmd_str_map;
273
275 std::string cmdline_main_cmd_;
276
278 char const **cmd_names;
279
281 std::vector<std::string> cmd_help;
282
284 std::vector<std::string> cmd_rethelp;
285
287 std::vector<size_t> cmd_n_args_min;
288
290 std::vector<size_t> cmd_n_args_max;
291
293 std::vector< std::vector<std::string> > cmd_arghelp;
294
296 std::vector<std::string> cmd_full_help;
297
299 std::vector<int (*)(void *, int, unsigned char * const *)> cmd_fns;
300
302 inline int (*get_cmd_fn(std::string const &cmd_key))(void *,
303 int,
304 unsigned char * const *)
305 {
306 if (cmd_str_map.count(cmd_key) > 0) {
307 return cmd_fns[cmd_str_map[cmd_key]];
308 }
309 return NULL;
310 }
311
313 template <typename T>
314 int set_result_text(T const &x, unsigned char *obj);
315
317 template <typename T>
318 int pack_vector_elements_text(T const &x, std::string &x_str);
319
321 int set_result_text_from_str(std::string const &x_str, unsigned char *obj);
322
323
324};
325
326
328inline static colvarscript *colvarscript_obj()
329{
330 return cvm::main()->proxy->script;
331}
332
333
335inline static colvar *colvar_obj(void *pobj)
336{
337 return reinterpret_cast<colvar *>(pobj);
338}
339
340
342inline static colvarbias *colvarbias_obj(void *pobj)
343{
344 return reinterpret_cast<colvarbias *>(pobj);
345}
346
347
348
349template<colvarscript::Object_type T>
350unsigned char *colvarscript::get_cmd_arg(int iarg,
351 int objc,
352 unsigned char *const objv[])
353{
354 int const shift = cmd_arg_shift<T>();
355 return (shift+iarg < objc) ? objv[shift+iarg] : NULL;
356}
357
358
359inline unsigned char *colvarscript::get_module_cmd_arg(int iarg, int objc,
360 unsigned char *const objv[])
361{
362 return get_cmd_arg<use_module>(iarg, objc, objv);
363}
364
365
366inline unsigned char *colvarscript::get_colvar_cmd_arg(int iarg, int objc,
367 unsigned char *const objv[])
368{
369 return get_cmd_arg<use_colvar>(iarg, objc, objv);
370}
371
372
373inline unsigned char *colvarscript::get_bias_cmd_arg(int iarg, int objc,
374 unsigned char *const objv[])
375{
376 return get_cmd_arg<use_bias>(iarg, objc, objv);
377}
378
379
380template<colvarscript::Object_type T>
382 int objc,
383 int n_args_min,
384 int n_args_max)
385{
386 int const shift = cmd_arg_shift<T>();
387 if (objc < shift+n_args_min) {
388 add_error_msg("Insufficient number of arguments ("+cvm::to_str(objc)+
389 ") for script function \""+std::string(cmd)+
390 "\":\n"+get_command_full_help(cmd));
391 return COLVARSCRIPT_ERROR;
392 }
393 if (objc > shift+n_args_max) {
394 add_error_msg("Too many arguments ("+cvm::to_str(objc)+
395 ") for script function \""+std::string(cmd)+
396 "\":\n"+get_command_full_help(cmd));
397 return COLVARSCRIPT_ERROR;
398 }
399 return COLVARSCRIPT_OK;
400}
401
402
403inline int colvarscript::check_module_cmd_nargs(char const *cmd,
404 int objc,
405 int n_args_min,
406 int n_args_max)
407{
408 return check_cmd_nargs<use_module>(cmd, objc, n_args_min, n_args_max);
409}
410
411
412inline int colvarscript::check_colvar_cmd_nargs(char const *cmd,
413 int objc,
414 int n_args_min,
415 int n_args_max)
416{
417 return check_cmd_nargs<use_colvar>(cmd, objc, n_args_min, n_args_max);
418}
419
420
421inline int colvarscript::check_bias_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_bias>(cmd, objc, n_args_min, n_args_max);
427}
428
429
430template<colvarscript::Object_type T>
432{
433 int shift = 0;
434 if (T == use_module) {
435 // "cv" and "COMMAND" are 1st and 2nd argument, and shift is equal to 2
436 shift = 2;
437 } else if (T == use_colvar) {
438 // Same as above with additional arguments "colvar" and "NAME"
439 shift = 4;
440 } else if (T == use_bias) {
441 shift = 4;
442 }
443 return shift;
444}
445
446
447extern "C" {
448
450 int run_colvarscript_command(int objc, unsigned char *const objv[]);
451
453 const char * get_colvarscript_result();
454
455}
456
457
458#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:723
Collective variables module (main class)
Definition: colvarmodule.h:69
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:141
static colvarmodule * main()
Access the one instance of the Colvars module.
Definition: colvarmodule.cpp:175
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:943
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2416
colvarscript * script
Definition: colvarproxy.h:525
Definition: colvarproxy.h:559
Definition: colvarscript.h:29
int clear_str_result()
Clear the string result.
Definition: colvarscript.cpp:606
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:904
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:914
char const * get_command_arghelp(char const *cmd, int i)
Definition: colvarscript.cpp:211
int proc_features(colvardeps *obj, int argc, unsigned char *const argv[])
Run subcommands on base colvardeps object (colvar, bias, ...)
Definition: colvarscript.cpp:506
void add_error_msg(std::string const &s)
Add the given string to the error message of the script interface.
Definition: colvarscript.cpp:596
int run(int objc, unsigned char *const objv[])
Run a script command with space-separated positional arguments (objects)
Definition: colvarscript.cpp:347
int set_result_str(std::string const &s)
Set the return value to the given string.
Definition: colvarscript.cpp:584
std::string & modify_str_result()
Modify the string result of the current scripting call.
Definition: colvarscript.h:59
char const * get_command_rethelp(char const *cmd)
Definition: colvarscript.cpp:199
std::string const & str_result() const
Get the string result of the current scripting call.
Definition: colvarscript.h:53
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:908
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:166
int set_result_text(T const &x, unsigned char *obj)
Set obj equal to x, using its string representation.
Definition: colvarscript.cpp:780
command
Commands available.
Definition: colvarscript.h:74
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:463
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:948
int unsupported_op()
Set error code for unsupported script operation.
Definition: colvarscript.cpp:577
std::map< std::string, command > cmd_str_map
Internal identifiers of command strings.
Definition: colvarscript.h:272
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:767
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:894
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:403
char * obj_to_str(unsigned char *obj)
Get the string representation of an object (by default, a simple cast)
Definition: colvarscript.cpp:452
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.
char const * get_command_help(char const *cmd)
Definition: colvarscript.cpp:187
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:104
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:412
int cmd_arg_shift()
Number of positional arguments to shift for each object type.
Definition: colvarscript.h:431
std::vector< size_t > cmd_n_args_max
Maximum number of arguments for each command.
Definition: colvarscript.h:290
char const * get_command_full_help(char const *cmd)
Definition: colvarscript.cpp:247
int get_command_n_args_min(char const *cmd)
Definition: colvarscript.cpp:223
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:366
int pack_vector_elements_text(T const &x, std::string &x_str)
Code reused by instances of set_result_text()
Definition: colvarscript.cpp:787
colvarmodule * module()
Pointer to the Colvars main object.
Definition: colvarscript.h:192
std::vector< std::string > cmd_rethelp
Description of the return values of each command (may be empty)
Definition: colvarscript.h:284
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:421
char const ** get_command_names() const
Get names of all commands.
Definition: colvarscript.h:137
std::vector< size_t > cmd_n_args_min
Minimum number of arguments for each command.
Definition: colvarscript.h:287
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:350
char const ** cmd_names
Inverse of cmd_str_map (to be exported outside this class)
Definition: colvarscript.h:278
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:953
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:924
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:373
Object_type
Type of object handling a script command.
Definition: colvarscript.h:90
int get_command_n_args_max(char const *cmd)
Definition: colvarscript.cpp:235
colvarproxy * proxy()
Pointer to the colvarproxy object (interface with host engine)
Definition: colvarscript.h:198
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:359
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:898
int init_commands()
Set up all script API functions.
Definition: colvarscript.cpp:65
std::string get_command_cmdline_syntax(Object_type t, command c)
Definition: colvarscript.cpp:259
std::vector< std::vector< std::string > > cmd_arghelp
Help strings for each command argument.
Definition: colvarscript.h:293
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:302
std::string cmdline_main_cmd_
Main command used in command line ("cv" by default)
Definition: colvarscript.h:275
std::string get_cmdline_help_summary(Object_type t)
Definition: colvarscript.cpp:301
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:381
std::vector< std::string > cmd_help
Help strings for each command.
Definition: colvarscript.h:281
std::string get_cmd_prefix(Object_type t)
Return the prefix of the individual command for each object function.
Definition: colvarscript.cpp:170
std::string get_command_cmdline_help(Object_type t, std::string const &cmd)
Definition: colvarscript.cpp:333
std::vector< std::string > cmd_full_help
Full help strings for each command.
Definition: colvarscript.h:296
std::string str_result_
String representation of the result of a script call.
Definition: colvarscript.h:47
std::vector< int(*)(void *, int, unsigned char *const *)> cmd_fns
Implementations of each command.
Definition: colvarscript.h:299
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.