Skip to content

[Code scan] Make find_neighbors handle mixed PrimitiveCell and SuperCell inputs #18

Description

@njzjz

This issue comes from a Codex global scan of deepmodeling/tbplas at commit 4d3652b.

Severity: Medium

The signature allows cell_bra and cell_ket to be either PrimitiveCell or SuperCell, but the implementation dispatches position access mostly from the type of cell_bra. Mixed calls such as primitive-to-supercell or supercell-to-primitive can therefore call attributes/methods that do not exist on cell_ket.

Code reference:

def find_neighbors(cell_bra: Union[PrimitiveCell, SuperCell],
cell_ket: Union[PrimitiveCell, SuperCell] = None,
a_max: int = 0,
b_max: int = 0,
c_max: int = 0,
max_distance: float = 1.0) -> List[namedtuple]:
"""
Find neighbours between the (0, 0, 0) cell of model_bra and nearby cells of
cell_ket up to given cutoff distance.
NOTE: only neighbours with distance > 0 will be returned.
The searching range of nearby cells is:
[-a_max, a_max] * [-b_max, b_max] * [-c_max, c_max].
The named tuples have four attributes: rn for cell index, pair for orbital
indices, rij for Cartesian coordinates of displacement vector in nm and
distance for the norm of rij.
:param cell_bra: the 'bra' primitive cell or supercell
:param cell_ket: the 'ket' primitive cell or supercell
default to pc_ket if not set
:param a_max: upper bound of range on a-axis
:param b_max: upper bound of range on b-axis
:param c_max: upper bound of range on c-axis
:param max_distance: cutoff distance in NM
:return: list of neighbors as named tuples
:raise PCOrbEmptyError: if cell_bra or cell_ket does not contain orbitals
"""
if cell_ket is None:
cell_ket = cell_bra
# Check for number of orbitals
if isinstance(cell_bra, PrimitiveCell):
cell_bra.verify_orbitals()
cell_ket.verify_orbitals()
# Get orbital positions
if isinstance(cell_bra, PrimitiveCell):
pos_bra = cell_bra.orb_pos_nm
pos_ket = cell_ket.orb_pos_nm
else:
pos_bra = cell_bra.get_orb_pos()
pos_ket = cell_ket.get_orb_pos()
# Get lattice vectors of cell_ket
if isinstance(cell_ket, PrimitiveCell):
lat_ket = cell_ket.lat_vec
else:
lat_ket = cell_ket.sc_lat_vec

Suggested fix: resolve orbital positions independently for cell_bra and cell_ket, or narrow the accepted API and validate unsupported mixed-type calls explicitly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions