Skip to content

[Code scan] Fix LDOS valence-band slicing and z-scan indexing #40

Description

@njzjz

This issue comes from a Codex global repository scan.

Problem

LDOS.__init__() computes self.n_valbands with /, so it stores a float:

dftio/dftio/calc/ldos.py

Lines 49 to 50 in c9d128f

if z_valence is not None:
self.n_valbands = sum([z_valence[atomic_numbers_r[i]] for i in self.atomic_numbers]) / self.nspin

LDOS.get() later assigns that float to n and uses it as a slice bound:

dftio/dftio/calc/ldos.py

Lines 97 to 105 in c9d128f

if self.n_valbands:
assert n >= self.n_valbands, f"Number of bands must be at least {self.n_valbands}"
n = self.n_valbands
else:
print("Warning: Number of valence bands not provided. All input coeff and eigenvalues are considered corresponding to valence bands.")
assert m >= n
coefficients = coefficients[:,:n]
eigenvalues = eigenvalues[:,:n]

This raises TypeError: slice indices must be integers when z_valence is provided.

LDOS.scan() also has a left-branch off-by-one: ldos_wbias_diff has length nz - 1, but the loop indexes it up to i == nz - 1:

dftio/dftio/calc/ldos.py

Lines 205 to 207 in c9d128f

for i in range(1,nz):
mask = torch.logical_or(mask, ldos_wbias_diff[:,:,i] < 0)
ldos_wbias[:,:,i][mask] = 100

Suggested fix

Validate valence-band divisibility and store an integer band count. In scan(), iterate only over valid diff indices and explicitly write to the corresponding shifted z-plane.

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