def test_sum_of_exponentials(): """ correlation: Test the sum of exponentials. """ tlist = [0.0, 0.5, 1.0, 1.5, 2.0] # Complex coefficients and frequencies. ck1 = [0.5 + 2j, -0.1] vk1 = [-0.5 + 3j, 1 - 1j] corr1 = sum_of_exponentials(ck1, vk1, tlist) y1 = np.array([ 0.4 + 2.0j, -1.67084356 + 0.57764922j, -0.61828702 - 0.92938927j, 0.84201641 + 0.0170242j, 0.68968912 + 1.32694318j, ]) assert_array_almost_equal(corr1, y1) # Real coefficients and frequencies. ck2 = [0.5, -0.3] vk2 = [-0.9, -0.3] corr2 = sum_of_exponentials(ck2, vk2, tlist) y2 = np.array([0.2, 0.060602, -0.018961, -0.061668, -0.081994]) assert_array_almost_equal(corr2, y2)
def test_biexp_fit(): """ correlation: Tests biexponential fitting. """ tlist = np.linspace(0.0, 10, 100) ck = [-0.21, -0.13] vk = [-0.4, -1.5] corr = sum_of_exponentials(ck, vk, tlist) ck_fit, vk_fit = biexp_fit(tlist, corr) corr_fit = sum_of_exponentials(ck_fit, vk_fit, tlist) max_error = np.max(np.abs(corr - corr_fit)) max_amplitude = np.max(np.abs(corr)) assert max_error < max_amplitude / 1e3
"--", color="red", label="Unconstrained fitting") ax1.plot(w, total_spectrum_constrained, "--", color="b", label="Constrained fitting") ax1.set_xlabel(r"$\omega$") ax1.set_ylabel(r"$S(\omega)$") ax1.legend() ax1.set_title("Total spectrum of the correlation") plt.savefig("plots/positivity_spectrum.png", bbox_inches="tight") plt.show() reconstructed_mats = sum_of_exponentials(ck2, vk2, tlist) reconstructed_mats_constrained = sum_of_exponentials(ck2c, vk2c, tlist) #compare original and new fit in terms of correlations fig, [ax1, ax2] = plt.subplots(1, 2, figsize=(13, 4)) ax1.plot(tlist, mats_data_zero, label="Original data") ax1.plot(tlist, reconstructed_mats, linestyle='--', label="Unconstrained") ax1.plot(tlist, reconstructed_mats_constrained, linestyle='-.', label="Constrained") ax1.set_ylabel("Correlation (Matsubara))") ax1.set_xlabel("t") ax1.legend() ax2.plot(tlist,
Calculate Matsubara and Non-matsubara correlations for the underdamped brownian motion spectral density. """ import numpy as np from matsubara.correlation import (nonmatsubara_exponents, matsubara_zero_analytical, biexp_fit, sum_of_exponentials) import matplotlib.pyplot as plt coup_strength, bath_broad, bath_freq = 0.2, 0.05, 1. tlist = np.linspace(0, 100, 1000) # Zero temperature case beta = 1/kT beta = np.inf ck1, vk1 = nonmatsubara_exponents(coup_strength, bath_broad, bath_freq, beta) corr_nonmats = sum_of_exponentials(1j * ck1, vk1, tlist) # Analytical zero temperature calculation of the Matsubara correlation mats_data_zero = matsubara_zero_analytical(coup_strength, bath_broad, bath_freq, tlist) # Fitting a biexponential function ck20, vk20 = biexp_fit(tlist, mats_data_zero) print("Non matsubara coefficients: ", ck1) print("Non matsubara frequencies:", vk1) print("Fitted matsubara coefficients: ", ck20) print("Fitted matsubara frequencies:", vk20) # Plotting the fit and non Matsubara parts corr_fit = sum_of_exponentials(ck20, vk20, tlist)
def test_exponents(): """ correlation: Tests the Matsubara and non Matsubara exponents. """ lam, gamma, w0 = 0.2, 0.05, 1.0 tlist = np.linspace(0, 100, 1000) # Finite temperature cases beta = 0.1 N_exp = 200 ck_nonmats = [0.190164 - 0.004997j, 0.21017 + 0.004997j] vk_nonmats = [-0.025 + 0.999687j, -0.025 - 0.999687j] ck1, vk1 = nonmatsubara_exponents(lam, gamma, w0, beta) assert_array_almost_equal(ck1, ck_nonmats) assert_array_almost_equal(vk1, vk_nonmats) ck2, vk2 = matsubara_exponents(lam, gamma, w0, beta, N_exp) corr_fit = sum_of_exponentials(np.concatenate([ck1, ck2]), np.concatenate([vk1, vk2]), tlist) corr = sum_of_exponentials(ck_nonmats, vk_nonmats, tlist) max_residue = np.max(np.abs(corr_fit - corr)) max_amplitude = np.max(np.abs(corr)) assert_(max_residue < max_amplitude / 1e5) # Lower temperature beta = 1.0 N_exp = 100 ck_nonmats = [0.011636 - 0.00046j, 0.031643 + 0.00046j] vk_nonmats = [-0.025 + 0.999687j, -0.025 - 0.999687j] ck1, vk1 = nonmatsubara_exponents(lam, gamma, w0, beta) assert_array_almost_equal(ck1, ck_nonmats) assert_array_almost_equal(vk1, vk_nonmats) ck2, vk2 = matsubara_exponents(lam, gamma, w0, beta, N_exp) corr_fit = sum_of_exponentials(np.concatenate([ck1, ck2]), np.concatenate([vk1, vk2]), tlist) corr = sum_of_exponentials(ck_nonmats, vk_nonmats, tlist) max_residue = np.max(np.abs(corr_fit - corr)) max_amplitude = np.max(np.abs(corr)) assert_(max_residue < max_amplitude / 1e3) # Zero temperature case beta = np.inf N_exp = 100 ck_nonmats = [0.0, 0.020006] vk_nonmats = [-0.025 + 0.999687j, -0.025 - 0.999687j] ck1, vk1 = nonmatsubara_exponents(lam, gamma, w0, beta) assert_array_almost_equal(ck1, ck_nonmats) assert_array_almost_equal(vk1, vk_nonmats) ck2, vk2 = matsubara_exponents(lam, gamma, w0, beta, N_exp) mats_data_zero = matsubara_zero_analytical(lam, gamma, w0, tlist) ck_mats_zero = [-0.000208, -0.000107] vk_mats_zero = [-1.613416, -0.329604] ck20, vk20 = biexp_fit(tlist, mats_data_zero) assert_array_almost_equal(ck20, ck_mats_zero) assert_array_almost_equal(vk20, vk_mats_zero)