forked from diffpy/diffpy.structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_structure.py
More file actions
693 lines (620 loc) · 24.9 KB
/
test_structure.py
File metadata and controls
693 lines (620 loc) · 24.9 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Unit tests for Structure class."""
import copy
import pickle
import unittest
import numpy
import pytest
from diffpy.structure import Atom, Lattice, Structure
# ----------------------------------------------------------------------------
class TestStructure(unittest.TestCase):
"""Test methods of Structure class."""
@pytest.fixture(autouse=True)
def prepare_fixture(self, datafile):
self.cdsefile = datafile("CdSe_bulk.stru")
self.teifile = datafile("TeI.cif")
self.pbtefile = datafile("PbTe.cif")
_loaded_structures = {}
def setUp(self):
self.stru = Structure(
[Atom("C", [0, 0, 0]), Atom("C", [1, 1, 1])],
lattice=Lattice(1, 1, 1, 90, 90, 120),
)
# useful variables
if not self._loaded_structures:
self._loaded_structures.update(
[
("cdse", Structure(filename=self.cdsefile)),
("tei", Structure(filename=self.teifile)),
("pbte", Structure(filename=self.pbtefile)),
]
)
self.__dict__.update(self._loaded_structures)
self.places = 12
return
def test___init__(self):
"""Check Structure.__init__()"""
atoms = [Atom("C", [0, 0, 0]), Atom("C", [0.5, 0.5, 0.5])]
self.assertRaises(ValueError, Structure, atoms, filename=self.teifile)
self.assertRaises(ValueError, Structure, lattice=Lattice(), filename=self.teifile)
self.assertRaises(ValueError, Structure, title="test", filename=self.teifile)
stru1 = Structure(title="my title")
self.assertEqual("my title", stru1.title)
stru2a = Structure(atoms)
stru2b = Structure(iter(atoms))
stru2c = Structure(a for a in atoms)
s2a = str(stru2a)
self.assertEqual(s2a, str(stru2b))
self.assertEqual(s2a, str(stru2c))
return
def test_copy(self):
"""Check Structure.copy()"""
class MyDerivedStructure(Structure):
def __copy__(self):
rv = MyDerivedStructure()
Structure.__copy__(self, target=rv)
return rv
pass
pbte = self.pbte
pbte2 = pbte.copy()
self.assertFalse(pbte2.lattice is pbte.lattice)
self.assertTrue(numpy.array_equal(pbte.xyz_cartn, pbte2.xyz_cartn))
self.assertTrue(numpy.array_equal(pbte.U, pbte2.U))
stru = MyDerivedStructure()
stru += pbte2[pbte2.element.startswith("Pb")]
pb3 = stru.copy()
self.assertTrue(isinstance(pb3, MyDerivedStructure))
self.assertTrue(all(pb3.element == "Pb2+"))
self.assertEqual(4, len(pb3))
return
def test___copy__(self):
"""Check Structure.__copy__()"""
cdse = Structure(filename=self.cdsefile)
cdse_str = cdse.writeStr("pdffit")
cdse2 = copy.copy(cdse)
self.assertEqual(cdse_str, cdse2.writeStr("pdffit"))
self.assertFalse(cdse.lattice is cdse2.lattice)
sameatoms = set(cdse).intersection(cdse2)
self.assertFalse(sameatoms)
return
# def test___str__(self):
# """check Structure.__str__()"""
# return
# def test_addNewAtom(self):
# """check Structure.addNewAtom()"""
# return
# def test_getLastAtom(self):
# """check Structure.getLastAtom()"""
# return
def test_getLastAtom(self):
"""Check Structure.getLastAtom()"""
s_lat = Lattice()
expected = Atom("C", [0, 0, 0])
structure = Structure(atoms=[Atom("C", [0, 0, 0])], lattice=s_lat)
actual = structure.getLastAtom()
assert actual.element == expected.element
assert numpy.allclose(expected.xyz, actual.xyz)
def test_get_last_atom(self):
"""Check Structure.get_last_atom()"""
s_lat = Lattice()
expected = Atom("C", [0, 0, 0])
structure = Structure(atoms=[Atom("C", [0, 0, 0])], lattice=s_lat)
actual = structure.get_last_atom()
assert actual.element == expected.element
assert numpy.allclose(expected.xyz, actual.xyz)
def test_addNewAtom(self):
"""Duplicate test for the deprecated addNewAtom method.
Remove this test in version 4.0.0
"""
s_lat = Lattice()
structure = Structure(lattice=s_lat)
length = len(structure)
structure.addNewAtom(atype="C", xyz=[0.1, 0.2, 0.3])
expected = len(structure)
actual = length + 1
assert expected == actual
atom_object = structure[-1]
assert atom_object.element == "C"
assert numpy.allclose(atom_object.xyz, [0.1, 0.2, 0.3])
def test_assignUniqueLabels(self):
"""Check Structure.assignUniqueLabels()"""
self.assertEqual("", "".join([a.label for a in self.stru]))
self.stru.assignUniqueLabels()
self.assertEqual("C1", self.stru[0].label)
self.assertEqual("C2", self.stru[1].label)
return
def test_distance(self):
"""Check Structure.distance()"""
from math import sqrt
self.stru.assignUniqueLabels()
self.assertRaises(IndexError, self.stru.distance, 333, "C1")
self.assertRaises(IndexError, self.stru.distance, "C", "C1")
self.assertAlmostEqual(sqrt(2.0), self.stru.distance(0, 1), self.places)
self.assertAlmostEqual(sqrt(2.0), self.stru.distance("C1", "C2"), self.places)
self.assertEqual(0, self.stru.distance(0, "C1"))
return
def test_angle(self):
"""Check Structure.angle()"""
cdse = Structure(filename=self.cdsefile)
cdse.assignUniqueLabels()
self.assertEqual(109, round(cdse.angle(0, 2, 1)))
self.assertEqual(109, round(cdse.angle("Cd1", "Se1", "Cd2")))
return
def test_placeInLattice(self):
"""Check Structure.placeInLattice() -- conversion of
coordinates."""
stru = self.stru
new_lattice = Lattice(0.5, 0.5, 0.5, 90, 90, 60)
stru.placeInLattice(new_lattice)
a0 = stru[0]
self.assertTrue(numpy.allclose(a0.xyz, [0.0, 0.0, 0.0]))
a1 = stru[1]
self.assertTrue(numpy.allclose(a1.xyz, [2.0, 0.0, 2.0]))
# def test_read(self):
# """check Structure.read()"""
# return
# def test_readStr(self):
# """check Structure.readStr()"""
# return
# def test_write(self):
# """check Structure.write()"""
# return
# def test_writeStr(self):
# """check Structure.writeStr()"""
# return
def test_aslist(self):
"""Check Structure.tolist()"""
lst = self.stru.tolist()
self.assertEqual(tuple(lst), tuple(self.stru))
self.assertEqual(list, type(lst))
return
def test_append(self):
"""Check Structure.append()"""
a = Atom("Si", (0.1, 0.2, 0.3))
lat = self.stru.lattice
self.stru.append(a)
alast = self.stru[-1]
self.assertEqual(3, len(self.stru))
self.assertEqual("Si", alast.element)
self.assertTrue(lat is alast.lattice)
self.assertTrue(numpy.array_equal(a.xyz, alast.xyz))
self.assertFalse(a is alast)
self.assertFalse(lat is a.lattice)
return
def test_insert(self):
"""Check Structure.insert()"""
a = Atom("Si", (0.1, 0.2, 0.3))
lat = self.stru.lattice
self.stru.insert(1, a)
a1 = self.stru[1]
self.assertEqual(3, len(self.stru))
self.assertEqual("Si", a1.element)
self.assertTrue(lat is a1.lattice)
self.assertTrue(numpy.array_equal(a.xyz, a1.xyz))
self.assertFalse(a is a1)
self.assertFalse(lat is a.lattice)
return
def test_extend(self):
"""Check Structure.extend()"""
stru = self.stru
cdse = Structure(filename=self.cdsefile)
lst = stru.tolist()
stru.extend(cdse)
self.assertEqual(6, len(stru))
self.assertTrue(all(a.lattice is stru.lattice for a in stru))
self.assertEqual(lst, stru.tolist()[:2])
self.assertFalse(stru[-1] is cdse[-1])
return
def test___getitem__(self):
"""Check Structure.__getitem__()"""
stru = self.stru
self.assertTrue(stru[0] is stru.tolist()[0])
intidx = list(range(len(stru)))[::-1]
self.assertEqual(stru[intidx].tolist(), stru.tolist()[::-1])
flagidx = numpy.arange(len(stru)) > 0
self.assertEqual(stru[flagidx].tolist(), stru.tolist()[1:])
cdse = Structure(self.cdse)
self.assertEqual([cdse[0], cdse[-2]], cdse[0, -2].tolist())
cdse013 = cdse.tolist()
cdse013.pop(2)
self.assertEqual(cdse013, cdse[:2, 3].tolist())
self.assertRaises(IndexError, cdse.__getitem__, "Cd1")
cdse.assignUniqueLabels()
self.assertTrue(cdse[0] is cdse["Cd1"])
cdse[0].label = "Hohenzollern"
self.assertRaises(IndexError, cdse.__getitem__, "Cd1")
self.assertTrue(cdse[0] is cdse["Hohenzollern"])
self.assertEqual([cdse[0], cdse[3], cdse[1]], cdse["Hohenzollern", 3:0:-2].tolist())
stru.label = ["A", "B"]
self.assertTrue(stru[0] is stru["A"])
self.assertTrue(stru[1] is stru["B"])
stru[1].label = "A"
self.assertRaises(IndexError, stru.__getitem__, "A")
return
def test___getitem__slice(self):
"""Check Structure.__getitem__() with a slice argument."""
stru = self.stru
self.assertEqual([stru[0]], stru[:1].tolist())
self.assertEqual([stru[1], stru[0]], stru[::-1].tolist())
return
def test___setitem__(self):
"""Check Structure.__setitem__()"""
a = Atom("Si", (0.1, 0.2, 0.3))
lat = self.stru.lattice
self.stru[1] = a
a1 = self.stru[1]
self.assertEqual(2, len(self.stru))
self.assertEqual("Si", a1.element)
self.assertTrue(lat is a1.lattice)
self.assertTrue(numpy.array_equal(a.xyz, a1.xyz))
self.assertFalse(a is a1)
self.assertFalse(lat is a.lattice)
return
def test___setitem__slice(self):
"""Check Structure.__setitem__() with a slice argument."""
a = Atom("Si", (0.1, 0.2, 0.3))
lat = self.stru.lattice
self.stru[:] = [a]
a0 = self.stru[0]
self.assertEqual(1, len(self.stru))
self.assertEqual("Si", a0.element)
self.assertTrue(lat is a0.lattice)
self.assertTrue(numpy.array_equal(a.xyz, a0.xyz))
self.assertFalse(a is a0)
self.assertFalse(lat is a.lattice)
return
def test___add__(self):
"""Check Structure.__add__()"""
stru = self.stru
cdse = Structure(filename=self.cdsefile)
total = stru + cdse
self.assertEqual(6, len(total))
ta0 = total[0]
tam1 = total[-1]
self.assertEqual("C", ta0.element)
self.assertTrue(numpy.array_equal(stru[0].xyz, ta0.xyz))
self.assertEqual("Se", tam1.element)
self.assertTrue(numpy.array_equal(cdse[-1].xyz, tam1.xyz))
self.assertFalse(total.lattice in (stru.lattice, cdse.lattice))
self.assertTrue(all([a.lattice is total.lattice for a in total]))
return
def test___iadd__(self):
"""Check Structure.__iadd__()"""
stru = self.stru
lat0 = stru.lattice
lst = stru.tolist()
cdse = Structure(filename=self.cdsefile)
stru += cdse
self.assertEqual(6, len(stru))
self.assertEqual(lst, stru[:2].tolist())
am1 = stru[-1]
self.assertEqual("Se", am1.element)
self.assertTrue(numpy.array_equal(cdse[-1].xyz, am1.xyz))
self.assertTrue(lat0 is stru.lattice)
self.assertFalse(stru.lattice is cdse.lattice)
self.assertTrue(all([a.lattice is stru.lattice for a in stru]))
return
def test___sub__(self):
"""Check Structure.__sub__()"""
cdse = Structure(filename=self.cdsefile)
cadmiums = cdse - cdse[2:]
self.assertEqual(2, len(cadmiums))
self.assertEqual("Cd", cadmiums[0].element)
self.assertEqual("Cd", cadmiums[1].element)
self.assertTrue(numpy.array_equal(cdse[0].xyz, cadmiums[0].xyz))
self.assertTrue(numpy.array_equal(cdse[1].xyz, cadmiums[1].xyz))
self.assertFalse(cdse[0] is cadmiums[0])
self.assertFalse(cdse.lattice is cadmiums.lattice)
return
def test___isub__(self):
"""Check Structure.__isub__()"""
cdse = Structure(filename=self.cdsefile)
lat = cdse.lattice
lst = cdse.tolist()
cdse -= cdse[2:]
self.assertEqual(2, len(cdse))
self.assertEqual(4, len(lst))
self.assertEqual("Cd", cdse[0].element)
self.assertEqual("Cd", cdse[1].element)
self.assertEqual(lat, cdse.lattice)
self.assertEqual(lst[:2], cdse.tolist())
return
def test___mul__(self):
"""Check Structure.__mul__()"""
cdse = Structure(filename=self.cdsefile)
self.assertEqual(12, len(set(3 * cdse)))
self.assertEqual(12, len(set(cdse * 3)))
cdsex3 = 3 * cdse
self.assertEqual(12, len(cdsex3))
self.assertEqual(3 * "Cd Cd Se Se".split(), [a.element for a in cdsex3])
self.assertTrue(numpy.array_equal(3 * [a.xyz for a in cdse], [a.xyz for a in cdsex3]))
self.assertFalse(set(cdse).intersection(cdsex3))
self.assertFalse(cdse.lattice is cdsex3.lattice)
return
def test___imul__(self):
"""Check Structure.__imul__()"""
cdse = Structure(filename=self.cdsefile)
lat = cdse.lattice
els = cdse.element
xyz = cdse.xyz
lst = cdse.tolist()
cdse *= 2
self.assertEqual(8, len(cdse))
self.assertEqual(lst, cdse[:4].tolist())
self.assertEqual(numpy.tile(els, 2).tolist(), cdse.element.tolist())
self.assertTrue(numpy.array_equal(numpy.tile(xyz, (2, 1)), cdse.xyz))
self.assertEqual(8, len(set(cdse)))
self.assertEqual(8 * [lat], [a.lattice for a in cdse])
self.stru *= -3
self.assertEqual(0, len(self.stru))
return
def test__get_lattice(self):
"""Check Structure._get_lattice()"""
lat = Lattice()
stru = Structure()
self.assertEqual((1, 1, 1, 90, 90, 90), stru.lattice.abcABG())
stru2 = Structure(lattice=lat)
self.assertTrue(lat is stru2.lattice)
return
def test__set_lattice(self):
"""Check Structure._set_lattice()"""
lat = Lattice()
self.stru.lattice = lat
self.assertEqual(2 * [lat], [a.lattice for a in self.stru])
return
def test_composition(self):
"""Check Structure.composition property."""
stru = self.stru
self.assertEqual({"C": 2}, stru.composition)
stru *= 2
self.assertEqual({"C": 4}, stru.composition)
stru[:] = []
self.assertEqual({}, stru.composition)
return
def test_element(self):
"""Check Structure.element."""
stru = self.stru
cdse = self.cdse
self.assertEqual("Cd Cd Se Se".split(), cdse.element.tolist())
self.assertEqual(cdse[:2], cdse[cdse.element == "Cd"])
stru.element = stru.element.replace("C", "Si")
self.assertEqual("Si", stru[0].element)
return
def test_xyz(self):
"""Check Structure.xyz."""
stru = self.stru
self.assertEqual((2, 3), stru.xyz.shape)
self.assertTrue(numpy.array_equal([1, 1, 1], stru.xyz[1]))
stru.xyz += 0.1
self.assertTrue(numpy.array_equal([0.1, 0.1, 0.1], stru[0].xyz))
self.assertTrue(numpy.array_equal([1.1, 1.1, 1.1], stru[1].xyz))
stru.xyz = 0
stru[1].xyz[:] = 1
self.assertTrue(numpy.array_equal([0, 0, 0], stru[0].xyz))
self.assertTrue(numpy.array_equal([1, 1, 1], stru[1].xyz))
# verify noop when changing empty slice
xyz0 = numpy.copy(stru.xyz)
stru[1:1].xyz += 1
self.assertTrue(numpy.array_equal(xyz0, stru.xyz))
return
def test_x(self):
"""Check Structure.x."""
cdse = self.cdse
self.assertEqual((4,), cdse.x.shape)
self.assertAlmostEqual(0.6666, cdse.x[3], 5)
stru = self.stru
stru.x = [3, 4]
self.assertEqual(3, stru[0].xyz[0])
self.assertEqual(4, stru[1].xyz[0])
return
def test_y(self):
"""Check Structure.y."""
cdse = self.cdse
self.assertEqual((4,), cdse.y.shape)
self.assertAlmostEqual(0.3333, cdse.y[3], 5)
stru = self.stru
stru.y = [3, 4]
self.assertEqual(3, stru[0].xyz[1])
self.assertEqual(4, stru[1].xyz[1])
return
def test_z(self):
"""Check Structure.z."""
cdse = self.cdse
self.assertEqual((4,), cdse.z.shape)
self.assertAlmostEqual(0.87667, cdse.z[3], 5)
stru = self.stru
stru.z = [3, 4]
self.assertEqual(3, stru[0].xyz[2])
self.assertEqual(4, stru[1].xyz[2])
return
def test_label(self):
"""Check Structure.label."""
cdse = Structure(filename=self.cdsefile)
self.assertEqual(4 * [""], cdse.label.tolist())
cdse.assignUniqueLabels()
self.assertEqual("Cd1 Cd2 Se1 Se2".split(), cdse.label.tolist())
cdse.label = cdse.label.lower()
self.assertEqual("cd1 cd2 se1 se2".split(), cdse.label.tolist())
return
def test_occupancy(self):
"""Check Structure.occupancy."""
cdse = self.cdse
self.assertTrue(numpy.array_equal(numpy.ones(4), cdse.occupancy))
self.stru.occupancy *= 0.5
self.assertEqual(1.0, sum([a.occupancy for a in self.stru]))
cdse.occupancy = 1
self.assertTrue(all(isinstance(a.occupancy, int) for a in cdse))
return
def test_xyz_cartn(self):
"""Check Structure.xyz_cartn."""
pbte = copy.copy(self.pbte)
self.assertEqual((8, 3), pbte.xyz_cartn.shape)
self.assertTrue(numpy.allclose(6.461 / 2.0 * numpy.ones(3), pbte.xyz_cartn[0]))
pbte.xyz_cartn += numpy.array([0.1, 0.2, 0.3]) * 6.461
self.assertTrue(numpy.allclose([0.6, 0.7, 0.8], pbte[0].xyz))
self.assertTrue(numpy.allclose([0.6, 0.7, 0.3], pbte[7].xyz))
return
def test_anisotropy(self):
"""Check Structure.anisotropy."""
self.assertEqual((2,), self.stru.anisotropy.shape)
self.assertFalse(numpy.any(self.stru.anisotropy))
tei = copy.copy(self.tei)
self.assertTrue(numpy.all(tei.anisotropy))
tei.anisotropy = False
self.assertFalse(numpy.any(tei.anisotropy))
self.assertAlmostEqual(0.019227, tei[0].U11, 6)
self.assertAlmostEqual(0.019227, tei[0].U22, 6)
self.assertAlmostEqual(0.019227, tei[0].U33, 6)
self.assertAlmostEqual(0.0, tei[0].U12, 6)
self.assertAlmostEqual(0.019227 * -numpy.cos(numpy.radians(128.09)), tei[0].U13, 6)
self.assertAlmostEqual(0.0, tei[0].U23, 6)
self.assertAlmostEqual(0.019227, tei[0].Uisoequiv, 6)
return
def test_U(self):
"""Check Structure.U."""
stru = self.stru
self.assertEqual((2, 3, 3), stru.U.shape)
self.assertFalse(numpy.any(stru.anisotropy))
stru.U = numpy.identity(3)
self.assertEqual(2, len(set([id(a.U) for a in stru])))
isou = stru.lattice.isotropicunit
self.assertTrue(numpy.array_equal(2 * [isou], stru.U))
self.assertFalse(numpy.any(stru.anisotropy))
stru.anisotropy = True
stru.U = numpy.identity(3)
self.assertTrue(numpy.array_equal(2 * [numpy.identity(3)], stru.U))
self.assertTrue(numpy.all(stru.anisotropy))
stru.U = 0
self.assertTrue(numpy.all(stru.anisotropy))
self.assertFalse(numpy.any(stru.U != 0.0))
stru[1].U[:] = 1
self.assertTrue(numpy.all(stru[0].U == 0.0))
self.assertTrue(numpy.all(stru[1].U == 1.0))
return
def test_Uisoequiv(self):
"""Check Structure.Uisoequiv."""
tei = copy.copy(self.tei)
self.assertEqual((16,), tei.Uisoequiv.shape)
self.assertAlmostEqual(0.019227, tei.Uisoequiv[0], 6)
self.assertAlmostEqual(0.019784, tei.Uisoequiv[4], 6)
self.assertAlmostEqual(0.024813, tei.Uisoequiv[8], 6)
self.assertAlmostEqual(0.026878, tei.Uisoequiv[12], 6)
u11old = tei[0].U11
tei.Uisoequiv = 0.001
self.assertAlmostEqual(u11old * 0.001 / 0.019227, tei[0].U[0, 0])
return
def test_Uij(self):
"""Check Structure.Uij."""
stru = self.stru
stru[1].anisotropy = True
stru[1].U = [[1.1, 0.12, 0.13], [0.12, 2.2, 0.23], [0.13, 0.23, 3.3]]
self.assertTrue(numpy.array_equal([0, 1.1], stru.U11))
self.assertTrue(numpy.array_equal([0, 2.2], stru.U22))
self.assertTrue(numpy.array_equal([0, 3.3], stru.U33))
self.assertTrue(numpy.array_equal([0, 0.12], stru.U12))
self.assertTrue(numpy.array_equal([0, 0.13], stru.U13))
self.assertTrue(numpy.array_equal([0, 0.23], stru.U23))
stru.U11 = stru.U22 = stru.U33 = stru.U12 = stru.U13 = stru.U23 = 0.0
self.assertFalse(numpy.any(stru.U != 0.0))
return
def test_Bisoequiv(self):
"""Check Structure.Bisoequiv."""
utob = 8 * numpy.pi**2
tei = copy.copy(self.tei)
self.assertEqual((16,), tei.Bisoequiv.shape)
self.assertAlmostEqual(utob * 0.019227, tei.Bisoequiv[0], 4)
self.assertAlmostEqual(utob * 0.019784, tei.Bisoequiv[4], 4)
self.assertAlmostEqual(utob * 0.024813, tei.Bisoequiv[8], 4)
self.assertAlmostEqual(utob * 0.026878, tei.Bisoequiv[12], 4)
b11old = tei[0].B11
tei.Bisoequiv = 0.1
self.assertAlmostEqual(b11old * 0.1 / (utob * 0.019227), tei[0].B11, 5)
return
def test_Bij(self):
"""Check Structure.Bij."""
stru = self.stru
stru[1].anisotropy = True
stru[1].U = [[1.1, 0.12, 0.13], [0.12, 2.2, 0.23], [0.13, 0.23, 3.3]]
stru[1].U /= 8 * numpy.pi**2
self.assertTrue(numpy.allclose([0, 1.1], stru.B11))
self.assertTrue(numpy.allclose([0, 2.2], stru.B22))
self.assertTrue(numpy.allclose([0, 3.3], stru.B33))
self.assertTrue(numpy.allclose([0, 0.12], stru.B12))
self.assertTrue(numpy.allclose([0, 0.13], stru.B13))
self.assertTrue(numpy.allclose([0, 0.23], stru.B23))
stru.B11 = stru.B22 = stru.B33 = stru.B12 = stru.B13 = stru.B23 = 0.0
self.assertFalse(numpy.any(stru.U != 0.0))
return
def test_pickling(self):
"""Make sure Atom in Structure can be consistently pickled."""
stru = self.stru
a = stru[0]
self.assertTrue(a is stru[0])
a1, stru1 = pickle.loads(pickle.dumps((a, stru)))
self.assertTrue(a1 is stru1[0])
return
# End of class TestStructure
# ----------------------------------------------------------------------------
@pytest.mark.parametrize(
"existing, atype, xyz, expected_len, expected_element, expected_xyz",
[
# Case 1: valid atom added to an empty structure.
# Expect the atom list length to go from 0 to 1.
# Expect the atom attributes are successfully loaded.
(
None,
"C",
[0.1, 0.2, 0.3],
1,
"C",
[0.1, 0.2, 0.3],
),
# Case 2: valid atom added to existing atom list.
# Expect the atom list length to go from 1 to 2.
# Expect the atom attributes are successfully loaded.
(
[Atom("C", [0, 0, 0])],
"Ni",
[0.8, 1.2, 0.9],
2,
"Ni",
[0.8, 1.2, 0.9],
),
],
)
def test_add_new_atom(existing, atype, xyz, expected_len, expected_element, expected_xyz):
"""Check Structure.add_new_item()"""
structure = Structure(existing, lattice=Lattice())
structure.add_new_atom(atype=atype, xyz=xyz)
actual_length = len(structure)
assert expected_len == actual_length
atom_object = structure[-1]
assert atom_object.element == expected_element
assert numpy.allclose(atom_object.xyz, expected_xyz)
def test_add_new_atom_duplicate():
# Case 3: duplicated atom added to the existing atom list.
# Expect the atom to be added and gives a UserWarning.
structure = Structure(
[Atom("C", [0.1, 0.2, 0.3]), Atom("Ni", [0.8, 1.2, 0.9])],
lattice=Lattice(),
)
with pytest.warns(UserWarning):
structure.add_new_atom(atype="C", xyz=[0.1, 0.2, 0.3])
assert len(structure) == 3
assert structure[-1].element == "C"
assert numpy.allclose(structure[-1].xyz, [0.1, 0.2, 0.3])
if __name__ == "__main__":
unittest.main()