Skip to content

Commit

Permalink
fixed wrong mulliken calculation for polarized calculations, #611
Browse files Browse the repository at this point in the history
This happened because we were forgetting to do a copy of the
element matrix. This resulted in creating a view, but calculating
on the same array leaving the exact calculation wrong.
Essentially the old Mulliken was non-sense
 M[0] was M[0] + M[0] - M[1]

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Sep 12, 2023
1 parent 6935c06 commit 1f705f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ we hit release version 1.0.0.


### Fixed
- fixed Mulliken calculations for polarized calculations due to missing copy, #611
- fixed single argument `ret_isc=True` of `close`, #604 and #605
- tiling Grid now only possible for commensurate grids (grid.lattice % grid.geometry.lattice)
- rare cases for non-Gamma calculations with actual Gamma matrices resulted
Expand Down
3 changes: 2 additions & 1 deletion src/sisl/physics/densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ def _convert(M):
M[3] = 0.5 * (M[3] - M[7]) # sign change again below
M = M[:4]
elif M.shape[0] == 2:
tmp = M[1]
# necessary to not overwrite data
tmp = M[1].copy()
M[1] = M[0] - M[1]
M[0] += tmp
elif M.shape[0] == 1:
Expand Down
46 changes: 35 additions & 11 deletions src/sisl/physics/tests/test_density_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def setup():
class t():
def __init__(self):
bond = 1.42
self.bond = bond = 1.42
sq3h = 3.**.5 * 0.5
self.lattice = Lattice(np.array([[1.5, sq3h, 0.],
[1.5, -sq3h, 0.],
Expand Down Expand Up @@ -127,14 +127,38 @@ def test_mulliken_values_non_orthogonal(self, setup):
assert mulliken[0] == pytest.approx(4)
assert mulliken.sum() == pytest.approx(4)

def test_mulliken_polarized(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
[1.5, -sq3h, 0.],
[0., 0., 10.]], np.float64) * bond, nsc=[3, 3, 1])

orb = AtomicOrbital('px', R=bond * 1.001)
C = Atom(6, orb)
g = Geometry(np.array([[0., 0., 0.],
[1., 0., 0.]], np.float64) * bond,
atoms=C, lattice=lattice)
D = DensityMatrix(g, spin=Spin('P'))
# 1 charge onsite for each spin-up
# 0.5 charge onsite for each spin-down
D.construct([[0.1, bond + 0.01], [(1., 0.5), (0.1, 0.1)]])

m = D.mulliken("orbital")
assert m[0].sum() == pytest.approx(3)
assert m[1].sum() == pytest.approx(1)
m = D.mulliken("atom")
assert m[0].sum() == pytest.approx(3)
assert m[1].sum() == pytest.approx(1)

def test_rho1(self, setup):
D = setup.D.copy()
D.construct(setup.func)
grid = Grid(0.2, geometry=setup.D.geometry)
D.density(grid)

@pytest.mark.filterwarnings("ignore", message="*is NOT Hermitian for on-site")
def test_rho2(self, setup):
def test_rho2(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand Down Expand Up @@ -178,7 +202,7 @@ def test_rho2(self, setup):
D.density(grid, Spin.Z)

@pytest.mark.filterwarnings("ignore", message="*is NOT Hermitian for on-site")
def test_orbital_momentum(self, setup):
def test_orbital_momentum(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -195,7 +219,7 @@ def test_orbital_momentum(self, setup):
D.orbital_momentum("atom")
D.orbital_momentum("orbital")

def test_spin_align_pol(self, setup):
def test_spin_align_pol(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -220,7 +244,7 @@ def test_spin_align_pol(self, setup):
assert not np.allclose(D_mull[1], d_mull[3])
assert np.allclose(D_mull[0], d_mull[0])

def test_spin_align_nc(self, setup):
def test_spin_align_nc(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -242,7 +266,7 @@ def test_spin_align_nc(self, setup):
assert np.allclose(D_mull[0], d_mull[0])

@pytest.mark.filterwarnings("ignore", message="*is NOT Hermitian for on-site")
def test_spin_align_so(self, setup):
def test_spin_align_so(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -263,7 +287,7 @@ def test_spin_align_so(self, setup):
assert not np.allclose(D_mull, d_mull)
assert np.allclose(D_mull[0], d_mull[0])

def test_spin_rotate_pol(self, setup):
def test_spin_rotate_pol(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -288,7 +312,7 @@ def test_spin_rotate_pol(self, setup):
assert not np.allclose(D_mull[1], d_mull[3])
assert np.allclose(D_mull[0], d_mull[0])

def test_spin_rotate_nc(self, setup):
def test_spin_rotate_nc(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -312,7 +336,7 @@ def test_spin_rotate_nc(self, setup):
assert np.allclose(D_mull[0], d_mull[0])

@pytest.mark.filterwarnings("ignore", message="*is NOT Hermitian for on-site")
def test_spin_rotate_so(self, setup):
def test_spin_rotate_so(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand Down Expand Up @@ -345,7 +369,7 @@ def test_rho_smaller_grid1(self, setup):
grid = Grid(0.2, geometry=setup.D.geometry.copy(), lattice=lattice)
D.density(grid)

def test_rho_fail_p(self, setup):
def test_rho_fail_p(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand All @@ -367,7 +391,7 @@ def test_rho_fail_p(self, setup):
with pytest.raises(ValueError):
D.density(grid, [1., -1, 0.])

def test_rho_fail_nc(self, setup):
def test_rho_fail_nc(self):
bond = 1.42
sq3h = 3.**.5 * 0.5
lattice = Lattice(np.array([[1.5, sq3h, 0.],
Expand Down

0 comments on commit 1f705f5

Please sign in to comment.