Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_structure.py
More file actions
956 lines (865 loc) · 34.5 KB
/
Copy pathtest_structure.py
File metadata and controls
956 lines (865 loc) · 34.5 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
#!/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 as np
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(np.array_equal(pbte.xyz_cartn, pbte2.xyz_cartn))
self.assertTrue(np.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.write_structure("pdffit")
cdse2 = copy.copy(cdse)
self.assertEqual(cdse_str, cdse2.write_structure("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 np.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 np.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 np.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_assign_unique_labels(self):
"""Check Structure.assign_unique_labels()"""
self.assertEqual("", "".join([a.label for a in self.stru]))
self.stru.assign_unique_labels()
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.assign_unique_labels()
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.assign_unique_labels()
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(np.allclose(a0.xyz, [0.0, 0.0, 0.0]))
a1 = stru[1]
self.assertTrue(np.allclose(a1.xyz, [2.0, 0.0, 2.0]))
def test_place_in_lattice(self):
"""Check Structure.placeInLattice() -- conversion of
coordinates."""
stru = self.stru
new_lattice = Lattice(0.5, 0.5, 0.5, 90, 90, 60)
stru.place_in_lattice(new_lattice)
a0 = stru[0]
self.assertTrue(np.allclose(a0.xyz, [0.0, 0.0, 0.0]))
a1 = stru[1]
self.assertTrue(np.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(np.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(np.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 = np.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.assign_unique_labels()
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(np.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(np.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(np.array_equal(stru[0].xyz, ta0.xyz))
self.assertEqual("Se", tam1.element)
self.assertTrue(np.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(np.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(np.array_equal(cdse[0].xyz, cadmiums[0].xyz))
self.assertTrue(np.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(np.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(np.tile(els, 2).tolist(), cdse.element.tolist())
self.assertTrue(np.array_equal(np.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_dep(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__get_lattice(self):
"""Check Structure._get_lattice()"""
lat = Lattice()
stru = Structure()
self.assertEqual((1, 1, 1, 90, 90, 90), stru.lattice.cell_parms())
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(np.array_equal([1, 1, 1], stru.xyz[1]))
stru.xyz += 0.1
self.assertTrue(np.array_equal([0.1, 0.1, 0.1], stru[0].xyz))
self.assertTrue(np.array_equal([1.1, 1.1, 1.1], stru[1].xyz))
stru.xyz = 0
stru[1].xyz[:] = 1
self.assertTrue(np.array_equal([0, 0, 0], stru[0].xyz))
self.assertTrue(np.array_equal([1, 1, 1], stru[1].xyz))
# verify noop when changing empty slice
xyz0 = np.copy(stru.xyz)
stru[1:1].xyz += 1
self.assertTrue(np.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.assign_unique_labels()
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(np.array_equal(np.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(np.allclose(6.461 / 2.0 * np.ones(3), pbte.xyz_cartn[0]))
pbte.xyz_cartn += np.array([0.1, 0.2, 0.3]) * 6.461
self.assertTrue(np.allclose([0.6, 0.7, 0.8], pbte[0].xyz))
self.assertTrue(np.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(np.any(self.stru.anisotropy))
tei = copy.copy(self.tei)
self.assertTrue(np.all(tei.anisotropy))
tei.anisotropy = False
self.assertFalse(np.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 * -np.cos(np.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(np.any(stru.anisotropy))
stru.U = np.identity(3)
self.assertEqual(2, len(set([id(a.U) for a in stru])))
isou = stru.lattice.isotropicunit
self.assertTrue(np.array_equal(2 * [isou], stru.U))
self.assertFalse(np.any(stru.anisotropy))
stru.anisotropy = True
stru.U = np.identity(3)
self.assertTrue(np.array_equal(2 * [np.identity(3)], stru.U))
self.assertTrue(np.all(stru.anisotropy))
stru.U = 0
self.assertTrue(np.all(stru.anisotropy))
self.assertFalse(np.any(stru.U != 0.0))
stru[1].U[:] = 1
self.assertTrue(np.all(stru[0].U == 0.0))
self.assertTrue(np.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(np.array_equal([0, 1.1], stru.U11))
self.assertTrue(np.array_equal([0, 2.2], stru.U22))
self.assertTrue(np.array_equal([0, 3.3], stru.U33))
self.assertTrue(np.array_equal([0, 0.12], stru.U12))
self.assertTrue(np.array_equal([0, 0.13], stru.U13))
self.assertTrue(np.array_equal([0, 0.23], stru.U23))
stru.U11 = stru.U22 = stru.U33 = stru.U12 = stru.U13 = stru.U23 = 0.0
self.assertFalse(np.any(stru.U != 0.0))
return
def test_Bisoequiv(self):
"""Check Structure.Bisoequiv."""
utob = 8 * np.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 * np.pi**2
self.assertTrue(np.allclose([0, 1.1], stru.B11))
self.assertTrue(np.allclose([0, 2.2], stru.B22))
self.assertTrue(np.allclose([0, 3.3], stru.B33))
self.assertTrue(np.allclose([0, 0.12], stru.B12))
self.assertTrue(np.allclose([0, 0.13], stru.B13))
self.assertTrue(np.allclose([0, 0.23], stru.B23))
stru.B11 = stru.B22 = stru.B33 = stru.B12 = stru.B13 = stru.B23 = 0.0
self.assertFalse(np.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
def test_get_chemical_symbols(datafile):
"""Check Structure.get_chemical_symbols()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_chemical_symbols = pbte_stru.get_chemical_symbols()
expected_chemical_symbols = ["Pb"] * 4 + ["Te"] * 4
assert actual_chemical_symbols == expected_chemical_symbols
def test_get_fractional_coordinates(datafile):
"""Check Structure.get_fractional_coordinates()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_fractional_coords = pbte_stru.get_fractional_coordinates()
expected_fractional_coords = np.array(
[
[0.5, 0.5, 0.5],
[0.5, 0.0, 0.0],
[0.0, 0.5, 0.0],
[0.0, 0.0, 0.5],
[0.0, 0.0, 0.0],
[0.0, 0.5, 0.5],
[0.5, 0.0, 0.5],
[0.5, 0.5, 0.0],
]
)
assert np.allclose(actual_fractional_coords, expected_fractional_coords)
def test_get_cartesian_coordinates(datafile):
"""Check Structure.get_cartesian_coordinates()"""
cdse_stru = Structure(filename=datafile("CdSe_bulk.stru"))
actual_cartesian_coords = cdse_stru.get_cartesian_coordinates()
expected_cartesian_coords = np.array(
[
[1.22284264, 2.11760202, 0.0],
[2.44495161, 0.0, 3.4530135],
[1.22284264, 2.11760202, 2.60129319],
[2.44495161, 0.0, 6.05430669],
]
)
assert np.allclose(actual_cartesian_coords, expected_cartesian_coords, atol=1e-6)
@pytest.mark.parametrize(
"input,expected",
[ # case: user wants ADPs as an array so they set return_array=True
# expected: a 3D array of shape (num_atoms, 3, 3) with the Uij values
[
True,
np.array(
[
[[0.0211, 0.0, 0.0109], [0.0, 0.0195, 0.0], [0.0109, 0.0, 0.016]],
[[0.0223, 0.0, 0.0179], [0.0, 0.018, 0.0], [0.0179, 0.0, 0.0254]],
[[0.025, 0.0, 0.0226], [0.0, 0.0234, 0.0], [0.0226, 0.0, 0.0345]],
[[0.0234, 0.0, 0.0138], [0.0, 0.0295, 0.0], [0.0138, 0.0, 0.0253]],
]
),
],
# case: user wants ADPs as a dictionary so they set return_array=False (default behavior)
# expected: a dictionary with keys like "I_8_11" and values as the corresponding Uij values
[
False,
{
# Iodine
"I_8_11": 0.025,
"I_8_12": 0.0,
"I_8_13": 0.0226,
"I_8_22": 0.0234,
"I_8_23": 0.0,
"I_8_33": 0.0345,
"I_9_11": 0.025,
"I_9_12": 0.0,
"I_9_13": 0.0226,
"I_9_22": 0.0234,
"I_9_23": 0.0,
"I_9_33": 0.0345,
"I_10_11": 0.025,
"I_10_12": 0.0,
"I_10_13": 0.0226,
"I_10_22": 0.0234,
"I_10_23": 0.0,
"I_10_33": 0.0345,
"I_11_11": 0.025,
"I_11_12": 0.0,
"I_11_13": 0.0226,
"I_11_22": 0.0234,
"I_11_23": 0.0,
"I_11_33": 0.0345,
"I_12_11": 0.0234,
"I_12_12": 0.0,
"I_12_13": 0.0138,
"I_12_22": 0.0295,
"I_12_23": 0.0,
"I_12_33": 0.0253,
"I_13_11": 0.0234,
"I_13_12": 0.0,
"I_13_13": 0.0138,
"I_13_22": 0.0295,
"I_13_23": 0.0,
"I_13_33": 0.0253,
"I_14_11": 0.0234,
"I_14_12": 0.0,
"I_14_13": 0.0138,
"I_14_22": 0.0295,
"I_14_23": 0.0,
"I_14_33": 0.0253,
"I_15_11": 0.0234,
"I_15_12": 0.0,
"I_15_13": 0.0138,
"I_15_22": 0.0295,
"I_15_23": 0.0,
"I_15_33": 0.0253,
# Tellurium
"Te_0_11": 0.0211,
"Te_0_12": 0.0,
"Te_0_13": 0.0109,
"Te_0_22": 0.0195,
"Te_0_23": 0.0,
"Te_0_33": 0.016,
"Te_1_11": 0.0211,
"Te_1_12": 0.0,
"Te_1_13": 0.0109,
"Te_1_22": 0.0195,
"Te_1_23": 0.0,
"Te_1_33": 0.016,
"Te_2_11": 0.0211,
"Te_2_12": 0.0,
"Te_2_13": 0.0109,
"Te_2_22": 0.0195,
"Te_2_23": 0.0,
"Te_2_33": 0.016,
"Te_3_11": 0.0211,
"Te_3_12": 0.0,
"Te_3_13": 0.0109,
"Te_3_22": 0.0195,
"Te_3_23": 0.0,
"Te_3_33": 0.016,
"Te_4_11": 0.0223,
"Te_4_12": 0.0,
"Te_4_13": 0.0179,
"Te_4_22": 0.018,
"Te_4_23": 0.0,
"Te_4_33": 0.0254,
"Te_5_11": 0.0223,
"Te_5_12": 0.0,
"Te_5_13": 0.0179,
"Te_5_22": 0.018,
"Te_5_23": 0.0,
"Te_5_33": 0.0254,
"Te_6_11": 0.0223,
"Te_6_12": 0.0,
"Te_6_13": 0.0179,
"Te_6_22": 0.018,
"Te_6_23": 0.0,
"Te_6_33": 0.0254,
"Te_7_11": 0.0223,
"Te_7_12": 0.0,
"Te_7_13": 0.0179,
"Te_7_22": 0.018,
"Te_7_23": 0.0,
"Te_7_33": 0.0254,
},
],
],
)
def test_get_anisotropic_displacement_parameters(datafile, input, expected):
"""Check Structure.get_anisotropic_displacement_parameters()"""
tei_stru = Structure(filename=datafile("TeI.cif"))
actual_displacement = tei_stru.get_anisotropic_displacement_parameters(return_array=input)
if isinstance(expected, np.ndarray):
expected_displacement = np.repeat(expected, 4, axis=0)
assert np.allclose(actual_displacement, expected_displacement)
if isinstance(expected, dict):
assert actual_displacement == expected
@pytest.mark.parametrize(
"input,expected",
[ # case: user wants isotropic displacement parameters as an array
# so they set return_array=True
# expected: a 1D array of shape (num_atoms,) with the Uiso values
[
True,
np.array([0.0225566, 0.0225566, 0.0225566, 0.0225566, 0.0155528, 0.0155528, 0.0155528, 0.0155528]),
],
# case: user wants isotropic displacement parameters as a dictionary
# so they set return_array=False (default behavior)
# expected: a dictionary with keys like "I_Uiso" and values as the corresponding Uiso values
[
False,
{
"Pb_1_Uiso": 0.0225566,
"Pb_2_Uiso": 0.0225566,
"Pb_3_Uiso": 0.0225566,
"Pb_4_Uiso": 0.0225566,
"Te_5_Uiso": 0.0155528,
"Te_6_Uiso": 0.0155528,
"Te_7_Uiso": 0.0155528,
"Te_8_Uiso": 0.0155528,
},
],
],
)
def test_get_isotropic_displacement_parameters(datafile, input, expected):
"""Check Structure.get_isotropic_displacement_parameters()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_isotropic_displacement = pbte_stru.get_isotropic_displacement_parameters(return_array=input)
if isinstance(expected, np.ndarray):
assert np.allclose(actual_isotropic_displacement, expected)
if isinstance(expected, dict):
assert actual_isotropic_displacement == expected
def test_get_occupancies(datafile):
"""Check Structure.get_occupancies()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_occupancies = pbte_stru.get_occupancies()
expected_occupancies = np.ones(8)
assert np.allclose(actual_occupancies, expected_occupancies)
def test_get_lattice_vectors(datafile):
"""Check Structure.get_lattice_vectors()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_lattice_vectors = pbte_stru.get_lattice_vectors()
expected_lattice_vectors = np.array([[6.461, 0.0, 0.0], [0.0, 6.461, 0.0], [0.0, 0.0, 6.461]])
assert np.allclose(actual_lattice_vectors, expected_lattice_vectors)
def test_get_lattice_vector_angles(datafile):
"""Check Structure.get_lattice_vector_angles()"""
pbte_stru = Structure(filename=datafile("PbTe.cif"))
actual_lattice_vector_angles = pbte_stru.get_lattice_vector_angles()
expected_lattice_vector_angles = np.array([90.0, 90.0, 90.0])
assert np.allclose(actual_lattice_vector_angles, expected_lattice_vector_angles)
# ----------------------------------------------------------------------------
@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 np.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 np.allclose(structure[-1].xyz, [0.1, 0.2, 0.3])
if __name__ == "__main__":
unittest.main()
You can’t perform that action at this time.
