add lammps_expand() method and make available in python
This commit is contained in:
@ -178,6 +178,9 @@ class lammps(object):
|
||||
|
||||
self.lib.lammps_error.argtypes = [c_void_p, c_int, c_char_p]
|
||||
|
||||
self.lib.lammps_expand.argtypes = [c_void_p, c_char_p]
|
||||
self.lib.lammps_expand.restype = c_char_p
|
||||
|
||||
self.lib.lammps_file.argtypes = [c_void_p, c_char_p]
|
||||
self.lib.lammps_file.restype = None
|
||||
|
||||
@ -613,6 +616,28 @@ class lammps(object):
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
def expand(self,line):
|
||||
"""Expand a single LAMMPS string like an input line
|
||||
|
||||
This is a wrapper around the :cpp:func:`lammps_expand`
|
||||
function of the C-library interface.
|
||||
|
||||
:param cmd: a single lammps line
|
||||
:type cmd: string
|
||||
:return: expanded string
|
||||
:rtype: string
|
||||
"""
|
||||
if line: newline = line.encode()
|
||||
else: return None
|
||||
|
||||
with ExceptionCheck(self):
|
||||
strptr = self.lib.lammps_expand(self.lmp, newline)
|
||||
rval = strptr.decode()
|
||||
return rval
|
||||
|
||||
return None
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
def file(self, path):
|
||||
"""Read LAMMPS commands from a file.
|
||||
|
||||
|
||||
@ -501,6 +501,51 @@ void lammps_error(void *handle, int error_type, const char *error_text)
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
/** expand a single LAMMPS input line from a string.
|
||||
*
|
||||
\verbatim embed:rst
|
||||
|
||||
This function tells LAMMPS to expand the string in *cmd* like it would process
|
||||
an input line fed to :cpp:func:`lammps_command` **without** executing it.
|
||||
The entire string is considered as command and need not have a
|
||||
(final) newline character. Newline characters in the body of the
|
||||
string, however, will be treated as part of the command and will **not**
|
||||
start a second command.
|
||||
|
||||
The function returns the expanded string in a new string buffer that
|
||||
must be freed with :cpp:func:`lammps_free` after use to avoid a memory leak.
|
||||
|
||||
\endverbatim
|
||||
*
|
||||
* \param handle pointer to a previously created LAMMPS instance
|
||||
* \param cmd string with a single LAMMPS input line
|
||||
* \return string with expanded line */
|
||||
|
||||
char *lammps_expand(void *handle, const char *line)
|
||||
{
|
||||
auto lmp = (LAMMPS *) handle;
|
||||
char *copy, *work;
|
||||
int n, maxcopy, maxwork;
|
||||
|
||||
if (!line) return nullptr;
|
||||
|
||||
BEGIN_CAPTURE
|
||||
{
|
||||
n = strlen(line) + 1;
|
||||
copy = (char *) malloc(n * sizeof(char));
|
||||
work = (char *) malloc(n * sizeof(char));
|
||||
maxwork = maxcopy = n;
|
||||
memcpy(copy, line, maxcopy);
|
||||
lmp->input->substitute(copy, work, maxcopy, maxwork, 0);
|
||||
free(work);
|
||||
}
|
||||
END_CAPTURE
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Library functions to process commands
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@ -133,6 +133,8 @@ void lammps_python_finalize();
|
||||
|
||||
void lammps_error(void *handle, int error_type, const char *error_text);
|
||||
|
||||
char *lammps_expand(void *handle, const char *line);
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* Library functions to process commands
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user