def get_test_impurity_model(norb=2, ntau=1000, beta=10.0): """ Function that generates a random impurity model for testing """ from pytriqs.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)) spin_idxs = ['up', 'do'] gf_struct = [[spin_idx, orb_idxs] for spin_idx in spin_idxs] # -- Random Hamiltonian fundamental_operators = fundamental_operators_from_gf_struct(gf_struct) 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 '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 from pytriqs.gf import MeshImTime, BlockGf mesh = MeshImTime(beta, 'Fermion', ntau) Delta_tau = BlockGf(mesh=mesh, gf_struct=gf_struct) for block_name, delta_tau in Delta_tau: delta_tau.data[:] = -0.5 return gf_struct, Delta_tau, H_loc
indices=[1]) g_iwn = GfImFreq(name='$g$', beta=beta, statistic='Fermion', n_points=10, indices=[1]) ed.set_g2_tau(g_tau, c(up, 0), c_dag(up, 0)) ed.set_g2_iwn(g_iwn, 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) ed.set_g4_tau(g4_tau, 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, c(up, 0), c_dag(up, 0), c(up, 0) * c_dag(up, 0))
beta = 10.0 U = 2.0 mu = 1.0 h = 0.1 #V = 0.5 V = 1.0 epsilon = 2.3 H = U * n('up', 0) * n('do', 0) + \ - mu * (n('up', 0) + n('do', 0)) + \ h * n('up', 0) - h * n('do', 0) + \ epsilon * (n('up', 1) + n('do', 1)) - epsilon * (n('up', 2) + n('do', 2)) + \ V * ( c_dag('up', 0) * c('up', 1) + c_dag('up', 1) * c('up', 0) ) + \ V * ( c_dag('do', 0) * c('do', 1) + c_dag('do', 1) * c('do', 0) ) + \ V * ( c_dag('up', 0) * c('up', 2) + c_dag('up', 2) * c('up', 0) ) + \ V * ( c_dag('do', 0) * c('do', 2) + c_dag('do', 2) * c('do', 0) ) ed = TriqsExactDiagonalization(H, fundamental_operators, beta) n_tau = 101 G_tau_up = Gf(mesh=MeshImTime(beta, 'Fermion', n_tau), target_shape=[]) G_tau_do = Gf(mesh=MeshImTime(beta, 'Fermion', n_tau), target_shape=[]) ed.set_g2_tau(G_tau_up, c('up', 0), c_dag('up', 0)) ed.set_g2_tau(G_tau_do, c('do', 0), c_dag('do', 0)) with HDFArchive('anderson.pyed.h5', 'w') as Results: Results['up'] = G_tau_up Results['dn'] = G_tau_do
from pytriqs.gf import Gf, MeshImFreq, MeshImTime, iOmega_n, inverse, Fourier, InverseFourier beta = 10.0 gf_struct = [['0', [0, 1]]] target_shape = [2, 2] nw = 48 nt = 3 * nw S = SolverCore(beta=beta, gf_struct=gf_struct, n_iw=nw, n_tau=nt) h_int = n('0', 0) * n('0', 1) wmesh = MeshImFreq(beta=beta, S='Fermion', n_max=nw) tmesh = MeshImTime(beta=beta, S='Fermion', n_max=nt) Delta_iw = Gf(mesh=wmesh, target_shape=target_shape) Ek = np.array([ [1.00, 0.75], [0.75, -1.20], ]) E_loc = np.array([ [0.2, 0.3], [0.3, 0.4], ]) V = np.array([ [1.0, 0.25],
def make_calc(U=10): # ------------------------------------------------------------------ # -- Hubbard atom with two bath sites, Hamiltonian params = dict( beta=2.0, V1=2.0, V2=5.0, epsilon1=0.00, epsilon2=4.00, mu=2.0, U=U, ntau=40, niw=15, ) # ------------------------------------------------------------------ class Dummy(): def __init__(self): pass d = Dummy() # storage space d.params = params print '--> Solving SIAM with parameters' for key, value in params.items(): print '%10s = %-10s' % (key, str(value)) globals()[key] = value # populate global namespace # ------------------------------------------------------------------ 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) d.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(d.H, fundamental_operators, beta) # ------------------------------------------------------------------ # -- Single-particle Green's functions Gopt = dict(beta=beta, statistic='Fermion', indices=[1]) d.G_tau = GfImTime(name=r'$G(\tau)$', n_points=ntau, **Gopt) d.G_iw = GfImFreq(name='$G(i\omega_n)$', n_points=niw, **Gopt) ed.set_g2_tau(d.G_tau, c(up, 0), c_dag(up, 0)) ed.set_g2_iwn(d.G_iw, c(up, 0), c_dag(up, 0)) # chi2pp = + < c^+_u(\tau^+) c_u(0^+) c^+_d(\tau) c_d(0) > # = - < c^+_u(\tau^+) c^+_d(\tau) c_u(0^+) c_d(0) > chi2opt = dict(beta=beta, statistic='Fermion', indices=[1], n_points=ntau) d.chi2pp_tau = GfImTime(name=r'$\chi^{(2)}_{PP}(\tau)$', **chi2opt) ed.set_g2_tau(d.chi2pp_tau, c_dag(up, 0) * c_dag(do, 0), c(up, 0) * c(do, 0)) d.chi2pp_tau *= -1.0 * -1.0 # commutation sign and gf sign d.chi2pp_iw = g_iw_from_tau(d.chi2pp_tau, niw) # chi2ph = < c^+_u(\tau^+) c_u(\tau) c^+_d(0^+) c_d(0) > d.chi2ph_tau = GfImTime(name=r'$\chi^{(2)}_{PH}(\tau)$', **chi2opt) #d.chi2ph_tau = Gf(name=r'$\chi^{(2)}_{PH}(\tau)$', **chi2opt) ed.set_g2_tau(d.chi2ph_tau, c_dag(up, 0) * c(up, 0), c_dag(do, 0) * c(do, 0)) d.chi2ph_tau *= -1.0 # gf sign d.chi2ph_iw = g_iw_from_tau(d.chi2ph_tau, niw) # ------------------------------------------------------------------ # -- Two particle Green's functions imtime = MeshImTime(beta, 'Fermion', ntau) prodmesh = MeshProduct(imtime, imtime, imtime) G2opt = dict(mesh=prodmesh, target_shape=[1, 1, 1, 1]) d.G02_tau = Gf(name='$G^{(2)}_0(\tau_1, \tau_2, \tau_3)$', **G2opt) ed.set_g40_tau(d.G02_tau, d.G_tau) d.G02_iw = chi4_iw_from_tau(d.G02_tau, niw) d.G2_tau = Gf(name='$G^{(2)}(\tau_1, \tau_2, \tau_3)$', **G2opt) ed.set_g4_tau(d.G2_tau, c_dag(up, 0), c(up, 0), c_dag(do, 0), c(do, 0)) #ed.set_g4_tau(d.G2_tau, c(up,0), c_dag(up,0), c(do,0), c_dag(do,0)) # <cc^+cc^+> d.G2_iw = chi4_iw_from_tau(d.G2_tau, niw) # -- trying to fix the bug in the fft for w2 d.G02_iw.data[:] = d.G02_iw.data[:, ::-1, ...].conj() d.G2_iw.data[:] = d.G2_iw.data[:, ::-1, ...].conj() # ------------------------------------------------------------------ # -- 3/2-particle Green's functions (equal times) prodmesh = MeshProduct(imtime, imtime) chi3opt = dict(mesh=prodmesh, target_shape=[1, 1, 1, 1]) # chi3pp = <c^+_u(\tau) c_u(0^+) c^+_d(\tau') c_d(0) > # = - <c^+_u(\tau) c^+_d(\tau') c_u(0^+) c_d(0) > d.chi3pp_tau = Gf(name='$\Chi^{(3)}_{PP}(\tau_1, \tau_2, \tau_3)$', **chi3opt) ed.set_g3_tau(d.chi3pp_tau, c_dag(up, 0), c_dag(do, 0), c(up, 0) * c(do, 0)) d.chi3pp_tau *= -1.0 # from commutation d.chi3pp_iw = chi3_iw_from_tau(d.chi3pp_tau, niw) # chi3ph = <c^+_u(\tau) c_u(\tau') c^+_d(0^+) c_d(0) > d.chi3ph_tau = Gf(name='$\Chi^{(3)}_{PH}(\tau_1, \tau_2, \tau_3)$', **chi3opt) ed.set_g3_tau(d.chi3ph_tau, c_dag(up, 0), c(up, 0), c_dag(do, 0) * c(do, 0)) d.chi3ph_iw = chi3_iw_from_tau(d.chi3ph_tau, niw) # ------------------------------------------------------------------ # -- Store to hdf5 filename = 'data_ed.h5' with HDFArchive(filename, 'w') as res: for key, value in d.__dict__.items(): res[key] = value
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)
debug_H.append(H) # ------------------------------------------------------------------ Delta_iw = BlockGf(mesh=iw_mesh, gf_struct=gf_struct) H_loc_block = [] for block, g0_iw in G0_iw: tail, err = g0_iw.fit_hermitian_tail() H_loc = tail[2] Delta_iw[block] << inverse(g0_iw) + H_loc - iOmega_n H_loc_block.append(H_loc) tau_mesh = MeshImTime(beta, 'Fermion', n_tau) Delta_tau = BlockGf(mesh=tau_mesh, gf_struct=gf_struct) Delta_tau << Fourier(Delta_iw) # ------------------------------------------------------------------ # ------------------------------------------------------------------ exit() from pytriqs.plot.mpl_interface import oplot, oploti, oplotr, plt subp = [3, 1, 1] plt.subplot(*subp) subp[-1] += 1 oplot(G0_iw) plt.subplot(*subp) subp[-1] += 1
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)
def make_calc(beta=2.0, h_field=0.0): # ------------------------------------------------------------------ # -- Hubbard atom with two bath sites, Hamiltonian p = ParameterCollection( beta=beta, V1=2.0, V2=5.0, epsilon1=0.10, epsilon2=3.00, h_field=h_field, mu=0.0, U=5.0, ntau=800, niw=15, ) # ------------------------------------------------------------------ print '--> Solving SIAM with parameters' print p # ------------------------------------------------------------------ up, do = 'up', 'dn' docc = c_dag(up, 0) * c(up, 0) * c_dag(do, 0) * c(do, 0) mA = 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) p.H = -p.mu * nA + p.U * docc + p.h_field * mA + \ p.epsilon1 * nB + p.epsilon2 * nC + \ p.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) ) + \ p.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) ) # ------------------------------------------------------------------ fundamental_operators = [ c(up, 0), c(do, 0), c(up, 1), c(do, 1), c(up, 2), c(do, 2) ] ed = TriqsExactDiagonalization(p.H, fundamental_operators, p.beta) g_tau = GfImTime(beta=beta, statistic='Fermion', n_points=400, indices=[0]) g_iw = GfImFreq(beta=beta, statistic='Fermion', n_points=10, indices=[0]) p.G_tau = BlockGf(name_list=[up, do], block_list=[g_tau] * 2, make_copies=True) p.G_iw = BlockGf(name_list=[up, do], block_list=[g_iw] * 2, make_copies=True) ed.set_g2_tau(p.G_tau[up][0, 0], c(up, 0), c_dag(up, 0)) ed.set_g2_tau(p.G_tau[do][0, 0], c(do, 0), c_dag(do, 0)) ed.set_g2_iwn(p.G_iw[up][0, 0], c(up, 0), c_dag(up, 0)) ed.set_g2_iwn(p.G_iw[do][0, 0], c(do, 0), c_dag(do, 0)) p.magnetization = ed.get_expectation_value(0.5 * mA) p.O_tau = Gf(mesh=MeshImTime(beta, 'Fermion', 400), target_shape=[]) ed.set_g2_tau(p.O_tau, n(up, 0), n(do, 0)) p.O_tau.data[:] *= -1. p.exp_val = ed.get_expectation_value(n(up, 0) * n(do, 0)) # ------------------------------------------------------------------ # -- Store to hdf5 filename = 'data_pyed_h_field_%4.4f.h5' % h_field with HDFArchive(filename, 'w') as res: res['p'] = p
def ctqmc_solver(h_int_, G0_iw_): # --------- Construct the CTHYB solver ---------- if solver == "triqs": constr_params = { 'beta': beta, 'gf_struct': gf_struct, 'n_iw': n_iw, 'n_tau': 10000, # triqs value 'n_l': 50, #'complex': True # only necessary for w2dyn } elif solver == "w2dyn": constr_params = { 'beta': beta, 'gf_struct': gf_struct, 'n_iw': n_iw, 'n_tau': 9999, # w2dyn value 'n_l': 50, 'complex': True # only necessary for w2dyn } S = Solver(**constr_params) # --------- Initialize G0_iw ---------- S.G0_iw << G0_iw_ # --------- Solve! ---------- solve_params = { 'h_int': h_int_, 'n_warmup_cycles': n_warmup_cycles, #'n_cycles' : 1000000000, 'n_cycles': n_cycles, 'max_time': max_time, 'length_cycle': length_cycle, 'move_double': True, 'measure_pert_order': True, 'measure_G_l': True } #start = time.time() print 'running solver...' process = psutil.Process(os.getpid()) print "memory info before: ", process.memory_info( ).rss / 1024 / 1024, " MB" S.solve(**solve_params) process = psutil.Process(os.getpid()) print "memory info after: ", process.memory_info().rss / 1024 / 1024, " MB" print 'exited solver rank ', rank #end = time.time() G_iw_from_legendre = G0_iw_.copy() G_iw_from_legendre << LegendreToMatsubara(S.G_l) print 'G_iw_from_legendre', G_iw_from_legendre ##exit() ### giw from legendre, calculated within the interface #print 'S.G_iw_from_leg', S.G_iw_from_leg #exit() n_tau = 200 tau_mesh2 = MeshImTime(beta, 'Fermion', n_tau) my_G_tau = BlockGf(mesh=tau_mesh2, gf_struct=gf_struct) print 'S.G_tau["bl"][:,:].data ', S.G_tau["bl"][:, :].data.shape my_G_tau["bl"][:, :].data[...] = S.G_tau["bl"][:, :].data.reshape( 200, 50, N_bands * 2, N_bands * 2).mean(axis=1) #return my_G_tau, S.G_iw_from_leg #return my_G_tau, S.G_iw if solver == 'triqs': return my_G_tau, G_iw_from_legendre, S.average_sign else: return my_G_tau, S.G_iw_from_leg, S.average_sign