Skip to content

Commit 4e9039b

Browse files
committed
Check if there is enough AOs to fit the electrons
1 parent 44da03f commit 4e9039b

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

qstack/spahm/guesses.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import warnings
23
import numpy
34
import scipy
45
import pyscf
@@ -7,10 +8,10 @@
78

89
def hcore(mol, *_):
910
"""Uses the diagonalization of the core to compute the guess Hamiltonian.
10-
11+
1112
Args:
1213
mol (pyscf Mole): pyscf Mole object.
13-
14+
1415
Returns:
1516
A numpy ndarray containing the computed approximate Hamiltonian.
1617
"""
@@ -19,14 +20,14 @@ def hcore(mol, *_):
1920
return h
2021

2122
def GWH(mol, *_):
22-
"""Uses the generalized Wolfsberg-Helmholtz to compute the guess Hamiltonian.
23-
24-
Args:
25-
mol (pyscf Mole): pyscf Mole object.
26-
27-
Returns:
28-
A numpy ndarray containing the computed approximate Hamiltonian.
29-
"""
23+
"""Uses the generalized Wolfsberg-Helmholtz to compute the guess Hamiltonian.
24+
25+
Args:
26+
mol (pyscf Mole): pyscf Mole object.
27+
28+
Returns:
29+
A numpy ndarray containing the computed approximate Hamiltonian.
30+
"""
3031
h = hcore(mol)
3132
S = mol.intor_symmetric('int1e_ovlp')
3233
K = 1.75 # See J. Chem. Phys. 1952, 20, 837
@@ -41,11 +42,11 @@ def GWH(mol, *_):
4142

4243
def SAD(mol, func):
4344
"""Uses the superposition of atomic densities to compute the guess Hamiltonian.
44-
45+
4546
Args:
4647
mol (pyscf Mole): pyscf Mole object.
4748
func (str): Exchange-correlation functional.
48-
49+
4950
Returns:
5051
A numpy ndarray containing the computed approximate Hamiltonian.
5152
"""
@@ -59,10 +60,10 @@ def SAD(mol, func):
5960

6061
def SAP(mol, *_):
6162
"""Uses the superposition of atomic potentials to compute the guess Hamiltonian.
62-
63+
6364
Args:
6465
mol (pyscf Mole): pyscf Mole object.
65-
66+
6667
Returns:
6768
A numpy ndarray containing the computed approximate Hamiltonian.
6869
"""
@@ -74,29 +75,29 @@ def SAP(mol, *_):
7475

7576
def LB(mol, *_):
7677
"""Uses the Laikov-Briling model with HF-based parameters to compute the guess Hamiltonian.
77-
78+
7879
Args:
7980
mol (pyscf Mole): pyscf Mole object.
80-
81+
8182
Returns:
8283
A numpy ndarray containing the computed approximate Hamiltonian.
8384
"""
8485
return LB20(parameters='HF').Heff(mol)
8586

8687
def LB_HFS(mol, *_):
8788
""" Laikov-Briling using HFS-based parameters
88-
89+
8990
Args:
9091
mol (pyscf Mole): pyscf Mole object.
91-
92+
9293
Returns:
9394
A numpy ndarray containing the computed approximate Hamiltonian.
9495
"""
9596
return LB20(parameters='HFS').Heff(mol)
9697

9798
def solveF(mol, fock):
9899
"""Computes the eigenvalues and eigenvectors corresponding to the given Hamiltonian.
99-
100+
100101
Args:
101102
mol (pyscf Mole): pyscf Mole object.
102103
fock (numpy ndarray): Approximate Hamiltonian.
@@ -106,10 +107,10 @@ def solveF(mol, fock):
106107

107108
def get_guess(arg):
108109
"""Returns the function of the method selected to compute the approximate hamiltoninan
109-
110+
110111
Args:
111112
arg (str): Approximate Hamiltonian
112-
113+
113114
Returns:
114115
The function of the selected method.
115116
"""
@@ -120,17 +121,29 @@ def get_guess(arg):
120121
exit(1)
121122
return guesses[arg]
122123

124+
125+
def check_nelec(nelec, nao):
126+
""" Checks if there is enough orbitals
127+
for the electrons"""
128+
if numpy.any(numpy.array(nelec) > nao):
129+
raise RuntimeError(f'Too many electrons ({nelec}) for {nao} orbitals')
130+
elif numpy.any(numpy.array(nelec) == nao):
131+
msg = f'{nelec} electrons for {nao} orbitals. Is the input intended to have a complete shell?'
132+
warnings.warn(msg)
133+
134+
123135
def get_occ(e, nelec, spin):
124-
"""Computes the occupancy.
125-
136+
"""Returns the occupied subset of e
137+
126138
Args:
127139
e (numpy ndarray): Energy eigenvalues.
128140
nelec(tuple): Number of alpha and beta electrons.
129141
spin(int): Spin.
130-
142+
131143
Returns:
132-
A numpy ndarray containing the occupancy.
144+
A numpy ndarray containing the occupied eigenvalues.
133145
"""
146+
check_nelec(nelec, e.shape[0])
134147
if spin==None:
135148
nocc = nelec[0]
136149
return e[:nocc,...]
@@ -141,17 +154,20 @@ def get_occ(e, nelec, spin):
141154
e1[1,:nocc[1],...] = e[:nocc[1],...]
142155
return e1
143156

157+
144158
def get_dm(v, nelec, spin):
145159
"""Computes the density matrix.
146-
160+
147161
Args:
148162
v (numpy ndarray): Eigenvectors of a previously solve Hamiltoinan.
149163
nelec(tuple): Number of alpha and beta electrons.
150164
spin(int): Spin.
151-
165+
152166
Return:
153167
A numpy ndarray containing the density matrix computed using the guess Hamiltonian.
154168
"""
169+
170+
check_nelec(nelec, len(v))
155171
if spin==None:
156172
nocc = nelec[0]
157173
dm = v[:,:nocc] @ v[:,:nocc].T

0 commit comments

Comments
 (0)