-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_electron.py
More file actions
312 lines (228 loc) · 10.1 KB
/
Copy pathtest_electron.py
File metadata and controls
312 lines (228 loc) · 10.1 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
import numpy as np
import pytest
import unittest
from electron import Electrons, _check_sizes
class TestCheckSizesFunction(unittest.TestCase):
def test_check_sizes_valid(self):
N_el = 3
mass2 = np.zeros((3, 2))
_check_sizes(N_el, mass2)
def test_check_sizes_invalid(self):
N_el = 3
mass2 = np.zeros((4, 2))
with pytest.raises(ValueError, match='Not equal sizes'):
_check_sizes(N_el, mass2)
class TestElectronsInitialization(unittest.TestCase):
def test_init_valid(self):
N = 5
electrons = Electrons(N)
self.assertEqual(electrons.N_el_in_array, N)
self.assertEqual(electrons.coor.shape, (N, 7))
ref = np.zeros((N, 7))
ref[:, -1] += 1
np.testing.assert_array_equal(electrons.coor, ref)
def test_init_zero_or_negative(self):
with pytest.raises(ValueError, match='N_el_in_array must be greater than zero'):
Electrons(0)
with pytest.raises(ValueError, match='N_el_in_array must be greater than zero'):
Electrons(-5)
class TestElectronsProperties(unittest.TestCase):
def setUp(self):
self.N = 3
self.electrons = Electrons(self.N)
def test_set_electron_properties_valid(self):
mass = 0.5
self.electrons.set_electron_properties(mass)
self.assertEqual(self.electrons.effective_mass, mass)
def test_set_electron_properties_invalid(self):
with pytest.raises(ValueError, match='Effective mass must be positive'):
self.electrons.set_electron_properties(0)
with pytest.raises(ValueError, match='Effective mass must be positive'):
self.electrons.set_electron_properties(-1.0)
class TestElectronsCoordinates(unittest.TestCase):
def setUp(self):
self.N = 3
self.electrons = Electrons(self.N)
test_coor = np.array([
[1.0, 2.0, 3.0, 0.1, 0.2, 0.3, 0],
[4.0, 5.0, 6.0, 0.4, 0.5, 0.6, 0],
[7.0, 8.0, 9.0, 0.7, 0.8, 0.9, 0]
])
self.electrons.coor = test_coor.copy()
def test_set_coor_all(self):
new_coor = np.array([
[10.0, 11.0, 12.0, 0.1, 0.2, 0.3],
[13.0, 14.0, 15.0, 0.4, 0.5, 0.6],
[16.0, 17.0, 18.0, 0.7, 0.8, 0.9]
])
self.electrons.set_coor(new_coor)
expected = np.hstack([new_coor, np.zeros((self.N, 1))])
np.testing.assert_array_almost_equal(self.electrons.coor, expected)
def test_set_coor_single(self):
new_coor = np.array([10.0, 11.0, 12.0, 0.1, 0.2, 0.3])
self.electrons.set_coor(new_coor, indx_el=1)
np.testing.assert_array_almost_equal(self.electrons.coor[1, :-1], new_coor)
np.testing.assert_array_almost_equal(self.electrons.coor[0, :-1],
[1.0, 2.0, 3.0, 0.1, 0.2, 0.3])
def test_set_coor_wrong_size(self):
wrong_coor = np.array([
[10.0, 11.0, 12.0, 0.1, 0.2, 0.3],
[13.0, 14.0, 15.0, 0.4, 0.5, 0.6]
])
with pytest.raises(ValueError, match='Not equal sizes'):
self.electrons.set_coor(wrong_coor)
def test_get_coor_all(self):
result = self.electrons.get_coor()
expected = self.electrons.coor[:, :-1]
np.testing.assert_array_almost_equal(result, expected)
def test_get_coor_single(self):
result = self.electrons.get_coor(indx_el=1)
expected = self.electrons.coor[1, :-1]
np.testing.assert_array_almost_equal(result, expected)
def test_set_xcoor_all(self):
new_xcoor = np.array([
[10.0, 11.0, 12.0],
[13.0, 14.0, 15.0],
[16.0, 17.0, 18.0]
])
self.electrons.set_xcoor(new_xcoor)
np.testing.assert_array_almost_equal(self.electrons.coor[:, :3], new_xcoor)
np.testing.assert_array_almost_equal(self.electrons.coor[:, 3:-1],
self.electrons.coor[:, 3:-1])
def test_add_coor(self):
shift = np.array([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]
])
original_coor = self.electrons.coor[:, :3].copy()
self.electrons.add_coor(shift)
expected = original_coor + shift
np.testing.assert_array_almost_equal(self.electrons.coor[:, :3], expected)
class TestElectronsVelocity(unittest.TestCase):
def setUp(self):
self.N = 3
self.electrons = Electrons(self.N)
test_coor = np.array([
[1.0, 2.0, 3.0, 0.1, 0.2, 0.3, 0],
[4.0, 5.0, 6.0, 0.4, 0.5, 0.6, 0],
[7.0, 8.0, 9.0, 0.7, 0.8, 0.9, 0]
])
self.electrons.coor = test_coor.copy()
self.electrons.set_electron_properties(0.5)
def test_set_velocity_all(self):
new_velocity = np.array([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]
])
self.electrons.set_velocity(new_velocity)
expected = np.hstack([
self.electrons.coor[:, :3],
new_velocity,
self.electrons.coor[:, -1:]
])
np.testing.assert_array_almost_equal(self.electrons.coor, expected)
def test_get_velocity(self):
result_all = self.electrons.get_velocity()
expected_all = self.electrons.coor[:, 3:-1]
np.testing.assert_array_almost_equal(result_all, expected_all)
result_single = self.electrons.get_velocity(indx_el=1)
expected_single = self.electrons.coor[1, 3:-1]
np.testing.assert_array_almost_equal(result_single, expected_single)
def test_get_module_velocity(self):
module_all = self.electrons.get_module_velocity()
expected_all = np.sqrt(0.1**2 + 0.2**2 + 0.3**2)
self.assertAlmostEqual(module_all[0], expected_all, places=10)
module_single = self.electrons.get_module_velocity(indx_el=2)
expected_single = np.sqrt(0.7**2 + 0.8**2 + 0.9**2)
self.assertAlmostEqual(module_single, expected_single, places=10)
class TestElectronsEnergy(unittest.TestCase):
def setUp(self):
self.N = 2
self.electrons = Electrons(self.N)
test_coor = np.array([
[0.0, 0.0, 0.0, 1e6, 0.0, 0.0, 0],
[0.0, 0.0, 0.0, 0.0, 2e6, 0.0, 0]
])
self.electrons.coor = test_coor.copy()
self.electrons.set_electron_properties(0.5)
def test_get_energy(self):
energy1 = self.electrons.get_E(indx_el=0)
expected1 = 0.5 * 9.109 * (1e6)**2 / (2 * 1.602176634) * 1e6
self.assertAlmostEqual(energy1, expected1, delta=expected1*0.01)
energies = self.electrons.get_E()
self.assertEqual(len(energies), self.N)
def test_add_energy_all(self):
initial_energy = self.electrons.get_E(0)
delta_E = 1e-18
self.electrons.add_energy(np.array([delta_E, delta_E]))
final_energy = self.electrons.get_E(0)
self.assertAlmostEqual(final_energy, initial_energy + delta_E,
delta=final_energy*0.01)
def test_add_energy_single(self):
initial_energy = self.electrons.get_E(indx_el=1).copy()
delta_E = 1e-18
self.electrons.add_energy(delta_E, indx_el=1)
final_energy = self.electrons.get_E(indx_el=1)
self.assertAlmostEqual(final_energy, initial_energy + delta_E,
delta=final_energy*0.01)
class TestElectronsFlags(unittest.TestCase):
def setUp(self):
self.N = 4
self.electrons = Electrons(self.N)
def test_set_get_flags(self):
flags = np.array([0, 1, 0, 1])
self.electrons.set_flags(flags)
np.testing.assert_array_equal(self.electrons.get_flags(), flags)
self.electrons.set_flags(1, indx_el=2)
self.assertEqual(self.electrons.get_flags(indx_el=2), 1)
def test_kill_electron(self):
self.electrons.kill_electron(2)
self.assertEqual(self.electrons.coor[2, -1], 0)
self.assertFalse(self.electrons.is_alive(2))
def test_is_alive(self):
self.electrons.set_flags(1, indx_el=0)
self.electrons.set_flags(0, indx_el=1)
self.assertTrue(self.electrons.is_alive(0))
self.assertFalse(self.electrons.is_alive(1))
def test_is_end(self):
self.electrons.set_flags(np.array([0, 1, 0, 1]))
self.assertFalse(self.electrons.is_end())
self.electrons.set_flags(np.array([0, 0, 0, 0]))
self.assertTrue(self.electrons.is_end())
def test_kill_low_energy_electron(self):
self.N = 3
self.electrons = Electrons(self.N)
self.electrons.set_electron_properties(0.5)
velocities = np.array([
[1e4, 0.0, 0.0],
[1e6, 0.0, 0.0],
[1e4, 0.0, 0.0]
])
self.electrons.set_velocity(velocities)
low_energy = self.electrons.get_E(indx_el=0)
high_energy = self.electrons.get_E(indx_el=1)
kill_energy = (low_energy + high_energy) / 2
self.electrons.kill_low_energy_electron(kill_energy)
flags = self.electrons.get_flags()
expected = np.array([0, 1, 0])
np.testing.assert_array_equal(flags, expected)
class TestElectronsOtherMethods(unittest.TestCase):
def setUp(self):
self.N = 2
self.electrons = Electrons(self.N)
self.electrons.set_electron_properties(0.5)
def test_get_N_el_in_ar(self):
self.assertEqual(self.electrons.get_N_el(), self.N)
def test_get_effective_mass(self):
self.assertEqual(self.electrons.get_effective_mass(), 0.5)
def test_get_log(self):
self.electrons.E_a = 0.1
self.electrons.E_g = 1.5
self.electrons.delta_E_DOS = 0.05
log = self.electrons.get_log()
expected = f"M_e={0.5}\nE_a={0.1}\nE_g={1.5}\ndelta_E_DOS={0.05}\n"
self.assertEqual(log, expected)
if __name__ == '__main__':
unittest.main()