def get_test_impurity_model(norb=2, ntau=1000, beta=10.0): """ Function that generates a random impurity model for testing """ from triqs.operators import c, c_dag, Operator, dagger from pyed.OperatorUtils import fundamental_operators_from_gf_struct from pyed.OperatorUtils import symmetrize_quartic_tensor from pyed.OperatorUtils import get_quadratic_operator from pyed.OperatorUtils import operator_from_quartic_tensor orb_idxs = list(np.arange(norb)) #print "orb_idxs ", orb_idxs spin_idxs = ['up', 'do'] gf_struct = [[spin_idx, orb_idxs] for spin_idx in spin_idxs] #print "gf_struct", gf_struct # -- Random Hamiltonian fundamental_operators = fundamental_operators_from_gf_struct(gf_struct) #print "fundamental_operators ", fundamental_operators N = len(fundamental_operators) t_OO = np.random.random((N, N)) + 1.j * np.random.random((N, N)) t_OO = 0.5 * (t_OO + np.conj(t_OO.T)) #print "N", N #print "t_OO", t_OO.shape #print 't_OO.real =\n', t_OO.real #print 't_OO.imag =\n', t_OO.imag U_OOOO = np.random.random((N, N, N, N)) + 1.j * np.random.random( (N, N, N, N)) U_OOOO = symmetrize_quartic_tensor(U_OOOO, conjugation=True) #print 'gf_struct =', gf_struct #print 'fundamental_operators = ', fundamental_operators H_loc = get_quadratic_operator(t_OO, fundamental_operators) + \ operator_from_quartic_tensor(U_OOOO, fundamental_operators) #print 'H_loc =', H_loc #print "H_loc.type", H_loc.type() from triqs.gf import MeshImTime, BlockGf mesh = MeshImTime(beta, 'Fermion', ntau) Delta_tau = BlockGf(mesh=mesh, gf_struct=gf_struct) #print "mesh", mesh #print "Delta_tau", Delta_tau for block_name, delta_tau in Delta_tau: delta_tau.data[:] = -0.5 return gf_struct, Delta_tau, H_loc
def __init__(self, beta, gf_struct, n_iw=1025, n_tau=10001, n_l=30, delta_interface=False, complex=False): """Constructor setting up response function parameters Arguments: beta : inverse temperature gf_struct : Triqs Green's function block structure n_iw : number of Matsubara frequencies n_tau : number of imaginary time points """ self.constr_params = { "beta": beta, "gf_struct": gf_struct, "n_iw": n_iw, "n_tau": n_tau, "n_l": n_l, "delta_interface": delta_interface } self.beta = beta self.gf_struct = gf_struct self.n_iw = n_iw self.n_tau = n_tau self.n_l = n_l self.delta_interface = delta_interface self.complex = complex self.tau_mesh = MeshImTime(beta, 'Fermion', n_tau) self.iw_mesh = MeshImFreq(beta, 'Fermion', n_iw) if self.delta_interface: self.Delta_tau = BlockGf(mesh=self.tau_mesh, gf_struct=self.gf_struct) else: self.G0_iw = BlockGf(mesh=self.iw_mesh, gf_struct=gf_struct)
g_tau = GfImTime(name=r'$g$', beta=beta, statistic='Fermion', n_points=50, target_shape=(1,1)) g_iwn = GfImFreq(name='$g$', beta=beta, statistic='Fermion', n_points=10, target_shape=(1,1)) ed.set_g2_tau(g_tau[0,0], c(up,0), c_dag(up,0)) ed.set_g2_iwn(g_iwn[0,0], c(up,0), c_dag(up,0)) # ------------------------------------------------------------------ # -- Two particle Green's functions ntau = 20 imtime = MeshImTime(beta, 'Fermion', ntau) prodmesh = MeshProduct(imtime, imtime, imtime) g40_tau = Gf(name='g40_tau', mesh=prodmesh, target_shape=[1, 1, 1, 1]) g4_tau = Gf(name='g4_tau', mesh=prodmesh, target_shape=[1, 1, 1, 1]) ed.set_g40_tau(g40_tau, g_tau[0,0]) ed.set_g4_tau(g4_tau[0,0,0,0], c(up,0), c_dag(up,0), c(up,0), c_dag(up,0)) # ------------------------------------------------------------------ # -- Two particle Green's functions (equal times) prodmesh = MeshProduct(imtime, imtime) g3pp_tau = Gf(name='g4_tau', mesh=prodmesh, target_shape=[1, 1, 1, 1]) ed.set_g3_tau(g3pp_tau[0,0,0,0], c(up,0), c_dag(up,0), c(up,0)*c_dag(up,0))
for tau in g_tau.mesh: g_tau[tau] = np.exp(-beta * tau) for idx, tau in enumerate(g_tau.mesh): # comparison does not work at beta since the evaluation g_tau() wraps.. if idx == len(g_tau.mesh) - 1: break #diff_interp = g_tau(tau)[0,0] - g_ref[idx] # FIXME: tau is complex diff_interp = g_tau(tau.real)[0, 0] - g_ref[idx] diff_dbrack = g_tau[tau][0, 0] - g_ref[idx] np.testing.assert_almost_equal(diff_interp, 0.0) np.testing.assert_almost_equal(diff_dbrack, 0.0) # -- three imaginary time gf imtime = MeshImTime(beta=beta, S='Fermion', n_max=ntau) g4_tau = Gf(name='g4_tau', mesh=MeshProduct(imtime, imtime, imtime), indices=[1]) for t1, t2, t3 in g4_tau.mesh: g4_tau[t1, t2, t3] = g_tau(t1) * g_tau(t3) - g_tau(t1) * g_tau(t3) for t1, t2, t3 in g4_tau.mesh: val = g4_tau[t1, t2, t3] val_ref = g_tau(t1) * g_tau(t3) - g_tau(t1) * g_tau(t3) np.testing.assert_array_almost_equal(val, val_ref)
def test_two_particle_greens_function(): # ------------------------------------------------------------------ # -- Hubbard atom with two bath sites, Hamiltonian beta = 2.0 V1 = 2.0 V2 = 5.0 epsilon1 = 0.00 epsilon2 = 4.00 mu = 2.0 U = 0.0 up, do = 0, 1 docc = c_dag(up, 0) * c(up, 0) * c_dag(do, 0) * c(do, 0) nA = c_dag(up, 0) * c(up, 0) + c_dag(do, 0) * c(do, 0) nB = c_dag(up, 1) * c(up, 1) + c_dag(do, 1) * c(do, 1) nC = c_dag(up, 2) * c(up, 2) + c_dag(do, 2) * c(do, 2) H = -mu * nA + epsilon1 * nB + epsilon2 * nC + U * docc + \ V1 * (c_dag(up, 0) * c(up, 1) + c_dag(up, 1) * c(up, 0) + c_dag(do, 0) * c(do, 1) + c_dag(do, 1) * c(do, 0)) + \ V2 * (c_dag(up, 0) * c(up, 2) + c_dag(up, 2) * c(up, 0) + c_dag(do, 0) * c(do, 2) + c_dag(do, 2) * c(do, 0)) # ------------------------------------------------------------------ # -- Exact diagonalization fundamental_operators = [ c(up, 0), c(do, 0), c(up, 1), c(do, 1), c(up, 2), c(do, 2) ] ed = TriqsExactDiagonalization(H, fundamental_operators, beta) # ------------------------------------------------------------------ # -- single particle Green's functions g_tau = GfImTime(name=r'$g$', beta=beta, statistic='Fermion', n_points=100, target_shape=(1, 1)) ed.set_g2_tau(g_tau[0, 0], c(up, 0), c_dag(up, 0)) # ------------------------------------------------------------------ # -- Two particle Green's functions ntau = 10 imtime = MeshImTime(beta, 'Fermion', ntau) prodmesh = MeshProduct(imtime, imtime, imtime) g40_tau = Gf(name='g40_tau', mesh=prodmesh, target_shape=(1, 1, 1, 1)) g4_tau = Gf(name='g4_tau', mesh=prodmesh, target_shape=(1, 1, 1, 1)) ed.set_g40_tau_matrix(g40_tau, g_tau) ed.set_g4_tau(g4_tau[0, 0, 0, 0], c(up, 0), c_dag(up, 0), c(up, 0), c_dag(up, 0)) # ------------------------------------------------------------------ # -- compare zero_outer_planes_and_equal_times(g4_tau) zero_outer_planes_and_equal_times(g40_tau) np.testing.assert_array_almost_equal(g4_tau.data, g40_tau.data)
#try with Nb % n_orb != 0 V_opt, e_opt, delta_disc_iw = discretize_bath(delta_in=delta_iw, Nb=3, eps0=2.5, V0=None, tol=1e-10) V_opt, e_opt, delta_disc_iw = discretize_bath(delta_in=delta_iw, Nb=1, eps0=2.5, V0=None, tol=1e-10) ################################################# # test basin hopping V_opt, e_opt, delta_disc_iw = discretize_bath(delta_in=delta_iw, Nb=2, eps0=energies, V0=None, tol=1e-10, maxiter=100, method='basinhopping') print('max hopping deviation', np.max(np.abs(hoppings) - np.abs(V_opt))) # compare to given values assert np.max(np.abs(hoppings) - np.abs(V_opt)) < 1e-6, 'did not achieved requiered accuracy for bath fit \n'+str(V_opt)+' vs \n'+str(hoppings) assert np.max(np.abs(energies - e_opt)) < 1e-6, 'did not achieved requiered accuracy for bath fit \n'+str(e_opt)+' vs \n'+str(energies) assert_gfs_are_close(delta_disc_iw, delta_iw) ################################################# # second test with 2x2 delta input and init guess mesh = MeshImTime(beta=40, S='Fermion', n_max=1001) hoppings = np.array([[6.1916012067e-01, 0.0, 3.4998359745e-01, 0.0, 1.3545326772e-01, 0.0, 2.4378742241e-01, 0.0], [0.0, 3.3503877422e-01, 0.0, 1.8849071562e-01, 0.0, -2.2889123931e-01, 0.0, -4.7688592983e-02]]) energies = np.array([-1.5437759000e+00, -4.0244628425e-01, -3.3678965769e-01, -3.8795127563e-02, 9.9602047476e-03, 1.6012620754e-01, 3.4692574848e-01, 2.0529795026e+03]) delta_tau = make_delta(V=hoppings, eps=energies, mesh=mesh) V_opt, e_opt, delta_disc_tau = discretize_bath(delta_in=delta_tau, Nb=8, eps0=energies, V0=None, tol=1e-15, maxiter=10000000, method='BFGS') print('max hopping deviation', np.max(np.abs(hoppings) - np.abs(V_opt))) # # compare to given values assert np.max(np.abs(hoppings) - np.abs(V_opt)) < 1e-5, 'did not achieved requiered accuracy for bath fit \n'+str(V_opt)+' vs \n'+str(hoppings) assert np.max(np.abs(energies - e_opt)) < 1e-5, 'did not achieved requiered accuracy for bath fit \n'+str(e_opt)+' vs \n'+str(energies)
def w2dyn_ndarray_to_triqs_BlockGF_tau_beta_ntau(gtau_osost, beta, gf_struct): """ Convert a DistributedSample of data in W2Dynamics ndarray format with indices [osost] where t is time, o is orbital, s is spin index to a spin-block Triqs imaginary time response function Takes: gtau : Green function as DistributedSample beta : inverse temperature ntau : number of tau points (including tau=0 and beta) Returns: BlockGF : triqs block Green function the response function data Author: Hugo U. R. Strand (2019) """ gtau = gtau_osost.mean() gtau_err = gtau_osost.stderr() n_tau = gtau.shape[-1] assert n_tau % 2 == 0, "Need an even number of tau points to downsample to Triqs tau mesh" # -- Average over interior bins to simulate Triqs bin structure # -- with half-width edge bins. def average_interior_bins(gtau): gtau_0 = gtau[..., 0] gtau_beta = gtau[..., -1] gtau_mid = gtau[..., 1:-1] gtau_mid = 0.5 * (gtau_mid[..., ::2] + gtau_mid[..., 1::2]) shape = list(gtau_mid.shape) shape[-1] += 2 gtau = np.zeros(shape, dtype=complex) gtau[..., 0] = gtau_0 gtau[..., -1] = gtau_beta gtau[..., 1:-1] = gtau_mid return gtau gtau = average_interior_bins(gtau) gtau_err = average_interior_bins(gtau_err) ### Reshape to rank 3 assert (len(gtau.shape) == 5) norbs, nspin, _, _, n_tau = gtau.shape shape = (norbs * nspin, norbs * nspin, n_tau) gtau, gtau_err = gtau.reshape(shape), gtau_err.reshape(shape) ### generate triqs Green function with same dimension than ### the input; this may not be a good idea if worm is used ### since then the output can have another structure... from triqs.gf import MeshImTime, BlockGf tau_mesh = MeshImTime(beta, 'Fermion', n_tau) G_tau_data = BlockGf(mesh=tau_mesh, gf_struct=gf_struct) G_tau_error = BlockGf(mesh=tau_mesh, gf_struct=gf_struct) gtau = exchange_fastest_running_index_ffw(gtau) ### read out blocks from full w2dyn matrices offset = 0 for name, _ in G_tau_data: size1, size2 = G_tau_data[name].target_shape assert (size1 == size2) size_block = size1 gtau_block = gtau[offset:offset + size_block, offset:offset + size_block, :] gtau_err_block = gtau_err[offset:offset + size_block, offset:offset + size_block, :] G_tau_data[name].data[:] = -gtau_block.transpose(2, 0, 1) G_tau_error[name].data[:] = -gtau_err_block.transpose(2, 0, 1) offset += size_block return G_tau_data, G_tau_error