Coverage for gpaw/test/cdft/test_cdft_fd_forces.py: 100%
21 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-09 00:21 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-09 00:21 +0000
1import numpy as np
2import pytest
3from ase import Atoms
5from gpaw import GPAW, Mixer
6from gpaw.cdft.cdft import CDFT
9@pytest.mark.old_gpaw_only
10def test_cdft_fd_forces(in_tmp_dir):
12 sys = Atoms('N2', positions=([0., 0., 0.], [0., 0., 1]))
13 sys.center(3)
14 sys.set_pbc(False)
15 sys.set_initial_magnetic_moments([0.5, 0.5])
17 calc_b = GPAW(h=0.2,
18 basis='dzp',
19 charge=1,
20 mode='lcao',
21 xc='PBE',
22 spinpol=True,
23 mixer=Mixer(beta=0.25, nmaxold=3, weight=100.0),
24 txt='N2.txt',
25 convergence={'eigenstates': 1.0e-2, 'density': 1.0e-2,
26 'energy': 1e-2})
28 sys.calc = calc_b
29 sys.get_potential_energy()
31 cdft_b = CDFT(calc=calc_b,
32 atoms=sys,
33 charge_regions=[[0]],
34 charges=[1],
35 charge_coefs=[22],
36 forces='fd',
37 method='L-BFGS-B',
38 txt='N2.cdft',
39 minimizer_options={'gtol': 0.1})
41 sys.calc = cdft_b
42 sys.get_potential_energy()
43 f = sys.get_forces()
44 # check that forces in x and y directions are small
45 for i in [0, 1]:
46 assert (np.isclose(f[0, i], 0.))
47 assert (np.isclose(f[1, i], 0.))