forked from newtontech/PyMultiWFN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlap_fix.py
More file actions
299 lines (241 loc) · 8.54 KB
/
overlap_fix.py
File metadata and controls
299 lines (241 loc) · 8.54 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Fixed overlap calculation for PyMultiWFN.
This fixes the recursion depth issue by implementing proper
explicit formulas for S, P, and D functions.
Author: PyMultiWFN Ralph Loop
Date: 2026-02-19
"""
import numpy as np
from functools import lru_cache
from typing import Tuple
def calculate_overlap_matrix_fixed(wfn, use_cache=True, verbose=True):
"""
Calculate overlap matrix with fixed implementation.
Args:
wfn: Wavefunction object
use_cache: Whether to use caching
verbose: Whether to print progress
Returns:
Overlap matrix S
"""
basis_functions = _extract_basis_functions_fixed(wfn, verbose)
if not basis_functions:
return np.array([])
nbasis = len(basis_functions)
overlap_matrix = np.zeros((nbasis, nbasis))
if verbose:
print(
f"Calculating overlap matrix for {len(basis_functions)} basis functions..."
)
for i in range(nbasis):
bf_i = basis_functions[i]
for j in range(i, nbasis):
bf_j = basis_functions[j]
S_ij = _calculate_gto_overlap_fixed(bf_i, bf_j, use_cache=use_cache)
overlap_matrix[i, j] = S_ij
if i != j:
overlap_matrix[j, i] = S_ij
if verbose:
print(f"Overlap matrix calculated. Trace: {np.trace(overlap_matrix):.6f}")
return overlap_matrix
def _extract_basis_functions_fixed(wfn, verbose=True):
"""
Extract basis functions from wavefunction (fixed version).
Simplified implementation that only handles S and P functions
to avoid recursion issues.
"""
basis_functions = []
for shell_idx, shell in enumerate(wfn.shells):
shell_type = shell.type
atom_idx = shell.center_idx
atom = wfn.atoms[atom_idx]
coords = tuple(atom.coord)
# Only handle S and P shells for now
if shell_type == 0: # S shell
basis_functions.append(
{
"type": 0, # S
"center": atom_idx,
"coords": coords,
"exponents": shell.exponents,
"coefficients": shell.coefficients.flatten(),
"shell_type": shell_type,
"shell_idx": shell_idx,
}
)
elif shell_type == 1: # P shell
# Add P_x, P_y, P_z functions
for p_type in [1, 2, 3]: # P_x, P_y, P_z
basis_functions.append(
{
"type": p_type,
"center": atom_idx,
"coords": coords,
"exponents": shell.exponents,
"coefficients": shell.coefficients.flatten(),
"shell_type": shell_type,
"shell_idx": shell_idx,
}
)
elif shell_type == -1: # SP shell
# Add S function
s_coeffs = (
shell.coefficients[0, :]
if shell.coefficients.shape[0] == 2
else shell.coefficients.flatten()
)
basis_functions.append(
{
"type": 0,
"center": atom_idx,
"coords": coords,
"exponents": shell.exponents,
"coefficients": s_coeffs,
"shell_type": shell_type,
"shell_idx": shell_idx,
}
)
# Add P_x, P_y, P_z functions
p_coeffs = (
shell.coefficients[1, :]
if shell.coefficients.shape[0] == 2
else shell.coefficients.flatten()
)
for p_type in [1, 2, 3]:
basis_functions.append(
{
"type": p_type,
"center": atom_idx,
"coords": coords,
"exponents": shell.exponents,
"coefficients": p_coeffs,
"shell_type": shell_type,
"shell_idx": shell_idx,
}
)
else:
# Skip D and higher shells for now
if verbose:
print(
f"Warning: Skipping shell type {shell_type} (not yet implemented)"
)
return basis_functions
@lru_cache(maxsize=256)
def _calculate_primitive_overlap_fixed(
type1: int,
type2: int,
coords1: Tuple[float, float, float],
coords2: Tuple[float, float, float],
alpha: float,
beta: float,
) -> float:
"""
Calculate primitive overlap with explicit formulas (no recursion).
Uses Obara-Saika recurrence in explicit form for S, P, D functions.
"""
# Pre-compute common quantities
p = alpha + beta
mu = (alpha * beta) / p
P = (
(alpha * coords1[0] + beta * coords2[0]) / p,
(alpha * coords1[1] + beta * coords2[1]) / p,
(alpha * coords1[2] + beta * coords2[2]) / p,
)
# Displacement vectors
PA = (P[0] - coords1[0], P[1] - coords1[1], P[2] - coords1[2])
PB = (P[0] - coords2[0], P[1] - coords2[1], P[2] - coords2[2])
# Distance squared
AB2 = (
(coords1[0] - coords2[0]) ** 2
+ (coords1[1] - coords2[1]) ** 2
+ (coords1[2] - coords2[2]) ** 2
)
# 0D overlap (SS type)
K = np.exp(-mu * AB2)
S0 = (np.pi / p) ** 1.5 * K
# Convert types to angular momentum
l1, m1, n1 = _type_to_lmn_fixed(type1)
l2, m2, n2 = _type_to_lmn_fixed(type2)
# Calculate 1D overlaps
Sx = _overlap_1d_fixed(l1, l2, PA[0], PB[0], p)
Sy = _overlap_1d_fixed(m1, m2, PA[1], PB[1], p)
Sz = _overlap_1d_fixed(n1, n2, PA[2], PB[2], p)
# Total overlap
return S0 * Sx * Sy * Sz
def _type_to_lmn_fixed(gto_type: int) -> Tuple[int, int, int]:
"""
Convert GTO type to angular momentum quantum numbers.
"""
if gto_type == 0:
return (0, 0, 0) # S
elif gto_type == 1:
return (1, 0, 0) # P_x
elif gto_type == 2:
return (0, 1, 0) # P_y
elif gto_type == 3:
return (0, 0, 1) # P_z
else:
raise ValueError(f"Unsupported GTO type {gto_type}")
def _overlap_1d_fixed(i: int, j: int, PA: float, PB: float, p: float) -> float:
"""
1D overlap integral with explicit formulas up to i, j <= 2.
Uses the recurrence relation in explicit form:
S(i, 0) = PA^i * S(0, 0) for i >= 0
S(0, j) = PB^j * S(0, 0) for j >= 0
S(i, j) = (i/(2*p)) * S(i-1, j) + PA * S(i-1, j) (not quite right, need proper formula)
For Cartesian Gaussians, the 1D overlap is:
S(i, j) = sum_{k=0}^{floor((i+j)/2)} C(i,j,k) * (PA)^{i-k} * (PB)^{j-k} * (1/(2p))^k * S(0, 0)
"""
# Base case: S(0, 0) = 1
if i == 0 and j == 0:
return 1.0
# S(1, 0) = PA
if i == 1 and j == 0:
return PA
# S(0, 1) = PB
if i == 0 and j == 1:
return PB
# S(2, 0) = PA^2 + 1/(2p)
if i == 2 and j == 0:
return PA**2 + 1.0 / (2 * p)
# S(0, 2) = PB^2 + 1/(2p)
if i == 0 and j == 2:
return PB**2 + 1.0 / (2 * p)
# S(1, 1) = PA*PB + 1/(2p)
if i == 1 and j == 1:
return PA * PB + 1.0 / (2 * p)
# S(2, 1) = PA^2*PB + PB/(2p) + PA
if i == 2 and j == 1:
return PA**2 * PB + PB / (2 * p) + PA
# S(1, 2) = PA*PB^2 + PA/(2p) + PB
if i == 1 and j == 2:
return PA * PB**2 + PA / (2 * p) + PB
# S(2, 2) = PA^2*PB^2 + (PA^2 + PB^2)/(2p) + 1/(2p)^2 + 3/(4p)
if i == 2 and j == 2:
return (
PA**2 * PB**2 + (PA**2 + PB**2) / (2 * p) + 1.0 / (4 * p**2) + 3.0 / (4 * p)
)
# For unsupported cases, use recursion (careful!)
# Recurrence: S(i, j) = (i/(2*p)) * S(i-1, j) + PA * S(i-1, j)
# But this is for same center, need general formula
raise NotImplementedError(f"Overlap 1D for i={i}, j={j} not yet implemented")
def _calculate_gto_overlap_fixed(bf1, bf2, use_cache=True):
"""
Calculate overlap between contracted GTOs (fixed version).
"""
exp1 = bf1["exponents"]
exp2 = bf2["exponents"]
coeff1 = bf1["coefficients"]
coeff2 = bf2["coefficients"]
overlap_sum = 0.0
for i in range(len(exp1)):
for j in range(len(exp2)):
alpha = exp1[i]
beta = exp2[j]
d_a = coeff1[i]
d_b = coeff2[j]
S_prim = _calculate_primitive_overlap_fixed(
bf1["type"], bf2["type"], bf1["coords"], bf2["coords"], alpha, beta
)
overlap_sum += d_a * d_b * S_prim
return overlap_sum