Skip to content

Commit

Permalink
completely removed dtype in Spin class
Browse files Browse the repository at this point in the history
It does not belong there, and should have been removed
ages ago.

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Nov 26, 2024
1 parent c6253c8 commit 6dbd9d6
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 227 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ we hit release version 1.0.0.
This yield significant perf. improvements for DFT sparse matrices
with *many* edges in the sparse matrix, but a perf. hit for very
small TB matrices.
- dtype removed from `Spin` class


## [0.15.2] - 2024-11-06
Expand Down
4 changes: 2 additions & 2 deletions src/sisl/io/siesta/binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def write_density_matrix(self, DM, **kwargs):
dm = csr._D[:, : DM.S_idx]

# Ensure shapes (say if only 1 spin)
dm.shape = (-1, len(DM.spin))
dm.shape = (-1, DM.spin.size(DM.dtype))

nsc = DM.geometry.lattice.nsc.astype(np.int32)

Expand Down Expand Up @@ -2583,7 +2583,7 @@ def write_header(self, bz, E, mu: float = 0.0, obj=None):
"""
if obj is None:
obj = bz.parent
nspin = len(obj.spin)
nspin = obj.spin.size(obj.dtype)
cell = obj.geometry.lattice.cell
na_u = obj.geometry.na
no_u = obj.geometry.no
Expand Down
18 changes: 9 additions & 9 deletions src/sisl/io/siesta/siesta_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def read_hamiltonian(self, **kwargs) -> Hamiltonian:
f"{self}.read_hamiltonian requires the stored matrix to be in Ry!"
)

for i in range(len(H.spin)):
for i in range(H.spin.size(H.dtype)):
H._csr._D[:, i] = sp.variables["H"][i, :] * Ry2eV

# fix siesta specific notation
Expand Down Expand Up @@ -281,7 +281,7 @@ def read_density_matrix(self, **kwargs) -> DensityMatrix:
DM = self._r_class_spin(DensityMatrix, **kwargs)

sp = self.groups["SPARSE"]
for i in range(len(DM.spin)):
for i in range(DM.spin.size(DM.dtype)):
DM._csr._D[:, i] = sp.variables["DM"][i, :]

# fix siesta specific notation
Expand All @@ -299,7 +299,7 @@ def read_energy_density_matrix(self, **kwargs) -> EnergyDensityMatrix:
Ef = np.tile(Ef, 2)

sp = self.groups["SPARSE"]
for i in range(len(EDM.spin)):
for i in range(EDM.spin.size(EDM.dtype)):
EDM._csr._D[:, i] = sp.variables["EDM"][i, :] * Ry2eV
if i < 2 and "DM" in sp.variables:
EDM._csr._D[:, i] -= sp.variables["DM"][i, :] * Ef[i]
Expand Down Expand Up @@ -629,7 +629,7 @@ def write_hamiltonian(self, H, **kwargs):
# Ensure that the geometry is written
self.write_geometry(H.geometry)

self._crt_dim(self, "spin", len(H.spin))
self._crt_dim(self, "spin", H.spin.size(H.dtype))

if H.dkind != "f":
raise NotImplementedError(
Expand Down Expand Up @@ -660,7 +660,7 @@ def write_hamiltonian(self, H, **kwargs):
)
v.info = "Hamiltonian"
v.unit = "Ry"
for i in range(len(H.spin)):
for i in range(H.spin.size(H.dtype)):
v[i, :] = csr._D[:, i] / Ry2eV

self._write_settings()
Expand Down Expand Up @@ -688,7 +688,7 @@ def write_density_matrix(self, DM, **kwargs):
# Ensure that the geometry is written
self.write_geometry(DM.geometry)

self._crt_dim(self, "spin", len(DM.spin))
self._crt_dim(self, "spin", DM.spin.size(DM.dtype))

if DM.dkind != "f":
raise NotImplementedError(
Expand Down Expand Up @@ -718,7 +718,7 @@ def write_density_matrix(self, DM, **kwargs):
**self._cmp_args,
)
v.info = "Density matrix"
for i in range(len(DM.spin)):
for i in range(DM.spin.size(DM.dtype)):
v[i, :] = csr._D[:, i]

self._write_settings()
Expand Down Expand Up @@ -746,7 +746,7 @@ def write_energy_density_matrix(self, EDM, **kwargs):
# Ensure that the geometry is written
self.write_geometry(EDM.geometry)

self._crt_dim(self, "spin", len(EDM.spin))
self._crt_dim(self, "spin", EDM.spin.size(EDM.dtype))

if EDM.dkind != "f":
raise NotImplementedError(
Expand Down Expand Up @@ -781,7 +781,7 @@ def write_energy_density_matrix(self, EDM, **kwargs):
)
v.info = "Energy density matrix"
v.unit = "Ry"
for i in range(len(EDM.spin)):
for i in range(EDM.spin.size(EDM.dtype)):
v[i, :] = csr._D[:, i] / Ry2eV

self._write_settings()
Expand Down
6 changes: 3 additions & 3 deletions src/sisl/io/tbtrans/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def write_delta(self, delta, **kwargs):
# Ensure that the geometry is written
self.write_geometry(delta.geometry)

self._crt_dim(self, "spin", len(delta.spin))
self._crt_dim(self, "spin", delta.spin.size(delta.dtype))

# Determine the type of delta we are storing...
k = kwargs.get("k", None)
Expand Down Expand Up @@ -583,7 +583,7 @@ def write_delta(self, delta, **kwargs):
attrs={"info": "Imaginary part of delta", "unit": "Ry"},
**self._cmp_args,
)
for i in range(len(delta.spin)):
for i in range(delta.spin.size(delta.dtype)):
sl[-2] = i
v1[sl] = csr._D[:, i].real * eV2Ry
v2[sl] = csr._D[:, i].imag * eV2Ry
Expand All @@ -598,7 +598,7 @@ def write_delta(self, delta, **kwargs):
attrs={"info": "delta", "unit": "Ry"},
**self._cmp_args,
)
for i in range(len(delta.spin)):
for i in range(delta.spin.size(delta.dtype)):
sl[-2] = i
v[sl] = csr._D[:, i] * eV2Ry

Expand Down
6 changes: 3 additions & 3 deletions src/sisl/physics/densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def close(a, v):
out._csr._D[:, [0, 1]] = out._csr._D[:, [1, 0]]

else:
spin = Spin("nc", dtype=self.dtype)
spin = Spin("nc")
out = self.__class__(
self.geometry,
dtype=self.dtype,
Expand Down Expand Up @@ -305,7 +305,7 @@ def spin_align(self, vec: SeqFloat, atoms: AtomsIndex = None):

elif self.spin.is_polarized:
if vec[:2] @ vec[:2] > 1e-6:
spin = Spin("nc", dtype=self.dtype)
spin = Spin("nc")
out = self.__class__(
self.geometry,
dtype=self.dtype,
Expand Down Expand Up @@ -768,7 +768,7 @@ def density(

DM = _a.emptyz([self.nnz, 2, 2])
idx = _a.array_arange(csr.ptr[:-1], n=csr.ncol)
if self.spin.kind == Spin.NONCOLINEAR:
if self.spin.is_noncolinear:
# non-collinear
DM[:, 0, 0] = csr._D[idx, 0]
DM[:, 0, 1] = csr._D[idx, 2] + 1j * csr._D[idx, 3]
Expand Down
70 changes: 36 additions & 34 deletions src/sisl/physics/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ def _get_spin(M, spin, what: Literal["trace", "box", "vector"] = "box"):
if spin.is_polarized:
m[..., :2] = 0.0
elif spin.is_noncolinear:
if spin.dkind in ("f", "i"):
m[..., 0] = 2 * M[..., 2]
m[..., 1] = -2 * M[..., 3]
else:
if np.iscomplexobj(M):
m[..., 0] = 2 * M[..., 2].real
m[..., 1] = -2 * M[..., 2].imag
else:
m[..., 0] = 2 * M[..., 2]
m[..., 1] = -2 * M[..., 3]
else:
# spin-orbit
if spin.dkind in ("f", "i"):
m[..., 0] = M[..., 2] + M[..., 6]
m[..., 1] = -M[..., 3] + M[..., 7]
else:
if np.iscomplexobj(M):
tmp = M[..., 2].conj() + M[..., 3]
m[..., 0] = tmp.real
m[..., 1] = tmp.imag
else:
m[..., 0] = M[..., 2] + M[..., 6]
m[..., 1] = -M[..., 3] + M[..., 7]
return m

if what == "box":
Expand All @@ -82,27 +82,27 @@ def _get_spin(M, spin, what: Literal["trace", "box", "vector"] = "box"):
m[..., 0, 0] = M[..., 0]
m[..., 1, 1] = M[..., 1]
elif spin.is_noncolinear:
if spin.dkind in ("f", "i"):
if np.iscomplexobj(M):
m[..., 0, 0] = M[..., 0]
m[..., 1, 1] = M[..., 1]
m[..., 0, 1] = M[..., 2]
m[..., 1, 0] = M[..., 2].conj()
else:
m[..., 0, 0] = M[..., 0]
m[..., 1, 1] = M[..., 1]
m[..., 0, 1] = M[..., 2] + 1j * M[..., 3]
m[..., 1, 0] = m[..., 0, 1].conj()
else:
else:
if np.iscomplexobj(M):
m[..., 0, 0] = M[..., 0]
m[..., 1, 1] = M[..., 1]
m[..., 0, 1] = M[..., 2]
m[..., 1, 0] = M[..., 2].conj()
else:
if spin.dkind in ("f", "i"):
m[..., 1, 0] = M[..., 3]
else:
m[..., 0, 0] = M[..., 0] + 1j * M[..., 4]
m[..., 1, 1] = M[..., 1] + 1j * M[..., 5]
m[..., 0, 1] = M[..., 2] + 1j * M[..., 3]
m[..., 1, 0] = M[..., 6] + 1j * M[..., 7]
else:
m[..., 0, 0] = M[..., 0]
m[..., 1, 1] = M[..., 1]
m[..., 0, 1] = M[..., 2]
m[..., 1, 0] = M[..., 3]

return m

Expand Down Expand Up @@ -828,6 +828,8 @@ def __init__(
if isinstance(dim, Spin):
spin = dim
else:
# Back conversion, actually this should depend
# on dtype
spin = {
1: Spin.UNPOLARIZED,
2: Spin.POLARIZED,
Expand All @@ -836,17 +838,17 @@ def __init__(
}.get(dim)
else:
spin = kwargs.pop("spin")
self._spin = Spin(spin, dtype)
self._spin = Spin(spin)

super().__init__(geometry, len(self.spin), self.spin.dtype, nnzpr, **kwargs)
super().__init__(geometry, self.spin.size(dtype), dtype, nnzpr, **kwargs)
self._reset()

def _reset(self):
r"""Reset object according to the options, please refer to `SparseOrbital.reset` for details"""
super()._reset()

# Update the dtype of the spin
self._spin = Spin(self.spin, dtype=self.dtype)
self._spin = Spin(self.spin)

if self.spin.is_unpolarized:
self.UP = 0
Expand All @@ -865,7 +867,7 @@ def _reset(self):
self.dSk = self._dSk

elif self.spin.is_noncolinear:
if self.spin.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
self.M11 = 0
self.M22 = 1
self.M12r = 2
Expand All @@ -882,7 +884,7 @@ def _reset(self):
self.ddSk = self._ddSk_non_colinear

elif self.spin.is_spinorbit:
if self.spin.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
self.SX = np.array([0, 0, 1, 0, 0, 0, 1, 0], self.dtype)
self.SY = np.array([0, 0, 0, -1, 0, 0, 0, 1], self.dtype)
self.SZ = np.array([1, -1, 0, 0, 0, 0, 0, 0], self.dtype)
Expand Down Expand Up @@ -1509,7 +1511,7 @@ def transpose(self, hermitian: bool = False, spin: bool = True, sort: bool = Tru
if sp.is_spinorbit:
if hermitian and spin:
# conjugate the imaginary value and transpose spin-box
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
# imaginary components (including transposing)
# 12,11,22,21
D[:, [3, 4, 5, 7]] = -D[:, [7, 4, 5, 3]]
Expand All @@ -1519,15 +1521,15 @@ def transpose(self, hermitian: bool = False, spin: bool = True, sort: bool = Tru
D[:, [0, 1, 2, 3]] = np.conj(D[:, [0, 1, 3, 2]])
elif hermitian:
# conjugate the imaginary value
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
# imaginary components
# 12,11,22,21
D[:, [3, 4, 5, 7]] *= -1.0
else:
D[:, :] = np.conj(D[:, :])
elif spin:
# transpose spin-box, 12 <-> 21
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
D[:, [2, 3, 6, 7]] = D[:, [6, 7, 2, 3]]
else:
D[:, [2, 3]] = D[:, [3, 2]]
Expand All @@ -1544,7 +1546,7 @@ def transpose(self, hermitian: bool = False, spin: bool = True, sort: bool = Tru
# So for transposing we should negate the sign
# to ensure we put the opposite value in the
# correct place.
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
D[:, 3] = -D[:, 3]
else:
D[:, 2] = np.conj(D[:, 2])
Expand All @@ -1568,7 +1570,7 @@ def trs(self):

# Apply Pauli-Y on the left and right of each spin-box
if sp.is_spinorbit:
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
# [R11, R22, R12, I12, I11, I22, R21, I21]
# [R11, R22] = [R22, R11]
# [I12, I21] = [I21, I12] (conj + Y @ Y[sign-changes conj])
Expand All @@ -1579,7 +1581,7 @@ def trs(self):
else:
raise NotImplementedError
elif sp.is_noncolinear:
if sp.dkind in ("f", "i"):
if self.dkind in ("f", "i"):
# [R11, R22, R12, I12]
D[:, 2] = -D[:, 2]
else:
Expand Down Expand Up @@ -1648,16 +1650,16 @@ def transform(self, matrix=None, dtype=None, spin=None, orthogonal=None):
if spin is None:
spin = self.spin
else:
spin = Spin(spin, dtype)
spin = Spin(spin)

if orthogonal is None:
orthogonal = self.orthogonal

# get dimensions to check
N = n = self.spin.size
N = n = self.spin.size(self.dtype)
if not self.orthogonal:
N += 1
M = m = spin.size
M = m = spin.size(dtype)
if not orthogonal:
M += 1

Expand All @@ -1673,10 +1675,10 @@ def transform(self, matrix=None, dtype=None, spin=None, orthogonal=None):
# ensure the overlap matrix is carried over
matrix[-1, -1] = 1.0

if spin.is_unpolarized and self.spin.size > 1:
if spin.is_unpolarized and self.spin.size(self.dtype) > 1:
# average up and down components
matrix[0, [0, 1]] = 0.5
elif spin.size > 1 and self.spin.is_unpolarized:
elif spin.size(dtype) > 1 and self.spin.is_unpolarized:
# set up and down components to unpolarized value
matrix[[0, 1], 0] = 1.0

Expand Down
Loading

0 comments on commit 6dbd9d6

Please sign in to comment.