Coverage for gpaw/test/ext_potential/test_external.py: 93%
43 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
1import pytest
2from ase import Atoms
4from gpaw import GPAW
5from gpaw.external import ConstantPotential
8@pytest.mark.old_gpaw_only
9def test_ext_potential_external():
10 sc = 2.9
11 R = 0.7 # approx. experimental bond length
12 R = 1.0
13 a = 2 * sc
14 c = 3 * sc
15 H2 = Atoms('HH', [(a / 2, a / 2, (c - R) / 2),
16 (a / 2, a / 2, (c + R) / 2)],
17 cell=(a, a, c),
18 pbc=False)
20 txt = None
22 convergence = {'eigenstates': 1.e-4 * 40 * 1.5**3,
23 'density': 1.e-2,
24 'energy': 0.1}
26 # without potential
27 if True:
28 if txt:
29 print('\n################## no potential')
30 c00 = GPAW(mode='fd', h=0.3, nbands=-1,
31 convergence=convergence,
32 txt=txt)
33 c00.calculate(H2)
34 c00.get_eigenvalues()
36 # 0 potential
37 if True:
38 if txt:
39 print('\n################## 0 potential')
40 cp0 = ConstantPotential(0.0)
41 c01 = GPAW(mode='fd', h=0.3, nbands=-2, external=cp0,
42 convergence=convergence,
43 txt=txt)
44 c01.calculate(H2)
46 # 1 potential
47 if True:
48 if txt:
49 print('################## 1 potential')
50 cp1 = ConstantPotential(-1.0)
51 c1 = GPAW(mode='fd', h=0.3, nbands=-2, external=cp1,
52 convergence=convergence,
53 txt=txt)
54 c1.calculate(H2)
56 for i in range(c00.get_number_of_bands()):
57 f00 = c00.get_occupation_numbers()[i]
58 if f00 > 0.01:
59 e00 = c00.get_eigenvalues()[i]
60 e1 = c1.get_eigenvalues()[i]
61 print('Eigenvalues no pot, expected, error=',
62 e00, e1 + 1, e00 - e1 - 1)
63 assert e00 == pytest.approx(e1 + 1., abs=0.008)
65 E_c00 = c00.get_potential_energy()
66 E_c1 = c1.get_potential_energy()
67 DeltaE = E_c00 - E_c1
68 assert DeltaE == pytest.approx(0, abs=0.002)