def test_call_PM_solver(g0_wk, gamma): with patch("triqs_tprf.eliashberg.power_method_LR") as patched: patched.return_value = 0.0, g0_wk.data.flatten() solve_eliashberg(gamma, g0_wk, solver="PM") patched.assert_called()
def test_invalid_symmetrize_function(g0_wk, gamma): invalid_symmetrize_fct = lambda x: 1 try: solve_eliashberg(gamma, g0_wk, symmetrize_fct=invalid_symmetrize_fct) except AttributeError as e: assert str(e) == "'int' object has no attribute 'data'"
def test_call_eliashberg_product(g0_wk, gamma): with patch("triqs_tprf.eliashberg.eliashberg_product") as patched: patched.return_value = g0_wk solve_eliashberg(gamma, g0_wk, product="SUM") patched.assert_called()
def test_initial_delta_input(patched_solver, g0_wk, gamma): patched_solver.return_value = [0.0], [g0_wk.data.flatten()] with patch("triqs_tprf.eliashberg.semi_random_initial_delta") as patched: patched.return_value = g0_wk solve_eliashberg(gamma, g0_wk, initial_delta=g0_wk) patched.assert_not_called()
def test_wrong_input_for_solver(g0_wk, gamma): wrong_input = "false" try: solve_eliashberg(gamma, g0_wk, solver=wrong_input) except NotImplementedError as e: expected_message = "There is no solver called %s." % wrong_input assert str(e) == expected_message
def test_call_IRAM_solver(g0_wk, gamma): with patch("triqs_tprf.eliashberg.implicitly_restarted_arnoldi_method" ) as patched: patched.return_value = [0.0], [g0_wk.data.flatten()] solve_eliashberg(gamma, g0_wk, solver="IRAM") patched.assert_called()
def test_call_symmetrize_function(patched_product, g0_wk, gamma): patched_product.return_value = g0_wk symmetrize_fct = MagicMock() symmetrize_fct.return_value = g0_wk solve_eliashberg(gamma, g0_wk, symmetrize_fct=symmetrize_fct) symmetrize_fct.assert_called()
def test_call_eliashberg_product_fft_constant(g0_wk, gamma): with patch("triqs_tprf.eliashberg.eliashberg_product_fft_constant" ) as patched: patched.return_value = g0_wk non_dynamic_gamma = 0 * gamma solve_eliashberg(non_dynamic_gamma, g0_wk, product="FFT") patched.assert_called()
def test_wrong_input_for_product(g0_wk, gamma): wrong_input = "false" try: solve_eliashberg(gamma, g0_wk, product=wrong_input) except NotImplementedError as e: expected_message = ( "There is no implementation of the eliashberg product called %s." % wrong_input) assert str(e) == expected_message
def test_tol_used_in_PM(g0_wk, gamma): expected_tol = 1e-10 with patch("triqs_tprf.eliashberg.power_method_LR") as patched: patched.return_value = 0.0, g0_wk.data.flatten() solve_eliashberg(gamma, g0_wk, solver="PM", tol=expected_tol) kwargs = patched.call_args[-1] called_tol = kwargs["tol"] assert called_tol == expected_tol
def test_tol_used_in_IRAM(g0_wk, gamma): expected_tol = 1e-4 with patch("triqs_tprf.eliashberg.implicitly_restarted_arnoldi_method" ) as patched: patched.return_value = [0.0], [g0_wk.data.flatten()] solve_eliashberg(gamma, g0_wk, solver="IRAM", tol=expected_tol) kwargs = patched.call_args[-1] called_tol = kwargs["tol"] assert called_tol == expected_tol
def test_k_input(patched_product, g0_wk, gamma): patched_product.return_value = g0_wk for k_input in [1, 3]: Es, evs = solve_eliashberg(gamma, g0_wk, k=k_input) assert len(Es) == k_input assert len(evs) == k_input
def test_symmetry_constraint_of_solve_eliashberg(g0_wk, gamma, do_plot=False): variables = ["frequency", "momentum", "orbital"] all_symmetries = [('even', 'odd', 'even'), ('odd', 'even', 'even')] for symmetries in all_symmetries: symmetrize_fct = functools.partial(enforce_symmetry, variables=variables, symmetries=symmetries) E, eigen_modes = solve_eliashberg(gamma, g0_wk, product='FFT', solver='IRAM', symmetrize_fct=symmetrize_fct) translate_symmetries = {"even": +1, "odd": -1, None: None} expected_symmetries = {variable : translate_symmetries[symmetry] \ for (variable, symmetry) in zip(variables, symmetries)} for delta in eigen_modes: produced_symmetries = check_symmetry(delta) if not expected_symmetries == produced_symmetries: raise AssertionError("Incorrect symmetries were produced.") if do_plot: plot_delta(delta)
def test_solve_eliashberg(g0_wk, gamma, expected_E, expected_eigen_mode): Es, eigen_modes = solve_eliashberg(gamma, g0_wk, product='FFT', solver='IRAM') np.testing.assert_allclose(Es[0], expected_E) assert allclose_by_scalar_multiplication(eigen_modes[0], expected_eigen_mode),\ "Eigenvectors are not the same."
def test_equality_of_eigenvalue_solvers(g0_wk, gamma): initial_delta = semi_random_initial_delta(g0_wk, seed=1337) Es_PM, eigen_modes_PM = solve_eliashberg(gamma, g0_wk, product="FFT", solver="PM", initial_delta=initial_delta) Es_IRAM, eigen_modes_IRAM = solve_eliashberg(gamma, g0_wk, product="FFT", solver="IRAM", initial_delta=initial_delta, k=1) np.testing.assert_allclose(Es_PM[0], Es_IRAM[0]) assert allclose_by_scalar_multiplication( eigen_modes_PM[0], eigen_modes_IRAM[0]), "Eigenvectors are not the same." print("Both solvers yield the same results.")
def save_new_benchmarks(filename, p): eliashberg_ingredients = create_eliashberg_ingredients(p) g0_wk = eliashberg_ingredients.g0_wk gamma = eliashberg_ingredients.gamma Gamma_pp_dyn_tr, Gamma_pp_const_r = preprocess_gamma_for_fft(gamma) next_delta = eliashberg_product_fft(Gamma_pp_dyn_tr, Gamma_pp_const_r, g0_wk, g0_wk) Es, eigen_modes = solve_eliashberg(gamma, g0_wk, product='FFT', solver='IRAM') p.next_delta = next_delta p.E = Es[0] p.eigen_mode = eigen_modes[0] write_TarGZ_HDFArchive(filename, p=p)
next_delta = eliashberg_product(gamma, g0_wk, init_delta) # For the next steps the benchmark data differs more from the TPRF results. This is # properly due to the Fourier transformation version of the eliashberg product # in the benchmark data and the use of different eigenvalue search algorithms. atol = 10**(-6) # Compare the output to the benchmark data np.testing.assert_allclose(next_delta.data.reshape(-1, nk**2), next_delta_ref, atol=atol) # -- Solve the Eliashberg equation for the maximum eigenvalue and its eigenvector E, eigen_modes = solve_eliashberg(gamma, g0_wk) # Compare the eigenvalue to the benchmark data np.testing.assert_allclose(E[0], lamb_ref) # Helper function to compare eigenvectors with a phase def rotate_to_real_plane(arr): if np.max(np.abs(arr.imag)) < 10**(-8): return arr angles = np.angle(arr[np.abs(arr) > 10**(-8)]) # Check if eigenvector has a global phase np.testing.assert_allclose(np.max(angles) - np.min(angles),
gamma_big = gamma_PP_singlet(chi_c_big, chi_s_big, U_c, U_s) # -- Preprocess gamma for the FFT implementation gamma_dyn_wk, gamma_const_k = split_into_dynamic_wk_and_constant_k(gamma_big) gamma_dyn_tr, gamma_const_r = dynamic_and_constant_to_tr(gamma_dyn_wk, gamma_const_k) # -- Test the Eliashberg equation next_delta = eliashberg_product(gamma_big, g0_wk, g0_wk) next_delta_fft = eliashberg_product_fft(gamma_dyn_tr, gamma_const_r, g0_wk, g0_wk) np.testing.assert_allclose(next_delta.data, next_delta_fft.data, atol=1e-7) Es, eigen_modes = solve_eliashberg(gamma_big, g0_wk) Es_fft, eigen_modes_fft = solve_eliashberg_fft(gamma_big, g0_wk) E = Es[0] eigen_mode = eigen_modes[0] E_fft = Es_fft[0] eigen_mode_fft = eigen_modes_fft[0] np.testing.assert_allclose(E, E_fft, atol=1e-7) try: np.testing.assert_allclose(eigen_mode.data, eigen_mode_fft.data, atol=1e-7) except AssertionError: np.testing.assert_allclose(-eigen_mode.data, eigen_mode_fft.data, atol=1e-7) print('\nSame results for both implementations of the linearized Eliashberg equation.')