-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.py
250 lines (214 loc) · 7.93 KB
/
testing.py
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
from He import He
import configparser
import numpy as np
from hypothesis import strategies as st
from hypothesis import given
from hypothesis import settings
from datetime import timedelta
config = configparser.ConfigParser()
config.read("configuration.txt")
NUCLEAR_CHARGE = 2
def hydrogen_like_wavefunc(x):
"""
returns the wavefunction of He in the case
where there's no interaction between the electrons
"""
return x*np.exp(-NUCLEAR_CHARGE*x)/np.trapz((x*np.exp(-NUCLEAR_CHARGE*x))**2,x)**0.5
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))), R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))))
def test_He_init(R_MAX,SAMPLES):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
the edge values of r,
the correct array dimensions of attributes,
correct sign of nuclear potential
"""
test_atom = He(R_MAX,SAMPLES)
#tests edge values for r
assert(test_atom.r[-1]==R_MAX)
assert(test_atom.r[0]==0)
#tests correct array dimension of attributes
assert(len(test_atom.r)==SAMPLES)
assert(len(test_atom.u)==SAMPLES)
assert(len(test_atom.rho)==SAMPLES)
assert(len(test_atom.V_H)==SAMPLES)
assert(len(test_atom.V_N)==SAMPLES)
assert(len(test_atom.V_C)==SAMPLES)
assert(len(test_atom.V_X)==SAMPLES)
assert(len(test_atom.V)==SAMPLES)
#tests correct sign of nuclear potential
assert(np.all(test_atom.V_N<=0))
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))))
def test_compute_hartee_potential(R_MAX,SAMPLES):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
correct sign of Hartree potential,
boundary conditions of Hartree potential
The hyrogen like WF is used as reference
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
test_atom.compute_hartree_potential()
assert(np.all(test_atom.V_H >= 0))
#tests that the boundary condition is matched
np.testing.assert_almost_equal(test_atom.V_H[-1]*test_atom.r[-1],NUCLEAR_CHARGE,decimal=7)
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))))
def test_compute_correlation_potential(R_MAX,SAMPLES):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
The hyrogen like WF is used as reference.
correct sign of Correlation potential and no divergence
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
test_atom.compute_correlation_potential()
#tests correct sign and no divergence
assert(np.all(test_atom.V_C<=0))
assert(test_atom.V_C[0]==0)
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))))
def test_compute_exchange_potential(R_MAX,SAMPLES):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
correct sign of exchange potential and no divergence
The hyrogen like WF is used as reference.
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
test_atom.compute_exchange_potential()
#tests correct sign and no divergance
assert(np.all(test_atom.V_X<=0))
assert(test_atom.V_X[0]==0)
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))))
def test_hse_normalize(R_MAX,SAMPLES):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
correct sign of WF and density,
good normalization
The hyrogen like WF is used as reference
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
probability = np.trapz(test_atom.u**2,test_atom.r)
#tests positivity of wf and density
assert(np.all(test_atom.rho >= 0))
assert(np.all(test_atom.u >= 0))
#tests good normalization
np.testing.assert_almost_equal(probability,1,decimal=7)
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))),
E_N = st.floats(float(config.get('settings', 'HSE_E_MIN')),0))
def test_hse_integrate(R_MAX,SAMPLES,E_N):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
E_N : float
energy to use in the solution of SE
TESTS
----------
boundary conditions of WF,
continuity of WF
The hyrogen like WF is used as reference
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
#computing potentials for random WF
test_atom.compute_total_potential()
#obtain new wavefunction integrating using a random energy
test_atom.hse_integrate(1,E_N)
#this wavefunction is not an eignvalue but still needs to have basic properties
#verify boundary condition at R_MAX
assert(test_atom.u[-2] == test_atom.r[-2]*np.exp(-test_atom.r[-2]))
assert(test_atom.u[-1] == test_atom.r[-1]*np.exp(-test_atom.r[-1]))
#check continuity, using maximum difference from consecutive values of R_MAX/SAMPLES
assert(abs(test_atom.u[i]-test_atom.u[i+1])< R_MAX/SAMPLES for i in range(0,SAMPLES-1))
@given(SAMPLES = st.integers(10,int(config.get('settings', 'SAMPLES'))),
R_MAX = st.floats(4,float(config.get('settings', 'R_MAX'))),
PREC_HSE = st.floats(10e-10,float(config.get('settings','PREC_HSE'))),
HSE_E_MIN = st.floats(float(config.get('settings','HSE_E_MIN')),-3))
@settings(deadline=timedelta(seconds=1))
def test_hse_solve(R_MAX,SAMPLES,PREC_HSE,HSE_E_MIN):
"""
Parameters
----------
R_MAX : float
maximum value of r.
SAMPLES : integer
number of divisions of the radial space.
TESTS
----------
that the conputed WF has 1s orbital properties,
energy eignvalue has to be negative and within range
The hyrogen like WF is used as reference
"""
test_atom = He(R_MAX,SAMPLES)
#using the hydrogen like WF as reference
test_atom.u=hydrogen_like_wavefunc(test_atom.r)
#test_atom.u = np.zeros(SAMPLES)
test_atom.hse_normalize()
test_atom.rho = 2*test_atom.u**2
#computing potentials using random WF
test_atom.compute_total_potential()
#compute new wavefunction using potentials
test_atom.E_k = test_atom.hse_solve(1,0,PREC_HSE,HSE_E_MIN)
#1s orbitals have no nodes, wf needs to be positive
#(checked before normalizing)
assert(np.all(test_atom.u[1:] > 0))
#energy eignvalue needs to be negative and within range
assert(test_atom.E_k < 0 and test_atom.E_k > float(config.get('settings','HSE_E_MIN')))