From a038f526931a50d742e65e0326c2092d77c69253 Mon Sep 17 00:00:00 2001 From: YiTian Yang <79531875+Lonya0@users.noreply.github.com> Date: Tue, 2 Dec 2025 11:58:44 +0800 Subject: [PATCH] add structure check tol --- dpnegf/negf/negf_hamiltonian_init.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dpnegf/negf/negf_hamiltonian_init.py b/dpnegf/negf/negf_hamiltonian_init.py index 28868cc..e706f76 100644 --- a/dpnegf/negf/negf_hamiltonian_init.py +++ b/dpnegf/negf/negf_hamiltonian_init.py @@ -83,17 +83,18 @@ def __init__(self, self.structase = structure else: raise ValueError('structure must be ase.Atoms or str') - + # check the structure cell is larger than the range of device and leads # In DPNEGF, the whole structure should be completely included in the cell # for correct prediction of Hamiltonian and overlap matrix. # TODO: Add support for non-ortho cell - xrange,yrange,zrange = self.structase.positions[:,0].max()-self.structase.positions[:,0].min(),\ - self.structase.positions[:,1].max()-self.structase.positions[:,1].min(),\ - self.structase.positions[:,2].max()-self.structase.positions[:,2].min() - if xrange > self.structase.cell[0][0] or yrange > self.structase.cell[1][1] or zrange > self.structase.cell[2][2]: - log.error(msg="The structure cell is smaller than the range of device and leads.") - raise ValueError + tol = 1e-8 # If the difference is less than or equal to tol, it is considered to meet the standard (allowing machine error) + escape_distances = [self.structase.positions[:, dim].max() - self.structase.positions[:, dim].min() - + self.structase.cell[dim][dim] for dim in [0, 1, 2]] + if max(escape_distances) > tol: + msg = f"The structure cell is smaller than the range of device and leads. Out bound distance:{max(escape_distances)}" + log.error(msg=msg) + raise ValueError(msg) self.unit = unit self.stru_options = stru_options