Skip to content

[Code scan] Parse extxyz lattice per frame for PBC bond detection #2449

Description

@njzjz

This issue is a Codex global repository scan finding for deepmodeling/reacnetgenerator at commit 4fa8e2b.

The extxyz detector parses Lattice= only once in _readNfunc() and stores it on self.cell. _readstepfunc() then reuses self.cell for every frame instead of parsing the comment line for the current frame.

Relevant code:

if "Lattice=" in line:
lattice_str = line.split("Lattice=")[1].split('"')[1]
lattice_floats = list(map(float, lattice_str.split()))
self.cell = np.array(lattice_floats).reshape((3, 3))

def _readstepfunc(self, item) -> tuple[list[bytes], tuple[int, int]]:
step, lines = item
step_atoms = []
timestep = step, step # Use step as timestep fallback
boxsize = self.cell
if self.pbc and boxsize is None:
raise RuntimeError("No cell information is given in extxyz.")
for index, line in enumerate(lines):
if index > 1:
s = line.split()
step_atoms.append(
(index - 1, Atom(s[0], tuple(float(x) for x in s[1:4])))
)
_, step_atoms = zip(*sorted(step_atoms, key=operator.itemgetter(0)))
step_atoms = Atoms(step_atoms)
bond, level = self._getbondfromcrd(step_atoms, boxsize)

Impact:

  • variable-cell extxyz trajectories, common for NPT/PBC data, use the first frame's cell for all bond detection
  • bond perception can become wrong when the box changes over time
  • this can alter molecule identity and downstream reaction paths without an explicit error

Suggested fix:

Parse the extxyz metadata/comment line inside _readstepfunc() for each frame and pass that frame-specific lattice to _getbondfromcrd(). Keep the first-frame parsing in _readNfunc() only for atom count/type discovery if needed. Add a regression test with two frames that have different Lattice= values and assert the per-frame cell is used.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions