-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_consistency.py
More file actions
394 lines (332 loc) · 12.6 KB
/
test_consistency.py
File metadata and controls
394 lines (332 loc) · 12.6 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
"""
Consistency tests for PyMultiWFN against reference values.
This module tests PyMultiWFN calculations against known reference values
to ensure correctness and numerical stability.
"""
import pytest
import numpy as np
from pathlib import Path
from pymultiwfn.io.loader import load_wavefunction
class TestConsistencyDensity:
"""Test electron density calculations for consistency."""
def test_density_positive_everywhere(self):
"""Test that electron density is always positive."""
# Create a simple test case
from pymultiwfn.core.data import Atom, Shell, Wavefunction
# Hydrogen atom at origin
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0, # S shell
center_idx=0,
exponents=np.array([3.42525091]),
coefficients=np.array([0.15432897]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
# Evaluate density at various points
from pymultiwfn.math.density import calc_density
coords = np.array(
[
[0.0, 0.0, 0.0], # At nucleus
[0.5, 0.0, 0.0],
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0],
]
)
density = calc_density(wfn, coords)
# All density values should be positive
assert np.all(density > 0), "Electron density should be positive everywhere"
def test_density_symmetry_h2(self):
"""Test density symmetry for H2 molecule."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
# H2 molecule along z-axis
atoms = [
Atom(element="H", index=1, x=0.0, y=0.0, z=-0.7, charge=1.0),
Atom(element="H", index=1, x=0.0, y=0.0, z=0.7, charge=1.0),
]
shells = [
Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
),
Shell(
type=0,
center_idx=1,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
),
]
wfn = Wavefunction(
atoms=atoms,
num_electrons=2.0,
charge=0,
multiplicity=1,
num_basis=2,
num_atomic_orbitals=2,
num_primitives=2,
num_shells=2,
shells=shells,
occupations=np.array([1.0, 1.0]),
coefficients=np.array([[0.707, 0.707], [0.707, -0.707]]),
)
from pymultiwfn.math.density import calc_density
# Test points symmetric about origin
coords1 = np.array([[0.0, 0.0, 1.0]])
coords2 = np.array([[0.0, 0.0, -1.0]])
density1 = calc_density(wfn, coords1)
density2 = calc_density(wfn, coords2)
# Densities should be equal at symmetric points
np.testing.assert_allclose(density1, density2, rtol=1e-6)
def test_density_decay_far_from_nucleus(self):
"""Test that density decays to zero far from nucleus."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
from pymultiwfn.math.density import calc_density
# Test at increasing distances
distances = np.array([0.0, 1.0, 2.0, 5.0, 10.0])
coords = np.column_stack(
[distances, np.zeros_like(distances), np.zeros_like(distances)]
)
density = calc_density(wfn, coords)
# Density should be monotonically decreasing
assert np.all(np.diff(density) < 0), "Density should decrease with distance"
# Density at 10 Bohr should be very small
assert density[-1] < 1e-6, "Density should be negligible at large distances"
class TestConsistencyGradient:
"""Test electron density gradient calculations for consistency."""
def test_gradient_zero_at_nucleus(self):
"""Test that gradient of s-orbital density is zero at nucleus."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
from pymultiwfn.math.gradient import calc_density_gradient
coords = np.array([[0.0, 0.0, 0.0]])
gradient = calc_density_gradient(wfn, coords)
# Gradient should be zero at nucleus (for s-orbital)
np.testing.assert_allclose(gradient, np.zeros((1, 3)), atol=1e-10)
def test_gradient_numerical_consistency(self):
"""Test that analytical gradient matches numerical gradient."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
from pymultiwfn.math.density import calc_density
from pymultiwfn.math.gradient import calc_density_gradient
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0,
center_idx=0,
exponents=np.array([2.0]),
coefficients=np.array([1.0]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
coords = np.array([[1.0, 0.5, -0.3]])
# Calculate analytical gradient
grad_analytical = calc_density_gradient(wfn, coords)
# Calculate numerical gradient
h = 1e-5
numerical_grad = np.zeros_like(grad_analytical)
for i in range(3):
coords_plus = coords.copy()
coords_plus[0, i] += h
coords_minus = coords.copy()
coords_minus[0, i] -= h
density_plus = calc_density(wfn, coords_plus)
density_minus = calc_density(wfn, coords_minus)
numerical_grad[0, i] = (density_plus[0] - density_minus[0]) / (2 * h)
# Should match within tolerance
np.testing.assert_allclose(grad_analytical, numerical_grad, rtol=1e-4)
class TestConsistencyBasis:
"""Test basis function evaluation for consistency."""
def test_basis_normalization_consistency(self):
"""Test that basis functions are properly normalized."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
from pymultiwfn.math.basis import evaluate_basis
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
)
# Test at several points
coords = np.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
]
)
basis = evaluate_basis(wfn, coords)
# Basis functions should be well-behaved
assert not np.any(np.isnan(basis)), "Basis functions should not be NaN"
assert not np.any(np.isinf(basis)), "Basis functions should not be infinite"
def test_basis_s_p_d_f_ordering(self):
"""Test that S, P, D, F shells are in correct order."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
from pymultiwfn.math.basis import evaluate_basis
# Create shells for each angular momentum
shells = [
Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
), # S: 1 func
Shell(
type=1,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
), # P: 3 funcs
Shell(
type=2,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
), # D: 6 funcs
Shell(
type=3,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
), # F: 10 funcs
]
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=20, # 1 + 3 + 6 + 10
num_atomic_orbitals=20,
num_primitives=4,
num_shells=4,
shells=shells,
)
coords = np.array([[1.0, 0.0, 0.0]])
basis = evaluate_basis(wfn, coords)
# Should have 20 basis functions
assert basis.shape == (
1,
20,
), f"Expected 20 basis functions, got {basis.shape[1]}"
# S function (index 0) should be non-zero
assert basis[0, 0] > 0, "S function should be non-zero at (1,0,0)"
# Px function (index 1) should be non-zero (S: 1, P: X,Y,Z)
assert basis[0, 1] > 0, "Px function should be non-zero at (1,0,0)"
# Py function (index 2) should be zero at (1,0,0)
np.testing.assert_allclose(basis[0, 2], 0.0, atol=1e-10)
# Pz function (index 3) should be zero at (1,0,0)
np.testing.assert_allclose(basis[0, 3], 0.0, atol=1e-10)
class TestConsistencyIntegration:
"""Test numerical integration for consistency."""
def test_density_integrates_to_n_electrons(self):
"""Test that integrated density equals number of electrons."""
from pymultiwfn.core.data import Atom, Shell, Wavefunction
from pymultiwfn.math.density import calc_density
# Simple system: H atom
atom = Atom(element="H", index=1, x=0.0, y=0.0, z=0.0, charge=1.0)
shell = Shell(
type=0,
center_idx=0,
exponents=np.array([1.0]),
coefficients=np.array([1.0]),
)
wfn = Wavefunction(
atoms=[atom],
num_electrons=1.0,
charge=0,
multiplicity=1,
num_basis=1,
num_atomic_orbitals=1,
num_primitives=1,
num_shells=1,
shells=[shell],
occupations=np.array([1.0]),
coefficients=np.array([[1.0]]),
)
# Create a simple grid for integration (very coarse for speed)
# In practice, use a proper integration scheme
r_range = np.linspace(0, 5, 50)
theta = np.linspace(0, np.pi, 20)
phi = np.linspace(0, 2 * np.pi, 20)
# This is just a simplified test
# Real integration would use proper grid weights
# For now, just verify that density is integrable
coords = np.array([[1.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.5, 0.0, 0.0]])
density = calc_density(wfn, coords)
# Just check that density values are reasonable
assert np.all(density > 0), "Density should be positive"
assert np.all(np.isfinite(density)), "Density should be finite"