apply some codeql recommendations
- remove unused imports - avoid overlaps of variable names with keywords - make some exception processing more explicit
This commit is contained in:
@ -18,9 +18,6 @@ from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
import types
|
||||
import warnings
|
||||
from ctypes import *
|
||||
from os.path import dirname,abspath,join
|
||||
from inspect import getsourcefile
|
||||
@ -47,7 +44,7 @@ class ExceptionCheck:
|
||||
def __enter__(self):
|
||||
pass
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
if self.lmp.has_exceptions and self.lmp.lib.lammps_has_error(self.lmp.lmp):
|
||||
raise self.lmp._lammps_exception
|
||||
|
||||
@ -310,7 +307,7 @@ class lammps(object):
|
||||
# tested to work with mpi4py versions 2 and 3
|
||||
self.has_mpi4py = mpi4py_version.split('.')[0] in ['2','3']
|
||||
except:
|
||||
pass
|
||||
self.has_mpi4py = None
|
||||
|
||||
# if no ptr provided, create an instance of LAMMPS
|
||||
# don't know how to pass an MPI communicator from PyPar
|
||||
@ -878,71 +875,71 @@ class lammps(object):
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
def extract_compute(self,id,style,type):
|
||||
def extract_compute(self,cid,cstyle,ctype):
|
||||
"""Retrieve data from a LAMMPS compute
|
||||
|
||||
This is a wrapper around the :cpp:func:`lammps_extract_compute`
|
||||
function of the C-library interface.
|
||||
This function returns ``None`` if either the compute id is not
|
||||
recognized, or an invalid combination of :ref:`style <py_style_constants>`
|
||||
and :ref:`type <py_type_constants>` constants is used. The
|
||||
recognized, or an invalid combination of :ref:`cstyle <py_style_constants>`
|
||||
and :ref:`ctype <py_type_constants>` constants is used. The
|
||||
names and functionality of the constants are the same as for
|
||||
the corresponding C-library function. For requests to return
|
||||
a scalar or a size, the value is returned, otherwise a pointer.
|
||||
|
||||
:param id: compute ID
|
||||
:type id: string
|
||||
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
|
||||
:type style: int
|
||||
:param type: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
|
||||
:type type: int
|
||||
:param cid: compute ID
|
||||
:type cid: string
|
||||
:param cstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
|
||||
:type cstyle: int
|
||||
:param ctype: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
|
||||
:type ctype: int
|
||||
:return: requested data as scalar, pointer to 1d or 2d double array, or None
|
||||
:rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType
|
||||
"""
|
||||
if id: id = id.encode()
|
||||
if cid: cid = cid.encode()
|
||||
else: return None
|
||||
|
||||
if type == LMP_TYPE_SCALAR:
|
||||
if ctype == LMP_TYPE_SCALAR:
|
||||
if style == LMP_STYLE_GLOBAL:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(c_double)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr[0]
|
||||
elif style == LMP_STYLE_ATOM:
|
||||
return None
|
||||
elif style == LMP_STYLE_LOCAL:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(c_int)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr[0]
|
||||
|
||||
elif type == LMP_TYPE_VECTOR:
|
||||
elif ctype == LMP_TYPE_VECTOR:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(c_double)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr
|
||||
|
||||
elif type == LMP_TYPE_ARRAY:
|
||||
elif ctype == LMP_TYPE_ARRAY:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(POINTER(c_double))
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr
|
||||
|
||||
elif type == LMP_SIZE_COLS:
|
||||
if style == LMP_STYLE_GLOBAL \
|
||||
or style == LMP_STYLE_ATOM \
|
||||
or style == LMP_STYLE_LOCAL:
|
||||
elif ctype == LMP_SIZE_COLS:
|
||||
if cstyle == LMP_STYLE_GLOBAL \
|
||||
or cstyle == LMP_STYLE_ATOM \
|
||||
or cstyle == LMP_STYLE_LOCAL:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(c_int)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr[0]
|
||||
|
||||
elif type == LMP_SIZE_VECTOR or type == LMP_SIZE_ROWS:
|
||||
if style == LMP_STYLE_GLOBAL \
|
||||
or style == LMP_STYLE_LOCAL:
|
||||
elif ctype == LMP_SIZE_VECTOR or ctype == LMP_SIZE_ROWS:
|
||||
if cstyle == LMP_STYLE_GLOBAL \
|
||||
or cstyle == LMP_STYLE_LOCAL:
|
||||
self.lib.lammps_extract_compute.restype = POINTER(c_int)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type)
|
||||
ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
|
||||
return ptr[0]
|
||||
|
||||
return None
|
||||
@ -952,25 +949,25 @@ class lammps(object):
|
||||
# in case of global data, free memory for 1 double via lammps_free()
|
||||
# double was allocated by library interface function
|
||||
|
||||
def extract_fix(self,id,style,type,nrow=0,ncol=0):
|
||||
def extract_fix(self,fid,fstyle,ftype,nrow=0,ncol=0):
|
||||
"""Retrieve data from a LAMMPS fix
|
||||
|
||||
This is a wrapper around the :cpp:func:`lammps_extract_fix`
|
||||
function of the C-library interface.
|
||||
This function returns ``None`` if either the fix id is not
|
||||
recognized, or an invalid combination of :ref:`style <py_style_constants>`
|
||||
and :ref:`type <py_type_constants>` constants is used. The
|
||||
recognized, or an invalid combination of :ref:`fstyle <py_style_constants>`
|
||||
and :ref:`ftype <py_type_constants>` constants is used. The
|
||||
names and functionality of the constants are the same as for
|
||||
the corresponding C-library function. For requests to return
|
||||
a scalar or a size, the value is returned, also when accessing
|
||||
global vectors or arrays, otherwise a pointer.
|
||||
|
||||
:param id: fix ID
|
||||
:type id: string
|
||||
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
|
||||
:type style: int
|
||||
:param type: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
|
||||
:type type: int
|
||||
:param fid: fix ID
|
||||
:type fid: string
|
||||
:param fstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
|
||||
:type fstyle: int
|
||||
:param ftype: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
|
||||
:type ftype: int
|
||||
:param nrow: index of global vector element or row index of global array element
|
||||
:type nrow: int
|
||||
:param ncol: column index of global array element
|
||||
@ -979,53 +976,53 @@ class lammps(object):
|
||||
:rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType
|
||||
|
||||
"""
|
||||
if id: id = id.encode()
|
||||
if fid: fid = fid.encode()
|
||||
else: return None
|
||||
|
||||
if style == LMP_STYLE_GLOBAL:
|
||||
if type in (LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
|
||||
if fstyle == LMP_STYLE_GLOBAL:
|
||||
if ftype in (LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_double)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol)
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
|
||||
result = ptr[0]
|
||||
self.lib.lammps_free(ptr)
|
||||
return result
|
||||
elif type in (LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
|
||||
elif ftype in (LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_int)
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol)
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
|
||||
return ptr[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
elif style == LMP_STYLE_ATOM:
|
||||
if type == LMP_TYPE_VECTOR:
|
||||
elif fstyle == LMP_STYLE_ATOM:
|
||||
if ftype == LMP_TYPE_VECTOR:
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_double)
|
||||
elif type == LMP_TYPE_ARRAY:
|
||||
elif ftype == LMP_TYPE_ARRAY:
|
||||
self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double))
|
||||
elif type == LMP_SIZE_COLS:
|
||||
elif ftype == LMP_SIZE_COLS:
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_int)
|
||||
else:
|
||||
return None
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol)
|
||||
if type == LMP_SIZE_COLS:
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
|
||||
if ftype == LMP_SIZE_COLS:
|
||||
return ptr[0]
|
||||
else:
|
||||
return ptr
|
||||
|
||||
elif style == LMP_STYLE_LOCAL:
|
||||
if type == LMP_TYPE_VECTOR:
|
||||
elif fstyle == LMP_STYLE_LOCAL:
|
||||
if ftype == LMP_TYPE_VECTOR:
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_double)
|
||||
elif type == LMP_TYPE_ARRAY:
|
||||
elif ftype == LMP_TYPE_ARRAY:
|
||||
self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double))
|
||||
elif type in (LMP_TYPE_SCALAR, LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
|
||||
elif ftype in (LMP_TYPE_SCALAR, LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
|
||||
self.lib.lammps_extract_fix.restype = POINTER(c_int)
|
||||
else:
|
||||
return None
|
||||
with ExceptionCheck(self):
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol)
|
||||
if type in (LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
|
||||
ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
|
||||
if ftype in (LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
|
||||
return ptr
|
||||
else:
|
||||
return ptr[0]
|
||||
|
||||
Reference in New Issue
Block a user