Limit int types to LAMMPS_INT and LAMMPS_INT64

Keep LAMMPS_TAGINT and LAMMPS_BIGINT internal to LAMMPS.
An external caller now only needs to distinguish between 32 and 64bit
integers, doubles and C strings.
This commit is contained in:
Richard Berger
2020-09-18 12:05:37 -04:00
parent 0e81803c8b
commit 1afb355d09
8 changed files with 74 additions and 66 deletions

View File

@ -26,6 +26,8 @@ computes, fixes, or variables in LAMMPS.
----------------------- -----------------------
.. doxygenenum:: _LMP_DATATYPE_CONST
.. doxygenenum:: _LMP_STYLE_CONST .. doxygenenum:: _LMP_STYLE_CONST
.. doxygenenum:: _LMP_TYPE_CONST .. doxygenenum:: _LMP_TYPE_CONST

View File

@ -16,7 +16,7 @@ if len(argv) != 1:
print("Syntax: demo.py") print("Syntax: demo.py")
sys.exit() sys.exit()
from lammps import lammps, LAMMPS_INT, LAMMPS_DOUBLE, LAMMPS_DOUBLE2D, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL, LMP_VAR_ATOM from lammps import lammps, LAMMPS_INT, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL, LMP_VAR_ATOM
lmp = lammps() lmp = lammps()
# test out various library functions after running in.demo # test out various library functions after running in.demo

View File

@ -27,7 +27,7 @@ if len(argv) != 2:
infile = sys.argv[1] infile = sys.argv[1]
from lammps import lammps, LAMMPS_INT, LAMMPS_DOUBLE2D, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL from lammps import lammps, LAMMPS_INT, LMP_STYLE_GLOBAL, LMP_VAR_EQUAL
lmp = lammps() lmp = lammps()
# run infile one line at a time # run infile one line at a time

View File

@ -36,13 +36,12 @@ import sys
# in certain calls to select data formats # in certain calls to select data formats
LAMMPS_AUTODETECT = None LAMMPS_AUTODETECT = None
LAMMPS_INT = 0 LAMMPS_INT = 0
LAMMPS_INT2D = 1 LAMMPS_INT_2D = 1
LAMMPS_DOUBLE = 2 LAMMPS_DOUBLE = 2
LAMMPS_DOUBLE2D = 3 LAMMPS_DOUBLE_2D = 3
LAMMPS_BIGINT = 4 LAMMPS_INT64 = 4
LAMMPS_TAGINT = 5 LAMMPS_INT64_2D = 5
LAMMPS_STRING = 6 LAMMPS_STRING = 6
LAMMPS_TAGINT2D = 7
# these must be kept in sync with the enums in library.h # these must be kept in sync with the enums in library.h
LMP_STYLE_GLOBAL = 0 LMP_STYLE_GLOBAL = 0
@ -524,7 +523,7 @@ class lammps(object):
else: else:
nelem = self.lmp.extract_global("nlocal") nelem = self.lmp.extract_global("nlocal")
if dim == LAMMPS_AUTODETECT: if dim == LAMMPS_AUTODETECT:
if dtype in (LAMMPS_INT2D, LAMMPS_DOUBLE2D, LAMMPS_TAGINT2D): if dtype in (LAMMPS_INT_2D, LAMMPS_DOUBLE_2D, LAMMPS_INT64_2D):
# TODO add other fields # TODO add other fields
if name in ("x", "v", "f", "angmom", "torque", "csforce", "vforce"): if name in ("x", "v", "f", "angmom", "torque", "csforce", "vforce"):
dim = 3 dim = 3
@ -535,14 +534,12 @@ class lammps(object):
raw_ptr = self.lmp.extract_atom(name, dtype) raw_ptr = self.lmp.extract_atom(name, dtype)
if dtype in (LAMMPS_DOUBLE, LAMMPS_DOUBLE2D): if dtype in (LAMMPS_DOUBLE, LAMMPS_DOUBLE_2D):
return self.darray(raw_ptr, nelem, dim) return self.darray(raw_ptr, nelem, dim)
elif dtype in (LAMMPS_INT, LAMMPS_INT2D): elif dtype in (LAMMPS_INT, LAMMPS_INT_2D):
return self.iarray(c_int, raw_ptr, nelem, dim) return self.iarray(c_int32, raw_ptr, nelem, dim)
elif dtype in (LAMMPS_TAGINT, LAMMPS_TAGINT2D): elif dtype in (LAMMPS_INT64, LAMMPS_INT64_2D):
return self.iarray(self.lmp.c_tagint, raw_ptr, nelem, dim) return self.iarray(c_int64, raw_ptr, nelem, dim)
elif dtype == LAMMPS_BIGINT:
return self.iarray(self.lmp.c_bigint, raw_ptr, nelem, dim)
return raw_ptr return raw_ptr
def extract_atom_iarray(self, name, nelem, dim=1): def extract_atom_iarray(self, name, nelem, dim=1):
@ -558,7 +555,7 @@ class lammps(object):
if dim == 1: if dim == 1:
raw_ptr = self.lmp.extract_atom(name, LAMMPS_INT) raw_ptr = self.lmp.extract_atom(name, LAMMPS_INT)
else: else:
raw_ptr = self.lmp.extract_atom(name, LAMMPS_INT2D) raw_ptr = self.lmp.extract_atom(name, LAMMPS_INT_2D)
return self.iarray(c_int_type, raw_ptr, nelem, dim) return self.iarray(c_int_type, raw_ptr, nelem, dim)
@ -568,7 +565,7 @@ class lammps(object):
if dim == 1: if dim == 1:
raw_ptr = self.lmp.extract_atom(name, LAMMPS_DOUBLE) raw_ptr = self.lmp.extract_atom(name, LAMMPS_DOUBLE)
else: else:
raw_ptr = self.lmp.extract_atom(name, LAMMPS_DOUBLE2D) raw_ptr = self.lmp.extract_atom(name, LAMMPS_DOUBLE_2D)
return self.darray(raw_ptr, nelem, dim) return self.darray(raw_ptr, nelem, dim)
@ -889,7 +886,7 @@ class lammps(object):
This function returns ``None`` if the keyword is not This function returns ``None`` if the keyword is not
recognized. Otherwise it will return a positive integer value that recognized. Otherwise it will return a positive integer value that
corresponds to one of the constants define in the :py:mod:`lammps` module: corresponds to one of the constants define in the :py:mod:`lammps` module:
``LAMMPS_INT``, ``LAMMPS_INT2D``, ``LAMMPS_DOUBLE``, ``LAMMPS_DOUBLE2D``, ``LAMMPS_INT``, ``LAMMPS_INT_2D``, ``LAMMPS_DOUBLE``, ``LAMMPS_DOUBLE_2D``,
``LAMMPS_BIGINT``, ``LAMMPS_TAGINT``, ``LAMMPS_TAGINT2D``, and ``LAMMPS_STRING``. ``LAMMPS_BIGINT``, ``LAMMPS_TAGINT``, ``LAMMPS_TAGINT2D``, and ``LAMMPS_STRING``.
:param name: name of the property :param name: name of the property
@ -915,12 +912,11 @@ class lammps(object):
includes a list of the supported keywords and their data types. includes a list of the supported keywords and their data types.
Since Python needs to know the data type to be able to interpret Since Python needs to know the data type to be able to interpret
the result, by default, this function will try to auto-detect the datatype the result, by default, this function will try to auto-detect the datatype
by asking the library. You can also force a specific data type. For by asking the library. You can also force a specific data type. For that
that purpose the :py:mod:`lammps` module contains the constants purpose the :py:mod:`lammps` module contains the constants ``LAMMPS_INT``,
``LAMMPS_INT``, ``LAMMPS_DOUBLE``, ``LAMMPS_BIGINT``, ``LAMMPS_DOUBLE``, ``LAMMPS_INT64``, and ``LAMMPS_STRING``.
``LAMMPS_TAGINT``, and ``LAMMPS_STRING``. This function returns ``None`` if either the keyword is not recognized,
This function returns ``None`` if either the keyword is not or an invalid data type constant is used.
recognized, or an invalid data type constant is used.
:param name: name of the property :param name: name of the property
:type name: string :type name: string
@ -936,13 +932,11 @@ class lammps(object):
else: return None else: return None
if dtype == LAMMPS_INT: if dtype == LAMMPS_INT:
self.lib.lammps_extract_global.restype = POINTER(c_int) self.lib.lammps_extract_global.restype = POINTER(c_int32)
elif dtype == LAMMPS_DOUBLE: elif dtype == LAMMPS_DOUBLE:
self.lib.lammps_extract_global.restype = POINTER(c_double) self.lib.lammps_extract_global.restype = POINTER(c_double)
elif dtype == LAMMPS_BIGINT: elif dtype == LAMMPS_INT64:
self.lib.lammps_extract_global.restype = POINTER(self.c_bigint) self.lib.lammps_extract_global.restype = POINTER(c_int64)
elif dtype == LAMMPS_TAGINT:
self.lib.lammps_extract_global.restype = POINTER(self.c_tagint)
elif dtype == LAMMPS_STRING: elif dtype == LAMMPS_STRING:
self.lib.lammps_extract_global.restype = c_char_p self.lib.lammps_extract_global.restype = c_char_p
ptr = self.lib.lammps_extract_global(self.lmp, name) ptr = self.lib.lammps_extract_global(self.lmp, name)
@ -965,8 +959,8 @@ class lammps(object):
This function returns ``None`` if the keyword is not This function returns ``None`` if the keyword is not
recognized. Otherwise it will return a positive integer value that recognized. Otherwise it will return a positive integer value that
corresponds to one of the constants define in the :py:mod:`lammps` module: corresponds to one of the constants define in the :py:mod:`lammps` module:
``LAMMPS_INT``, ``LAMMPS_INT2D``, ``LAMMPS_DOUBLE``, ``LAMMPS_DOUBLE2D``, ``LAMMPS_INT``, ``LAMMPS_INT_2D``, ``LAMMPS_DOUBLE``, ``LAMMPS_DOUBLE_2D``,
``LAMMPS_BIGINT``, ``LAMMPS_TAGINT``, ``LAMMPS_TAGINT2D``, and ``LAMMPS_STRING``. ``LAMMPS_INT64``, ``LAMMPS_INT64_2D``, and ``LAMMPS_STRING``.
:param name: name of the property :param name: name of the property
:type name: string :type name: string
@ -987,11 +981,11 @@ class lammps(object):
function of the C-library interface. Its documentation includes a function of the C-library interface. Its documentation includes a
list of the supported keywords and their data types. list of the supported keywords and their data types.
Since Python needs to know the data type to be able to interpret Since Python needs to know the data type to be able to interpret
the result, by default, this function will try to auto-detect the datatype the result, by default, this function will try to auto-detect the data type
by asking the library. You can also force a specific data type. For by asking the library. You can also force a specific data type. For
that purpose the :py:mod:`lammps` module contains the constants that purpose the :py:mod:`lammps` module contains the constants
``LAMMPS_INT``, ``LAMMPS_DOUBLE``, ``LAMMPS_BIGINT``, ``LAMMPS_INT``, ``LAMMPS_INT_2D``, ``LAMMPS_DOUBLE``, ``LAMMPS_DOUBLE_2D``,
``LAMMPS_TAGINT``, and ``LAMMPS_STRING``. ``LAMMPS_INT64``, ``LAMMPS_INT64_2D``, and ``LAMMPS_STRING``.
This function returns ``None`` if either the keyword is not This function returns ``None`` if either the keyword is not
recognized, or an invalid data type constant is used. recognized, or an invalid data type constant is used.
@ -1018,19 +1012,17 @@ class lammps(object):
else: return None else: return None
if dtype == LAMMPS_INT: if dtype == LAMMPS_INT:
self.lib.lammps_extract_atom.restype = POINTER(c_int) self.lib.lammps_extract_atom.restype = POINTER(c_int32)
elif dtype == LAMMPS_INT2D: elif dtype == LAMMPS_INT_2D:
self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_int)) self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_int32))
elif dtype == LAMMPS_DOUBLE: elif dtype == LAMMPS_DOUBLE:
self.lib.lammps_extract_atom.restype = POINTER(c_double) self.lib.lammps_extract_atom.restype = POINTER(c_double)
elif dtype == LAMMPS_DOUBLE2D: elif dtype == LAMMPS_DOUBLE_2D:
self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_double)) self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_double))
elif dtype == LAMMPS_TAGINT: elif dtype == LAMMPS_INT64:
self.lib.lammps_extract_atom.restype = POINTER(self.c_tagint) self.lib.lammps_extract_atom.restype = POINTER(c_int64)
elif dtype == LAMMPS_TAGINT2D: elif dtype == LAMMPS_INT64_2D:
self.lib.lammps_extract_atom.restype = POINTER(POINTER(self.c_tagint)) self.lib.lammps_extract_atom.restype = POINTER(POINTER(c_int64))
elif dtype == LAMMPS_BIGINT:
self.lib.lammps_extract_atom.restype = POINTER(self.c_bigint)
else: return None else: return None
ptr = self.lib.lammps_extract_atom(self.lmp, name) ptr = self.lib.lammps_extract_atom(self.lmp, name)
if ptr: return ptr if ptr: return ptr

View File

@ -2611,15 +2611,15 @@ int Atom::extract_datatype(const char *name)
if (strcmp(name,"type") == 0) return LAMMPS_INT; if (strcmp(name,"type") == 0) return LAMMPS_INT;
if (strcmp(name,"mask") == 0) return LAMMPS_INT; if (strcmp(name,"mask") == 0) return LAMMPS_INT;
if (strcmp(name,"image") == 0) return LAMMPS_TAGINT; if (strcmp(name,"image") == 0) return LAMMPS_TAGINT;
if (strcmp(name,"x") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"x") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"v") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"v") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"f") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"f") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"molecule") == 0) return LAMMPS_TAGINT; if (strcmp(name,"molecule") == 0) return LAMMPS_TAGINT;
if (strcmp(name,"q") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"q") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"mu") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"mu") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"omega") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"omega") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"angmom") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"angmom") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"torque") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"torque") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"radius") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"radius") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"rmass") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"rmass") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"ellipsoid") == 0) return LAMMPS_INT; if (strcmp(name,"ellipsoid") == 0) return LAMMPS_INT;
@ -2629,16 +2629,16 @@ int Atom::extract_datatype(const char *name)
if (strcmp(name,"vfrac") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"vfrac") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"s0") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"s0") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"x0") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"x0") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"spin") == 0) return LAMMPS_INT; if (strcmp(name,"spin") == 0) return LAMMPS_INT;
if (strcmp(name,"eradius") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"eradius") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"ervel") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"ervel") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"erforce") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"erforce") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"ervelforce") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"ervelforce") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"cs") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"cs") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"csforce") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"csforce") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"vforce") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"vforce") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name,"etag") == 0) return LAMMPS_INT; if (strcmp(name,"etag") == 0) return LAMMPS_INT;
if (strcmp(name,"rho") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"rho") == 0) return LAMMPS_DOUBLE;
@ -2646,16 +2646,16 @@ int Atom::extract_datatype(const char *name)
if (strcmp(name,"esph") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"esph") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"desph") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"desph") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"cv") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"cv") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"vest") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name,"vest") == 0) return LAMMPS_DOUBLE_2D;
// USER-MESONT package // USER-MESONT package
if (strcmp(name,"length") == 0) return LAMMPS_DOUBLE; if (strcmp(name,"length") == 0) return LAMMPS_DOUBLE;
if (strcmp(name,"buckling") == 0) return LAMMPS_INT; if (strcmp(name,"buckling") == 0) return LAMMPS_INT;
if (strcmp(name,"bond_nt") == 0) return LAMMPS_TAGINT2D; if (strcmp(name,"bond_nt") == 0) return LAMMPS_TAGINT_2D;
if (strcmp(name, "contact_radius") == 0) return LAMMPS_DOUBLE; if (strcmp(name, "contact_radius") == 0) return LAMMPS_DOUBLE;
if (strcmp(name, "smd_data_9") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name, "smd_data_9") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name, "smd_stress") == 0) return LAMMPS_DOUBLE2D; if (strcmp(name, "smd_stress") == 0) return LAMMPS_DOUBLE_2D;
if (strcmp(name, "eff_plastic_strain") == 0) return LAMMPS_DOUBLE; if (strcmp(name, "eff_plastic_strain") == 0) return LAMMPS_DOUBLE;
if (strcmp(name, "eff_plastic_strain_rate") == 0) return LAMMPS_DOUBLE; if (strcmp(name, "eff_plastic_strain_rate") == 0) return LAMMPS_DOUBLE;
if (strcmp(name, "damage") == 0) return LAMMPS_DOUBLE; if (strcmp(name, "damage") == 0) return LAMMPS_DOUBLE;

View File

@ -46,13 +46,12 @@
enum _LMP_DATATYPE_CONST { enum _LMP_DATATYPE_CONST {
LAMMPS_INT = 0, LAMMPS_INT = 0,
LAMMPS_INT2D = 1, LAMMPS_INT_2D = 1,
LAMMPS_DOUBLE = 2, LAMMPS_DOUBLE = 2,
LAMMPS_DOUBLE2D = 3, LAMMPS_DOUBLE_2D = 3,
LAMMPS_BIGINT = 4, LAMMPS_INT64 = 4,
LAMMPS_TAGINT = 5, LAMMPS_INT64_2D = 5,
LAMMPS_STRING = 6, LAMMPS_STRING = 6
LAMMPS_TAGINT2D = 7
}; };
/** Style constants for extracting data from computes and fixes. /** Style constants for extracting data from computes and fixes.

View File

@ -101,6 +101,11 @@ typedef int64_t bigint;
#define ATOTAGINT atoi #define ATOTAGINT atoi
#define ATOBIGINT ATOLL #define ATOBIGINT ATOLL
#define LAMMPS_TAGINT LAMMPS_INT
#define LAMMPS_TAGINT_2D LAMMPS_INT_2D
#define LAMMPS_BIGINT LAMMPS_INT64
#define LAMMPS_BIGINT_2D LAMMPS_INT64_2D
#define IMGMASK 1023 #define IMGMASK 1023
#define IMGMAX 512 #define IMGMAX 512
#define IMGBITS 10 #define IMGBITS 10
@ -133,6 +138,11 @@ typedef int64_t bigint;
#define ATOTAGINT ATOLL #define ATOTAGINT ATOLL
#define ATOBIGINT ATOLL #define ATOBIGINT ATOLL
#define LAMMPS_TAGINT LAMMPS_INT64
#define LAMMPS_TAGINT_2D LAMMPS_INT64_2D
#define LAMMPS_BIGINT LAMMPS_INT64
#define LAMMPS_BIGINT_2D LAMMPS_INT64_2D
#define IMGMASK 2097151 #define IMGMASK 2097151
#define IMGMAX 1048576 #define IMGMAX 1048576
#define IMGBITS 21 #define IMGBITS 21
@ -164,6 +174,11 @@ typedef int bigint;
#define ATOTAGINT atoi #define ATOTAGINT atoi
#define ATOBIGINT atoi #define ATOBIGINT atoi
#define LAMMPS_TAGINT LAMMPS_INT
#define LAMMPS_TAGINT_2D LAMMPS_INT_2D
#define LAMMPS_BIGINT LAMMPS_INT
#define LAMMPS_BIGINT_2D LAMMPS_INT_2D
#define IMGMASK 1023 #define IMGMASK 1023
#define IMGMAX 512 #define IMGMAX 512
#define IMGBITS 10 #define IMGBITS 10

View File

@ -328,7 +328,7 @@ TEST_F(AtomProperties, type)
TEST_F(AtomProperties, position) TEST_F(AtomProperties, position)
{ {
EXPECT_EQ(lammps_extract_atom_datatype(lmp, "x"), LAMMPS_DOUBLE2D); EXPECT_EQ(lammps_extract_atom_datatype(lmp, "x"), LAMMPS_DOUBLE_2D);
double ** x = (double **)lammps_extract_atom(lmp, "x"); double ** x = (double **)lammps_extract_atom(lmp, "x");
ASSERT_NE(x, nullptr); ASSERT_NE(x, nullptr);
EXPECT_DOUBLE_EQ(x[0][0], 1.0); EXPECT_DOUBLE_EQ(x[0][0], 1.0);