def init_guess_by_chkfile(cell, chkfile_name, project=True, kpts=None): '''Read the KHF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, 3D ndarray ''' chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if kpts is None: kpts = scf_rec['kpts'] if 'kpt' in scf_rec: chk_kpts = scf_rec['kpt'].reshape(-1, 3) elif 'kpts' in scf_rec: chk_kpts = scf_rec['kpts'] else: chk_kpts = np.zeros((1, 3)) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if 'kpts' not in scf_rec: # gamma point or single k-point if mo.ndim == 2: mo = mo.reshape((1, ) + mo.shape) mo_occ = mo_occ.reshape((1, ) + mo_occ.shape) else: # UHF mo = mo.reshape((2, 1) + mo.shape[1:]) mo_occ = mo_occ.reshape((2, 1) + mo_occ.shape[1:]) def fproj(mo, kpt): if project: return addons.project_mo_nr2nr(chk_cell, mo, cell, kpt) else: return mo if kpts.shape == chk_kpts.shape and np.allclose(kpts, chk_kpts): def makedm(mos, occs): mos = [fproj(mo, None) for mo in mos] return make_rdm1(mos, occs) else: where = [np.argmin(lib.norm(chk_kpts - kpt, axis=1)) for kpt in kpts] def makedm(mos, occs): mos = [ fproj(mos[w], chk_kpts[w] - kpts[i]) for i, w in enumerate(where) ] return make_rdm1(mos, occs[where]) if mo.ndim == 3: # KRHF dm = makedm(mo, mo_occ) else: # KUHF dm = makedm(mo[0], mo_occ[0]) + makedm(mo[1], mo_occ[1]) # Real DM for gamma point if np.allclose(kpts, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=True, kpts=None): '''Read the KHF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, 3D ndarray ''' chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if kpts is None: kpts = scf_rec['kpts'] if 'kpt' in scf_rec: chk_kpts = scf_rec['kpt'].reshape(-1,3) elif 'kpts' in scf_rec: chk_kpts = scf_rec['kpts'] else: chk_kpts = np.zeros((1,3)) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if 'kpts' not in scf_rec: # gamma point or single k-point if mo.ndim == 2: mo = mo.reshape((1,)+mo.shape) mo_occ = mo_occ.reshape((1,)+mo_occ.shape) else: # UHF mo = mo.reshape((2,1)+mo.shape[1:]) mo_occ = mo_occ.reshape((2,1)+mo_occ.shape[1:]) def fproj(mo, kpt): if project: return addons.project_mo_nr2nr(chk_cell, mo, cell, kpt) else: return mo if kpts.shape == chk_kpts.shape and np.allclose(kpts, chk_kpts): def makedm(mos, occs): mos = [fproj(mo, None) for mo in mos] return make_rdm1(mos, occs) else: where = [np.argmin(lib.norm(chk_kpts-kpt, axis=1)) for kpt in kpts] def makedm(mos, occs): mos = [fproj(mos[w], chk_kpts[w]-kpts[i]) for i,w in enumerate(where)] return make_rdm1(mos, occs[where]) if mo.ndim == 3: # KRHF dm = makedm(mo, mo_occ) else: # KUHF dm = makedm(mo[0], mo_occ[0]) + makedm(mo[1], mo_occ[1]) # Real DM for gamma point if np.allclose(kpts, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=None, kpt=None): '''Read the HF results from checkpoint file and make the density matrix for UHF initial guess. Returns: Density matrix, (nao,nao) ndarray ''' from pyscf import gto chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if project is None: project = not gto.same_basis_set(chk_cell, cell) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if kpt is None: kpt = np.zeros(3) if 'kpt' in scf_rec: chk_kpt = scf_rec['kpt'] elif 'kpts' in scf_rec: kpts = scf_rec['kpts'] # the closest kpt from KRHF results where = np.argmin(lib.norm(kpts - kpt, axis=1)) chk_kpt = kpts[where] if getattr(mo[0], 'ndim', None) == 2: # KRHF mo = mo[where] mo_occ = mo_occ[where] else: # KUHF mo = [mo[0][where], mo[1][where]] mo_occ = [mo_occ[0][where], mo_occ[1][where]] else: # from molecular code chk_kpt = np.zeros(3) if project: s = cell.pbc_intor('int1e_ovlp', kpt=kpt) def fproj(mo): if project: mo = addons.project_mo_nr2nr(chk_cell, mo, cell, chk_kpt - kpt) norm = np.einsum('pi,pi->i', mo.conj(), s.dot(mo)) mo /= np.sqrt(norm) return mo if getattr(mo, 'ndim', None) == 2: mo = fproj(mo) mo_occa = (mo_occ > 1e-8).astype(np.double) mo_occb = mo_occ - mo_occa dm = mol_uhf.make_rdm1([mo, mo], [mo_occa, mo_occb]) else: # UHF dm = mol_uhf.make_rdm1([fproj(mo[0]), fproj(mo[1])], mo_occ) # Real DM for gamma point if kpt is None or np.allclose(kpt, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=None, kpt=None): '''Read the HF results from checkpoint file and make the density matrix for UHF initial guess. Returns: Density matrix, (nao,nao) ndarray ''' from pyscf import gto chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if project is None: project = not gto.same_basis_set(chk_cell, cell) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if kpt is None: kpt = np.zeros(3) if 'kpt' in scf_rec: chk_kpt = scf_rec['kpt'] elif 'kpts' in scf_rec: kpts = scf_rec['kpts'] # the closest kpt from KRHF results where = np.argmin(lib.norm(kpts-kpt, axis=1)) chk_kpt = kpts[where] if getattr(mo[0], 'ndim', None) == 2: # KRHF mo = mo[where] mo_occ = mo_occ[where] else: # KUHF mo = [mo[0][where], mo[1][where]] mo_occ = [mo_occ[0][where], mo_occ[1][where]] else: # from molecular code chk_kpt = np.zeros(3) if project: s = cell.pbc_intor('int1e_ovlp', kpt=kpt) def fproj(mo): if project: mo = addons.project_mo_nr2nr(chk_cell, mo, cell, chk_kpt-kpt) norm = np.einsum('pi,pi->i', mo.conj(), s.dot(mo)) mo /= np.sqrt(norm) return mo if getattr(mo, 'ndim', None) == 2: mo = fproj(mo) mo_occa = (mo_occ>1e-8).astype(np.double) mo_occb = mo_occ - mo_occa dm = mol_uhf.make_rdm1([mo,mo], [mo_occa,mo_occb]) else: # UHF dm = mol_uhf.make_rdm1([fproj(mo[0]),fproj(mo[1])], mo_occ) # Real DM for gamma point if kpt is None or np.allclose(kpt, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=True, kpt=None): '''Read the HF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, (nao,nao) ndarray ''' chk_cell, scf_rec = chkfile.load_scf(chkfile_name) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if kpt is None: kpt = np.zeros(3) if 'kpt' in scf_rec: chk_kpt = scf_rec['kpt'] elif 'kpts' in scf_rec: kpts = scf_rec['kpts'] # the closest kpt from KRHF results where = np.argmin(lib.norm(kpts - kpt, axis=1)) chk_kpt = kpts[where] if mo[0].ndim == 2: # KRHF mo = mo[where] mo_occ = mo_occ[where] else: mo = mo[:, where] mo_occ = mo_occ[:, where] else: # from molecular code chk_kpt = np.zeros(3) def fproj(mo): if project: return addons.project_mo_nr2nr(chk_cell, mo, cell, chk_kpt - kpt) else: return mo if mo.ndim == 2: mo = fproj(mo) mo_occa = (mo_occ > 1e-8).astype(np.double) mo_occb = mo_occ - mo_occa dm = mol_uhf.make_rdm1([mo, mo], [mo_occa, mo_occb]) else: # UHF dm = mol_uhf.make_rdm1([fproj(mo[0]), fproj(mo[1])], mo_occ) # Real DM for gamma point if kpt is None or np.allclose(kpt, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=True, kpt=None): '''Read the HF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, (nao,nao) ndarray ''' chk_cell, scf_rec = chkfile.load_scf(chkfile_name) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if kpt is None: kpt = np.zeros(3) if 'kpt' in scf_rec: chk_kpt = scf_rec['kpt'] elif 'kpts' in scf_rec: kpts = scf_rec['kpts'] # the closest kpt from KRHF results where = np.argmin(lib.norm(kpts - kpt, axis=1)) chk_kpt = kpts[where] if mo.ndim == 3: # KRHF: mo = mo[where] mo_occ = mo_occ[where] else: mo = mo[:, where] mo_occ = mo_occ[:, where] else: chk_kpt = np.zeros(3) def fproj(mo): if project: return addons.project_mo_nr2nr(chk_cell, mo, cell, chk_kpt - kpt) else: return mo if mo.ndim == 2: dm = pyscf.scf.hf.make_rdm1(fproj(mo), mo_occ) else: # UHF dm = np.asarray((pyscf.scf.hf.make_rdm1(fproj(mo[0]), mo_occ[0]), pyscf.scf.hf.make_rdm1(fproj(mo[1]), mo_occ[1]))) # Real DM for gamma point if kpt is None or np.allclose(kpt, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=True, kpt=None): '''Read the HF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, (nao,nao) ndarray ''' chk_cell, scf_rec = chkfile.load_scf(chkfile_name) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if kpt is None: kpt = np.zeros(3) if 'kpt' in scf_rec: chk_kpt = scf_rec['kpt'] elif 'kpts' in scf_rec: kpts = scf_rec['kpts'] # the closest kpt from KRHF results where = np.argmin(lib.norm(kpts-kpt, axis=1)) chk_kpt = kpts[where] if mo.ndim == 3: # KRHF: mo = mo[where] mo_occ = mo_occ[where] else: mo = mo[:,where] mo_occ = mo_occ[:,where] else: chk_kpt = np.zeros(3) def fproj(mo): if project: return addons.project_mo_nr2nr(chk_cell, mo, cell, chk_kpt-kpt) else: return mo if mo.ndim == 2: dm = pyscf.scf.hf.make_rdm1(fproj(mo), mo_occ) else: # UHF dm = np.asarray((pyscf.scf.hf.make_rdm1(fproj(mo[0]), mo_occ[0]), pyscf.scf.hf.make_rdm1(fproj(mo[1]), mo_occ[1]))) # Real DM for gamma point if kpt is None or np.allclose(kpt, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=None, kpts=None): '''Read the KHF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, 3D ndarray ''' from pyscf import gto chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if project is None: project = not gto.same_basis_set(chk_cell, cell) if kpts is None: kpts = scf_rec['kpts'] if 'kpt' in scf_rec: chk_kpts = scf_rec['kpt'].reshape(-1, 3) elif 'kpts' in scf_rec: chk_kpts = scf_rec['kpts'] else: chk_kpts = np.zeros((1, 3)) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if 'kpts' not in scf_rec: # gamma point or single k-point if mo.ndim == 2: mo = np.expand_dims(mo, axis=0) mo_occ = np.expand_dims(mo_occ, axis=0) else: # UHF mo = [np.expand_dims(mo[0], axis=0), np.expand_dims(mo[1], axis=0)] mo_occ = [ np.expand_dims(mo_occ[0], axis=0), np.expand_dims(mo_occ[1], axis=0) ] if project: s = cell.pbc_intor('int1e_ovlp', kpts=kpts) def fproj(mo, kpts): if project: mo = addons.project_mo_nr2nr(chk_cell, mo, cell, kpts) for k, c in enumerate(mo): norm = np.einsum('pi,pi->i', c.conj(), s[k].dot(c)) mo[k] /= np.sqrt(norm) return mo if kpts.shape == chk_kpts.shape and np.allclose(kpts, chk_kpts): def makedm(mos, occs): moa, mob = mos mos = ([fproj(mo, None) for mo in moa], [fproj(mo, None) for mo in mob]) return make_rdm1(mos, occs) else: def makedm(mos, occs): where = [ np.argmin(lib.norm(chk_kpts - kpt, axis=1)) for kpt in kpts ] moa, mob = mos occa, occb = occs dkpts = [chk_kpts[w] - kpts[i] for i, w in enumerate(where)] mos = (fproj([moa[w] for w in where], dkpts), fproj([mob[w] for w in where], dkpts)) occs = ([occa[i] for i in where], [occb[i] for i in where]) return make_rdm1(mos, occs) if getattr(mo[0], 'ndim', None) == 2: # KRHF mo_occa = [(occ > 1e-8).astype(np.double) for occ in mo_occ] mo_occb = [occ - mo_occa[k] for k, occ in enumerate(mo_occ)] dm = makedm((mo, mo), (mo_occa, mo_occb)) else: # KUHF dm = makedm(mo, mo_occ) # Real DM for gamma point if np.allclose(kpts, 0): dm = dm.real return dm
def init_guess_by_chkfile(cell, chkfile_name, project=None, kpts=None): '''Read the KHF results from checkpoint file, then project it to the basis defined by ``cell`` Returns: Density matrix, 3D ndarray ''' from pyscf import gto chk_cell, scf_rec = chkfile.load_scf(chkfile_name) if project is None: project = not gto.same_basis_set(chk_cell, cell) if kpts is None: kpts = scf_rec['kpts'] if 'kpt' in scf_rec: chk_kpts = scf_rec['kpt'].reshape(-1,3) elif 'kpts' in scf_rec: chk_kpts = scf_rec['kpts'] else: chk_kpts = np.zeros((1,3)) mo = scf_rec['mo_coeff'] mo_occ = scf_rec['mo_occ'] if 'kpts' not in scf_rec: # gamma point or single k-point if mo.ndim == 2: mo = np.expand_dims(mo, axis=0) mo_occ = np.expand_dims(mo_occ, axis=0) else: # UHF mo = [np.expand_dims(mo[0], axis=0), np.expand_dims(mo[1], axis=0)] mo_occ = [np.expand_dims(mo_occ[0], axis=0), np.expand_dims(mo_occ[1], axis=0)] if project: s = cell.pbc_intor('int1e_ovlp', kpts=kpts) def fproj(mo, kpts): if project: mo = addons.project_mo_nr2nr(chk_cell, mo, cell, kpts) for k, c in enumerate(mo): norm = np.einsum('pi,pi->i', c.conj(), s[k].dot(c)) mo[k] /= np.sqrt(norm) return mo if kpts.shape == chk_kpts.shape and np.allclose(kpts, chk_kpts): def makedm(mos, occs): moa, mob = mos mos =([fproj(mo, None) for mo in moa], [fproj(mo, None) for mo in mob]) return make_rdm1(mos, occs) else: def makedm(mos, occs): where = [np.argmin(lib.norm(chk_kpts-kpt, axis=1)) for kpt in kpts] moa, mob = mos occa, occb = occs dkpts = [chk_kpts[w]-kpts[i] for i,w in enumerate(where)] mos = (fproj([moa[w] for w in where], dkpts), fproj([mob[w] for w in where], dkpts)) occs = ([occa[i] for i in where], [occb[i] for i in where]) return make_rdm1(mos, occs) if getattr(mo[0], 'ndim', None) == 2: # KRHF mo_occa = [(occ>1e-8).astype(np.double) for occ in mo_occ] mo_occb = [occ-mo_occa[k] for k,occ in enumerate(mo_occ)] dm = makedm((mo, mo), (mo_occa, mo_occb)) else: # KUHF dm = makedm(mo, mo_occ) # Real DM for gamma point if np.allclose(kpts, 0): dm = dm.real return dm