allow extracting variables that are not atom or equal style compatible

This commit is contained in:
Axel Kohlmeyer
2020-11-19 13:47:23 -05:00
parent 9ea025295d
commit 3d7fd453c3
2 changed files with 31 additions and 23 deletions

View File

@ -1811,18 +1811,18 @@ void *lammps_extract_fix(void *handle, char *id, int style, int type,
\verbatim embed:rst \verbatim embed:rst
This function returns a pointer to data from a LAMMPS :doc:`variable` This function returns a pointer to data from a LAMMPS :doc:`variable`
identified by its name. The variable must be either an *equal*\ -style identified by its name. When the variable is either an *equal*\ -style
compatible or an *atom*\ -style variable. Variables of style *internal* compatible or an *atom*\ -style variable the variable is evaluated and
the corresponding value(s) returned. Variables of style *internal*
are compatible with *equal*\ -style variables and so are *python*\ are compatible with *equal*\ -style variables and so are *python*\
-style variables, if they return a numeric value. The function returns -style variables, if they return a numeric value. For other
variable styles their string value is returned. The function returns
``NULL`` when a variable of the provided *name* is not found or of an ``NULL`` when a variable of the provided *name* is not found or of an
incompatible style. The *group* argument is only used for *atom*\ incompatible style. The *group* argument is only used for *atom*\
-style variables and ignored otherwise. If set to ``NULL`` when -style variables and ignored otherwise. If set to ``NULL`` when
extracting data from and *atom*\ -style variable, the group is assumed extracting data from and *atom*\ -style variable, the group is assumed
to be "all". to be "all".
.. note::
When requesting data from an *equal*\ -style or compatible variable When requesting data from an *equal*\ -style or compatible variable
this function allocates storage for a single double value, copies the this function allocates storage for a single double value, copies the
returned value to it, and returns a pointer to the location of the returned value to it, and returns a pointer to the location of the
@ -1837,10 +1837,18 @@ to be "all".
For *atom*\ -style variables the data returned is a pointer to an For *atom*\ -style variables the data returned is a pointer to an
allocated block of storage of double of the length ``atom->nlocal``. allocated block of storage of double of the length ``atom->nlocal``.
To avoid a memory leak, also this pointer needs to be freed after use. Since the data is returned a copy, the location will persist, but its
content will not be updated, in case the variable is re-evaluated.
To avoid a memory leak this pointer needs to be freed after use in
the calling program.
Since the data is returned as copies, the location will persist, but its For other variable styles the returned pointer needs to be cast to
values will not be updated, in case the variable is re-evaluated. a char pointer.
.. code-block:: c
const char *cptr = (const char *) lammps_extract_variable(handle,name,NULL);
printf("The value of variable %s is %s\n", name, cptr);
.. note:: .. note::
@ -1858,7 +1866,7 @@ values will not be updated, in case the variable is re-evaluated.
* \return pointer (cast to ``void *``) to the location of the * \return pointer (cast to ``void *``) to the location of the
* requested data or ``NULL`` if not found. */ * requested data or ``NULL`` if not found. */
void *lammps_extract_variable(void *handle, char *name, char *group) void *lammps_extract_variable(void *handle, const char *name, const char *group)
{ {
LAMMPS *lmp = (LAMMPS *) handle; LAMMPS *lmp = (LAMMPS *) handle;
@ -1871,9 +1879,7 @@ void *lammps_extract_variable(void *handle, char *name, char *group)
double *dptr = (double *) malloc(sizeof(double)); double *dptr = (double *) malloc(sizeof(double));
*dptr = lmp->input->variable->compute_equal(ivar); *dptr = lmp->input->variable->compute_equal(ivar);
return (void *) dptr; return (void *) dptr;
} } else if (lmp->input->variable->atomstyle(ivar)) {
if (lmp->input->variable->atomstyle(ivar)) {
if (group == nullptr) group = (char *)"all"; if (group == nullptr) group = (char *)"all";
int igroup = lmp->group->find(group); int igroup = lmp->group->find(group);
if (igroup < 0) return nullptr; if (igroup < 0) return nullptr;
@ -1881,6 +1887,8 @@ void *lammps_extract_variable(void *handle, char *name, char *group)
double *vector = (double *) malloc(nlocal*sizeof(double)); double *vector = (double *) malloc(nlocal*sizeof(double));
lmp->input->variable->compute_atom(ivar,igroup,vector,1,0); lmp->input->variable->compute_atom(ivar,igroup,vector,1,0);
return (void *) vector; return (void *) vector;
} else {
return lmp->input->variable->retrieve(name);
} }
} }
END_CAPTURE END_CAPTURE

View File

@ -140,7 +140,7 @@ void *lammps_extract_atom(void *handle, const char *name);
void *lammps_extract_compute(void *handle, char *id, int, int); void *lammps_extract_compute(void *handle, char *id, int, int);
void *lammps_extract_fix(void *handle, char *, int, int, int, int); void *lammps_extract_fix(void *handle, char *, int, int, int, int);
void *lammps_extract_variable(void *handle, char *, char *); void *lammps_extract_variable(void *handle, const char *, const char *);
int lammps_set_variable(void *, char *, char *); int lammps_set_variable(void *, char *, char *);
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------