Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelWolloch committed Jun 18, 2024
1 parent 3be4afb commit b8b9dee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
39 changes: 21 additions & 18 deletions tests/calculation/test_partial_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def make_reference_partial_charge(raw_data, selection):
return parchg


def test_read(PartialCharge, Assert):
def test_read(PartialCharge, Assert, not_core):
actual = PartialCharge.read()
expected = PartialCharge.ref
Assert.allclose(actual["bands"], expected.bands)
Expand All @@ -114,31 +114,31 @@ def test_read(PartialCharge, Assert):
Assert.same_structure(actual["structure"], expected.structure.read())


def test_topology(PartialCharge):
def test_topology(PartialCharge, not_core):
actual = PartialCharge._topology()
expected = str(PartialCharge.ref.structure._topology())
assert actual == expected


def test_bands(PartialCharge, Assert):
def test_bands(PartialCharge, Assert, not_core):
actual = PartialCharge.bands()
expected = PartialCharge.ref.bands
Assert.allclose(actual, expected)


def test_kpoints(PartialCharge, Assert):
def test_kpoints(PartialCharge, Assert, not_core):
actual = PartialCharge.kpoints()
expected = PartialCharge.ref.kpoints
Assert.allclose(actual, expected)


def test_grid(PartialCharge, Assert):
def test_grid(PartialCharge, Assert, not_core):
actual = PartialCharge.grid()
expected = PartialCharge.ref.grid
Assert.allclose(actual, expected)


def test_non_split_to_numpy(PolarizedNonSplitPartialCharge, Assert):
def test_non_split_to_numpy(PolarizedNonSplitPartialCharge, Assert, not_core):
actual = PolarizedNonSplitPartialCharge.to_numpy("total")
expected = PolarizedNonSplitPartialCharge.ref.partial_charge
Assert.allclose(actual, expected[0, 0, 0].T)
Expand All @@ -150,7 +150,7 @@ def test_non_split_to_numpy(PolarizedNonSplitPartialCharge, Assert):
Assert.allclose(actual, 0.5 * (expected[0, 0, 0].T - expected[0, 0, 1].T))


def test_split_to_numpy(PolarizedAllSplitPartialCharge, Assert):
def test_split_to_numpy(PolarizedAllSplitPartialCharge, Assert, not_core):
bands = PolarizedAllSplitPartialCharge.ref.bands
kpoints = PolarizedAllSplitPartialCharge.ref.kpoints
for band_index, band in enumerate(bands):
Expand All @@ -176,28 +176,30 @@ def test_split_to_numpy(PolarizedAllSplitPartialCharge, Assert):
assert msg in str(excinfo.value)


def test_non_polarized_to_numpy(NonSplitPartialCharge, spin, Assert):
def test_non_polarized_to_numpy(NonSplitPartialCharge, spin, Assert, not_core):
actual = NonSplitPartialCharge.to_numpy(selection=spin)
expected = NonSplitPartialCharge.ref.partial_charge
Assert.allclose(actual, np.asarray(expected).T[:, :, :, 0, 0, 0])


def test_split_bands_to_numpy(NonPolarizedBandSplitPartialCharge, spin, Assert):
def test_split_bands_to_numpy(
NonPolarizedBandSplitPartialCharge, spin, Assert, not_core
):
bands = NonPolarizedBandSplitPartialCharge.ref.bands
for band_index, band in enumerate(bands):
actual = NonPolarizedBandSplitPartialCharge.to_numpy(spin, band=band)
expected = NonPolarizedBandSplitPartialCharge.ref.partial_charge
Assert.allclose(actual, np.asarray(expected).T[:, :, :, 0, band_index, 0])


def test_to_stm_split(PolarizedAllSplitPartialCharge):
def test_to_stm_split(PolarizedAllSplitPartialCharge, not_core):
msg = "set LSEPK and LSEPB to .FALSE. in the INCAR file."
with pytest.raises(NotImplemented) as excinfo:
PolarizedAllSplitPartialCharge.to_stm(selection="constant_current")
assert msg in str(excinfo.value)


def test_to_stm_nonsplit_tip_to_high(NonSplitPartialCharge):
def test_to_stm_nonsplit_tip_to_high(NonSplitPartialCharge, not_core):
actual = NonSplitPartialCharge
tip_height = 8.4
error = f"""The tip position at {tip_height:.2f} is above half of the
Expand All @@ -209,27 +211,28 @@ def test_to_stm_nonsplit_tip_to_high(NonSplitPartialCharge):

def test_to_stm_nonsplit_not_orthogonal_no_vacuum(
PolarizedNonSplitPartialChargeSr2TiO4,
not_core,
):
msg = "The vacuum region in your cell is too small for STM simulations."
with pytest.raises(IncorrectUsage) as excinfo:
PolarizedNonSplitPartialChargeSr2TiO4.to_stm()
assert msg in str(excinfo.value)


def test_to_stm_wrong_spin_nonsplit(PolarizedNonSplitPartialCharge):
def test_to_stm_wrong_spin_nonsplit(PolarizedNonSplitPartialCharge, not_core):
msg = "'up', 'down', or 'total'"
with pytest.raises(IncorrectUsage) as excinfo:
PolarizedNonSplitPartialCharge.to_stm(selection="all")
assert msg in str(excinfo.value)


def test_to_stm_wrong_mode(PolarizedNonSplitPartialCharge):
def test_to_stm_wrong_mode(PolarizedNonSplitPartialCharge, not_core):
with pytest.raises(IncorrectUsage) as excinfo:
PolarizedNonSplitPartialCharge.to_stm(selection="stm")
assert "STM mode" in str(excinfo.value)


def test_wrong_vacuum_direction(NonSplitPartialChargeNi_100):
def test_wrong_vacuum_direction(NonSplitPartialChargeNi_100, not_core):
msg = """The vacuum region in your cell is not located along
the third lattice vector."""
with pytest.raises(NotImplemented) as excinfo:
Expand Down Expand Up @@ -312,7 +315,7 @@ def test_to_stm_nonsplit_constant_current_non_ortho(
assert f"{current:.2f}" in actual.title


def test_stm_settings(PolarizedNonSplitPartialCharge):
def test_stm_settings(PolarizedNonSplitPartialCharge, not_core):
actual = dataclasses.asdict(PolarizedNonSplitPartialCharge.stm_settings)
defaults = {
"sigma_xy": 4.0,
Expand All @@ -333,7 +336,7 @@ def test_stm_settings(PolarizedNonSplitPartialCharge):
assert graph.series.settings == modified


def test_smoothening_change(PolarizedNonSplitPartialCharge):
def test_smoothening_change(PolarizedNonSplitPartialCharge, not_core):
mod_settings = STM_settings(sigma_xy=2.0, sigma_z=2.0, truncate=1.0)
data = PolarizedNonSplitPartialCharge.to_numpy("total", band=0, kpoint=0)
default_smoothed_density = PolarizedNonSplitPartialCharge._smooth_stm_data(
Expand All @@ -345,7 +348,7 @@ def test_smoothening_change(PolarizedNonSplitPartialCharge):
assert not np.allclose(default_smoothed_density, new_smoothed_density)


def test_enhancement_setting_change(PolarizedNonSplitPartialCharge, Assert):
def test_enhancement_setting_change(PolarizedNonSplitPartialCharge, Assert, not_core):
enhance_settings = STM_settings(
enhancement_factor=STM_settings().enhancement_factor / 2.0
)
Expand All @@ -356,7 +359,7 @@ def test_enhancement_setting_change(PolarizedNonSplitPartialCharge, Assert):
Assert.allclose(graph_def.series.data, graph_less_enhanced.series.data * 2)


def test_interpolation_setting_change(PolarizedNonSplitPartialCharge):
def test_interpolation_setting_change(PolarizedNonSplitPartialCharge, not_core):
interp_settings = STM_settings(
interpolation_factor=STM_settings().interpolation_factor / 4.0
)
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal_nulp
from scipy.stats import multivariate_normal

from py4vasp import exception, raw
from py4vasp._util import import_

stats = import_.optional("scipy.stats")

number_steps = 4
number_atoms = 7
Expand Down Expand Up @@ -717,7 +719,7 @@ def _partial_charge(selection):
for gy in range(grid_dim[1]):
for gx in range(grid_dim[2]):
m = int(grid_dim[0] / 2) + gy / 10 + gx / 10
val = multivariate_normal(mean=m, cov=cov).pdf(z)
val = stats.multivariate_normal(mean=m, cov=cov).pdf(z)
# Fill the gaussian_charge array
gaussian_charge[:, :, :, :, gy, gx] = val
gaussian_charge = raw.VaspData(gaussian_charge)
Expand Down

0 comments on commit b8b9dee

Please sign in to comment.