forked from thomaspigeon/LinearResponse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM_matrix.py
More file actions
244 lines (224 loc) · 10.3 KB
/
M_matrix.py
File metadata and controls
244 lines (224 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import numpy as np
from numpy import linalg as la
from K_matrices import K_matrices
import pylibxc
from grid.molgrid import MolGrid
class M_matrix(K_matrices):
"""Class for the M matrices"""
@property
def M_block_size(self):
"""Calculate the size of the M matrix
Return
------
M_block_size: int"""
M_block_size = self.K_shape(shape="square")[0]
return M_block_size
def M_s(self, complex=False):
""""Calculate the non interacting M matrix
Parameters
----------
complex: bool, default is False
Return
------
ndarray
Raises
------
TypeError
If complex is not a boolean"""
if not isinstance(complex, bool):
raise TypeError("""'complex' must be a bool""")
indices = self.K_indices(shape="square")
M_s = np.diag(
np.array(
[
(
self._molecule.mo.energies[indices[1][0]][:, None]
- self._molecule.mo.energies[indices[0][0]]
).T.reshape(len(indices[1][0]) * len(indices[0][0])),
(
self._molecule.mo.energies[indices[1][1]][:, None]
- self._molecule.mo.energies[indices[0][1]]
).T.reshape(len(indices[1][1]) * len(indices[0][1])),
]
).reshape(2 * self.M_block_size)
)
if complex == False:
return M_s
else:
return np.block(
[
[
M_s[: self.M_block_size, : self.M_block_size],
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
],
[
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, : self.M_block_size],
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
],
[
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
M_s[self.M_block_size :, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
],
[
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
M_s[: self.M_block_size, self.M_block_size :],
M_s[self.M_block_size :, self.M_block_size :],
],
]
)
def calculate_M(self, k=None, molgrid=None, complex=False, inverse=False):
"""Calculate the M matrix
Parameters
----------
k: str or None
The coupling matrix K type
If None : No coupling matrix, Independent particle approximation
If 'HF', K is the Hartree-Fock coupling matrix
If 'Functional_code', K is DFT the coupling matrix associated with the XC functional
corresponding to 'Functional_code' which can be found at:
https://tddft.org/programs/libxc/functionals/
All the LDA and GGA in the list are supported
molgrid: MolGrid class object (from grid package) suitable for numerical integration
of any real space function related to the molecule (such as the density)
Necessary for any DFT coupling matrices
complex : bool, default is False
inverse: bool, default is False
If True, return the inverse of the matrix M
Return
------
M: ndarray(M_size, M_size)
The M matrix (or its inverse depending on inverse option)
Raises
------
TypeError
If 'k' is not None or a str
If 'molgrid' is not a 'MolGrid' instance
If 'type' is not a bool
If 'inverse' is not a bool
ValueError
If 'k' is not 'HF' or a supported functional code"""
if k != None and not isinstance(k, str):
raise TypeError("""'k' must be None or a str""")
if molgrid != None and not isinstance(molgrid, MolGrid):
raise TypeError("""'molgrid' must be None or a 'MolGrid' instance""")
if not isinstance(complex, bool):
raise TypeError("""'complex' must be a bool""")
if not isinstance(inverse, bool):
raise TypeError("""'inverse' must be a bool""")
if not k in pylibxc.util.xc_available_functional_names() + ["HF"]:
raise ValueError(
"""'k' must be 'HF' of a supported functional code, fro them, see pylibxc.util.xc_available_functional_names() or https://tddft.org/programs/libxc/functionals/"""
)
M = self.M_s(complex=complex)
b = self.M_block_size
if complex == True:
if k == "HF":
K_1 = self.K_coulomb(Type=1, shape="square") + self.K_fxc_HF(
Type=1, shape="square"
)
K_2 = self.K_coulomb(Type=2, shape="square") + self.K_fxc_HF(
Type=2, shape="square"
)
elif isinstance(k, str):
K_1 = self.K_coulomb(Type=1, shape="square") + self.K_fxc_DFT(
molgrid=molgrid, Type=1, XC_functional=k, shape="square"
)
K_2 = self.K_coulomb(Type=2, shape="square") + self.K_fxc_DFT(
molgrid=molgrid, Type=2, XC_functional=k, shape="square"
)
M[0 * b : 1 * b, 0 * b : 1 * b] = M[0 * b : 1 * b, 0 * b : 1 * b] + K_1[0]
M[0 * b : 1 * b, 1 * b : 2 * b] = M[0 * b : 1 * b, 1 * b : 2 * b] + K_2[0]
M[0 * b : 1 * b, 2 * b : 3 * b] = M[0 * b : 1 * b, 2 * b : 3 * b] + K_1[1]
M[0 * b : 1 * b, 3 * b : 4 * b] = M[0 * b : 1 * b, 3 * b : 4 * b] + K_2[1]
M[1 * b : 2 * b, 0 * b : 1 * b] = (
M[1 * b : 2 * b, 0 * b : 1 * b] + K_2[0].conj()
)
M[1 * b : 2 * b, 1 * b : 2 * b] = (
M[1 * b : 2 * b, 1 * b : 2 * b] + K_1[0].conj()
)
M[1 * b : 2 * b, 2 * b : 3 * b] = (
M[1 * b : 2 * b, 2 * b : 3 * b] + K_2[1].conj()
)
M[1 * b : 2 * b, 3 * b : 4 * b] = (
M[1 * b : 2 * b, 3 * b : 4 * b] + K_1[1].conj()
)
M[2 * b : 3 * b, 0 * b : 1 * b] = M[2 * b : 3 * b, 0 * b : 1 * b] + K_1[2]
M[2 * b : 3 * b, 1 * b : 2 * b] = M[2 * b : 3 * b, 1 * b : 2 * b] + K_2[2]
M[2 * b : 3 * b, 2 * b : 3 * b] = M[2 * b : 3 * b, 2 * b : 3 * b] + K_1[3]
M[2 * b : 3 * b, 3 * b : 4 * b] = M[2 * b : 3 * b, 3 * b : 4 * b] + K_2[3]
M[3 * b : 4 * b, 0 * b : 1 * b] = (
M[3 * b : 4 * b, 0 * b : 1 * b] + K_2[2].conj()
)
M[3 * b : 4 * b, 1 * b : 2 * b] = (
M[3 * b : 4 * b, 1 * b : 2 * b] + K_1[2].conj()
)
M[3 * b : 4 * b, 2 * b : 3 * b] = (
M[3 * b : 4 * b, 2 * b : 3 * b] + K_2[3].conj()
)
M[3 * b : 4 * b, 3 * b : 4 * b] = (
M[3 * b : 4 * b, 3 * b : 4 * b] + K_1[3].conj()
)
else:
if k == "HF":
K = self.K_coulomb(Type=1, shape="square") + self.K_fxc_HF(
Type=1, shape="square"
)
elif isinstance(k, str):
K = self.K_coulomb(Type=1, shape="square") + self.K_fxc_DFT(
molgrid=molgrid, Type=1, XC_functional=k, shape="square"
)
M[0 * b : 1 * b, 0 * b : 1 * b] = M[0 * b : 1 * b, 0 * b : 1 * b] + 2 * K[0]
M[0 * b : 1 * b, 1 * b : 2 * b] = M[0 * b : 1 * b, 1 * b : 2 * b] + 2 * K[1]
M[1 * b : 2 * b, 0 * b : 1 * b] = M[1 * b : 2 * b, 0 * b : 1 * b] + 2 * K[2]
M[1 * b : 2 * b, 1 * b : 2 * b] = M[1 * b : 2 * b, 1 * b : 2 * b] + 2 * K[3]
if inverse == True:
M = la.inv(M)
return M
def Excitations_energies_real_MO(self, k=None, molgrid=None):
"""Calculate the excitation energies
Parameters
----------
k: str or None
The coupling matrix K type
If None : No coupling matrix, Independent particle approximation
If 'HF', K is the Hartree-Fock coupling matrix
If 'Functional_code', K is DFT the coupling matrix associated with the XC functional
corresponding to 'Functional_code' which can be found at:
https://tddft.org/programs/libxc/functionals/
All the LDA and GGA in the list are supported
molgrid: MolGrid class object (from grid package) suitable for numerical integration
of any real space function related to the molecule (such as the density)
Necessary for any DFT coupling matrices
Return
------
Exci: dict
'Excitations': ndarray shape = M_size
'Transition densities: ndarray shape = [M_size, M_size]
Raises
------
TypeError
If 'k' is not None or a str
If 'molgrid' is not a 'MolGrid' instance
ValueError
If 'k' is not 'HF' or a supported functional code"""
if k != None and not isinstance(k, str):
raise TypeError("""'k' must be None or a str""")
if molgrid != None and not isinstance(molgrid, MolGrid):
raise TypeError("""'molgrid' must be None or a 'MolGrid' instance""")
if not k in pylibxc.util.xc_available_functional_names() + ["HF"]:
raise ValueError(
"""'k' must be 'HF' of a supported functional code, fro them, see pylibxc.util.xc_available_functional_names() or https://tddft.org/programs/libxc/functionals/"""
)
M = self.calculate_M(k=k, molgrid=molgrid)
Omega = np.dot(np.sqrt(self.M_s(complex=False)), M).dot(
np.sqrt(self.M_s(complex=False))
)
Excitations = la.eigvalsh(Omega)
return np.sqrt(Excitations)