From 7860b3285225c66837bff75ae5dd194ad11e6393 Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Wed, 25 Oct 2023 18:22:26 -0700 Subject: [PATCH] Update mf type check code in all post HF functions --- pyscf/adc/__init__.py | 14 ++++++---- pyscf/agf2/__init__.py | 2 +- pyscf/cc/__init__.py | 52 ++++++++++++++++++++++--------------- pyscf/cc/addons.py | 6 ++--- pyscf/cc/ccsd.py | 2 ++ pyscf/cc/dfccsd.py | 1 + pyscf/cc/gccsd.py | 3 ++- pyscf/cc/gccsd_t.py | 4 +-- pyscf/cc/gccsd_t_rdm.py | 2 +- pyscf/cc/rccsd.py | 2 ++ pyscf/cc/uccsd.py | 5 +++- pyscf/ci/__init__.py | 52 +++++++++++++++++++++++-------------- pyscf/ci/addons.py | 3 +-- pyscf/mcscf/__init__.py | 30 +++++++++++++++------ pyscf/mp/__init__.py | 20 +++++++++----- pyscf/mp/dfgmp2.py | 1 + pyscf/mp/dfmp2_native.py | 4 +-- pyscf/mp/dfump2_native.py | 5 ++-- pyscf/mp/gmp2.py | 3 ++- pyscf/mp/mp2.py | 2 ++ pyscf/mp/ump2.py | 2 ++ pyscf/pbc/adc/__init__.py | 2 +- pyscf/pbc/cc/__init__.py | 17 +++++++----- pyscf/pbc/cc/kccsd.py | 2 +- pyscf/pbc/ci/__init__.py | 8 +++--- pyscf/pbc/mp/__init__.py | 6 ++--- pyscf/pbc/mpicc/__init__.py | 6 +++-- 27 files changed, 160 insertions(+), 96 deletions(-) diff --git a/pyscf/adc/__init__.py b/pyscf/adc/__init__.py index 4729982664..7426479b13 100644 --- a/pyscf/adc/__init__.py +++ b/pyscf/adc/__init__.py @@ -46,7 +46,7 @@ def ADC(mf, frozen=None, mo_coeff=None, mo_occ=None): #elif isinstance(mf, scf.rohf.ROHF): # lib.logger.warn(mf, 'RADC method does not support ROHF reference. ROHF object ' # 'is converted to UHF object and UADC method is called.') - # mf = scf.addons.convert_to_uhf(mf) + # mf = mf.to_uhf(mf) # return UADC(mf, frozen, mo_coeff, mo_occ) # TODO add ROHF functionality elif isinstance(mf, scf.rhf.RHF): @@ -64,8 +64,10 @@ def UADC(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_uhf(mf) + if not isinstance(mf, scf.uhf.UHF): + mf = mf.to_uhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() return uadc.UADC(mf, frozen, mo_coeff, mo_occ) @@ -79,8 +81,10 @@ def RADC(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.rhf.RHF): - mf = scf.addons.convert_to_rhf(mf) + if not isinstance(mf, scf.rhf.RHF): + mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() return radc.RADC(mf, frozen, mo_coeff, mo_occ) diff --git a/pyscf/agf2/__init__.py b/pyscf/agf2/__init__.py index b02eced361..6baecf6d3e 100644 --- a/pyscf/agf2/__init__.py +++ b/pyscf/agf2/__init__.py @@ -102,7 +102,7 @@ def AGF2(mf, nmom=(None,0), frozen=None, mo_energy=None, mo_coeff=None, mo_occ=N elif isinstance(mf, scf.rohf.ROHF): lib.logger.warn(mf, 'RAGF2 method does not support ROHF reference. ' 'Converting to UHF and using UAGF2.') - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return UAGF2(mf, nmom, frozen, mo_energy, mo_coeff, mo_occ) elif isinstance(mf, scf.rhf.RHF): diff --git a/pyscf/cc/__init__.py b/pyscf/cc/__init__.py index 4c22444e12..a3ba9d5869 100644 --- a/pyscf/cc/__init__.py +++ b/pyscf/cc/__init__.py @@ -79,9 +79,9 @@ from pyscf import scf def CCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): return UCCSD(mf, frozen, mo_coeff, mo_occ) - elif isinstance(mf, scf.ghf.GHF): + elif mf.istype('GHF'): return GCCSD(mf, frozen, mo_coeff, mo_occ) else: return RCCSD(mf, frozen, mo_coeff, mo_occ) @@ -93,21 +93,24 @@ def CCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): def RCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): import numpy from pyscf import lib + from pyscf.df.df_jk import _DFHF from pyscf.soscf import newton_ah from pyscf.cc import dfccsd - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise RuntimeError('RCCSD cannot be used with UHF method.') - elif isinstance(mf, scf.rohf.ROHF): + elif mf.istype('ROHF'): lib.logger.warn(mf, 'RCCSD method does not support ROHF method. ROHF object ' 'is converted to UHF object and UCCSD method is called.') - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return UCCSD(mf, frozen, mo_coeff, mo_occ) - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.hf.RHF): - mf = scf.addons.convert_to_rhf(mf) + if not mf.istype('RHF'): + mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: return dfccsd.RCCSD(mf, frozen, mo_coeff, mo_occ) elif numpy.iscomplexobj(mo_coeff) or numpy.iscomplexobj(mf.mo_coeff): @@ -119,12 +122,15 @@ def RCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): def UCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.df.df_jk import _DFHF from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_uhf(mf) + if not mf.istype('UHF'): + mf = mf.to_uhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: # TODO: DF-UCCSD with memory-efficient particle-particle ladder, # similar to dfccsd.RCCSD return uccsd.UCCSD(mf, frozen, mo_coeff, mo_occ) @@ -134,12 +140,15 @@ def UCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): def GCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.df.df_jk import _DFHF from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.ghf.GHF): - mf = scf.addons.convert_to_ghf(mf) + if not mf.istype('GHF'): + mf = mf.to_ghf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: raise NotImplementedError('DF-GCCSD') else: return gccsd.GCCSD(mf, frozen, mo_coeff, mo_occ) @@ -147,9 +156,9 @@ def GCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): def QCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise NotImplementedError - elif isinstance(mf, scf.ghf.GHF): + elif mf.istype('GHF'): raise NotImplementedError else: return RQCISD(mf, frozen, mo_coeff, mo_occ) @@ -162,16 +171,17 @@ def RQCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf import lib from pyscf.soscf import newton_ah - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise RuntimeError('RQCISD cannot be used with UHF method.') - elif isinstance(mf, scf.rohf.ROHF): + elif mf.istype('ROHF'): lib.logger.warn(mf, 'RQCISD method does not support ROHF method. ROHF object ' 'is converted to UHF object and UQCISD method is called.') - mf = scf.addons.convert_to_uhf(mf) raise NotImplementedError - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.hf.RHF): - mf = scf.addons.convert_to_rhf(mf) + if not mf.istype('RHF'): + mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() elif numpy.iscomplexobj(mo_coeff) or numpy.iscomplexobj(mf.mo_coeff): raise NotImplementedError diff --git a/pyscf/cc/addons.py b/pyscf/cc/addons.py index 2063c6eb80..134ad60df2 100644 --- a/pyscf/cc/addons.py +++ b/pyscf/cc/addons.py @@ -122,14 +122,13 @@ def spin2spatial(tx, orbspin): return t2aa,t2ab,t2bb def convert_to_uccsd(mycc): - from pyscf import scf from pyscf.cc import uccsd, gccsd if isinstance(mycc, uccsd.UCCSD): return mycc elif isinstance(mycc, gccsd.GCCSD): raise NotImplementedError - mf = scf.addons.convert_to_uhf(mycc._scf) + mf = mycc._scf.to_uhf() ucc = uccsd.UCCSD(mf) assert (mycc._nocc is None) assert (mycc._nmo is None) @@ -143,12 +142,11 @@ def convert_to_uccsd(mycc): return ucc def convert_to_gccsd(mycc): - from pyscf import scf from pyscf.cc import gccsd if isinstance(mycc, gccsd.GCCSD): return mycc - mf = scf.addons.convert_to_ghf(mycc._scf) + mf = mycc._scf.to_ghf() gcc = gccsd.GCCSD(mf) assert (mycc._nocc is None) assert (mycc._nmo is None) diff --git a/pyscf/cc/ccsd.py b/pyscf/cc/ccsd.py index 15df586dc4..bbe2181c05 100644 --- a/pyscf/cc/ccsd.py +++ b/pyscf/cc/ccsd.py @@ -1496,6 +1496,8 @@ def _make_eris_incore(mycc, mo_coeff=None): return eris def _make_eris_outcore(mycc, mo_coeff=None): + from pyscf.scf.hf import RHF + assert isinstance(mycc._scf, RHF) cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mycc.stdout, mycc.verbose) eris = _ChemistsERIs() diff --git a/pyscf/cc/dfccsd.py b/pyscf/cc/dfccsd.py index 6c7af7ebf3..632617a059 100644 --- a/pyscf/cc/dfccsd.py +++ b/pyscf/cc/dfccsd.py @@ -121,6 +121,7 @@ def _contract_vvvv_t2(self, mycc, t2, direct=False, out=None, verbose=None): return _contract_vvvv_t2(mycc, self.mol, self.vvL, t2, out, verbose) def _make_df_eris(cc, mo_coeff=None): + assert cc._scf.istype('RHF') eris = _ChemistsERIs() eris._common_init_(cc, mo_coeff) nocc = eris.nocc diff --git a/pyscf/cc/gccsd.py b/pyscf/cc/gccsd.py index f9b4687727..d7e842ffa1 100644 --- a/pyscf/cc/gccsd.py +++ b/pyscf/cc/gccsd.py @@ -117,7 +117,6 @@ class GCCSD(ccsd.CCSDBase): conv_tol_normt = getattr(__config__, 'cc_gccsd_GCCSD_conv_tol_normt', 1e-6) def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None): - assert (isinstance(mf, scf.ghf.GHF)) ccsd.CCSDBase.__init__(self, mf, frozen, mo_coeff, mo_occ) def init_amps(self, eris=None): @@ -385,6 +384,8 @@ def _make_eris_incore(mycc, mo_coeff=None, ao2mofn=None): return eris def _make_eris_outcore(mycc, mo_coeff=None): + from pyscf.scf.ghf import GHF + assert isinstance(mycc._scf, GHF) cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mycc.stdout, mycc.verbose) diff --git a/pyscf/cc/gccsd_t.py b/pyscf/cc/gccsd_t.py index 18bff61776..3a16f4a1b7 100644 --- a/pyscf/cc/gccsd_t.py +++ b/pyscf/cc/gccsd_t.py @@ -85,13 +85,13 @@ def get_wv(a, b, c): mycc = cc.CCSD(mf).set(conv_tol=1e-11).run() et = mycc.ccsd_t() - mycc = cc.GCCSD(scf.addons.convert_to_ghf(mf)).set(conv_tol=1e-11).run() + mycc = cc.GCCSD(mf.to_ghf()).set(conv_tol=1e-11).run() eris = mycc.ao2mo() print(kernel(mycc, eris) - et) numpy.random.seed(1) mf.mo_coeff = numpy.random.random(mf.mo_coeff.shape) - .9 - mycc = cc.GCCSD(scf.addons.convert_to_ghf(mf)) + mycc = cc.GCCSD(scf.addons.convert_to_ghf()) eris = mycc.ao2mo() nocc = 10 nvir = mol.nao_nr() * 2 - nocc diff --git a/pyscf/cc/gccsd_t_rdm.py b/pyscf/cc/gccsd_t_rdm.py index 83acca3c58..f76b09c7a2 100644 --- a/pyscf/cc/gccsd_t_rdm.py +++ b/pyscf/cc/gccsd_t_rdm.py @@ -120,7 +120,7 @@ def make_rdm2(mycc, t1, t2, l1, l2, eris=None): mol.basis = '631g' mol.build() mf0 = mf = scf.RHF(mol).run(conv_tol=1.) - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() from pyscf.cc import ccsd_t_lambda_slow as ccsd_t_lambda from pyscf.cc import ccsd_t_rdm_slow as ccsd_t_rdm diff --git a/pyscf/cc/rccsd.py b/pyscf/cc/rccsd.py index e38fefcb09..f23f183a87 100644 --- a/pyscf/cc/rccsd.py +++ b/pyscf/cc/rccsd.py @@ -259,6 +259,8 @@ def _make_eris_incore(mycc, mo_coeff=None, ao2mofn=None): return eris def _make_eris_outcore(mycc, mo_coeff=None): + from pyscf.scf.hf import RHF + assert isinstance(mycc._scf, RHF) cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mycc.stdout, mycc.verbose) eris = _ChemistsERIs() diff --git a/pyscf/cc/uccsd.py b/pyscf/cc/uccsd.py index b7ff3a57e3..8f521d3679 100644 --- a/pyscf/cc/uccsd.py +++ b/pyscf/cc/uccsd.py @@ -547,7 +547,7 @@ class UCCSD(ccsd.CCSDBase): # * A pair of list : First list is the orbital indices to be frozen for alpha # orbitals, second list is for beta orbitals def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None): - assert isinstance(mf, scf.uhf.UHF) + assert mf.istype('UHF') ccsd.CCSDBase.__init__(self, mf, frozen, mo_coeff, mo_occ) get_nocc = get_nocc @@ -942,6 +942,7 @@ def _make_eris_incore(mycc, mo_coeff=None, ao2mofn=None): return eris def _make_df_eris_outcore(mycc, mo_coeff=None): + assert mycc._scf.istype('UHF') cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mycc.stdout, mycc.verbose) eris = _ChemistsERIs() @@ -1059,6 +1060,8 @@ def _make_df_eris_outcore(mycc, mo_coeff=None): return eris def _make_eris_outcore(mycc, mo_coeff=None): + from pyscf.scf.uhf import UHF + assert isinstance(mycc._scf, UHF) eris = _ChemistsERIs() eris._common_init_(mycc, mo_coeff) diff --git a/pyscf/ci/__init__.py b/pyscf/ci/__init__.py index 3ab124c471..4f67bbdb23 100644 --- a/pyscf/ci/__init__.py +++ b/pyscf/ci/__init__.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from pyscf import lib from pyscf import scf from pyscf.ci import cisd from pyscf.ci import ucisd @@ -23,9 +22,10 @@ def CISD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): return UCISD(mf, frozen, mo_coeff, mo_occ) - elif isinstance(mf, scf.rohf.ROHF): + elif mf.istype('ROHF'): + from pyscf import lib lib.logger.warn(mf, 'RCISD method does not support ROHF method. ROHF object ' 'is converted to UHF object and UCISD method is called.') return UCISD(mf, frozen, mo_coeff, mo_occ) @@ -34,12 +34,18 @@ def CISD(mf, frozen=None, mo_coeff=None, mo_occ=None): CISD.__doc__ = cisd.CISD.__doc__ def RCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.df.df_jk import _DFHF from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.hf.RHF): - mf = scf.addons.convert_to_rhf(mf) + if not mf.istype('RHF'): + mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: + from pyscf import lib + lib.logger.warn(mf, f'DF-RCISD for DFHF method {mf} is not available. ' + 'Normal RCISD method is called.') return cisd.RCISD(mf, frozen, mo_coeff, mo_occ) else: return cisd.RCISD(mf, frozen, mo_coeff, mo_occ) @@ -48,10 +54,15 @@ def RCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): def UCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_uhf(mf) + if not mf.istype('UHF'): + mf = mf.to_uhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: + from pyscf import lib + lib.logger.warn(mf, f'DF-UCISD for DFHF method {mf} is not available. ' + 'Normal UCISD method is called.') return ucisd.UCISD(mf, frozen, mo_coeff, mo_occ) else: return ucisd.UCISD(mf, frozen, mo_coeff, mo_occ) @@ -61,10 +72,12 @@ def UCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): def GCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.ghf.GHF): - mf = scf.addons.convert_to_ghf(mf) + if not mf.istype('GHF'): + mf = mf.to_ghf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: raise NotImplementedError('DF-GCISD') else: return gcisd.GCISD(mf, frozen, mo_coeff, mo_occ) @@ -72,9 +85,9 @@ def GCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): def QCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise NotImplementedError - elif isinstance(mf, scf.ghf.GHF): + elif mf.istype('GHF'): raise NotImplementedError else: return RQCISD(mf, frozen, mo_coeff, mo_occ) @@ -87,16 +100,17 @@ def RQCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf import lib from pyscf.soscf import newton_ah - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise RuntimeError('RQCISD cannot be used with UHF method.') - elif isinstance(mf, scf.rohf.ROHF): + elif mf.istype('ROHF'): lib.logger.warn(mf, 'RQCISD method does not support ROHF method. ROHF object ' 'is converted to UHF object and UQCISD method is called.') - mf = scf.addons.convert_to_uhf(mf) raise NotImplementedError - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.hf.RHF): - mf = scf.addons.convert_to_rhf(mf) + if not mf.istype('RHF'): + mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() elif numpy.iscomplexobj(mo_coeff) or numpy.iscomplexobj(mf.mo_coeff): raise NotImplementedError diff --git a/pyscf/ci/addons.py b/pyscf/ci/addons.py index 468e1e281c..6c19c5a731 100644 --- a/pyscf/ci/addons.py +++ b/pyscf/ci/addons.py @@ -17,14 +17,13 @@ # import numpy as np -from pyscf import scf def convert_to_gcisd(myci): from pyscf.ci import gcisd if isinstance(myci, gcisd.GCISD): return myci - mf = scf.addons.convert_to_ghf(myci._scf) + mf = myci._scf.to_ghf() gci = gcisd.GCISD(mf) assert (myci._nocc is None) assert (myci._nmo is None) diff --git a/pyscf/mcscf/__init__.py b/pyscf/mcscf/__init__.py index cd6a6670b6..8d8b105ee6 100644 --- a/pyscf/mcscf/__init__.py +++ b/pyscf/mcscf/__init__.py @@ -193,14 +193,15 @@ def CASSCF(mf_or_mol, ncas, nelecas, ncore=None, frozen=None): from pyscf import gto from pyscf import scf + from pyscf.df.df_jk import _DFHF if isinstance(mf_or_mol, gto.MoleBase): mf = mf_or_mol.RHF() else: mf = mf_or_mol if isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_rhf(mf) - if getattr(mf, 'with_df', None): + mf = mf.to_rhf() + if isinstance(mf, _DFHF) and mf.with_df: return DFCASSCF(mf, ncas, nelecas, ncore, frozen) if mf.mol.symmetry: @@ -215,15 +216,16 @@ def CASSCF(mf_or_mol, ncas, nelecas, ncore=None, frozen=None): def CASCI(mf_or_mol, ncas, nelecas, ncore=None): from pyscf import gto from pyscf import scf + from pyscf.df.df_jk import _DFHF if isinstance(mf_or_mol, gto.MoleBase): mf = mf_or_mol.RHF() else: mf = mf_or_mol if isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() - if getattr(mf, 'with_df', None): + if isinstance(mf, _DFHF) and mf.with_df: return DFCASCI(mf, ncas, nelecas, ncore) if mf.mol.symmetry: @@ -238,13 +240,19 @@ def CASCI(mf_or_mol, ncas, nelecas, ncore=None): def UCASCI(mf_or_mol, ncas, nelecas, ncore=None): from pyscf import gto from pyscf import scf + from pyscf.df.df_jk import _DFHF if isinstance(mf_or_mol, gto.MoleBase): mf = mf_or_mol.UHF() else: mf = mf_or_mol if not isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_uhf(mf, remove_df=True) + mf = mf.to_uhf() + if isinstance(mf, _DFHF) and mf.with_df: + from pyscf.lib import logger + logger.warn(mf, f'DF-UCASCI for DFHF method {mf} is not available. ' + 'Normal UCASCI method is called.') + mf = mf.undo_df() mc = ucasci.UCASCI(mf, ncas, nelecas, ncore) return mc @@ -252,13 +260,19 @@ def UCASCI(mf_or_mol, ncas, nelecas, ncore=None): def UCASSCF(mf_or_mol, ncas, nelecas, ncore=None, frozen=None): from pyscf import gto from pyscf import scf + from pyscf.df.df_jk import _DFHF if isinstance(mf_or_mol, gto.MoleBase): mf = mf_or_mol.UHF() else: mf = mf_or_mol if not isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_uhf(mf, remove_df=True) + mf = mf.to_uhf() + if isinstance(mf, _DFHF) and mf.with_df: + from pyscf.lib import logger + logger.warn(mf, f'DF-UCASSCF for DFHF method {mf} is not available. ' + 'Normal UCASSCF method is called.') + mf = mf.undo_df() mc = umc1step.UCASSCF(mf, ncas, nelecas, ncore, frozen) return mc @@ -277,7 +291,7 @@ def DFCASSCF(mf_or_mol, ncas, nelecas, auxbasis=None, ncore=None, mf = mf_or_mol if isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_rhf(mf, remove_df=False) + mf = mf.to_rhf() if mf.mol.symmetry: mc = mc1step_symm.CASSCF(mf, ncas, nelecas, ncore, frozen) @@ -294,7 +308,7 @@ def DFCASCI(mf_or_mol, ncas, nelecas, auxbasis=None, ncore=None): mf = mf_or_mol if isinstance(mf, scf.uhf.UHF): - mf = scf.addons.convert_to_rhf(mf, remove_df=False) + mf = mf.to_rhf() if mf.mol.symmetry: mc = casci_symm.CASCI(mf, ncas, nelecas, ncore) diff --git a/pyscf/mp/__init__.py b/pyscf/mp/__init__.py index 7e01ea4176..ecd29b1eb6 100644 --- a/pyscf/mp/__init__.py +++ b/pyscf/mp/__init__.py @@ -24,9 +24,9 @@ from pyscf.mp import dfgmp2 def MP2(mf, frozen=None, mo_coeff=None, mo_occ=None): - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): return UMP2(mf, frozen, mo_coeff, mo_occ) - elif isinstance(mf, scf.ghf.GHF): + elif mf.istype('GHF'): return GMP2(mf, frozen, mo_coeff, mo_occ) else: return RMP2(mf, frozen, mo_coeff, mo_occ) @@ -36,15 +36,17 @@ def RMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf import lib from pyscf.soscf import newton_ah - if isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): raise RuntimeError('RMP2 cannot be used with UHF method.') - elif isinstance(mf, scf.rohf.ROHF): + elif mf.istype('ROHF'): lib.logger.warn(mf, 'RMP2 method does not support ROHF method. ROHF object ' 'is converted to UHF object and UMP2 method is called.') return UMP2(mf, frozen, mo_coeff, mo_occ) - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.hf.RHF): + if not mf.istype('RHF'): mf = mf.to_rhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() if getattr(mf, 'with_df', None): return dfmp2.DFMP2(mf, frozen, mo_coeff, mo_occ) @@ -55,8 +57,10 @@ def RMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): def UMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.uhf.UHF): + if mf.istype('UHF'): mf = mf.to_uhf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() if getattr(mf, 'with_df', None): #raise NotImplementedError('DF-UMP2') @@ -68,8 +72,10 @@ def UMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): def GMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah - if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.ghf.GHF): + if not mf.istype('GHF'): mf = mf.to_ghf() + if isinstance(mf, newton_ah._CIAH_SOSCF): + mf = mf.undo_soscf() if getattr(mf, 'with_df', None): return dfgmp2.DFGMP2(mf, frozen, mo_coeff, mo_occ) diff --git a/pyscf/mp/dfgmp2.py b/pyscf/mp/dfgmp2.py index 62704ca922..70e11587fd 100644 --- a/pyscf/mp/dfgmp2.py +++ b/pyscf/mp/dfgmp2.py @@ -85,6 +85,7 @@ def kernel(mp, mo_energy=None, mo_coeff=None, eris=None, with_t2=WITH_T2, class DFGMP2(dfmp2.DFMP2): def loop_ao2mo(self, mo_coeff, nocc, orbspin): + assert self._scf.istype('GHF') nao, nmo = mo_coeff.shape complex_orb = mo_coeff.dtype == np.complex128 if orbspin is None: diff --git a/pyscf/mp/dfmp2_native.py b/pyscf/mp/dfmp2_native.py index 186a32f46c..92a2ef0627 100644 --- a/pyscf/mp/dfmp2_native.py +++ b/pyscf/mp/dfmp2_native.py @@ -41,8 +41,6 @@ def __init__(self, mf, frozen=None, auxbasis=None): auxbasis : name of auxiliary basis set, otherwise determined automatically ''' - if not isinstance(mf, scf.rhf.RHF): - raise TypeError('Class initialization with non-RHF object') self.mo_coeff = mf.mo_coeff self.mo_energy = mf.mo_energy self.nocc = np.count_nonzero(mf.mo_occ) @@ -204,6 +202,8 @@ def calculate_integrals_(self): ''' Calculates the three center integrals for MP2. ''' + if not isinstance(self._scf, scf.rhf.RHF): + raise TypeError('Class initialization with non-RHF object') Co = self.mo_coeff[:, self.occ_mask] Cv = self.mo_coeff[:, self.nocc:] logger = lib.logger.new_logger(self) diff --git a/pyscf/mp/dfump2_native.py b/pyscf/mp/dfump2_native.py index c125fe146f..5a3a3b0db8 100644 --- a/pyscf/mp/dfump2_native.py +++ b/pyscf/mp/dfump2_native.py @@ -40,9 +40,6 @@ def __init__(self, mf, frozen=None, auxbasis=None): auxbasis : name of auxiliary basis set, otherwise determined automatically ''' - if not isinstance(mf, scf.uhf.UHF): - raise TypeError('Class initialization with non-UHF object') - # UHF quantities are stored as numpy arrays self.mo_coeff = np.array(mf.mo_coeff) self.mo_energy = np.array(mf.mo_energy) @@ -205,6 +202,8 @@ def calculate_integrals_(self): ''' Calculates the three center integrals for MP2. ''' + if not isinstance(self._scf, scf.uhf.UHF): + raise TypeError('Class initialization with non-UHF object') intsfile = [] logger = lib.logger.new_logger(self) logger.info('') diff --git a/pyscf/mp/gmp2.py b/pyscf/mp/gmp2.py index f91bcc3863..7fae2ffdd0 100644 --- a/pyscf/mp/gmp2.py +++ b/pyscf/mp/gmp2.py @@ -175,7 +175,6 @@ def make_rdm2(mp, t2=None, ao_repr=False): class GMP2(mp2.MP2): def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None): - assert (isinstance(mf, scf.ghf.GHF)) mp2.MP2.__init__(self, mf, frozen, mo_coeff, mo_occ) def ao2mo(self, mo_coeff=None): @@ -302,6 +301,8 @@ def _make_eris_incore(mp, mo_coeff=None, ao2mofn=None, verbose=None): return eris def _make_eris_outcore(mp, mo_coeff=None, verbose=None): + from pyscf.scf.ghf import GHF + assert isinstance(mp._scf, GHF) cput0 = (logger.process_clock(), logger.perf_counter()) log = logger.Logger(mp.stdout, mp.verbose) eris = _PhysicistsERIs() diff --git a/pyscf/mp/mp2.py b/pyscf/mp/mp2.py index a90548f1d2..ba33e7ad2f 100644 --- a/pyscf/mp/mp2.py +++ b/pyscf/mp/mp2.py @@ -756,6 +756,8 @@ def _make_eris(mp, mo_coeff=None, ao2mofn=None, verbose=None): # or => (ij|ol) => (oj|ol) => (oj|ov) => (ov|ov) # def _ao2mo_ovov(mp, orbo, orbv, feri, max_memory=2000, verbose=None): + from pyscf.scf.hf import RHF + assert isinstance(mp._scf, RHF) time0 = (logger.process_clock(), logger.perf_counter()) log = logger.new_logger(mp, verbose) diff --git a/pyscf/mp/ump2.py b/pyscf/mp/ump2.py index ac1a9c1c45..425b3c7c7b 100644 --- a/pyscf/mp/ump2.py +++ b/pyscf/mp/ump2.py @@ -558,6 +558,8 @@ def _make_eris(mp, mo_coeff=None, ao2mofn=None, verbose=None): return eris def _ao2mo_ovov(mp, orbs, feri, max_memory=2000, verbose=None): + from pyscf.scf.uhf import UHF + assert isinstance(mp._scf, UHF) time0 = (logger.process_clock(), logger.perf_counter()) log = logger.new_logger(mp, verbose) orboa = numpy.asarray(orbs[0], order='F') diff --git a/pyscf/pbc/adc/__init__.py b/pyscf/pbc/adc/__init__.py index a2a8d16c47..4e0d2c69c0 100644 --- a/pyscf/pbc/adc/__init__.py +++ b/pyscf/pbc/adc/__init__.py @@ -21,5 +21,5 @@ def KRADC(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.pbc.adc import kadc_rhf if not isinstance(mf, scf.khf.KRHF): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return kadc_rhf.RADC(mf, frozen, mo_coeff, mo_occ) diff --git a/pyscf/pbc/cc/__init__.py b/pyscf/pbc/cc/__init__.py index 614cba498f..fe813029d4 100644 --- a/pyscf/pbc/cc/__init__.py +++ b/pyscf/pbc/cc/__init__.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from pyscf.pbc import scf from pyscf.pbc.cc import ccsd from pyscf.pbc.cc import kccsd_rhf as krccsd from pyscf.pbc.cc import kccsd_uhf as kuccsd @@ -24,31 +23,35 @@ from pyscf.pbc.cc.kccsd_rhf_ksymm import KsymAdaptedRCCSD def RCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return ccsd.RCCSD(mf, frozen, mo_coeff, mo_occ) CCSD = RCCSD def UCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return ccsd.UCCSD(mf, frozen, mo_coeff, mo_occ) def GCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() return ccsd.GCCSD(mf, frozen, mo_coeff, mo_occ) def KGCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() return kgccsd.GCCSD(mf, frozen, mo_coeff, mo_occ) def KRCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.pbc import scf + assert isinstance(mf, scf.khf.KSCF) if not isinstance(mf, scf.khf.KRHF): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return krccsd.RCCSD(mf, frozen, mo_coeff, mo_occ) KCCSD = KRCCSD def KUCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.pbc import scf + assert isinstance(mf, scf.khf.KSCF) if not isinstance(mf, scf.kuhf.KUHF): - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return kuccsd.UCCSD(mf, frozen, mo_coeff, mo_occ) diff --git a/pyscf/pbc/cc/kccsd.py b/pyscf/pbc/cc/kccsd.py index 16b493975d..3fa54e7024 100644 --- a/pyscf/pbc/cc/kccsd.py +++ b/pyscf/pbc/cc/kccsd.py @@ -333,7 +333,7 @@ class GCCSD(gccsd.GCCSD): def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None): assert (isinstance(mf, scf.khf.KSCF)) if not isinstance(mf, scf.kghf.KGHF): - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() self.kpts = mf.kpts self.khelper = kpts_helper.KptsHelper(mf.cell, mf.kpts) gccsd.GCCSD.__init__(self, mf, frozen, mo_coeff, mo_occ) diff --git a/pyscf/pbc/ci/__init__.py b/pyscf/pbc/ci/__init__.py index ec8457eeac..e6dc4241ca 100644 --- a/pyscf/pbc/ci/__init__.py +++ b/pyscf/pbc/ci/__init__.py @@ -16,21 +16,21 @@ from pyscf.pbc.ci import cisd, kcis_rhf def RCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return cisd.RCISD(mf, frozen, mo_coeff, mo_occ) CISD = RCISD def UCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return cisd.UCISD(mf, frozen, mo_coeff, mo_occ) def GCISD(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() return cisd.GCISD(mf, frozen, mo_coeff, mo_occ) def KCIS(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return kcis_rhf.KCIS(mf, frozen, mo_coeff, mo_occ) CIS = KCIS diff --git a/pyscf/pbc/mp/__init__.py b/pyscf/pbc/mp/__init__.py index 5ecd9bb1b5..12920be70b 100644 --- a/pyscf/pbc/mp/__init__.py +++ b/pyscf/pbc/mp/__init__.py @@ -20,17 +20,17 @@ from pyscf.pbc.lib import kpts as libkpts def RMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_rhf(mf) + mf = mf.to_rhf() return mp2.RMP2(mf, frozen, mo_coeff, mo_occ) MP2 = RMP2 def UMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_uhf(mf) + mf = mf.to_uhf() return mp2.UMP2(mf, frozen, mo_coeff, mo_occ) def GMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): - mf = scf.addons.convert_to_ghf(mf) + mf = mf.to_ghf() return mp2.GMP2(mf, frozen, mo_coeff, mo_occ) def KRMP2(mf, frozen=None, mo_coeff=None, mo_occ=None): diff --git a/pyscf/pbc/mpicc/__init__.py b/pyscf/pbc/mpicc/__init__.py index 3954fb745b..4778d367e3 100644 --- a/pyscf/pbc/mpicc/__init__.py +++ b/pyscf/pbc/mpicc/__init__.py @@ -13,11 +13,13 @@ # limitations under the License. from pyscf.pbc.cc import ccsd -from pyscf.pbc import scf def KRCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): + from pyscf.pbc import scf from pyscf.pbc.mpicc import kccsd_rhf - mf = scf.addons.convert_to_rhf(mf) + assert isinstance(mf, scf.khf.KSCF) + if not isinstance(mf, scf.khf.KRHF): + mf = mf.to_rhf() return kccsd_rhf.RCCSD(mf, frozen) KCCSD = KRCCSD