|
| 1 | +import numpy as np |
1 | 2 | from pyscf import scf, grad
|
2 | 3 | from qstack.spahm.guesses import solveF, get_guess, get_occ, get_dm, eigenvalue_grad, get_guess_g
|
3 | 4 |
|
4 |
| -def get_guess_orbitals(mol, guess, xc="pbe"): |
5 |
| - """ Compute the guess Hamiltonian. |
| 5 | + |
| 6 | +def get_guess_orbitals(mol, guess, xc="pbe", field=None): |
| 7 | + """ Compute the guess Hamiltonian orbitals |
6 | 8 |
|
7 | 9 | Args:
|
8 | 10 | mol (pyscf Mole): pyscf Mole object.
|
9 |
| - guess (funct): Method used to compute the guess Hamiltonian. Output of get_guess. |
| 11 | + guess (func): Method used to compute the guess Hamiltonian. Output of get_guess. |
10 | 12 | xc (str): Exchange-correlation functional. Defaults to pbe.
|
| 13 | + field (numpy.array(3)): applied uniform electric field i.e. $\\vec \\nabla \\phi$ in a.u. |
11 | 14 |
|
12 | 15 | Returns:
|
13 | 16 | A 1D numpy array containing the eigenvalues and a 2D numpy array containing the eigenvectors of the guess Hamiltonian.
|
14 | 17 | """
|
15 | 18 | if guess == 'huckel':
|
16 |
| - e,v = scf.hf._init_guess_huckel_orbitals(mol) |
| 19 | + if field is not None: |
| 20 | + raise NotImplementedError |
| 21 | + e, v = scf.hf._init_guess_huckel_orbitals(mol) |
17 | 22 | else:
|
18 | 23 | fock = guess(mol, xc)
|
19 |
| - e,v = solveF(mol, fock) |
20 |
| - return e,v |
| 24 | + if field is not None: |
| 25 | + with mol.with_common_orig((0,0,0)): |
| 26 | + ao_dip = mol.intor_symmetric('int1e_r', comp=3) |
| 27 | + fock += np.einsum('xij,x->ij', ao_dip, field) |
| 28 | + e, v = solveF(mol, fock) |
| 29 | + return e, v |
| 30 | + |
| 31 | + |
| 32 | +def ext_field_generator(mol, field): |
| 33 | + """ Generator for Hext (i.e. applied uniform electiric field interaction) gradient |
| 34 | +
|
| 35 | + Args: |
| 36 | + mol (pyscf Mole): pyscf Mole object. |
| 37 | + field (numpy.array(3)): applied uniform electric field i.e. $\\vec \\nabla \\phi$ in a.u. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + func(int: iat): returns the derivative of Hext wrt the coordinates of atom iat, i.e. dHext/dr[iat] |
| 41 | + """ |
| 42 | + |
| 43 | + shls_slice = (0, mol.nbas, 0, mol.nbas) |
| 44 | + with mol.with_common_orig((0,0,0)): |
| 45 | + int1e_irp = mol.intor('int1e_irp', shls_slice=shls_slice).reshape(3, 3, mol.nao, mol.nao) # ( | rc nabla | ) |
| 46 | + aoslices = mol.aoslice_by_atom()[:,2:] |
| 47 | + if field is None: |
| 48 | + field = (0,0,0) |
| 49 | + def field_deriv(iat): |
| 50 | + p0, p1 = aoslices[iat] |
| 51 | + dmu_dr = np.zeros_like(int1e_irp) # dim(mu)×dim(r)×nao×nao |
| 52 | + dmu_dr[:,:,p0:p1,:] -= int1e_irp[:,:,:,p0:p1].transpose((0,1,3,2)) # TODO not sure why minus |
| 53 | + dmu_dr[:,:,:,p0:p1] -= int1e_irp[:,:,:,p0:p1] # TODO check/fix E definition |
| 54 | + dhext_dr = np.einsum('x,xypq->ypq', field, dmu_dr) |
| 55 | + return dhext_dr |
| 56 | + return field_deriv |
| 57 | + |
| 58 | + |
| 59 | +def get_guess_orbitals_grad(mol, guess, field=None): |
| 60 | + """ Compute the guess Hamiltonian eigenvalues and their derivatives |
| 61 | +
|
| 62 | + Args: |
| 63 | + mol (pyscf Mole): pyscf Mole object. |
| 64 | + guess (func): Tuple of methods used to compute the guess Hamiltonian and its eigenvalue derivatives. Output of get_guess_g |
| 65 | + field (numpy.array(3)): applied uniform electric field i.e. $\\vec \\nabla \\phi$ in a.u. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + numpy 1d array (mol.nao,): eigenvalues |
| 69 | + numpy 3d ndarray (mol.nao,mol.natm,3): gradient of the eigenvalues in Eh/bohr |
| 70 | + """ |
21 | 71 |
|
22 |
| -def get_guess_orbitals_grad(mol, guess): |
23 |
| - e, c = get_guess_orbitals(mol, guess[0]) |
| 72 | + e, c = get_guess_orbitals(mol, guess[0], field=field) |
24 | 73 | mf = grad.rhf.Gradients(scf.RHF(mol))
|
25 | 74 | s1 = mf.get_ovlp(mol)
|
26 |
| - h1 = guess[1](mf) |
27 |
| - return eigenvalue_grad(mol, e, c, s1, h1) |
| 75 | + h0 = guess[1](mf) |
| 76 | + |
| 77 | + if field is None: |
| 78 | + h1 = h0 |
| 79 | + else: |
| 80 | + hext = ext_field_generator(mf.mol, field) |
| 81 | + h1 = lambda iat: h0(iat) + hext(iat) |
28 | 82 |
|
29 |
| -def get_guess_dm(mol, guess, xc="pbe", openshell=None): |
| 83 | + return e, eigenvalue_grad(mol, e, c, s1, h1) |
| 84 | + |
| 85 | + |
| 86 | +def get_guess_dm(mol, guess, xc="pbe", openshell=None, field=None): |
30 | 87 | """ Compute the density matrix with the guess Hamiltonian.
|
31 | 88 |
|
32 | 89 | Args:
|
33 | 90 | mol (pyscf Mole): pyscf Mole object.
|
34 |
| - guess (funct): Method used to compute the guess Hamiltonian. Output of get_guess. |
| 91 | + guess (func): Method used to compute the guess Hamiltonian. Output of get_guess. |
35 | 92 | xc (str): Exchange-correlation functional. Defaults to pbe
|
36 | 93 | openshell (bool): . Defaults to None.
|
37 | 94 |
|
38 | 95 | Returns:
|
39 | 96 | A numpy ndarray containing the density matrix computed using the guess Hamiltonian.
|
40 | 97 | """
|
41 |
| - e,v = get_guess_orbitals(mol, guess, xc) |
| 98 | + e,v = get_guess_orbitals(mol, guess, xc, field=field) |
42 | 99 | return get_dm(v, mol.nelec, mol.spin if mol.spin>0 or not openshell is None else None)
|
43 | 100 |
|
44 |
| -def get_spahm_representation(mol, guess_in, xc="pbe"): |
| 101 | + |
| 102 | +def get_spahm_representation(mol, guess_in, xc="pbe", field=None): |
45 | 103 | """ Compute the SPAHM representation.
|
46 | 104 |
|
47 | 105 | Args:
|
48 | 106 | mol (pyscf Mole): pyscf Mole object.
|
49 | 107 | guess_in (str): Method used to obtain the guess Hamiltoninan.
|
50 | 108 | xc (str): Exchange-correlation functional. Defaults to pbe.
|
| 109 | + field (numpy.array(3)): applied uniform electric field i.e. $\\vec \\nabla \\phi$ in a.u. |
51 | 110 |
|
52 | 111 | Returns:
|
53 | 112 | A numpy ndarray containing the SPAHM representation.
|
54 | 113 | """
|
55 | 114 | guess = get_guess(guess_in)
|
56 |
| - e, v = get_guess_orbitals(mol, guess, xc) |
| 115 | + e, v = get_guess_orbitals(mol, guess, xc, field=field) |
57 | 116 | e1 = get_occ(e, mol.nelec, mol.spin)
|
58 | 117 | return e1
|
59 | 118 |
|
60 |
| -def get_spahm_representation_grad(mol, guess_in): |
| 119 | + |
| 120 | +def get_spahm_representation_grad(mol, guess_in, field=None): |
| 121 | + """ Compute the SPAHM representation and its gradient |
| 122 | +
|
| 123 | + Args: |
| 124 | + mol (pyscf Mole): pyscf Mole object. |
| 125 | + guess_in (str): Method used to obtain the guess Hamiltoninan. |
| 126 | + xc (str): Exchange-correlation functional. Defaults to pbe. |
| 127 | + field (numpy.array(3)): applied uniform electric field i.e. $\\vec \\nabla \\phi$ in a.u. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + numpy 1d array (occ,): the SPAHM representation (Eh). |
| 131 | + numpy 3d array (occ,mol.natm,3): gradient of the representation (Eh/bohr) |
| 132 | + """ |
61 | 133 | guess = get_guess_g(guess_in)
|
62 |
| - agrad = get_guess_orbitals_grad(mol, guess) |
63 |
| - return get_occ(agrad, mol.nelec, mol.spin) |
| 134 | + e, agrad = get_guess_orbitals_grad(mol, guess, field=field) |
| 135 | + return get_occ(e, mol.nelec, mol.spin), get_occ(agrad, mol.nelec, mol.spin) |
0 commit comments