Coverage for gpaw/test/response/test_goldstone.py: 100%
19 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-19 00:19 +0000
1import pytest
2from functools import partial
4import numpy as np
6from gpaw.response.goldstone import find_root
9def parabolic_function(lambd: float, *, root: float):
10 """Define a parabolic function f(λ) with a root nearby λ=1.
12 Defining,
14 f(λ) = aλ² + bλ + c
16 we need the function to be monotonically decreasing in the range
17 λ∊]0.1, 10[. With parametrization
19 (a, b) = (1/4, -6)
21 the minimum lies at λ = -b/(2a) = 12 and the lower root at
22 λ_r = -(b+d)/(2a) = 12 - d/(2a).
24 Solving for c;
26 d = ⎷(b²-4ac) = 2a (12 - λ_r)
28 c = b² - (12 - λ_r)²/4
29 """
30 assert 0.1 < root < 10.
31 a = 1 / 4.
32 b = -6.
33 c = b**2. - (12. - root)**2. / 4.
34 return a * lambd**2. + b * lambd + c
37@pytest.mark.response
38@pytest.mark.parametrize('target', np.linspace(0.4, 2.4, 51))
39def test_find_root(target):
40 fnct = partial(parabolic_function, root=target)
42 def is_converged(value):
43 return 0. <= value < 1e-7
45 root = find_root(fnct, is_converged)
46 assert root == pytest.approx(target)
47 assert is_converged(fnct(root))