Coverage for gpaw/test/ext_potential/test_constant_e_field.py: 100%
35 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-20 00:19 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-20 00:19 +0000
1import pytest
2import numpy as np
3from ase import Atoms
4from ase.units import Hartree, Bohr
6from gpaw import GPAW
7from gpaw.external import ConstantElectricField
8from gpaw.external import static_polarizability
11@pytest.mark.old_gpaw_only
12def test_ext_potential_constant_e_field(in_tmp_dir):
13 """A proton in an electric field."""
14 h = Atoms('H')
15 h.center(vacuum=2.5)
16 h.calc = GPAW(mode='fd',
17 external=ConstantElectricField(1.0), # 1 V / Ang
18 charge=1,
19 txt='h.txt')
20 e = h.get_potential_energy()
21 f1 = h.get_forces()[0, 2]
22 h[0].z += 0.001
23 de = h.get_potential_energy() - e
24 f2 = -de / 0.001
25 print(f1, f2)
26 assert abs(f1 - 1) < 1e-4
27 assert abs(f2 - 1) < 5e-3
29 # Check writing and reading:
30 h.calc.write('h')
31 vext = GPAW('h', txt=None).hamiltonian.vext
32 assert abs(vext.field_v[2] - 1.0 * Bohr / Hartree) < 1e-13
35@pytest.mark.old_gpaw_only
36def test_polarizability(in_tmp_dir):
37 H2 = Atoms('H2', positions=[(0, 0, 0), (0.7, 0, 0)])
38 H2.center(vacuum=2.5)
39 H2.calc = GPAW(mode='fd', symmetry={'point_group': False})
41 strength = 0.1 # V/Ang
42 alpha_cc = static_polarizability(H2, strength)
44 # make sure no external potential is left over
45 assert H2.calc.parameters.external is None
47 assert alpha_cc.shape == (3, 3)
48 assert alpha_cc == pytest.approx(
49 np.diag([6.48529231e-02, 4.61303856e-2, 4.61303856e-2]))
51 # displace positions and make sure that you can still
52 # get the energy
53 H2[1].position[0] -= 0.001
54 H2.get_potential_energy()