This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
/
regtests.py
2952 lines (2571 loc) · 105 KB
/
regtests.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
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Defines a set of regressions tests that should be run succesfully before
anything is pushed to the central repository.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest
import sys
import numpy as np
from numpy.random import RandomState
from ase import Atoms
from ase.build import bcc100, molecule
from ase.visualize import view
import ase.build
from ase.build import nanotube
import ase.lattice.hexagonal
from ase.lattice.compounds import Zincblende
from ase.lattice.cubic import SimpleCubicFactory
import ase.io
import json
from systax import Classifier
from systax import PeriodicFinder
from systax.classifications import \
Class0D, \
Class1D, \
Class2D, \
Class3D, \
Atom, \
Molecule, \
Crystal, \
Material1D, \
Material2D, \
Unknown, \
Surface
from systax import Class3DAnalyzer
from systax.data.constants import WYCKOFF_LETTER_POSITIONS
import systax.geometry
from networkx import draw_networkx
import matplotlib.pyplot as mpl
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def get_atoms_from_viz(filename):
"""Used to construct an ase.Atoms from a custom visualization file.
"""
with open(filename, "r") as fin:
data = json.load(fin)
pos = data["positions"]
cell = data["normalizedCell"]
num = data["labels"]
atoms = Atoms(
scaled_positions=pos,
cell=1e10*np.array(cell),
symbols=num,
pbc=True
)
return atoms
def get_atoms_from_arch(filename):
"""Used to construct an ase.Atoms from a NOMAD Archive file.
"""
with open(filename, "r") as fin:
data = json.load(fin)
section_system = data["sections"]["section_run-0"]["sections"]["section_system-0"]
atoms = Atoms(
positions=1e10*np.array(section_system["atom_positions"]),
cell=1e10*np.array(section_system["simulation_cell"]),
symbols=section_system["atom_labels"],
pbc=True,
)
return atoms
class ExceptionTests(unittest.TestCase):
"""Tests for exceptions that arise from invalid arguments.
"""
def test_too_many_atoms(self):
system = bcc100('Fe', size=(11, 10, 10), vacuum=8)
classifier = Classifier()
with self.assertRaises(ValueError):
classifier.classify(system)
class GeometryTests(unittest.TestCase):
"""Tests for the geometry module.
"""
def test_thickness(self):
"""Getting the thickness of structures.
"""
sys = molecule("H2O")
thickness_x = systax.geometry.get_thickness(sys, 0)
self.assertEqual(thickness_x, 0)
thickness_y = systax.geometry.get_thickness(sys, 1)
self.assertEqual(thickness_y, 1.526478)
thickness_z = systax.geometry.get_thickness(sys, 2)
self.assertEqual(thickness_z, 0.596309)
def test_minimize_cell(self):
"""Cell minimization.
"""
sys = molecule("H2O")
sys.set_cell([3, 3, 3])
# Minimize with minimum size smaller than found minimum size
minimized_system = systax.geometry.get_minimized_cell(sys, 2, 0.1)
cell = minimized_system.get_cell()
pos = minimized_system.get_scaled_positions()
expected_cell = np.array([
[3., 0., 0.],
[0., 3., 0.],
[0., 0., 0.596309]
])
expected_pos = np.array([
[0., 0., 1.],
[0., 0.254413, 0.],
[0., -0.254413, 0.]
])
self.assertTrue(np.allclose(expected_cell, cell, atol=0.001, rtol=0))
self.assertTrue(np.allclose(expected_pos, pos, atol=0.001, rtol=0))
# Minimize with minimum size larger than found minimum size
minimized_system = systax.geometry.get_minimized_cell(sys, 2, 2)
cell = minimized_system.get_cell()
pos = minimized_system.get_scaled_positions()
expected_cell = np.array([
[3., 0., 0.],
[0., 3., 0.],
[0., 0., 2.]
])
expected_pos = np.array([
[0., 0., 0.64907725],
[0., 0.254413, 0.35092275],
[0., -0.254413, 0.35092275]
])
self.assertTrue(np.allclose(expected_cell, cell, atol=0.001, rtol=0))
self.assertTrue(np.allclose(expected_pos, pos, atol=0.001, rtol=0))
def test_center_of_mass(self):
"""Tests that the center of mass correctly takes periodicity into
account.
"""
system = bcc100('Fe', size=(3, 3, 4), vacuum=8)
adsorbate = ase.Atom(position=[4, 4, 4], symbol="H")
system += adsorbate
system.set_pbc([True, True, True])
system.translate([0, 0, 10])
system.wrap()
# view(system)
# Test periodic COM
cm = systax.geometry.get_center_of_mass(system)
self.assertTrue(np.allclose(cm, [4., 4., 20.15], atol=0.1))
# Test finite COM
system.set_pbc(False)
cm = systax.geometry.get_center_of_mass(system)
self.assertTrue(np.allclose(cm, [3.58770672, 3.58770672, 10.00200455], atol=0.1))
def test_matches_non_orthogonal(self):
"""Test that the correct factor is returned when finding matches that
are in the neighbouring cells.
"""
system = ase.build.mx2(
formula="MoS2",
kind="2H",
a=3.18,
thickness=3.19,
size=(5, 5, 1),
vacuum=8)
system.set_pbc(True)
system = system[[0, 12]]
# view(system)
searched_pos = system.get_positions()[0][None, :]
basis = np.array([[1.59, -2.75396078, 0]])
searched_pos += basis
matches, subst, vac, factors = systax.geometry.get_matches(
system,
searched_pos,
numbers=[system.get_atomic_numbers()[0]],
tolerances=np.array([0.2])
)
# Make sure that the atom is found in the correct copy
self.assertEqual(tuple(factors[0]), (0, -1, 0))
# Make sure that the correct atom is found
self.assertTrue(np.array_equal(matches, [1]))
def test_displacement_non_orthogonal(self):
"""Test that the correct displacement is returned when the cell in
non-orthorhombic.
"""
positions = np.array([
[1.56909, 2.71871, 6.45326],
[3.9248, 4.07536, 6.45326]
])
cell = np.array([
[4.7077, -2.718, 0.],
[0., 8.15225, 0.],
[0., 0., 50.]
])
# Fully periodic with minimum image convention
dist_mat = systax.geometry.get_distance_matrix(
positions[0, :],
positions[1, :],
cell,
pbc=True,
mic=True)
# The minimum image should be within the same cell
expected = np.linalg.norm(positions[0, :] - positions[1, :])
self.assertTrue(np.allclose(dist_mat[0], expected))
def test_distance_matrix(self):
pos1 = np.array([
[0, 0, 0],
])
pos2 = np.array([
[0, 0, 7],
[6, 0, 0],
])
cell = np.array([
[7, 0, 0],
[0, 7, 0],
[0, 0, 7]
])
# Non-periodic
dist_mat = systax.geometry.get_distance_matrix(pos1, pos2)
expected = np.array(
[[7, 6]]
)
self.assertTrue(np.allclose(dist_mat, expected))
# Fully periodic with minimum image convention
dist_mat = systax.geometry.get_distance_matrix(pos1, pos2, cell, pbc=True, mic=True)
expected = np.array(
[[0, 1]]
)
self.assertTrue(np.allclose(dist_mat, expected))
# Partly periodic with minimum image convention
dist_mat = systax.geometry.get_distance_matrix(pos1, pos2, cell, pbc=[False, True, True], mic=True)
expected = np.array(
[[0, 6]]
)
self.assertTrue(np.allclose(dist_mat, expected))
def test_displacement_tensor(self):
# Non-periodic
cell = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
])
pos1 = np.array([
[0, 0, 0],
])
pos2 = np.array([
[1, 1, 1],
[0.9, 0, 0],
])
disp_tensor = systax.geometry.get_displacement_tensor(pos1, pos2)
expected = np.array(-pos2)
self.assertTrue(np.allclose(disp_tensor, expected))
# Fully periodic
disp_tensor = systax.geometry.get_displacement_tensor(pos1, pos2, pbc=True, cell=cell, mic=True)
expected = np.array([[
[0, 0, 0],
[0.1, 0, 0],
]])
self.assertTrue(np.allclose(disp_tensor, expected))
# Fully periodic, reversed direction
disp_tensor = systax.geometry.get_displacement_tensor(pos2, pos1, pbc=True, cell=cell, mic=True)
expected = np.array([[
[0, 0, 0],
], [
[-0.1, 0, 0],
]])
self.assertTrue(np.allclose(disp_tensor, expected))
# Periodic in one direction
disp_tensor = systax.geometry.get_displacement_tensor(pos1, pos2, pbc=[True, False, False], cell=cell, mic=True)
expected = np.array([[
[0, -1, -1],
[0.1, 0, 0],
]])
self.assertTrue(np.allclose(disp_tensor, expected))
def test_to_cartesian(self):
# Inside, unwrapped
cell = np.array([
[1, 1, 0],
[0, 2, 0],
[1, 0, 1]
])
rel_pos = np.array([
[0, 0, 0],
[1, 1, 1],
[0.5, 0.5, 0.5],
])
expected_pos = np.array([
[0, 0, 0],
[2, 3, 1],
[1, 1.5, 0.5],
])
cart_pos = systax.geometry.to_cartesian(cell, rel_pos)
self.assertTrue(np.allclose(cart_pos, expected_pos))
# Outside, unwrapped
cell = np.array([
[1, 1, 0],
[0, 2, 0],
[1, 0, 1]
])
rel_pos = np.array([
[0, 0, 0],
[2, 2, 2],
[0.5, 1.5, 0.5],
])
expected_pos = np.array([
[0, 0, 0],
[4, 6, 2],
[1, 3.5, 0.5],
])
cart_pos = systax.geometry.to_cartesian(cell, rel_pos)
self.assertTrue(np.allclose(cart_pos, expected_pos))
# Outside, wrapped
cell = np.array([
[1, 1, 0],
[0, 2, 0],
[1, 0, 1]
])
rel_pos = np.array([
[0, 0, 0],
[2, 2, 2],
[0.5, 1.5, 0.5],
])
expected_pos = np.array([
[0, 0, 0],
[0, 0, 0],
[1, 1.5, 0.5],
])
cart_pos = systax.geometry.to_cartesian(cell, rel_pos, wrap=True, pbc=True)
self.assertTrue(np.allclose(cart_pos, expected_pos))
class DimensionalityTests(unittest.TestCase):
"""Unit tests for finding the dimensionality of different systems.
"""
# Read the defaults
classifier = Classifier()
cluster_threshold = classifier.cluster_threshold
# 0D system
sys0d = molecule("H2O")
sys0d.set_pbc([False, False, False])
sys0d.set_cell([3, 3, 3])
sys0d.center()
# 1D system
sys1d = nanotube(3, 3, length=6, bond=1.4, symbol='Si')
sys1d.set_pbc([True, True, True])
sys1d.set_cell((10, 10, 15))
sys1d.center()
# 2D system
sys2d = Atoms(
symbols=[6, 6],
cell=np.array((
[2.4595121467478055, 0.0, 0.0],
[-1.2297560733739028, 2.13, 0.0],
[0.0, 0.0, 10.0]
)),
scaled_positions=np.array((
[0.3333333333333333, 0.6666666666666666, 0.5],
[0.6666666666666667, 0.33333333333333337, 0.5]
)),
)
sys2d = sys2d.repeat((3, 3, 1))
# 3D system
sys3d = ase.lattice.cubic.Diamond(
size=(1, 1, 1),
symbol='Si',
pbc=True,
latticeconstant=5.430710)
def test_0d_n_pbc0(self):
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys0d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 0)
def test_0d_n_pbc3(self):
DimensionalityTests.sys1d.set_pbc([True, True, True])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys0d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 0)
def test_1d_n_pbc3(self):
DimensionalityTests.sys1d.set_pbc([True, True, True])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys1d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 1)
def test_1d_n_pbc2(self):
DimensionalityTests.sys1d.set_pbc([False, True, True])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys1d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 1)
def test_1d_n_pbc1(self):
DimensionalityTests.sys1d.set_pbc([False, False, True])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys1d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 1)
def test_2d_n_pbc3(self):
DimensionalityTests.sys2d.set_pbc([True, True, True])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys2d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 2)
def test_2d_n_pbc2(self):
DimensionalityTests.sys2d.set_pbc([True, True, False])
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys2d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 2)
def test_3d_n_pbc3(self):
dimensionality = systax.geometry.get_dimensionality(
DimensionalityTests.sys3d,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 3)
def test_non_orthogonal_crystal(self):
"""Test a system that has a non-orthogonal cell.
"""
system = get_atoms_from_arch("./structures/PSX9X4dQR2r1cjQ9kBtuC-wI6MO8B.json")
dimensionality = systax.geometry.get_dimensionality(
system,
DimensionalityTests.cluster_threshold
)
self.assertEqual(dimensionality, 3)
def test_surface_split(self):
"""Test a surface that has been split by the cell boundary
"""
system = bcc100('Fe', size=(5, 1, 3), vacuum=8)
system.translate([0, 0, 9])
system.set_pbc(True)
system.wrap(pbc=True)
dimensionality = systax.geometry.get_dimensionality(
system,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 2)
def test_surface_wavy(self):
"""Test a surface with a high amplitude wave. This would break a
regular linear vacuum gap search.
"""
system = bcc100('Fe', size=(15, 3, 3), vacuum=8)
pos = system.get_positions()
x_len = np.linalg.norm(system.get_cell()[0, :])
x = pos[:, 0]
z = pos[:, 2]
z_new = z + 3*np.sin(4*(x/x_len)*np.pi)
pos_new = np.array(pos)
pos_new[:, 2] = z_new
system.set_positions(pos_new)
system.set_pbc(True)
# view(system)
dimensionality = systax.geometry.get_dimensionality(
system,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 2)
def test_graphite(self):
system = ase.lattice.hexagonal.Graphite(
size=(1, 1, 1),
symbol='C',
pbc=True,
latticeconstant=(2.461, 6.708))
# view(system)
dimensionality = systax.geometry.get_dimensionality(
system,
DimensionalityTests.cluster_threshold)
self.assertEqual(dimensionality, 3)
class PeriodicFinderTests(unittest.TestCase):
"""Unit tests for the class that is used to find periodic regions.
"""
classifier = Classifier()
max_cell_size = classifier.max_cell_size
angle_tol = classifier.angle_tol
delaunay_threshold = classifier.delaunay_threshold
bond_threshold = classifier.bond_threshold
pos_tol = classifier.pos_tol
pos_tol_scaling = classifier.pos_tol_scaling
cell_size_tol = classifier.cell_size_tol
def test_cell_selection(self):
"""Testing that the correct cell is selected.
"""
# 3D: Selecting orthogonal from two options with same volume
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 2, 1],
])
metrics = np.array([0, 0, 0, 0])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 1, 2])))
# 3D: Selecting the non-orthogonal because another combination has higer
# periodicity
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 2, 1],
])
metrics = np.array([2, 2, 1, 2])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 1, 3])))
# 3D: Selecting first by volume, then by orthogonality.
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, 0.5, 0.5],
])
metrics = np.array([0, 0, 0, 0])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 1, 3])))
# 2D: Selecting orthogonal from two options with same volume
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[1, 1, 0],
])
metrics = np.array([0, 0, 0])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 1])))
# 2D: Selecting the non-orthogonal because another combination has higer
# periodicity
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[1, 2, 0],
])
metrics = np.array([2, 1, 2])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 2])))
# 2D: Selecting first by area, then by orthogonality.
spans = np.array([
[1, 0, 0],
[0, 1, 0],
[0, 0.5, 0],
])
metrics = np.array([0, 0, 0])
finder = PeriodicFinder()
indices = finder._find_best_basis(spans, metrics)
self.assertTrue(np.array_equal(indices, np.array([0, 2])))
# def test_proto_cell_in_curved(self):
# """Tests that the relative positions in the prototype cell are found
# robustly even in distorted cells.
# """
# # Create an Fe 100 surface as an ASE Atoms object
# class NaClFactory(SimpleCubicFactory):
# "A factory for creating NaCl (B1, Rocksalt) lattices."
# bravais_basis = [[0, 0, 0], [0, 0, 0.5], [0, 0.5, 0], [0, 0.5, 0.5],
# [0.5, 0, 0], [0.5, 0, 0.5], [0.5, 0.5, 0],
# [0.5, 0.5, 0.5]]
# element_basis = (0, 1, 1, 0, 1, 0, 0, 1)
# system = NaClFactory()
# system = system(symbol=["Na", "Cl"], latticeconstant=5.64)
# system = system.repeat((4, 4, 1))
# cell = system.get_cell()
# cell[2, :] *= 3
# system.set_cell(cell)
# system.center()
# # Bulge the surface
# cell_width = np.linalg.norm(system.get_cell()[0, :])
# for atom in system:
# pos = atom.position
# distortion_z = 0.6*np.cos(pos[0]/cell_width*2.0*np.pi)
# pos += np.array((0, 0, distortion_z))
# # view(system)
# # Classified as surface
# classifier = Classifier()
# classification = classifier.classify(system)
# self.assertIsInstance(classification, Surface)
# # No defects or unknown atoms
# adsorbates = classification.adsorbates
# interstitials = classification.interstitials
# substitutions = classification.substitutions
# vacancies = classification.vacancies
# self.assertEqual(len(interstitials), 0)
# self.assertEqual(len(substitutions), 0)
# self.assertEqual(len(vacancies), 0)
# self.assertEqual(len(adsorbates), 0)
# # Test that the relative positions are robust in the prototype cell
# proto_cell = classification.region.cell
# # view(proto_cell)
# relative_pos = proto_cell.get_scaled_positions()
# assumed_pos = np.array([
# [0.5, 0.0, 0.5],
# [0, 0, 0],
# ])
# self.assertTrue(np.allclose(relative_pos, assumed_pos, atol=0.1))
# def test_cell_2d_adsorbate(self):
# """Test that the cell is correctly identified even if adsorbates are
# near.
# """
# system = ase.build.mx2(
# formula="MoS2",
# kind="2H",
# a=3.18,
# thickness=3.19,
# size=(5, 5, 1),
# vacuum=8)
# system.set_pbc(True)
# ads = molecule("C6H6")
# ads.translate([4.9, 5.5, 13])
# system += ads
# # view(system)
# classifier = Classifier()
# classification = classifier.classify(system)
# self.assertIsInstance(classification, Material2D)
# # One adsorbate
# adsorbates = classification.adsorbates
# interstitials = classification.interstitials
# substitutions = classification.substitutions
# vacancies = classification.vacancies
# self.assertEqual(len(interstitials), 0)
# self.assertEqual(len(substitutions), 0)
# self.assertEqual(len(vacancies), 0)
# self.assertEqual(len(adsorbates), 12)
# self.assertTrue(np.array_equal(adsorbates, range(75, 87)))
# def test_random(self):
# """Test a structure with random atom positions.
# """
# n_atoms = 50
# rng = RandomState(8)
# for i in range(10):
# rand_pos = rng.rand(n_atoms, 3)
# system = Atoms(
# scaled_positions=rand_pos,
# cell=(10, 10, 10),
# symbols=n_atoms*['C'],
# pbc=(1, 1, 1))
# classifier = Classifier()
# classification = classifier.classify(system)
# self.assertIsInstance(classification, Class3D)
# def test_nanocluster(self):
# """Test the periodicity finder on an artificial perfect nanocluster.
# """
# system = bcc100('Fe', size=(7, 7, 12), vacuum=0)
# system.set_cell([30, 30, 30])
# system.set_pbc(True)
# system.center()
# # Make the thing spherical
# center = np.array([15, 15, 15])
# pos = system.get_positions()
# dist = np.linalg.norm(pos - center, axis=1)
# valid_ind = dist < 10
# system = system[valid_ind]
# # Get the index of the atom that is closest to center of mass
# cm = system.get_center_of_mass()
# seed_index = np.argmin(np.linalg.norm(pos-cm, axis=1))
# # view(system)
# # Find the region with periodicity
# finder = PeriodicFinder()
# region = finder.get_region(
# system,
# seed_index,
# pos_tol=0.01,
# max_cell_size=4,
# )
# # No defects or unknown atoms
# adsorbates = region.get_adsorbates()
# interstitials = region.get_interstitials()
# substitutions = region.get_substitutions()
# vacancies = region.get_vacancies()
# self.assertEqual(len(interstitials), 0)
# self.assertEqual(len(substitutions), 0)
# self.assertEqual(len(vacancies), 0)
# self.assertEqual(len(adsorbates), 0)
# def test_optimized_nanocluster(self):
# """Test the periodicity finder on a DFT-optimized nanocluster.
# """
# system = ase.io.read("./structures/cu55.xyz")
# system.set_cell([20, 20, 20])
# system.set_pbc(True)
# system.center()
# # Get the index of the atom that is closest to center of mass
# cm = system.get_center_of_mass()
# pos = system.get_positions()
# seed_index = np.argmin(np.linalg.norm(pos-cm, axis=1))
# view(system)
# # Find the region with periodicity
# finder = PeriodicFinder()
# region = finder.get_region(system, seed_index, 4, 2.75)
# # print(region)
# rec = region.recreate_valid()
# view(rec)
# # view(rec.unit_cell)
# # No defects or unknown atoms
# adsorbates = region.get_adsorbates()
# interstitials = region.get_interstitials()
# substitutions = region.get_substitutions()
# vacancies = region.get_vacancies()
# unknowns = region.get_unknowns()
# self.assertEqual(len(interstitials), 0)
# self.assertEqual(len(substitutions), 0)
# self.assertEqual(len(vacancies), 0)
# self.assertEqual(len(adsorbates), 0)
# self.assertEqual(len(unknowns), 0)
class DelaunayTests(unittest.TestCase):
"""Tests for the Delaunay triangulation.
"""
classifier = Classifier()
delaunay_threshold = classifier.delaunay_threshold
def test_surface(self):
system = bcc100('Fe', size=(5, 5, 3), vacuum=8)
# view(system)
decomposition = systax.geometry.get_tetrahedra_decomposition(
system,
DelaunayTests.delaunay_threshold
)
# Atom inside
test_pos = np.array([7, 7, 9.435])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
# Atoms at the edges should belong to the surface
test_pos = np.array([14, 2, 9.435])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
test_pos = np.array([1.435, 13, 9.435])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
# Atoms outside
test_pos = np.array([5, 5, 10.9])
self.assertEqual(decomposition.find_simplex(test_pos), None)
test_pos = np.array([5, 5, 7.9])
self.assertEqual(decomposition.find_simplex(test_pos), None)
def test_2d(self):
system = ase.build.mx2(
formula="MoS2",
kind="2H",
a=3.18,
thickness=3.19,
size=(2, 2, 1),
vacuum=8)
system.set_pbc(True)
# view(system)
decomposition = systax.geometry.get_tetrahedra_decomposition(
system,
DelaunayTests.delaunay_threshold
)
# Atom inside
test_pos = np.array([2, 2, 10])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
test_pos = np.array([2, 2, 10.5])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
# # Atoms at the edges should belong to the surface
test_pos = np.array([0, 4, 10])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
test_pos = np.array([5, 1, 10])
self.assertNotEqual(decomposition.find_simplex(test_pos), None)
# # Atoms outside
test_pos = np.array([2, 2, 11.2])
self.assertEqual(decomposition.find_simplex(test_pos), None)
test_pos = np.array([0, 0, 7.9])
self.assertEqual(decomposition.find_simplex(test_pos), None)
class AtomTests(unittest.TestCase):
"""Tests for detecting an Atom.
"""
def test_finite(self):
classifier = Classifier()
c = Atoms(symbols=["C"], positions=np.array([[0.0, 0.0, 0.0]]), pbc=False)
clas = classifier.classify(c)
self.assertIsInstance(clas, Atom)
def test_periodic(self):
classifier = Classifier()
c = Atoms(symbols=["C"], positions=np.array([[0.0, 0.0, 0.0]]), pbc=True, cell=[10, 10, 10])
clas = classifier.classify(c)
self.assertIsInstance(clas, Atom)
c = Atoms(symbols=["C"], positions=np.array([[0.0, 0.0, 0.0]]), pbc=[1, 0, 1], cell=[10, 10, 10])
clas = classifier.classify(c)
self.assertIsInstance(clas, Atom)
c = Atoms(symbols=["C"], positions=np.array([[0.0, 0.0, 0.0]]), pbc=[1, 0, 0], cell=[10, 10, 10])
clas = classifier.classify(c)
self.assertIsInstance(clas, Atom)
class Class0DTests(unittest.TestCase):
"""Tests for detecting zero-dimensional systems.
"""
def test_h2o_no_pbc(self):
h2o = molecule("H2O")
classifier = Classifier()
clas = classifier.classify(h2o)
self.assertIsInstance(clas, Class0D)
def test_h2o_pbc(self):
h2o = molecule("CH4")
gap = 10
h2o.set_cell([[gap, 0, 0], [0, gap, 0], [0, 0, gap]])
h2o.set_pbc([True, True, True])
h2o.center()
classifier = Classifier()
clas = classifier.classify(h2o)
self.assertIsInstance(clas, Class0D)
def test_unknown_molecule(self):
"""An unknown molecule should be classified as Class0D
"""
sys = Atoms(
positions=[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]],
symbols=["Au", "Ag"]
)
gap = 12
sys.set_cell([[gap, 0, 0], [0, gap, 0], [0, 0, gap]])
sys.set_pbc([True, True, True])
sys.center()
# view(sys)
classifier = Classifier()
clas = classifier.classify(sys)
self.assertIsInstance(clas, Class0D)
class Class1DTests(unittest.TestCase):
"""Tests detection of one-dimensional structures.
"""
def test_nanotube_full_pbc(self):
tube = nanotube(6, 0, length=1)
tube.set_pbc([True, True, True])
cell = tube.get_cell()
cell[0][0] = 20
cell[1][1] = 20
tube.set_cell(cell)
tube.center()
classifier = Classifier()
clas = classifier.classify(tube)
self.assertIsInstance(clas, Class1D)
def test_nanotube_partial_pbc(self):
tube = nanotube(6, 0, length=1)
tube.set_pbc([False, False, True])
cell = tube.get_cell()
cell[0][0] = 6
cell[1][1] = 6
tube.set_cell(cell)
tube.center()
classifier = Classifier()
clas = classifier.classify(tube)
self.assertIsInstance(clas, Class1D)
def test_nanotube_full_pbc_shaken(self):
tube = nanotube(6, 0, length=1)
tube.set_pbc([True, True, True])
cell = tube.get_cell()
cell[0][0] = 20
cell[1][1] = 20
tube.set_cell(cell)
tube.rattle(0.1, seed=42)
tube.center()
classifier = Classifier()
clas = classifier.classify(tube)
self.assertIsInstance(clas, Class1D)
def test_nanotube_too_big(self):
"""Test that too big 1D structures are classifed as unknown.
"""
tube = nanotube(20, 0, length=1)
tube.set_pbc([True, True, True])
cell = tube.get_cell()
cell[0][0] = 40
cell[1][1] = 40
tube.set_cell(cell)
tube.center()
classifier = Classifier()
clas = classifier.classify(tube)
self.assertIsInstance(clas, Class1D)
class Material2DTests(unittest.TestCase):
"""Tests detection of 2D structures.
"""
graphene = Atoms(
symbols=[6, 6],
cell=np.array((
[2.4595121467478055, 0.0, 0.0],
[-1.2297560733739028, 2.13, 0.0],
[0.0, 0.0, 20.0]
)),
scaled_positions=np.array((
[0.3333333333333333, 0.6666666666666666, 0.5],
[0.6666666666666667, 0.33333333333333337, 0.5]
)),
pbc=True
)
# def test_2d_adsorption_small_cell(self):
# """This test does not currently pass, because for too small cells the
# adsorbate cannot be determined. This could be fixed by using a smaller
# max_cell_size when such a case is detected.
# """
# system = Material2DTests.graphene.repeat([2, 2, 1])