use a global constant for a common string buffer size

This commit is contained in:
Axel Kohlmeyer
2025-03-23 22:21:05 -04:00
parent 2b718d3b86
commit 9577343429
2 changed files with 21 additions and 16 deletions

View File

@ -50,6 +50,8 @@ LMP_VAR_ATOM = 1
LMP_VAR_VECTOR = 2 LMP_VAR_VECTOR = 2
LMP_VAR_STRING = 3 LMP_VAR_STRING = 3
# default buffer size for string buffers
LMP_BUFSIZE = 1024
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
def get_ctypes_int(size): def get_ctypes_int(size):

View File

@ -29,7 +29,7 @@ from lammps.constants import LAMMPS_AUTODETECT, LAMMPS_STRING, \
LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY, \ LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY, \
LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS, \ LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS, \
LMP_VAR_EQUAL, LMP_VAR_ATOM, LMP_VAR_VECTOR, LMP_VAR_STRING, \ LMP_VAR_EQUAL, LMP_VAR_ATOM, LMP_VAR_VECTOR, LMP_VAR_STRING, \
get_ctypes_int LMP_BUFSIZE, get_ctypes_int
from lammps.data import NeighList from lammps.data import NeighList
@ -347,6 +347,7 @@ class lammps(object):
self.lib.lammps_get_last_error_message.argtypes = [c_void_p, c_char_p, c_int] self.lib.lammps_get_last_error_message.argtypes = [c_void_p, c_char_p, c_int]
self.lib.lammps_get_last_error_message.restype = c_int self.lib.lammps_get_last_error_message.restype = c_int
self.lib.lammps_set_show_error.argtypes = [c_void_p, c_int]
self.lib.lammps_extract_global.argtypes = [c_void_p, c_char_p] self.lib.lammps_extract_global.argtypes = [c_void_p, c_char_p]
self.lib.lammps_extract_global_datatype.argtypes = [c_void_p, c_char_p] self.lib.lammps_extract_global_datatype.argtypes = [c_void_p, c_char_p]
@ -704,8 +705,8 @@ class lammps(object):
:rtype: string :rtype: string
""" """
sb = create_string_buffer(512) sb = create_string_buffer(LMP_BUFSIZE)
self.lib.lammps_get_os_info(sb,512) self.lib.lammps_get_os_info(sb, LMP_BUFSIZE)
return sb.value.decode() return sb.value.decode()
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@ -734,8 +735,8 @@ class lammps(object):
@property @property
def _lammps_exception(self): def _lammps_exception(self):
sb = create_string_buffer(100) sb = create_string_buffer(LMP_BUFSIZE)
error_type = self.lib.lammps_get_last_error_message(self.lmp, sb, 100) error_type = self.lib.lammps_get_last_error_message(self.lmp, sb, LMP_BUFSIZE)
error_msg = sb.value.decode().strip() error_msg = sb.value.decode().strip()
if error_type == 2: if error_type == 2:
@ -2319,8 +2320,9 @@ class lammps(object):
:rtype: string :rtype: string
""" """
sb = create_string_buffer(8192) BUFSIZE = 8192
self.lib.lammps_get_gpu_device_info(sb,8192) sb = create_string_buffer(BUFSIZE)
self.lib.lammps_get_gpu_device_info(sb, BUFSIZE)
return sb.value.decode() return sb.value.decode()
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@ -2337,9 +2339,9 @@ class lammps(object):
if self._installed_packages is None: if self._installed_packages is None:
self._installed_packages = [] self._installed_packages = []
npackages = self.lib.lammps_config_package_count() npackages = self.lib.lammps_config_package_count()
sb = create_string_buffer(100) sb = create_string_buffer(LMP_BUFSIZE)
for idx in range(npackages): for idx in range(npackages):
self.lib.lammps_config_package_name(idx, sb, 100) self.lib.lammps_config_package_name(idx, sb, LMP_BUFSIZE)
self._installed_packages.append(sb.value.decode()) self._installed_packages.append(sb.value.decode())
return self._installed_packages return self._installed_packages
@ -2375,6 +2377,7 @@ class lammps(object):
:return: list of style names in given category :return: list of style names in given category
:rtype: list :rtype: list
""" """
BUFSIZE = 8192
if self._available_styles is None: if self._available_styles is None:
self._available_styles = {} self._available_styles = {}
@ -2382,10 +2385,10 @@ class lammps(object):
self._available_styles[category] = [] self._available_styles[category] = []
with ExceptionCheck(self): with ExceptionCheck(self):
nstyles = self.lib.lammps_style_count(self.lmp, category.encode()) nstyles = self.lib.lammps_style_count(self.lmp, category.encode())
sb = create_string_buffer(100) sb = create_string_buffer(BUFSIZE)
for idx in range(nstyles): for idx in range(nstyles):
with ExceptionCheck(self): with ExceptionCheck(self):
self.lib.lammps_style_name(self.lmp, category.encode(), idx, sb, 100) self.lib.lammps_style_name(self.lmp, category.encode(), idx, sb, BUFSIZE)
self._available_styles[category].append(sb.value.decode()) self._available_styles[category].append(sb.value.decode())
return self._available_styles[category] return self._available_styles[category]
@ -2430,9 +2433,9 @@ class lammps(object):
available_ids = [] available_ids = []
if category in categories: if category in categories:
num = self.lib.lammps_id_count(self.lmp, category.encode()) num = self.lib.lammps_id_count(self.lmp, category.encode())
sb = create_string_buffer(100) sb = create_string_buffer(LMP_BUFSIZE)
for idx in range(num): for idx in range(num):
self.lib.lammps_id_name(self.lmp, category.encode(), idx, sb, 100) self.lib.lammps_id_name(self.lmp, category.encode(), idx, sb, LMP_BUFSIZE)
available_ids.append(sb.value.decode()) available_ids.append(sb.value.decode())
return available_ids return available_ids
@ -2452,10 +2455,10 @@ class lammps(object):
available_plugins = [] available_plugins = []
num = self.lib.lammps_plugin_count(self.lmp) num = self.lib.lammps_plugin_count(self.lmp)
sty = create_string_buffer(100) sty = create_string_buffer(LMP_BUFSIZE)
nam = create_string_buffer(100) nam = create_string_buffer(LMP_BUFSIZE)
for idx in range(num): for idx in range(num):
self.lib.lammps_plugin_name(idx, sty, nam, 100) self.lib.lammps_plugin_name(idx, sty, nam, LMP_BUFSIZE)
available_plugins.append([sty.value.decode(), nam.value.decode()]) available_plugins.append([sty.value.decode(), nam.value.decode()])
return available_plugins return available_plugins