refactor variable info in Info class, make info accessible from library interface

This commit is contained in:
Axel Kohlmeyer
2023-08-25 23:55:24 -04:00
parent 5b5210130c
commit d864b4789f
9 changed files with 93 additions and 20 deletions

View File

@ -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

View File

@ -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);

View File

@ -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 *);

View File

@ -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;
}

View File

@ -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);

View File

@ -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 <info>` 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
// ----------------------------------------------------------------------

View File

@ -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,

View File

@ -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;

View File

@ -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);