diff --git a/doc/src/Library_objects.rst b/doc/src/Library_objects.rst index 8ebecfcc94..db21817cfd 100644 --- a/doc/src/Library_objects.rst +++ b/doc/src/Library_objects.rst @@ -9,6 +9,7 @@ fixes, or variables in LAMMPS using the following functions: - :cpp:func:`lammps_extract_variable_datatype` - :cpp:func:`lammps_extract_variable` - :cpp:func:`lammps_set_variable` +- :cpp:func:`lammps_variable_info` ----------------------- @@ -37,6 +38,11 @@ fixes, or variables in LAMMPS using the following functions: ----------------------- +.. doxygenfunction:: lammps_variable_info + :project: progguide + +----------------------- + .. doxygenenum:: _LMP_DATATYPE_CONST .. doxygenenum:: _LMP_STYLE_CONST diff --git a/examples/COUPLE/plugin/liblammpsplugin.c b/examples/COUPLE/plugin/liblammpsplugin.c index 0cf9bea512..5228e07e9c 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.c +++ b/examples/COUPLE/plugin/liblammpsplugin.c @@ -110,6 +110,7 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib) ADDSYM(extract_variable); ADDSYM(extract_variable_datatype); ADDSYM(set_variable); + ADDSYM(variable_info); ADDSYM(gather_atoms); ADDSYM(gather_atoms_concat); diff --git a/examples/COUPLE/plugin/liblammpsplugin.h b/examples/COUPLE/plugin/liblammpsplugin.h index 4138a5218b..329285f317 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.h +++ b/examples/COUPLE/plugin/liblammpsplugin.h @@ -155,6 +155,7 @@ struct _liblammpsplugin { void *(*extract_variable)(void *, const char *, char *); int (*extract_variable_datatype)(void *, const char *); int (*set_variable)(void *, char *, char *); + int (*variable_info)(void *, int, char *, int); void (*gather_atoms)(void *, const char *, int, int, void *); void (*gather_atoms_concat)(void *, const char *, int, int, void *); diff --git a/src/info.cpp b/src/info.cpp index d99cbbe562..948cbbfe15 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -601,23 +601,10 @@ void Info::command(int narg, char **arg) if (flags & VARIABLES) { int nvar = input->variable->nvar; - int *style = input->variable->style; - char **names = input->variable->names; - char ***data = input->variable->data; fputs("\nVariable information:\n",out); for (int i=0; i < nvar; ++i) { - int ndata = 1; - fmt::print(out,"Variable[{:3d}]: {:16} style = {:16} def =", - i,std::string(names[i])+',',std::string(varstyles[style[i]])+','); - if (style[i] == Variable::INTERNAL) { - fmt::print(out,"{:.8}\n",input->variable->dvalue[i]); - continue; - } - if ((style[i] != Variable::LOOP) && (style[i] != Variable::ULOOP)) - ndata = input->variable->num[i]; - for (int j=0; j < ndata; ++j) - if (data[i][j]) fmt::print(out," {}",data[i][j]); - fputs("\n",out); + auto vinfo = get_variable_info(i); + fmt::print(out, get_variable_info(i)); } } @@ -1316,3 +1303,29 @@ char **Info::get_variable_names(int &num) { num = input->variable->nvar; return input->variable->names; } + +/* ---------------------------------------------------------------------- */ + +std::string Info::get_variable_info(int num) { + int *style = input->variable->style; + char **names = input->variable->names; + char ***data = input->variable->data; + std::string text; + int ndata = 1; + text = fmt::format("Variable[{:3d}]: {:16} style = {:16} def =", num, + std::string(names[num]) + ',', std::string(varstyles[style[num]]) + ','); + if (style[num] == Variable::INTERNAL) { + text += fmt::format("{:.8}\n",input->variable->dvalue[num]); + return text; + } + + if ((style[num] != Variable::LOOP) && (style[num] != Variable::ULOOP)) + ndata = input->variable->num[num]; + else + input->variable->retrieve(names[num]); + + for (int j=0; j < ndata; ++j) + if (data[num][j]) text += fmt::format(" {}",data[num][j]); + text += "\n"; + return text; +} diff --git a/src/info.h b/src/info.h index 40ec2cf23c..c4230b063e 100644 --- a/src/info.h +++ b/src/info.h @@ -53,6 +53,7 @@ class Info : public Command { void get_memory_info(double *); char **get_variable_names(int &num); + std::string get_variable_info(int num); private: void available_styles(FILE *out, int flags); diff --git a/src/library.cpp b/src/library.cpp index f48f43cc5b..b28e524db9 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -2460,6 +2460,42 @@ int lammps_set_variable(void *handle, char *name, char *str) return err; } + +/* ---------------------------------------------------------------------- */ + +/** Retrieve informational string for a variable. + * + * .. versionadded:: TBD + * + * This function copies a string with human readable information about + * a defined variable: name, style, current value(s) into the provided + * C-style string buffer. That is the same info as produced by the + * :doc:`info variables ` command. The length of the buffer must + * be provided as *buf_size* argument. If the info exceeds the length + * of the buffer, it will be truncated accordingly. If the index is + * out of range, the function returns 0 and *buffer* is set to an empty + * string, otherwise 1. + * + * \param handle pointer to a previously created LAMMPS instance cast to ``void *``. + * \param idx index of the variable (0 <= idx < nvar) + * \param buffer string buffer to copy the info to + * \param buf_size size of the provided string buffer + * \return 1 if successful, otherwise 0 */ + +int lammps_variable_info(void *handle, int idx, char *buffer, int buf_size) { + auto lmp = (LAMMPS *) handle; + Info info(lmp); + auto varinfo = info.get_variable_info(idx); + + if ((idx >= 0) && (idx < lmp->input->variable->nvar)) { + strncpy(buffer, varinfo.c_str(), buf_size); + return 1; + } + + buffer[0] = '\0'; + return 0; +} + // ---------------------------------------------------------------------- // Library functions for scatter/gather operations of data // ---------------------------------------------------------------------- diff --git a/src/library.h b/src/library.h index 30a12ebdef..7db86bd71d 100644 --- a/src/library.h +++ b/src/library.h @@ -177,7 +177,8 @@ void *lammps_extract_compute(void *handle, const char *, int, int); void *lammps_extract_fix(void *handle, const char *, int, int, int, int); void *lammps_extract_variable(void *handle, const char *, const char *); int lammps_extract_variable_datatype(void *handle, const char *name); -int lammps_set_variable(void *, char *, char *); +int lammps_set_variable(void *handle, char *name, char *str); +int lammps_variable_info(void *handle, int idx, char *buf, int bufsize); /* ---------------------------------------------------------------------- * Library functions for scatter/gather operations of data @@ -198,11 +199,11 @@ void lammps_gather_impropers(void *handle, void *data); void lammps_gather(void *handle, const char *name, int type, int count, void *data); void lammps_gather_concat(void *handle, const char *name, int type, int count, void *data); -void lammps_gather_subset(void *handle, const char *name, int type, int count, int ndata, - int *ids, void *data); +void lammps_gather_subset(void *handle, const char *name, int type, int count, int ndata, int *ids, + void *data); void lammps_scatter(void *handle, const char *name, int type, int count, void *data); -void lammps_scatter_subset(void *handle, const char *name, int type, int count, int ndata, - int *ids, void *data); +void lammps_scatter_subset(void *handle, const char *name, int type, int count, int ndata, int *ids, + void *data); #if !defined(LAMMPS_BIGBIG) int lammps_create_atoms(void *handle, int n, const int *id, const int *type, const double *x, diff --git a/tools/lammps-gui/lammpswrapper.cpp b/tools/lammps-gui/lammpswrapper.cpp index 52ea177596..d1a130c3c5 100644 --- a/tools/lammps-gui/lammpswrapper.cpp +++ b/tools/lammps-gui/lammpswrapper.cpp @@ -97,6 +97,19 @@ int LammpsWrapper::id_name(const char *keyword, int idx, char *buf, int len) return val; } +int LammpsWrapper::variable_info(int idx, char *buf, int len) +{ + int val = 0; + if (lammps_handle) { +#if defined(LAMMPS_GUI_USE_PLUGIN) + val = ((liblammpsplugin_t *)plugin_handle)->variable_info(lammps_handle, idx, buf, len); +#else + val = lammps_variable_info(lammps_handle, idx, buf, len); +#endif + } + return val; +} + double LammpsWrapper::get_thermo(const char *keyword) { double val = 0.0; diff --git a/tools/lammps-gui/lammpswrapper.h b/tools/lammps-gui/lammpswrapper.h index 3a18b61802..f5a391aed8 100644 --- a/tools/lammps-gui/lammpswrapper.h +++ b/tools/lammps-gui/lammpswrapper.h @@ -35,6 +35,7 @@ public: int id_count(const char *idtype); int id_name(const char *idtype, int idx, char *buf, int buflen); + int variable_info(int idx, char *buf, int buflen); double get_thermo(const char *keyword); void *last_thermo(const char *keyword, int idx);