Skip to content

Commit

Permalink
Fix basis parser and other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Sep 11, 2023
1 parent 189b20b commit ed0611c
Show file tree
Hide file tree
Showing 34 changed files with 1,231 additions and 1,191 deletions.
8 changes: 1 addition & 7 deletions pyscf/cc/ccsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def __init__(self, cc):
self._scf = cc._scf.as_scanner()

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, gto.Mole):
if isinstance(mol_or_geom, gto.MoleBase):
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down Expand Up @@ -934,12 +934,6 @@ class CCSDBase(lib.StreamObject):
))

def __init__(self, mf, frozen=None, mo_coeff=None, mo_occ=None):
if isinstance(mf, gto.Mole):
raise RuntimeError('''
You see this error message because of the API updates in pyscf v0.10.
In the new API, the first argument of CC class is HF objects. Please see
http://sunqm.net/pyscf/code-rule.html#api-rules for the details of API conventions''')

from pyscf.scf import hf
if isinstance(mf, hf.KohnShamDFT):
raise RuntimeError(
Expand Down
4 changes: 2 additions & 2 deletions pyscf/ci/cisd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from functools import reduce
import numpy
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf.lib import logger
from pyscf.cc import ccsd
Expand Down Expand Up @@ -786,7 +786,7 @@ def __init__(self, ci):
self._scf = ci._scf.as_scanner()

def __call__(self, mol_or_geom, ci0=None, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.Mole):
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/df/grad/casscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from itertools import product
import numpy
from scipy import linalg
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf import ao2mo
from pyscf.lib import logger
Expand Down Expand Up @@ -160,7 +160,8 @@ def __init__(self, g):
lib.GradScanner.__init__(self, g)

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, gto.Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/df/grad/sacasscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import gc
from functools import reduce
from scipy import linalg
from pyscf.gto import Mole
from pyscf import gto
from pyscf import mcscf, lib, ao2mo
from pyscf.grad import lagrange
from pyscf.grad import rhf as rhf_grad
Expand Down Expand Up @@ -331,7 +331,8 @@ def __init__(self, g, state):
self.state = state

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
6 changes: 3 additions & 3 deletions pyscf/df/incore.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def aux_e2(mol, auxmol_or_auxbasis, intor='int3c2e', aosym='s1', comp=None, out=
cintopt = gto.moleintor.make_cintopt(mol._atm, mol._bas, mol._env, 'int3c2e')
'''
if isinstance(auxmol_or_auxbasis, gto.Mole):
if isinstance(auxmol_or_auxbasis, gto.MoleBase):
auxmol = auxmol_or_auxbasis
else:
auxbasis = auxmol_or_auxbasis
Expand Down Expand Up @@ -76,7 +76,7 @@ def aux_e1(mol, auxmol_or_auxbasis, intor='int3c2e', aosym='s1', comp=None, out=
The same arguments as function aux_e2 can be input to aux_e1.
'''
if isinstance(auxmol_or_auxbasis, gto.Mole):
if isinstance(auxmol_or_auxbasis, gto.MoleBase):
auxmol = auxmol_or_auxbasis
else:
auxbasis = auxmol_or_auxbasis
Expand All @@ -96,7 +96,7 @@ def aux_e1(mol, auxmol_or_auxbasis, intor='int3c2e', aosym='s1', comp=None, out=
def fill_2c2e(mol, auxmol_or_auxbasis, intor='int2c2e', comp=None, hermi=1, out=None):
'''2-center 2-electron AO integrals for auxiliary basis (auxmol)
'''
if isinstance(auxmol_or_auxbasis, gto.Mole):
if isinstance(auxmol_or_auxbasis, gto.MoleBase):
auxmol = auxmol_or_auxbasis
else:
auxbasis = auxmol_or_auxbasis
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/casci.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from functools import reduce
import numpy
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf import ao2mo
from pyscf.lib import logger
Expand Down Expand Up @@ -226,7 +226,8 @@ def __init__(self, g, state):
self.state = state

def __call__(self, mol_or_geom, state=None, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/casscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from functools import reduce
import numpy
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf import ao2mo
from pyscf.lib import logger
Expand Down Expand Up @@ -171,7 +171,8 @@ def __init__(self, g):
lib.GradScanner.__init__(self, g)

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/ccsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import numpy
from functools import reduce
from pyscf import lib
from pyscf.gto import Mole
from pyscf import gto
from pyscf.lib import logger
from pyscf.cc import ccsd
from pyscf.cc import _ccsd
Expand Down Expand Up @@ -231,7 +231,8 @@ def __init__(self, g):
lib.GradScanner.__init__(self, g)

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/cisd.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'''

import numpy
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf.lib import logger
from pyscf.ci import cisd
Expand Down Expand Up @@ -89,7 +89,8 @@ def __init__(self, g, state):
self.state = state

def __call__(self, mol_or_geom, state=None, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/mp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import numpy
from pyscf import lib
from functools import reduce
from pyscf.gto import Mole
from pyscf import gto
from pyscf.lib import logger
from pyscf.grad import rhf as rhf_grad
from pyscf.scf import cphf
Expand Down Expand Up @@ -224,7 +224,8 @@ def __init__(self, g):
lib.GradScanner.__init__(self, g)

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
3 changes: 2 additions & 1 deletion pyscf/grad/rhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def __init__(self, g):
lib.GradScanner.__init__(self, g)

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, gto.Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/sacasscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from functools import reduce
from itertools import product
from scipy import linalg
from pyscf.gto import Mole
from pyscf import gto
from pyscf.grad import lagrange
from pyscf.mcscf.addons import StateAverageMCSCFSolver, StateAverageFCISolver
from pyscf.mcscf.addons import StateAverageMixFCISolver, state_average_mix_
Expand Down Expand Up @@ -374,7 +374,8 @@ def __init__(self, g, state):
self._converged = False

def __call__(self, mol_or_geom, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
5 changes: 3 additions & 2 deletions pyscf/grad/tdrhf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from functools import reduce
import numpy
from pyscf.gto import Mole
from pyscf import gto
from pyscf import lib
from pyscf.lib import logger
from pyscf.grad import rhf as rhf_grad
Expand Down Expand Up @@ -207,7 +207,8 @@ def __init__(self, g, state):
self.state = state

def __call__(self, mol_or_geom, state=None, **kwargs):
if isinstance(mol_or_geom, Mole):
if isinstance(mol_or_geom, gto.MoleBase):
assert mol_or_geom.__class__ == gto.Mole
mol = mol_or_geom
else:
mol = self.mol.set_geom_(mol_or_geom, inplace=False)
Expand Down
14 changes: 7 additions & 7 deletions pyscf/gto/basis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,27 +608,27 @@ def load(filename_or_basisname, symb, optimize=OPTIMIZE_CONTRACTION):
try:
return parse_nwchem.parse(filename_or_basisname, symb,
optimize=optimize)
except IndexError:
raise BasisNotFoundError(filename_or_basisname)
except BasisNotFoundError as basis_err:
pass
except Exception:
raise BasisNotFoundError(filename_or_basisname)

try:
return parse_nwchem.parse(filename_or_basisname, optimize=optimize)
except IndexError:
raise BasisNotFoundError(f'Invalid basis {filename_or_basisname}')
except BasisNotFoundError:
pass
except Exception:
raise BasisNotFoundError(f'Invalid basis {filename_or_basisname}')

try:
return parse_cp2k.parse(filename_or_basisname, optimize=optimize)
except IndexError:
raise BasisNotFoundError(f'Invalid basis {filename_or_basisname}')
except BasisNotFoundError:
pass
except Exception:
raise BasisNotFoundError(f'Invalid basis {filename_or_basisname}')

# Last, a trial to access Basis Set Exchange database
from pyscf.basis import bse
from pyscf.gto.basis import bse
if bse.basis_set_exchange is not None:
try:
bse_obj = bse.basis_set_exchange.api.get_basis(
Expand Down
1 change: 1 addition & 0 deletions pyscf/gto/basis/bse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'''

try:
import basis_set_exchange
from basis_set_exchange import lut, manip, sort
except ImportError:
basis_set_exchange = None
Expand Down
62 changes: 39 additions & 23 deletions pyscf/gto/basis/parse_cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,49 @@ def load(basisfile, symb, optimize=False):
return _parse(search_seg(basisfile, symb), optimize)

def _parse(blines, optimize=False):
header_ln = blines.pop(0) # noqa: F841
nsets = int(blines.pop(0))
blines_iter = iter(blines)
try:
header_ln = next(blines_iter) # noqa: F841
nsets = int(next(blines_iter))
except Exception:
raise BasisNotFoundError('Not basis data')

basis = []
for n in range(nsets):
comp = [int(p) for p in blines.pop(0).split()]
lmin, lmax, nexps, ncontractions = comp[1], comp[2], comp[3], comp[4:]
basis_n = [[l] for l in range(lmin,lmax+1)]
for nexp in range(nexps):
line = blines.pop(0)
dat = line.split()
try:
bfun = [float(x) for x in dat]
except ValueError:
if DISABLE_EVAL:
raise ValueError('Failed to parse basis %s' % line)
else:
bfun = eval(','.join(dat))
exp = bfun.pop(0)
for i,l in enumerate(range(lmin,lmax+1)):
cl = [exp]
for c in range(ncontractions[i]):
cl.append(bfun.pop(0))
basis_n[i].append(tuple(cl))
basis.extend(basis_n)
try:
for n in range(nsets):
comp = [int(p) for p in next(blines_iter).split()]
lmin, lmax, nexps, ncontractions = comp[1], comp[2], comp[3], comp[4:]
basis_n = [[l] for l in range(lmin,lmax+1)]
for nexp in range(nexps):
line = next(blines_iter)
dat = line.split()
try:
bfun = [float(x) for x in dat]
except ValueError:
if DISABLE_EVAL:
raise ValueError('Failed to parse %s' % line)
else:
bfun = eval(','.join(dat))

if len(bfun) != sum(ncontractions) + 1:
raise ValueError('Basis data incomplete')

bfun_iter = iter(bfun)
exp = next(bfun_iter)
for i,l in enumerate(range(lmin,lmax+1)):
cl = [exp]
for c in range(ncontractions[i]):
cl.append(next(bfun_iter))
basis_n[i].append(cl)
basis.extend(basis_n)
except StopIteration:
raise ValueError('Basis data incomplete')

basis_sorted = []
for l in range(MAXL):
basis_sorted.extend([b for b in basis if b[0] == l])
if not basis_sorted:
raise BasisNotFoundError('Basis data not found')

if optimize:
basis_sorted = parse_nwchem.optimize_contraction(basis_sorted)
Expand Down
1 change: 1 addition & 0 deletions pyscf/gto/basis/parse_molpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import re
import numpy
import numpy as np

try:
from pyscf.gto.basis.parse_nwchem import optimize_contraction
Expand Down
Loading

0 comments on commit ed0611c

Please sign in to comment.