def __init__(self, comm, system, beta, dt, options={}, H1=None, verbose=False): OneBody.__init__(self, comm, system, beta, dt, options, H1=H1, verbose=verbose) self.alpha = options.get('alpha', 0.75) self.max_scf_it = options.get('max_scf_it', self.max_it) self.max_macro_it = options.get('max_macro_it', self.max_it) self.find_mu = options.get('find_mu', True) if comm.rank == 0: P, HMF, mu = self.thermal_hartree_fock(system, beta) muN = mu * numpy.eye(system.nbasis, dtype=self.G.dtype) dmat = numpy.array([ scipy.linalg.expm(-dt * (HMF[0] - muN)), scipy.linalg.expm(-dt * (HMF[1] - muN)) ]) dmat_inv = numpy.array([ scipy.linalg.inv(self.dmat[0], check_finite=False), scipy.linalg.inv(self.dmat[1], check_finite=False) ]) G = numpy.array( [greens_function(self.dmat[0]), greens_function(self.dmat[1])]) data = { 'P': P, 'mu': mu, 'dmat': dmat, 'dmat_inv': dmat_inv, 'G': G } else: data = None data = comm.bcast(data, root=0) self.P = data['P'] self.nav = particle_number(self.P).real self.dmat = data['dmat'] self.dmat_inv = data['dmat_inv'] self.G = data['G'] self.mu = data['mu']
def get_trial_density_matrices(comm, options, system, cplx, beta, dt, verbose=False): """Wrapper to select trial wavefunction class. Parameters ---------- options : dict Trial wavefunction input options. system : class System class. cplx : bool If true then trial wavefunction will be complex. Returns ------- trial : class or None Trial wavfunction class. """ trial_type = options.get('name', 'one_body') if trial_type == 'one_body_mod': trial = OneBody(comm, system, beta, dt, options=options, H1=system.h1e_mod, verbose=verbose) elif trial_type == 'one_body': trial = OneBody(comm, system, beta, dt, options=options, verbose=verbose) elif trial_type == 'thermal_hartree_fock': trial = MeanField(comm, system, beta, dt, options=options, verbose=verbose) else: trial = None return trial
def find_mu_opt(options): comm = MPI.COMM_WORLD # Guess initial chemical potential from trial density matrix (mu_xc < 0) system = UEG(options['system']) qmcopt = QMCOpts(options['qmc'], system) trial = OneBody(comm, system, qmcopt.beta, qmcopt.dt) mu0 = trial.mu # guess for bracket. mu1 = mu0 - 0.5 * abs(mu0) mu_opt = secant(comm, options, mu0, mu1, system.ne) if comm.rank == 0: print("# Converged mu: {}".format(mu_opt)) # Run longer simulation at optimal mu. sys_opts['mu'] = mu_opt qmc['nsteps'] = 50 estim['basename'] = 'optimal' afqmc = ThermalAFQMC(comm, options=options, verbose=(comm.rank == 0)) afqmc.run(comm=comm, verbose=True)
def test_hubbard(): options = {'nx': 4, 'ny': 4, 'U': 4, 'mu': 1.0, 'nup': 7, 'ndown': 7} system = Hubbard(options, verbose=False) comm = MPI.COMM_WORLD beta = 2.0 dt = 0.05 nslice = int(round(beta / dt)) trial = OneBody(comm, system, beta, dt) numpy.random.seed(7) qmc = dotdict({'dt': dt, 'nstblz': 10}) prop = ThermalDiscrete({}, qmc, system, trial, verbose=False) walker1 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) for ts in range(0, nslice): prop.propagate_walker(system, walker1, ts, 0) walker1.weight /= 1.0e6 numpy.random.seed(7) walker2 = ThermalWalker({ 'stack_size': 10, 'low_rank': False }, system, trial, verbose=False) energies = [] for ts in range(0, nslice): prop.propagate_walker(system, walker2, ts, 0) walker2.weight /= 1.0e6 # if ts % 10 == 0: # energies.append(walker2.local_energy(system)[0]) # import matplotlib.pyplot as pl # pl.plot(energies, markersize=2) # pl.show() assert walker1.weight == pytest.approx(walker2.weight) assert numpy.linalg.norm(walker1.G - walker2.G) == pytest.approx(0) assert walker1.local_energy(system)[0] == pytest.approx( walker2.local_energy(system)[0])
def test_propagate_walker(): options = {'nx': 4, 'ny': 4, 'U': 4, 'mu': 1.0, 'nup': 7, 'ndown': 7} system = Hubbard(options, verbose=False) comm = MPI.COMM_WORLD beta = 2.0 dt = 0.05 nslice = int(round(beta / dt)) trial = OneBody(comm, system, beta, dt) numpy.random.seed(7) qmc = dotdict({'dt': dt, 'nstblz': 1}) prop = ThermalDiscrete({}, qmc, system, trial, verbose=False) walker1 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) walker2 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) rands = numpy.random.random(system.nbasis) I = numpy.eye(system.nbasis) BV = numpy.zeros((2, system.nbasis)) BV[0] = 1.0 BV[1] = 1.0 walker2.greens_function(trial, slice_ix=0) walker1.greens_function(trial, slice_ix=0) for it in range(0, nslice): rands = numpy.random.random(system.nbasis) BV = numpy.zeros((2, system.nbasis)) BV[0] = 1.0 BV[1] = 1.0 for i in range(system.nbasis): if rands[i] > 0.5: xi = 0 else: xi = 1 BV[0, i] = prop.auxf[xi, 0] BV[1, i] = prop.auxf[xi, 1] # Check overlap ratio if it % 20 == 0: probs1 = prop.calculate_overlap_ratio(walker1, i) G2old = walker2.greens_function(trial, slice_ix=it, inplace=False) B = numpy.einsum('ki,kij->kij', BV, prop.BH1) walker2.stack.stack[it] = B walker2.greens_function(trial, slice_ix=it) G2 = walker2.G pdirect = numpy.linalg.det(G2old[0]) / numpy.linalg.det(G2[0]) pdirect *= 0.5 * numpy.linalg.det(G2old[1]) / numpy.linalg.det( G2[1]) pdirect == pytest.approx(probs1[xi]) prop.update_greens_function(walker1, i, xi) B = numpy.einsum('ki,kij->kij', BV, prop.BH1) walker1.stack.update(B) if it % prop.nstblz == 0: walker1.greens_function(None, walker1.stack.time_slice - 1) walker2.stack.stack[it] = B walker2.greens_function(trial, slice_ix=it) numpy.linalg.norm(walker1.G - walker2.G) == pytest.approx(0.0) prop.propagate_greens_function(walker1)
"beta": 1, "rng_seed": 7 } estimates = {"mixed": {"thermal": True}} trial = {"name": "one_body", "mu": 0.4} options = { "model": sys, "qmc_options": qmc, "estimates": estimates, "trial": trial } (afqmc, comm) = setup_calculation(options) #afqmc.run(comm=comm) scan = numpy.linspace(0.5, 1.5, 10) # afqmc.run(comm=comm) # scan = numpy.linspace(0.5, 1.5, 10) for mu in [0.5]: afqmc.trial = OneBody({'mu': mu}, afqmc.system, afqmc.qmc.beta, afqmc.qmc.dt) afqmc.propagators = get_propagator({}, afqmc.qmc, afqmc.system, afqmc.trial) afqmc.psi.reset(afqmc.trial) afqmc.estimators.json_string = to_json(afqmc) afqmc.estimators.reset(comm.rank == 0) afqmc.run(comm=comm) # if comm.rank == 0: # files = glob.glob('*.h5') # data = analyse_energy(files) # print (data.to_string())
def unit_test(): from pauxy.systems.ueg import UEG from pauxy.trial_density_matrices.onebody import OneBody from pauxy.thermal_propagation.planewave import PlaneWave from pauxy.qmc.options import QMCOpts inputs = { 'nup': 1, 'ndown': 1, 'rs': 1.0, 'ecut': 0.5, "name": "one_body", "mu": 1.94046021, "beta": 0.5, "dt": 0.05, "optimised": True } beta = inputs['beta'] dt = inputs['dt'] system = UEG(inputs, verbose=False) qmc = QMCOpts(inputs, system, True) trial = OneBody(inputs, system, beta, dt, system.H1, verbose=False) walker = ThermalWalker(inputs, system, trial, True) # walker.greens_function(trial) E, T, V = walker.local_energy(system) numpy.random.seed(0) inputs['optimised'] = False propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) propagator.propagate_walker_free(system, walker, trial, False) Gold = walker.G[0].copy() system = UEG(inputs, verbose=False) qmc = QMCOpts(inputs, system, verbose=False) trial = OneBody(inputs, system, beta, dt, system.H1, verbose=False) propagator = PlaneWave(inputs, qmc, system, trial, True) walker = ThermalWalker(inputs, system, trial, verbose=False) # walker.greens_function(trial) E, T, V = walker.local_energy(system) numpy.random.seed(0) inputs['optimised'] = True propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) propagator.propagate_walker_free(system, walker, trial, False) Gnew = walker.G[0].copy() assert (scipy.linalg.norm(Gold[:, 0] - Gnew[:, 0]) < 1e-10) inputs['stack_size'] = 1 walker = ThermalWalker(inputs, system, trial, verbose=False) numpy.random.seed(0) propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) for i in range(0, 5): propagator.propagate_walker(system, walker, trial) Gs1 = walker.G[0].copy() for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) E, T, V = walker.local_energy(system) # print(E) inputs['stack_size'] = 5 walker = ThermalWalker(inputs, system, trial, verbose=False) numpy.random.seed(0) propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) for i in range(0, 5): propagator.propagate_walker(system, walker, trial) Gs5 = walker.G[0].copy() for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) E, T, V = walker.local_energy(system) # print(E) assert (numpy.linalg.norm(Gs1 - Gs5) < 1e-10) N = 5 A = numpy.random.rand(N, N) Q, R, P = scipy.linalg.qr(A, pivoting=True) #### test permutation start # Pmat = numpy.zeros((N,N)) # for i in range (N): # Pmat[P[i],i] = 1 # print(P) # tmp = Q.dot(R)#.dot(Pmat.T) # print(tmp) # print("==================") # tmp2 = tmp.dot(Pmat.T) # print(tmp2) # print("==================") # tmp[:,P] = tmp [:,range(N)] # print(tmp) #### test permutation end B = numpy.random.rand(N, N) (Q1, R1, P1) = scipy.linalg.qr(B, pivoting=True, check_finite=False) # Form permutation matrix P1mat = numpy.zeros(B.shape, B.dtype) P1mat[P1, range(len(P1))] = 1.0 # Form D matrices D1 = numpy.diag(R1.diagonal()) D1inv = numpy.diag(1.0 / R1.diagonal()) T1 = numpy.dot(numpy.dot(D1inv, R1), P1mat.T) assert (numpy.linalg.norm(B - numpy.einsum('ij,jj->ij', Q1, D1).dot(T1)) < 1e-10) # tmp[:,:] = tmp[:,P] # print(A - tmp) # print(Q * Q.T) # print(R) # Test walker green's function. from pauxy.systems.hubbard import Hubbard from pauxy.estimators.thermal import greens_function, one_rdm_from_G from pauxy.estimators.hubbard import local_energy_hubbard sys_dict = { 'name': 'Hubbard', 'nx': 4, 'ny': 4, 'nup': 7, 'ndown': 7, 'U': 4, 't': 1 } system = Hubbard(sys_dict) beta = 4 mu = 1 trial = OneBody({"mu": mu}, system, beta, dt, verbose=True) dt = 0.05 num_slices = int(beta / dt) eref = 0 for ek in system.eks: eref += 2 * ek * 1.0 / (numpy.exp(beta * (ek - mu)) + 1) walker = ThermalWalker({"stack_size": 1}, system, trial) Gs1 = walker.G[0].copy() rdm = one_rdm_from_G(walker.G) ekin = local_energy_hubbard(system, rdm)[1] try: assert (abs(eref - ekin) < 1e-8) except AssertionError: print("Error in kinetic energy check. Ref: %13.8e Calc:%13.8e" % (eref, ekin)) walker = ThermalWalker({"stack_size": 10}, system, trial) rdm = one_rdm_from_G(walker.G) ekin = local_energy_hubbard(system, rdm)[1] try: assert (abs(eref - ekin) < 1e-8) except AssertionError: print("Error in kinetic energy check. Ref: %13.10e Calc: %13.10e" " Error: %13.8e" % (eref.real, ekin.real, abs(eref - ekin))) for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) assert (numpy.linalg.norm(Gs1 - walker.G[0]) < 1e-10)
def unit_test(): import cProfile from pauxy.systems.ueg import UEG from pauxy.systems.pw_fft import PW_FFT from pauxy.estimators.ueg import local_energy_ueg from pauxy.estimators.pw_fft import local_energy_pw_fft from pauxy.qmc.options import QMCOpts from pauxy.trial_density_matrices.onebody import OneBody from pauxy.qmc.comm import FakeComm from pauxy.walkers.thermal import ThermalWalker beta = 16.0 dt = 0.005 # beta = 0.5 # dt = 0.05 lowrank = True # lowrank = False stack_size = 10 ecuts = [4.0, 8.0, 10.0, 12.0, 16.0, 21.0, 21.5, 32.0] for ecut in ecuts: inputs = { 'nup': 33, 'ndown': 33, 'thermal': True, 'beta': beta, 'rs': 1.0, 'ecut': ecut, 'dt': dt, 'nwalkers': 10, 'lowrank': lowrank, 'stack_size': stack_size } system = UEG(inputs, True) qmc = QMCOpts(inputs, system, True) comm = FakeComm() trial = OneBody(comm, system, beta, dt, options=inputs, verbose=True) propagator = PlaneWave(system, trial, qmc, inputs, True) walker = ThermalWalker( { 'stack_size': trial.stack_size, 'low_rank': lowrank }, system, trial, verbose=True) eshift = 0.0 + 0.0j numpy.random.seed(7) pr = cProfile.Profile() pr.enable() for ts in range(0, walker.num_slices): propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) if (lowrank): system = PW_FFT(inputs, False) sort_basis = numpy.argsort(numpy.diag(system.H1[0]), kind='mergesort') inv_sort_basis = numpy.zeros_like(sort_basis) for i, idx in enumerate(sort_basis): inv_sort_basis[idx] = i mT = walker.stack.mT Ctrial = numpy.zeros((system.nbasis, walker.stack.mT * 2), dtype=numpy.complex128) Ctrial[:, :mT] = walker.stack.CT[0][:, :mT] Ctrial[:, mT:] = walker.stack.CT[1][:, :mT] P = one_rdm_from_G(walker.G) # Ptmp = Ctrial[:,:mT].conj().dot(walker.stack.theta[0,:mT,:]) # Reorder to FFT P[:, :, :] = P[:, inv_sort_basis, :] P[:, :, :] = P[:, :, inv_sort_basis] Theta = walker.stack.theta[:, :mT, :] Theta[:, :, :] = Theta[:, :, inv_sort_basis] Ctrial = Ctrial[inv_sort_basis, :] print("E = {}".format( local_energy_pw_fft(system, G=P, Ghalf=Theta, trial=Ctrial))) else: P = one_rdm_from_G(walker.G) print(numpy.diag(walker.G[0].real)) print("weight = {}".format(walker.weight)) print("E = {}".format(local_energy_ueg(system, P))) pr.disable() pr.print_stats(sort='tottime')
"nup": 2, "ndown": 2, "mu": 0.2, "sparse": False, "integrals": "hamil.h5" } system = Generic(inputs=sys_opts) # trial = OneBody(comm, system, 1.0, 0.05, verbose=True) mus = numpy.arange(-1, 1) data = [] dt = 0.05 fci = pd.read_csv('be_fixed_n.out', sep=r'\s+') for b, n in zip(fci.beta, fci.N): trial = OneBody(comm, system, b, dt, options={"nav": n}, verbose=True) data.append([ local_energy(system, trial.P, opt=False)[0].real, particle_number(trial.P).real ]) pl.plot(fci.beta, zip(*data)[0], label='Match N') match = zip(*data)[0] data = [] for b, n in zip(fci.beta, fci.N): trial = MeanField(comm, system, b, dt, options={"nav": n}, verbose=True) data.append([ local_energy(system, trial.P, opt=False)[0].real, particle_number(trial.P).real ]) pl.plot(fci.beta, fci.E, label='FCI') pl.plot(fci.beta, zip(*data)[0], label='THF', linestyle=':')