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

1import pytest 

2from functools import partial 

3 

4import numpy as np 

5 

6from gpaw.response.goldstone import find_root 

7 

8 

9def parabolic_function(lambd: float, *, root: float): 

10 """Define a parabolic function f(λ) with a root nearby λ=1. 

11 

12 Defining, 

13 

14 f(λ) = aλ² + bλ + c 

15 

16 we need the function to be monotonically decreasing in the range 

17 λ∊]0.1, 10[. With parametrization 

18 

19 (a, b) = (1/4, -6) 

20 

21 the minimum lies at λ = -b/(2a) = 12 and the lower root at 

22 λ_r = -(b+d)/(2a) = 12 - d/(2a). 

23 

24 Solving for c; 

25 

26 d = ⎷(b²-4ac) = 2a (12 - λ_r) 

27 

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 

35 

36 

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) 

41 

42 def is_converged(value): 

43 return 0. <= value < 1e-7 

44 

45 root = find_root(fnct, is_converged) 

46 assert root == pytest.approx(target) 

47 assert is_converged(fnct(root))