-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_performance.py
More file actions
400 lines (330 loc) · 12.3 KB
/
test_performance.py
File metadata and controls
400 lines (330 loc) · 12.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
Performance tests for PyMultiWFN.
This module tests performance-critical functions and validates
that optimizations don't break correctness.
"""
import pytest
import numpy as np
import time
from pymultiwfn.core.data import Atom, Shell, Wavefunction
from pymultiwfn.math.density import (
calc_density,
clear_density_cache,
get_cache_stats,
)
from pymultiwfn.analysis.bonding.bondorder import calculate_mayer_bond_order
class TestDensityPerformance:
"""Test performance optimizations in density calculations."""
@pytest.fixture
def medium_molecule(self):
"""Create a medium-sized molecule for performance testing."""
# Benzene-like molecule (12 atoms)
atoms = []
# Ring of 6 carbons
for i in range(6):
angle = i * np.pi / 3
atoms.append(
Atom(
element="C",
index=6,
x=1.4 * np.cos(angle),
y=1.4 * np.sin(angle),
z=0.0,
charge=6.0,
)
)
# 6 hydrogens
for i in range(6):
angle = i * np.pi / 3
atoms.append(
Atom(
element="H",
index=1,
x=2.5 * np.cos(angle),
y=2.5 * np.sin(angle),
z=0.0,
charge=1.0,
)
)
# Minimal basis (1 s-type per atom for testing)
shells = []
for i in range(12):
shells.append(
Shell(
type=0, center_idx=i, exponents=np.array([3.0]), coefficients=np.array([1.0])
)
)
# Create wavefunction
num_basis = 12
# Simple coefficients (not physically accurate, but useful for perf testing)
coeffs = np.random.randn(num_basis, num_basis) * 0.1
occupations = np.ones(num_basis)
wfn = Wavefunction(
atoms=atoms,
num_electrons=30.0,
charge=0,
multiplicity=1,
num_basis=num_basis,
num_atomic_orbitals=num_basis,
num_primitives=num_basis,
num_shells=len(shells),
shells=shells,
occupations=occupations,
coefficients=coeffs,
)
return wfn
def test_density_caching_works(self, medium_molecule):
"""Test that density matrix caching reduces computation time."""
wfn = medium_molecule
# Clear cache
clear_density_cache()
# First call (cache miss)
coords = np.random.randn(100, 3)
start = time.time()
rho1 = calc_density(wfn, coords, use_cache=True)
time_first = time.time() - start
# Second call with same wavefunction (cache hit)
start = time.time()
rho2 = calc_density(wfn, coords, use_cache=True)
time_second = time.time() - start
# Results should be identical
assert np.allclose(rho1, rho2), "Cached result differs from original"
# Cache should have entries
stats = get_cache_stats()
assert stats["cache_size"] > 0, "Cache should not be empty"
# Second call should be faster (allow some variance)
# Note: may not always be faster for small systems, so just check correctness
print(f"\nPerformance: First={time_first:.4f}s, Second={time_second:.4f}s")
def test_density_no_cache(self, medium_molecule):
"""Test density calculation without caching."""
wfn = medium_molecule
coords = np.random.randn(100, 3)
# Clear cache
clear_density_cache()
# Calculate without cache
rho = calc_density(wfn, coords, use_cache=False)
# Should return valid densities
assert len(rho) == 100
assert np.all(np.isfinite(rho))
# Cache should still be empty
stats = get_cache_stats()
assert stats["cache_size"] == 0, "Cache should be empty when disabled"
def test_density_batch_processing(self, medium_molecule):
"""Test processing multiple coordinate batches efficiently."""
wfn = medium_molecule
# Large number of points
n_points = 10000
coords = np.random.randn(n_points, 3) * 3.0
# Clear cache
clear_density_cache()
# Process all points
start = time.time()
rho = calc_density(wfn, coords, use_cache=True)
elapsed = time.time() - start
# Should process efficiently
assert len(rho) == n_points
assert np.all(np.isfinite(rho))
# Performance benchmark (should be < 5 seconds for 10k points)
assert elapsed < 5.0, f"Processing took {elapsed:.2f}s (> 5s threshold)"
print(f"\nBatch processing: {n_points} points in {elapsed:.3f}s ({n_points/elapsed:.0f} points/s)")
def test_cache_memory_management(self):
"""Test that cache doesn't grow unboundedly."""
# Clear cache
clear_density_cache()
# Create many different wavefunctions
for i in range(150): # More than cache max size
atoms = [Atom(element="H", index=1, x=0.0, y=0.0, z=i * 0.1, charge=1.0)]
shells = [
Shell(type=0, center_idx=0, exponents=np.array([1.0]), coefficients=np.array([1.0]))
]
wfn = Wavefunction(
atoms=atoms,
num_electrons=1.0,
charge=0,
multiplicity=2,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=shells,
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
coords = np.array([[0.0, 0.0, 0.0]])
calc_density(wfn, coords, use_cache=True)
# Cache should not exceed max size
stats = get_cache_stats()
assert stats["cache_size"] <= stats["max_size"], \
f"Cache size {stats['cache_size']} exceeds max {stats['max_size']}"
class TestBondOrderPerformance:
"""Test performance of bond order calculations."""
def test_mayer_bond_order_large_molecule(self):
"""Test Mayer bond order calculation on larger molecule."""
# Create a larger molecule (20 atoms)
atoms = []
for i in range(20):
atoms.append(
Atom(
element="C",
index=6,
x=i * 1.0,
y=0.0,
z=0.0,
charge=6.0,
)
)
# Minimal basis
shells = []
for i in range(20):
shells.append(
Shell(type=0, center_idx=i, exponents=np.array([2.0]), coefficients=np.array([1.0]))
)
# Create wavefunction
num_basis = 20
coeffs = np.random.randn(num_basis, num_basis) * 0.1
# Ensure orthonormal-ish coefficients
coeffs, _ = np.linalg.qr(coeffs)
# Create density matrix
P = coeffs @ np.diag(np.ones(num_basis)) @ coeffs.T
# Overlap matrix (nearly identity for distant atoms)
S = np.eye(num_basis)
for i in range(num_basis - 1):
S[i, i + 1] = 0.1
S[i + 1, i] = 0.1
wfn = Wavefunction(
atoms=atoms,
num_electrons=40.0,
charge=0,
multiplicity=1,
num_basis=num_basis,
num_atomic_orbitals=num_basis,
num_primitives=num_basis,
num_shells=len(shells),
shells=shells,
occupations=np.ones(num_basis),
coefficients=coeffs,
overlap_matrix=S,
Ptot=P,
)
# Calculate bond orders
start = time.time()
bond_order_dict = calculate_mayer_bond_order(wfn)
elapsed = time.time() - start
bond_order = bond_order_dict["total"]
# Should be symmetric
assert np.allclose(bond_order, bond_order.T)
# Should have correct shape
assert bond_order.shape == (20, 20)
# Should complete in reasonable time (< 2s)
assert elapsed < 2.0, f"Bond order calculation took {elapsed:.2f}s (> 2s threshold)"
print(f"\nBond order (20 atoms): {elapsed:.3f}s")
class TestMemoryEfficiency:
"""Test memory efficiency of calculations."""
def test_density_matrix_memory(self):
"""Test that density matrix construction doesn't use excessive memory."""
# Large basis set (100 basis functions)
num_basis = 100
atoms = [Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)]
shells = [
Shell(type=0, center_idx=0, exponents=np.array([1.0]), coefficients=np.array([1.0]))
]
# Random coefficients
coeffs = np.random.randn(num_basis, num_basis) * 0.1
occupations = np.ones(num_basis)
# This should not cause memory issues
wfn = Wavefunction(
atoms=atoms,
num_electrons=100.0,
charge=0,
multiplicity=1,
num_basis=num_basis,
num_atomic_orbitals=num_basis,
num_primitives=num_basis,
num_shells=len(shells),
shells=shells,
occupations=occupations,
coefficients=coeffs,
)
coords = np.array([[0.0, 0.0, 0.0]])
# Should handle large basis without issues
rho = calc_density(wfn, coords)
assert np.isfinite(rho[0])
def test_large_coordinate_array(self):
"""Test handling of large coordinate arrays."""
# Small molecule
atoms = [Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)]
shells = [
Shell(type=0, center_idx=0, exponents=np.array([1.0]), coefficients=np.array([1.0]))
]
wfn = Wavefunction(
atoms=atoms,
num_electrons=1.0,
charge=0,
multiplicity=2,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=shells,
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
# Large number of coordinates (100,000)
n_points = 100000
coords = np.random.randn(n_points, 3)
# Should handle without memory error
rho = calc_density(wfn, coords, use_cache=True)
assert len(rho) == n_points
assert np.all(np.isfinite(rho))
class TestNumericalStability:
"""Test numerical stability under extreme conditions."""
def test_very_large_coordinates(self):
"""Test density calculation with very large coordinates."""
atoms = [Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)]
shells = [
Shell(type=0, center_idx=0, exponents=np.array([1.0]), coefficients=np.array([1.0]))
]
wfn = Wavefunction(
atoms=atoms,
num_electrons=1.0,
charge=0,
multiplicity=2,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=shells,
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
# Very far from nucleus
coords = np.array([[1000.0, 1000.0, 1000.0]])
rho = calc_density(wfn, coords)
# Should be very small but not NaN/Inf
assert np.isfinite(rho[0])
assert rho[0] >= 0
def test_near_nucleus_density(self):
"""Test density calculation very close to nucleus."""
atoms = [Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)]
shells = [
Shell(type=0, center_idx=0, exponents=np.array([100.0]), coefficients=np.array([1.0]))
]
wfn = Wavefunction(
atoms=atoms,
num_electrons=1.0,
charge=0,
multiplicity=2,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=shells,
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
# Very close to nucleus
coords = np.array([[1e-10, 0.0, 0.0]])
rho = calc_density(wfn, coords)
# Should be finite and positive
assert np.isfinite(rho[0])
assert rho[0] > 0