def __init__( self, mol, orb_coeff, nsweeps=5, tstep=0.50, warmup=100, naux=500, spin=None, electrons=None, kpts=None, ): if not (spin is None): if spin == 0: self._electrons = np.arange(0, mol.nelec[0]) elif spin == 1: self._electrons = np.arange(mol.nelec[0], np.sum(mol.nelec)) else: raise ValueError("Spin not equal to 0 or 1") elif not (electrons is None): self._electrons = electrons else: self._electrons = np.arange(0, np.sum(mol.nelec)) self.iscomplex = bool(sum(map(np.iscomplexobj, orb_coeff))) if hasattr(mol, "lattice_vectors"): assert kpts is not None assert len(orb_coeff) == len(kpts) self.iscomplex = self.iscomplex or np.linalg.norm(kpts) > 1e-12 self._kpts = kpts self.evaluate_orbitals = self._evaluate_orbitals_pbc if self.iscomplex: self.get_wrapphase = lambda x: np.exp(1j * x) else: self.get_wrapphase = lambda x: (-1)**np.round(x / np.pi) for attribute in ["original_cell", "S"]: if not hasattr(mol, attribute): from pyqmc.slaterpbc import get_supercell mol = get_supercell(mol, np.eye(3)) self.supercell = mol self._mol = mol.original_cell else: self._mol = mol self.evaluate_orbitals = self._evaluate_orbitals assert (len(orb_coeff.shape) == 2 ), "orb_coeff should be a list of orbital coefficients." self._orb_coeff = orb_coeff self._tstep = tstep self.nelec = len(self._electrons) self._nsweeps = nsweeps self._nstep = nsweeps * self.nelec self._extra_config = initial_guess(mol, int(naux / self.nelec) + 1) self._extra_config.reshape((-1, 3)) accept, extra_configs = self.sample_onebody(self._extra_config, warmup) self._extra_config = extra_configs[-1]
def get_ewald_energy(cell, S, configs): supercell = get_supercell(cell, S) ewald = pyqmc.ewald.Ewald(supercell) configs = PeriodicConfigs(configs, supercell.lattice_vectors()) ee, ei, ii = ewald.energy(configs) etot = ee + ei + ii print(dict(ee=ee, ei=ei, ii=ii)) print("total energy", etot) return etot
def test_pbc_wfs(): """ Ensure that the wave function objects are consistent in several situations. """ from pyscf.pbc import lib, gto, scf from pyqmc.slaterpbc import PySCFSlaterPBC, get_supercell from pyqmc.jastrowspin import JastrowSpin from pyqmc.multiplywf import MultiplyWF from pyqmc.coord import OpenConfigs import pyqmc mol = gto.M(atom="H 0. 0. 0.; H 1. 1. 1.", basis="sto-3g", unit="bohr", a=np.eye(3) * 4) mf = scf.KRKS(mol).run() # mf_rohf = scf.KROKS(mol).run() # mf_uhf = scf.KUKS(mol).run() epsilon = 1e-5 nconf = 10 supercell = get_supercell(mol, S=np.eye(3)) epos = pyqmc.initial_guess(supercell, nconf) for wf in [ MultiplyWF(PySCFSlaterPBC(supercell, mf), JastrowSpin(mol)), PySCFSlaterPBC(supercell, mf), # PySCFSlaterPBC(supercell, mf_uhf), # PySCFSlaterPBC(supercell, mf_rohf), ]: for k in wf.parameters: if k != "mo_coeff": wf.parameters[k] = np.random.rand(*wf.parameters[k].shape) for fname, func in zip( ["gradient", "laplacian", "pgradient"], [ testwf.test_wf_gradient, testwf.test_wf_laplacian, testwf.test_wf_pgradient, ], ): err = [] for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]: err.append(func(wf, epos, delta)[0]) print(fname, min(err)) assert min(err) < epsilon for k, item in testwf.test_updateinternals(wf, epos).items(): print(k, item) assert item < epsilon
def test_RKS(kind=0, nk=(1, 1, 1)): L = 2 mol = gto.M( atom="""He {0} {0} {0}""".format(0.0), basis="sto-3g", a=np.eye(3) * L, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KRKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind)
def noncubic(kind=0, nk=(1, 1, 1)): L = 3 mol = gto.M( atom="""H {0} {0} {0} H {1} {1} {1}""".format(0.0, L / 4), basis="sto-3g", a=(np.ones((3, 3)) - np.eye(3)) * L / 2, spin=0, unit="bohr", ) kpts = mol.make_kpts(nk) mf = scf.KUKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind)
def multislater(kind=0, nk=(1, 1, 1)): L = 3 mol = gto.Cell( atom="""H {0} {0} {0} H {1} {1} {1}""".format(0.0, L / 2), basis="cc-pvtz", spin=0, unit="bohr", ) mol.exp_to_discard = 0.1 mol.build(a=np.eye(3) * L) kpts = mol.make_kpts(nk) mf = scf.UKS(mol, (0, 0, 0)) mf.xc = "pbe" mf = multigrid(mf) mf = remove_linear_dep_(mf) mf.chkfile = "h_bcc.chkfile" mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind, do_mc=True)
def cubic_with_ecp(kind=0, nk=(1, 1, 1)): from pyscf.pbc.dft.multigrid import multigrid start = time.time() L = 6.63 mol = gto.Cell( atom="""Li {0} {0} {0} Li {1} {1} {1}""".format(0.0, L / 2), basis="bfd-vdz", ecp="bfd", spin=0, unit="bohr", ) mol.exp_to_discard = 0.1 mol.build(a=np.eye(3) * L) kpts = mol.make_kpts(nk) mf = scf.KUKS(mol, kpts) mf.xc = "pbe" # mf = mf.density_fit() mf = multigrid(mf) mf = mf.run() supercell = get_supercell(mol, np.diag(nk)) runtest(supercell, mf, kind=kind)
def test_pbc(): from pyscf.pbc import gto, scf from pyqmc import PySCFSlaterUHF, PySCFSlaterPBC from pyqmc import slaterpbc import scipy lvecs = (np.ones((3, 3)) - np.eye(3)) * 2.0 mol = gto.M( atom="H 0. 0. -{0}; H 0. 0. {0}".format(0.7), basis="sto-3g", unit="bohr", verbose=0, a=lvecs, ) mf = scf.KRHF(mol, kpts=mol.make_kpts((2, 2, 2))) mf = mf.run() S = np.ones((3, 3)) - 2 * np.eye(3) mol = slaterpbc.get_supercell(mol, S) kpts = slaterpbc.get_supercell_kpts(mol)[:2] kdiffs = mf.kpts[np.newaxis] - kpts[:, np.newaxis] kinds = np.nonzero(np.linalg.norm(kdiffs, axis=-1) < 1e-12)[1] # Lowdin orthogonalized AO basis. # lowdin = lo.orth_ao(mol, "lowdin") loiao = lo.iao.iao(mol.original_cell, mf.mo_coeff, kpts=kpts) occs = [mf.mo_occ[k] for k in kinds] coefs = [mf.mo_coeff[k] for k in kinds] ovlp = mf.get_ovlp()[kinds] lowdin = [lo.vec_lowdin(l, o) for l, o in zip(loiao, ovlp)] lreps = [np.linalg.multi_dot([l.T, o, c]) for l, o, c in zip(lowdin, ovlp, coefs)] # make AO to localized orbital coefficients. mfobdm = [np.einsum("ij,j,kj->ik", l.conj(), o, l) for l, o in zip(lreps, occs)] ### Test OBDM calculation. nconf = 800 nsteps = 50 warmup = 6 wf = PySCFSlaterPBC(mol, mf) configs = initial_guess(mol, nconf) obdm_dict = dict(mol=mol, orb_coeff=lowdin, kpts=kpts, nsweeps=4, warmup=10) obdm = OBDMAccumulator(**obdm_dict) obdm_up = OBDMAccumulator(**obdm_dict, spin=0) obdm_down = OBDMAccumulator(**obdm_dict, spin=1) df, coords = vmc( wf, configs, nsteps=nsteps, accumulators={"obdm": obdm, "obdm_up": obdm_up, "obdm_down": obdm_down}, verbose=True, ) df = DataFrame(df) obdm_est = {} for k in ["obdm", "obdm_up", "obdm_down"]: avg_norm = np.array(df.loc[warmup:, k + "norm"].values.tolist()).mean(axis=0) avg_obdm = np.array(df.loc[warmup:, k + "value"].values.tolist()).mean(axis=0) obdm_est[k] = normalize_obdm(avg_obdm, avg_norm) print("Average OBDM(orb,orb)", obdm_est["obdm"].round(3)) mfobdm = scipy.linalg.block_diag(*mfobdm) print("mf obdm", mfobdm.round(3)) max_abs_err = np.max(np.abs(obdm_est["obdm"] - mfobdm)) assert max_abs_err < 0.05, "max abs err {0}".format(max_abs_err) print(obdm_est["obdm_up"].diagonal().round(3)) print(obdm_est["obdm_down"].diagonal().round(3)) mae = np.mean(np.abs(obdm_est["obdm_up"] + obdm_est["obdm_down"] - mfobdm)) maup = np.mean(np.abs(obdm_est["obdm_up"])) madn = np.mean(np.abs(obdm_est["obdm_down"])) mamf = np.mean(np.abs(mfobdm)) assert mae < 0.05, "mae {0}\n maup {1}\n madn {2}\n mamf {3}".format( mae, maup, madn, mamf )
print("EHF") print(ehf) return cell, kmf if __name__ == "__main__": import pandas as pd nconfig = 100 for nk in [2]: # Run SCF cell, kmf = run_scf(nk) # Set up wf and configs S = np.eye(3) * nk supercell = get_supercell(cell, S) slater = PySCFSlaterPBC(supercell, kmf) configs = pyqmc.initial_guess(supercell, nconfig) # Jastrow abasis = [ PolyPadeFunction(beta=0.8 * 2**i, rcut=7.5) for i in range(3) ] bbasis = [CutoffCuspFunction(gamma=8, rcut=7.5)] bbasis += [ PolyPadeFunction(beta=0.8 * 2**i, rcut=7.5) for i in range(3) ] jastrow = JastrowSpin(supercell, a_basis=abasis, b_basis=bbasis) jastrow.parameters["bcoeff"][0, [0, 1, 2]] = np.array( [-0.25, -0.50, -0.25]) freeze = {}