pylammps: use library API for variable access

This commit is contained in:
Richard Berger
2023-06-11 23:11:28 -06:00
parent 2f7c3bf959
commit d7ecf41ff6

View File

@ -28,6 +28,7 @@ import tempfile
from collections import namedtuple from collections import namedtuple
from .core import lammps from .core import lammps
from .constants import * # lgtm [py/polluting-import]
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@ -65,22 +66,43 @@ class OutputCapture(object):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
class Variable(object): class Variable(object):
def __init__(self, pylammps_instance, name, style, definition): def __init__(self, pylammps_instance, name):
self._pylmp = pylammps_instance self._pylmp = pylammps_instance
self.name = name self.name = name
self.style = style
self.definition = definition.split() @property
def style(self):
vartype = self._pylmp.lmp.lib.lammps_extract_variable_datatype(self._pylmp.lmp.lmp, self.name.encode())
if vartype == LMP_VAR_EQUAL:
return "equal"
elif vartype == LMP_VAR_ATOM:
return "atom"
elif vartype == LMP_VAR_VECTOR:
return "vector"
elif vartype == LMP_VAR_STRING:
return "string"
return None
@property @property
def value(self): def value(self):
if self.style == 'atom': return self._pylmp.lmp.extract_variable(self.name)
return list(self._pylmp.lmp.extract_variable(self.name, "all", 1))
@value.setter
def value(self, newvalue):
style = self.style
if style == "equal" or style == "string":
self._pylmp.variable("{} {} {}".format(self.name, style, newvalue))
else: else:
value = self._pylmp.lmp_print('"${%s}"' % self.name).strip() raise Exception("Setter not implemented for {} style variables.".format(style))
try:
return float(value) def __str__(self):
except ValueError: value = self.value
return value if isinstance(value, str):
value = "\"{}\"".format(value)
return "Variable(name=\"{}\", value={})".format(self.name, value)
def __repr__(self):
return self.__str__()
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@ -676,11 +698,9 @@ class PyLammps(object):
:getter: Returns a dictionary of all variables that are defined in this LAMMPS instance :getter: Returns a dictionary of all variables that are defined in this LAMMPS instance
:type: dict :type: dict
""" """
output = self.lmp_info("variables")
output = output[output.index("Variable information:")+1:]
variables = {} variables = {}
for v in self._parse_element_list(output): for name in self.lmp.available_ids("variable"):
variables[v['name']] = Variable(self, v['name'], v['style'], v['def']) variables[name] = Variable(self, name)
return variables return variables
def eval(self, expr): def eval(self, expr):