-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_gbmaker.py
More file actions
349 lines (293 loc) · 14.2 KB
/
test_gbmaker.py
File metadata and controls
349 lines (293 loc) · 14.2 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
# Copyright 2025, Battelle Energy Alliance, LLC, ALL RIGHTS RESERVED
import filecmp
import math
import tempfile
import unittest
from unittest.mock import patch
import numpy as np
import pytest
from GBOpt.Atom import Atom, AtomValueError
from GBOpt.GBMaker import GBMaker, GBMakerTypeError, GBMakerValueError
from GBOpt.UnitCell import UnitCell
class TestGBMaker(unittest.TestCase):
def setUp(self):
# Common test setup
self.a0 = 3.61
self.structure = "fcc"
self.atom_types = "Cu"
self.gb_thickness = 10.0
theta = math.radians(36.869898)
self.misorientation = np.array(
[theta, 0.0, 0.0, 0.0, -theta / 2.0])
self.repeat_factor = 6
self.x_dim_min = 60.0
self.vacuum = 10.0
self.gb_id = 0
self.interaction_distance = 10
self.gbm = GBMaker(self.a0, self.structure, self.gb_thickness,
self.misorientation, self.atom_types,
repeat_factor=self.repeat_factor, x_dim_min=self.x_dim_min,
vacuum=self.vacuum,
interaction_distance=self.interaction_distance,
gb_id=self.gb_id)
def test_initialization(self):
# Test that all values are set correctly at initialization
self.assertEqual(self.gbm.a0, self.a0)
self.assertEqual(self.gbm.structure, self.structure)
self.assertEqual(self.gbm.gb_thickness, self.gb_thickness)
np.testing.assert_array_equal(
self.gbm.misorientation, self.misorientation)
self.assertEqual(self.gbm.repeat_factor, [
self.repeat_factor, self.repeat_factor])
self.assertEqual(self.gbm.x_dim_min, self.x_dim_min)
self.assertEqual(self.gbm.vacuum_thickness, self.vacuum)
self.assertEqual(self.gbm.id, self.gb_id)
unit_cell = UnitCell()
unit_cell.init_by_structure(self.structure, self.a0, self.atom_types)
self.assertTrue(repr(self.gbm.unit_cell) == repr(unit_cell))
self.assertEqual(self.gbm.interaction_distance, self.interaction_distance)
self.assertTrue(self.gbm.whole_system.shape[0] > 0)
self.assertTrue(isinstance(self.gbm.whole_system, np.ndarray))
self.assertIsNotNone(self.gbm.left_grain)
self.assertIsNotNone(self.gbm.right_grain)
self.assertEqual(len(self.gbm.whole_system[0]), 4)
left_grain = self.gbm.left_grain
right_grain = self.gbm.right_grain
system = self.gbm.whole_system
self.assertEqual(
left_grain.shape[0] + right_grain.shape[0], system.shape[0])
# Tests for invalid values
def test_invalid_a0_type(self):
with self.assertRaises(GBMakerTypeError):
self.gbm.a0 = "invalid"
def test_invalid_a0_value(self):
with self.assertRaises(GBMakerValueError):
self.gbm.a0 = -5.0
def test_invalid_misorientation_length(self):
with self.assertRaises(GBMakerValueError):
self.gbm.misorientation = np.array([0.1, 0.2])
def test_invalid_misorientation_type(self):
with self.assertRaises(GBMakerTypeError):
self.gbm.misorientation = "invalid"
def test_invalid_structure_type(self):
with self.assertRaises(GBMakerTypeError):
self.gbm.structure = 123
def test_invalid_structure_value(self):
with self.assertRaises(GBMakerValueError):
self.gbm.structure = "invalid_structure"
def test_invalid_values_raise_exceptions(self):
with self.assertRaises(GBMakerValueError):
GBMaker(-1.0, self.structure,
self.gb_thickness, self.misorientation, self.atom_types)
with self.assertRaises(GBMakerValueError):
GBMaker(self.a0, "invalid_structure",
self.gb_thickness, self.misorientation, self.atom_types)
with self.assertRaises(GBMakerValueError):
GBMaker(self.a0, self.structure,
self.gb_thickness, np.array([0.1, 0.2]), self.atom_types)
with self.assertRaises(GBMakerValueError):
GBMaker(self.a0, self.structure, -5.0,
self.misorientation, self.atom_types)
with self.assertRaises(AtomValueError):
GBMaker(self.a0, self.structure, self.gb_thickness,
self.misorientation, "Invalid")
# Tests for additional getters
def test_additional_getters(self):
self.assertGreater(self.gbm.y_dim, 0)
self.assertGreater(self.gbm.z_dim, 0)
self.assertGreater(self.gbm.radius, 0)
def test_box_dimensions(self):
box_dims = self.gbm.box_dims
self.assertTrue(isinstance(box_dims, np.ndarray))
self.assertEqual(box_dims.shape, (3, 2))
# Tests for public methods
def test_get_supercell(self):
corners = np.array([[0, 0, 0], [10, 10, 10]])
supercell = self.gbm.get_supercell(corners)
self.assertTrue(isinstance(supercell, np.ndarray))
self.assertGreater(supercell.shape[0], 0)
def test_update_spacing(self):
initial_spacing = self.gbm.spacing
theta = math.radians(22.619865)
self.gbm.misorientation = np.array([theta, 0.0, 0.0, 0.0, -theta / 2.0])
self.assertNotEqual(initial_spacing, self.gbm.spacing)
def test_write_lammps(self):
atoms = self.gbm.whole_system
box_sizes = self.gbm.box_dims
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
self.gbm.write_lammps(temp_file.name, atoms, box_sizes)
with open(temp_file.name, "r") as f:
content = f.readlines()
self.assertGreater(len(content), 0)
self.assertIn("atoms", content[2].lower())
self.assertIn("atom types", content[3].lower())
# Tests for setters
def test_box_dimensions_after_updates(self):
original_box_dims = self.gbm.box_dims.copy()
self.gbm.x_dim_min = 80.0
self.assertFalse(np.allclose(original_box_dims, self.gbm.box_dims))
def test_misorientation_spacing(self):
original_spacing = self.gbm.spacing.copy()
theta = math.radians(22.619865)
self.gbm.misorientation = np.array([theta, 0.0, 0.0, 0.0, -theta / 2.0])
self.assertNotEqual(original_spacing, self.gbm.spacing)
def test_setters_update_properties(self):
self.gbm.interaction_distance = 6
self.assertEqual(self.gbm.interaction_distance, 6)
self.assertGreater(self.gbm.y_dim, 2*self.gbm.interaction_distance)
self.assertGreater(self.gbm.z_dim, 2*self.gbm.interaction_distance)
self.gbm.a0 = 4.0
self.assertEqual(self.gbm.a0, 4.0)
self.gbm.structure = "bcc"
self.assertEqual(self.gbm.structure, "bcc")
with self.assertRaises(GBMakerValueError):
self.gbm.structure = "fluorite"
self.gbm.gb_thickness = 12.0
self.assertEqual(self.gbm.gb_thickness, 12.0)
with self.assertWarns(UserWarning):
self.gbm.misorientation = np.array([0.3, 0.4, 0.5, 0.6, 0.7])
theta = math.radians(90-36.869898)
new_misorientation = np.array([theta, 0.0, 0.0, 0.0, -theta / 2.0])
self.gbm.misorientation = new_misorientation
np.testing.assert_array_equal(
self.gbm.misorientation, new_misorientation)
with self.assertRaises(GBMakerValueError):
self.gbm.repeat_factor = [-2, -1]
self.gbm.repeat_factor = 6
self.assertEqual(self.gbm.repeat_factor, [6, 6])
with self.assertWarns(UserWarning):
self.gbm.repeat_factor = 1
self.gbm.repeat_factor = [1, 1]
with self.assertRaises(GBMakerValueError):
self.gbm.repeat_factor = [1.5, 2.0]
self.gbm.x_dim_min = 80.0
self.assertEqual(self.gbm.x_dim_min, 80.0)
self.gbm.vacuum_thickness = 15.0
self.assertEqual(self.gbm.vacuum_thickness, 15.0)
self.gbm.id = 2
self.assertEqual(self.gbm.id, 2)
def test_thin_thick_box_dimensions(self):
# Thin box
self.gbm.x_dim_min = 5.0
self.assertGreaterEqual(self.gbm.box_dims[0][1], 5.0)
# Thick box
self.gbm.vacuum_thickness = 50.0
self.assertGreater(self.gbm.box_dims[0][1], 50.0)
# Tests for private methods
def test_approximate_rotation_matrix_as_int(self):
rotation_matrix = np.array([[0.70710678, 0.5, 0.5],
[0.70710678, -0.5, -0.5],
[0.0, 0.70710678, -0.70710678]])
approx_matrix = self.gbm._GBMaker__approximate_rotation_matrix_as_int(
rotation_matrix)
expected_matrix = np.array([[27720, 19601, 19601],
[27720, -19601, -19601],
[0, 1, -1]])
np.testing.assert_array_equal(approx_matrix, expected_matrix)
def test_calculate_periodic_spacing_logic(self):
with patch.object(GBMaker, "_GBMaker__calculate_periodic_spacing", return_value={"x": 5.0, "y": 10.0, "z": 15.0}):
self.gbm.update_spacing()
self.assertEqual(self.gbm.spacing["x"], 5.0)
self.assertEqual(self.gbm.spacing["y"], 10.0)
self.assertEqual(self.gbm.spacing["z"], 15.0)
# Tests for warnings
def test_repeat_factor_warning(self):
with self.assertWarns(UserWarning):
gbm = GBMaker(self.a0, self.structure, self.gb_thickness,
self.misorientation, self.atom_types, repeat_factor=[2, 3],
x_dim_min=self.x_dim_min, vacuum=self.vacuum,
interaction_distance=30,
gb_id=self.gb_id)
with self.assertWarns(UserWarning):
gbm.interaction_distance = 32
def test_non_periodic_boundary_warning(self):
with self.assertWarns(UserWarning):
self.gbm._GBMaker__approximate_rotation_matrix_as_int(
np.array(
[
[0.123456789, 0.56789123, -0.918273645],
[-0.135792468, 0.246813579, 0.1],
[0.159283746, -0.2, 0.1]
]
)
)
# Additional tests
# Output data file format is as expected.
def test_lammps_file_formatting(self):
atoms = np.array([("Cu", 0.0, 0.0, 0.0), ("H", 1.0, 1.0, 1.0)],
dtype=Atom.atom_dtype)
box_sizes = np.array([[0.0, 10.0], [0.0, 10.0], [0.0, 10.0]])
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
self.gbm.write_lammps(temp_file.name, atoms, box_sizes)
with open(temp_file.name, "r") as f:
content = f.readlines()
self.assertEqual(content[2].strip(), "2 atoms")
self.assertEqual(content[3].strip(), "2 atom types")
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
self.gbm.write_lammps(temp_file.name, atoms, box_sizes, type_as_int=False)
with open(temp_file.name, "r") as f:
content = f.readlines()
self.assertEqual(content[8].strip(), "Atom Type Labels")
self.assertEqual(content[10].strip(), "1 Cu")
self.assertEqual(content[11].strip(), "2 H")
def test_lammps_file_formatting_with_charge(self):
atoms = np.array(
[
('U', 0.0, 0.0, 0.0),
('O', 0.25, 0.25, 0.25),
('O', 0.25, 0.25, 0.75)
],
dtype=Atom.atom_dtype
)
charges = {'U': 2.4, 'O': -1.2}
box_sizes = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]])
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
self.gbm.write_lammps(temp_file.name, atoms, box_sizes, charges=charges)
with open(temp_file.name, 'r') as f:
content = f.readlines()
self.assertEqual(content[2].strip(), '3 atoms')
self.assertEqual(content[3].strip(), '2 atom types')
self.assertEqual(content[15].strip(),
'1 U 2.400000 0.000000 0.000000 0.000000')
self.assertEqual(content[16].strip(),
'2 O -1.200000 0.250000 0.250000 0.250000')
self.assertEqual(content[17].strip(),
'3 O -1.200000 0.250000 0.250000 0.750000')
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
self.gbm.write_lammps(temp_file.name, atoms, box_sizes,
charges=charges, type_as_int=True)
with open(temp_file.name, 'r') as f:
content = f.readlines()
self.assertEqual(content[2].strip(), '3 atoms')
self.assertEqual(content[3].strip(), '2 atom types')
self.assertEqual(content[10].strip(),
'1 2 2.400000 0.000000 0.000000 0.000000')
self.assertEqual(content[11].strip(),
'2 1 -1.200000 0.250000 0.250000 0.250000')
self.assertEqual(content[12].strip(),
'3 1 -1.200000 0.250000 0.250000 0.750000')
def test_data_integrity_in_gb(self):
left_grain = self.gbm.left_grain
right_grain = self.gbm.right_grain
system = self.gbm.whole_system
self.assertEqual(
left_grain.shape[0] + right_grain.shape[0], system.shape[0])
def test_inconsistent_data_raises_exceptions(self):
with self.assertRaises(GBMakerValueError):
GBMaker(self.a0, self.structure, -5.0,
self.misorientation, self.atom_types) # Negative thickness
@pytest.mark.xfail(reason="Fails due to issue #39")
def test_single_grain_creation(self):
gbm_single = GBMaker(3.54, "fcc", 5.0,
np.array([0, 0, 0, 0, 0]), "Cu",
repeat_factor=6, x_dim_min=10,
vacuum=10,
interaction_distance=5
)
with tempfile.NamedTemporaryFile(delete=True) as temp_file:
gbm_single.write_lammps(temp_file.name)
# This test _will_ fail until we address #39
self.assertFalse(
filecmp.cmp(temp_file.name, "./tests/gold/fcc_Cu.txt", shallow=False))
if __name__ == "__main__":
unittest.main()