-
Notifications
You must be signed in to change notification settings - Fork 2
/
chiraltube.py
1694 lines (1534 loc) · 60.5 KB
/
chiraltube.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
# This source file is part of the chiraltube project, made by Jose M. de Albornoz Caratozzolo and Felipe Cervantes Sodi
# This source code is released under the 3-Clause BSD License, see "LICENSE.txt".
import sys
import math
import random as rd
import statistics as stat
opts = [opt for opt in sys.argv[1:] if opt.startswith("-")]
args = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
ERROR=0.025
class point3d:
'''
3 dimensional point used for atoms (and vectors), with an
identifier for the element.
Examples
--------
>>> my_atom = point3d(0.0, 1.0, 0.5, 'H')
'''
def __init__(self, x, y, z, ele):
self.x=x #X, Y, Z coordinates
self.y=y
self.z=z
self.ele=ele #element
def __str__(self):
return "{}, {}, {}, {}".format(self.x, self.y, self.z, self.ele)
def __repr__(self):
return "({}, {}, {}, {})".format(self.x, self.y, self.z, self.ele)
def __eq__(self, other): #equality, element is not taken into account
return self.x==other.x and self.y==other.y and self.z==other.z
def __add__(self, other): #sum
return point3d(self.x+other.x, self.y+other.y, self.z+other.z, other.ele)
def __mul__(self, other): #multiplication
if isinstance(self, point3d):
if isinstance(other, point3d):
return point3d(self.x*other.x, self.y*other.y, self.z*other.z, self.ele)
else:
return point3d(self.x*other, self.y*other, self.z*other, self.ele)
elif isinstance(other, point3d):
return point3d(self*other.x, self*other.y, self*other.z, other.ele)
else:
return point3d(self*other.x, self*other.y, self*other.z, other.ele)
def __sub__(self,other): #substraction
return point3d(self.x-other.x, self.y-other.y, self.z-other.z, other.ele)
def mag(self):#magnitude
'''Returns the magnitude of the vector defined by the X, Y, Z coordinates of the atom'''
return math.sqrt(self.x**2+self.y**2+self.z**2)
def distance(p1, p2):
'''
Returns the distance between two point3d's
Parameters
----------
p1: point3d
First atom/vector
p2: point3d
Second aom/vector
Returns
-------
float
The Euclidean distance between the positions of p1 and p2
See Also
--------
point3d: 3 dimensional point used for atoms (and vectors)
Examples
--------
>>> p1 = point3d(0.0, 1.0, 0.5, 'Na')
>>> p2 = point3d(-1.0, 0.0, 1.5, 'Cl')
>>> distance(p1,p2)
1.7320508075688772
'''
return math.sqrt((p2.x-p1.x)**2 + (p2.y-p1.y)**2 + (p2.z-p1.z)**2)
def testgraph(Arr):
'''
VERY basic visualization for testing purposes using matplotlib.
Parameters
----------
Arr: List of point3d's
List of all the atoms that you want to graph
'''
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
x=[ele.x for ele in Arr]
y=[ele.y for ele in Arr]
z=[ele.z for ele in Arr]
C=[]
for ele in Arr:
if ele.ele=='S':
C.append('b')
elif ele.ele=='C':
C.append('g')
else:
C.append('r')
fig=plt.figure()
ax=plt.axes(projection='3d')
ax.scatter3D(x,y,z, c=C)
plt.show()
def arr_initial(n, m, x, y):
'''
Returns initial array of atoms
Repeats the Unit Cell given in the input file to create a 2d array of
unit cells, with the right dimensions from which the nanoribbon/nanotube
will be made.
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Returns
-------
List of point3d
Array of atoms arranged in a 2d plane.
See Also
--------
robtainxy: Gives a list of possible x,y pairs from a specific n,m pair
Example
-------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> Arr=arr_initial(n, m, x, y)
'''
y_tot=n+x+1
x_inf=-y-1
x_sup=m+1
Arr=[]
py=0
pz=0
for i in range(y_tot+1):
px=a2*x_inf+a1*i
for _ in range(x_sup-x_inf+1):
for atom in UnitCell:
Arr.append(px+atom)
px=px+a2
return Arr
def robtainxy(n, m, error=ERROR, MAX=100):
'''
Returns chiral angle theta, and an array with the X, Y pairs
and their respective errors corresponding to the n,m indices,
up to an error "error" and looks for x,y<"MAX".
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
error: float
The allowed error when looking for integer x,y pairs. Its in
units of the cell vectors.
MAX: int
The maximum range of search for x and y. Default is 100
Returns
-------
theta: float
The chiral angle defined by the indices n and m
Res: List of floats
A list with all the results of x, y pairs (not rounded to
nearest integer), along with their respective errors and
percentage errors.
See Also
--------
obtainxy: Prints theta and the X, Y pairs instead of returning them
Examples
--------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> coordinatesNT_xyz(n,m,x,y, arch_out="./OutputFile.xyz")
'''
Res=[]
if n==0:
theta=0
for x in range(1,MAX):
y=x*a1.mag()*math.cos(phi)/a2.mag()
if abs(round(y)-y)<error:
Res.append((x, y, abs(round(y)-y), (abs(round(y)-y)/y)*100))
for y in range(1,MAX):
x=y*a2.mag()/(a1.mag()*math.cos(phi))
if abs(round(x)-x)<error:
Res.append((x, y, abs(round(x)-x), (abs(round(x)-x)/x)*100))
elif m==0:
theta=phi
for x in range(1,MAX):
y=x*a1.mag()/(a2.mag()*math.cos(phi))
if abs(round(y)-y)<error:
Res.append((x, y, abs(round(y)-y), (abs(round(y)-y)/y)*100))
for y in range(1,MAX):
x=y*a2.mag()*math.cos(phi)/a1.mag()
if abs(round(x)-x)<error:
Res.append((x, y, abs(round(x)-x), (abs(round(x)-x)/x)*100))
else:
theta= math.acos((a2.x*(a2*m+a1*n).x + a2.y*(a2*m+a1*n).y+a2.z*(a2*m+a1*n).z)/(a2.mag()*(a2*m+a1*n).mag()))
for x in range(1, MAX):
#y=((n*a1.mag()*a1.mag())/(m*a2.mag()*a2.mag()))*(math.sin(math.pi-2*phi+theta)/math.sin(theta))*x
y=((n*a1.mag()*a1.mag())/(m*a2.mag()*a2.mag()))*(math.sin(phi-theta)*math.cos(phi-theta))*x/(math.sin(theta)*math.cos(theta))
if abs(round(y)-y)<error:
Res.append((x, y, abs(round(y)-y), (abs(round(y)-y)/y)*100))
for y in range(1, MAX):
#x=((m*a2.mag()*a2.mag())/(n*a1.mag()*a1.mag()))*(math.sin(theta)/math.sin(math.pi-2*phi+theta))*y
x=((m*a2.mag()*a2.mag())/(n*a1.mag()*a1.mag()))*(math.sin(theta)*math.cos(theta))*y/(math.sin(phi-theta)*math.cos(phi-theta))
if abs(round(x)-x)<error:
Res.append((x, y, abs(round(x)-x), (abs(round(x)-x)/x)*100))
Res.sort(key=sortfuncT)
return theta, Res
def obtainxy(n, m, error=ERROR, MAX=100):
'''
Prints chiral angle theta, and an array with the X, Y pairs
and their respective errors corresponding to the n,m indices,
up to an error "error" and looks for x,y<"MAX".
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
error: float
The allowed error when looking for integer x,y pairs. Its in
units of the cell vectors.
MAX: int
The maximum range of search for x and y. Default is 100
See Also
--------
robtainxy: Returns theta and the X, Y pairs instead of printing them
'''
theta, Res= robtainxy(n,m, error, MAX)
print("Chiral angle: ", math.degrees(theta), "\n\nResults up to an error <", error)
print("X, Y, error, percentage error")
for ele in Res:
x,y,err,por=ele
print("{}, {}, {}, {}%".format(round(x),round(y), err, por))
def obtainpq(n, m, x,y, MAX=100):
'''
Returns the symmetry vector R indices: P and Q as a tuple
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Returns
-------
p: int
First index of the symmetry vector
q: int
Second index of the symmetry vector
See Also
--------
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
'''
N=x*m + y*n
for p in range(MAX):
q=(1-y*p)/x
if int(q)==q and (0< m*p - n*q <= N):
break
return p, int(q)
def rotate(Arr, theta):
'''
Rotates every point in the array (phi-theta) radians counter-clockwise.
Returns the rotated array.
Parameters
----------
Arr: list of point3d
List of atoms in a 2D plane that will be rotated.
theta: float
Chiral angle in radians
Returns
-------
list of point3d
Rotated array of atoms
See Also
--------
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
Examples
--------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> Arr=arr_initial(n, m, x, y)
>>> Arr= rotate(Arr, theta)
'''
for ele in Arr:
tmpx=ele.x
tmpy=ele.y
ele.x=tmpx*math.cos(phi-theta)-tmpy*math.sin(phi-theta)
ele.y=tmpx*math.sin(phi-theta)+tmpy*math.cos(phi-theta)
return Arr
def eliminate(Arr, n, m, x, y):
'''
Removes those atoms in the array "Arr" which do not belong in the
nanoribbon. Returns the new array, the Y distance (|T|) and the X
distance (|C|).
Parameters
----------
Arr: list of point3d
Array of atoms in a rotated, 2D plane
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Returns
-------
new_Arr: list of point3d
Array of atoms that do belong in the nanoribbon
disy: float
The Y distance (height, |T|) of the nanoribbon
disx: float
The X distance (length, |C|) of the nanoribbon
See Also
--------
rotate: Rotates a 2d array of atoms (phi - theta) radians counter-clockwise.
Examples
--------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> Arr=arr_initial(n, m, x, y)
>>> Arr= rotate(Arr, theta)
>>> Arr, disy, disx = eliminate(Arr, n, m, x, y)
'''
new_Arr=[]
disy= distance(point3d(0,0,0,0), (a1*x-a2*y))
disx= distance(point3d(0,0,0,0), (a1*n+a2*m))
for ele in Arr:
if ele.y>=-0.000001 and ele.y<(disy-0.000001) and ele.x>=-0.000001 and ele.x<(disx-0.000001):
new_Arr.append(ele)
return new_Arr, disy, disx
def coordinates_xyz(n, m, x, y, prt=1, arch_out=False, grf=False):
'''
Does all the steps necessary to get a nanoribbon with chiral indices
(n,m) and translational indices (x,y). If "prt"=1, it prints each
completed step. It automatically prints to file "arch_out",
defaults to "./Ribbon_n-m.xyz". Output file is in special .xyz format.
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Optional
--------
prt: int
If set to 1 then it prints each completed step in the process
arch_out:
Name of the output file, if False, it defaults to "./Ribbon_n-m.xyz"
grf: int
If set to 1 then it also graphs the result using testgraph
See Also
--------
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
testgraph: VERY basic visualization for testing purposes using matplotlib.
Examples
--------
>>> n=5
>>> m=3
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> coordinates_xyz(n,m,x,y, arch_out="./OutputFile.xyz")
'''
if not arch_out:
arch_out= "./Ribbon_{}-{}.xyz".format(n,m)
theta, Res = robtainxy(n, m)
if prt==1:
print("\nChiral angle obtained correctly: Theta=", math.degrees(theta), "\n")
Arr=arr_initial(n, m, x, y)
if prt==1:
print("Initial array correctly created\n")
Arr= rotate(Arr, theta)
if prt==1:
print("Rotation succesfully completed\n")
Arr, disy, disx = eliminate(Arr, n, m, x, y)
for ele in Arr:
displaced = point3d(0,-disy, 0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
for ele in Arr:
displaced = point3d(-disx,0,0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
for ele in Arr:
displaced = point3d(-disx,-disy,0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
if prt==1:
print("Ribbon created correctly, {} atoms in total.".format(len(Arr)))
with open(arch_out, mode = 'w') as outfile:
print(len(Arr), file=outfile)
print("Nanoribbon (n,m)=({},{}). Y={} A, X={} A".format(n, m, disy, disx), file=outfile)
for ele in Arr:
print("{} {} {} {}".format(ele.ele, ele.x, ele.y, ele.z), file=outfile)
if prt==1:
print("Structure saved in file {}\n".format(arch_out))
if grf:
testgraph(Arr)
def coordinates_VASP(n, m, x, y, prt=1, arch_out=False, grf=False):
'''
Does all the steps necessary to get a nanoribbon with chiral indices
(n,m) and translational indices (x,y). If "prt"=1, it prints each
completed step. It automatically prints to file "arch_out",
defaults to "./POSCAR". Output file is POSCAR VASP format.
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Optional
--------
prt: int
If set to 1 then it prints each completed step in the process
arch_out:
Name of the output file, if False, it defaults to "./Ribbon_n-m.xyz"
grf: int
If set to 1 then it also graphs the result using testgraph
See Also
--------
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
testgraph: VERY basic visualization for testing purposes using matplotlib.
Examples
--------
>>> n=5
>>> m=3
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> coordinates_VASP(n,m,x,y, arch_out="./OutputFile.xyz")
'''
if not arch_out:
arch_out= "POSCAR"
theta, Res = robtainxy(n, m)
if prt==1:
print("\nChiral angle obtained correctly: Theta=", math.degrees(theta), "\n")
Arr=arr_initial(n, m, x, y)
if prt==1:
print("Initial array correctly created\n")
Arr= rotate(Arr, theta)
if prt==1:
print("Rotation succesfully completed\n")
Arr, disy, disx = eliminate(Arr, n, m, x, y)
for ele in Arr:
displaced = point3d(0,-disy, 0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
for ele in Arr:
displaced = point3d(-disx,0,0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
for ele in Arr:
displaced = point3d(-disx,-disy,0,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
if prt==1:
print("Ribbon created correctly, {} atoms in total.".format(len(Arr)))
with open(arch_out, mode = 'w') as outfile:
print("Nanoribbon (n,m)=({},{})".format(n, m), file=outfile)
print(1, file=outfile)
print("{} 0.0 0.0".format(disx), file=outfile)
print("0.0 {} 0.0".format(disy), file=outfile)
print("0.0 0.0 {}".format(a3.z), file=outfile)
unique_elements = []
for atom in Arr:
if atom.ele not in unique_elements:
unique_elements.append(atom.ele)
Arr_divided=[[atom for atom in Arr if atom.ele==element] for element in unique_elements]
ele_names=""
ele_numbers=""
for i in range(len(unique_elements)):
ele_names=ele_names+unique_elements[i]+' '
ele_numbers = ele_numbers + str(len(Arr_divided[i]))+' '
print(ele_names.strip(), file=outfile)
print(ele_numbers.strip(), file=outfile)
print('Cartesian', file=outfile)
for ele in Arr_divided:
for atom in ele:
print("{} {} {}".format(atom.x, atom.y, atom.z), file=outfile)
if prt==1:
print("Structure saved in file {}\n".format(arch_out))
if grf:
testgraph(Arr)
def nanotube(Arr, n, m, z_med=False):
'''
Rolls the ribbon into a nanotube with chiral indices (n,m).
"z_med" determines the central plane, defaults to a_3/2.
Returns the new array and the NT radius.
Parameters
----------
Arr: list of point3d
Array of atoms in a nanoribbon
n: int
First chiral index
m: int
Second chiral index
z_med: float
Determines the central plane in the z-direction. Defaults to a_3/2.
Returns
-------
Arr: list of point3d
Array of atoms folded into a nanotube
radio: float
Radius of the newly formed nanotube (measured at the central plane).
Examples
--------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> Arr=arr_initial(n, m, x, y)
>>> Arr= rotate(Arr, theta)
>>> Arr, disy, disx = eliminate(Arr, n, m, x, y)
>>> Arr, radio=nanotube(Arr, n, m)
'''
x_max= distance(point3d(0,0,0,0), (a1*n+a2*m))
radio= x_max/(2*math.pi)
if z_med==False:
z_med= (a3.z)/2
for ele in Arr:
gamma= ele.x/radio
tmpy=ele.y
radio_rel= ele.z-z_med
ele.x= (radio+radio_rel)*math.cos(gamma)
ele.y= (radio+radio_rel)*math.sin(gamma)
ele.z=tmpy
return Arr, radio
def coordinatesNT_xyz(n, m, x, y, rep=0,prt=1, arch_out=False, grf=False):
'''
Does all the steps necessary to get a nanotube with chiral indices (n,m)
and translational indices (x,y), repeated along the NT axis "repeat" times.
If "prt"=1, it prints the completed steps. It automatically prints to file
"arch_out", defaults to "./Nanotube_n-m.xyz". Output file is in special
.xyz format.
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Optional
--------
rep: int
Number of times the nanotube is repeated along its axis.
Defaults to 0 (no repetition)
prt: int
If set to 1 then it prints each completed step in the process
arch_out:
Name of the output file, if False, it defaults to "./Nanotbe_n-m.xyz"
grf: int
If set to 1 then it also graphs the result using testgraph
See Also
--------
coordinates_xyz: Does the same but only makes the nanoribbon
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
repeat: Repeats the array N times along the NT axis
testgraph: VERY basic visualization for testing purposes using matplotlib.
Examples
--------
>>> n=5
>>> m=3
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> coordinatesNT_xyz(n,m,x,y, arch_out="./OutputFile.xyz")
'''
extra=""
theta, Res = robtainxy(n, m)
if prt==1:
print("\nChiral angle obtained correctly: Theta=", math.degrees(theta), "\n")
Arr=arr_initial(n, m, x, y)
if prt==1:
print("Initial array correctly created\n")
Arr= rotate(Arr, theta)
if prt==1:
print("Rotation succesfully completed\n")
Arr, disy, disx = eliminate(Arr, n, m, x, y)
if prt==1:
print("Ribbon created correctly, {} atoms in total.\n".format(len(Arr)))
Arr, radio=nanotube(Arr, n, m)
for ele in Arr:
displaced = point3d(0,0,-disy,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
if prt==1:
print("Nanotube created correctly, {} atoms in total.\n".format(len(Arr)))
if rep!=0:
Arr=repeat(Arr, disy, rep)
extra="x{}".format(rep+1)
print("Nanotube repeated correctly {} times, {} atoms in total.\n".format(rep, len(Arr)))
if not arch_out:
arch_out= "Nanotube_{}-{}{}.xyz".format(n,m, extra)
with open(arch_out, mode = 'w') as outfile:
print(len(Arr), file=outfile)
print("NT (n,m)=({},{}). z={} A, r={} A. T(x,y)=({},{})".format(n, m, disy*(rep+1), radio, x,y), file=outfile)
for ele in Arr:
print("{} {} {} {}".format(ele.ele, ele.x, ele.y, ele.z), file=outfile)
print("alat", file=outfile)
print("1.0", file=outfile)
print("supercell", file=outfile)
print("{}\t0.0\t0.0".format(2*radio), file=outfile)
print("0.0\t{}\t0.0".format(2*radio), file=outfile)
print("0.0\t0.0\t{}".format(disy*(rep+1)), file=outfile)
print("cartesian coordinates", file=outfile)
if prt==1:
print("Structure saved in file {}\n".format(arch_out))
if grf:
testgraph(Arr)
def coordinatesNT_VASP(n, m, x, y, rep=0,prt=1, arch_out=False, grf=False):
'''
Does all the steps necessary to get a nanotube with chiral indices (n,m)
and translational indices (x,y), repeated along the NT axis "repeat" times.
If "prt"=1, it prints the completed steps. It automatically prints to file
"arch_out", defaults to "./POSCAR". Output file is POSCAR VASP
format.
Parameters
----------
n: int
The first chiral index
m: int
The second chiral index
x: int
The first translational index
y: int
The second translational index
Optional
--------
rep: int
Number of times the nanotube is repeated along its axis.
Defaults to 0 (no repetition)
prt: int
If set to 1 then it prints each completed step in the process
arch_out:
Name of the output file, if False, it defaults to "./Nanotbe_n-m.xyz"
grf: int
If set to 1 then it also graphs the result using testgraph
See Also
--------
coordinates_VASP: Does the same but only makes the nanoribbon
robtainxy: Gives the chiral angle and a list of
possible x,y pairs from a specific n,m pair
repeat: Repeats the array N times along the NT axis
testgraph: VERY basic visualization for testing purposes using matplotlib.
Examples
--------
>>> n=5
>>> m=3
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> coordinatesNT_VASP(n,m,x,y, arch_out="./OutputFile.xyz")
'''
theta, Res = robtainxy(n, m)
if prt==1:
print("\nChiral angle obtained correctly: Theta=", math.degrees(theta), "\n")
Arr=arr_initial(n, m, x, y)
if prt==1:
print("Initial array correctly created\n")
Arr= rotate(Arr, theta)
if prt==1:
print("Rotation succesfully completed\n")
Arr, disy, disx = eliminate(Arr, n, m, x, y)
if prt==1:
print("Ribbon created correctly, {} atoms in total.\n".format(len(Arr)))
Arr, radio=nanotube(Arr, n, m)
for ele in Arr:
displaced = point3d(0,0,-disy,'')+ ele
for ele2 in Arr:
if distance(displaced, ele2)<0.2:
Arr.remove(ele)
if prt==1:
print("Nanotube created correctly, {} atoms in total.\n".format(len(Arr)))
if rep!=0:
Arr=repeat(Arr, disy, rep)
print("Nanotube repeated correctly {} times, {} atoms in total.\n".format(rep, len(Arr)))
if not arch_out:
arch_out= "POSCAR"
with open(arch_out, mode = 'w') as outfile:
print("NT (n,m)=({},{}). T(x,y)=({},{})".format(n, m, x,y), file=outfile)
print(1, file=outfile)
print("{} 0.0 0.0".format(2*radio), file=outfile)
print("0.0 {} 0.0".format(2*radio), file=outfile)
print("0.0 0.0 {}".format(disy*(rep+1)), file=outfile)
unique_elements = []
for atom in Arr:
if atom.ele not in unique_elements:
unique_elements.append(atom.ele)
Arr_divided=[[atom for atom in Arr if atom.ele==element] for element in unique_elements]
ele_names=""
ele_numbers=""
for i in range(len(unique_elements)):
ele_names=ele_names+unique_elements[i]+' '
ele_numbers = ele_numbers + str(len(Arr_divided[i]))+' '
print(ele_names.strip(), file=outfile)
print(ele_numbers.strip(), file=outfile)
print('Cartesian', file=outfile)
for ele in Arr_divided:
for atom in ele:
print("{} {} {}".format(atom.x, atom.y, atom.z), file=outfile)
if prt==1:
print("Structure saved in file {}\n".format(arch_out))
if grf:
testgraph(Arr)
def repeat(Arr, disy, times):
'''
Repeats the array in the z direction "times" times. The height of
the array is "disy". Returns the new array.
Parameters
----------
Arr: list of point3d
Array of atoms to be repeated
disy: float
Height of the array (length in the z direction)
times: int
Times the array will be repeated (0 means no repetition, the array is
unchanged)
Returns
-------
list of point3d
New array of atoms that has been repeated the corresponding amount
of times.
Examples
--------
>>> n=5
>>> m=7
>>> theta, Res = robtainxy(n,m)
>>> x, y, error, p_error=Res[0]
>>> x=round(x)
>>> y=round(y)
>>> Arr=arr_initial(n, m, x, y)
>>> Arr= rotate(Arr, theta)
>>> Arr, disy, disx = eliminate(Arr, n, m, x, y)
>>> Arr, radio=nanotube(Arr, n, m)
>>> rep=2
>>> Arr=repeat(Arr, disy, rep)
'''
Copia=[]
for ele in Arr:
Copia.append(ele)
py=0
for _ in range(times):
for ele in Copia:
Arr.append(point3d(0,0,py+disy,'na')+ele)
py=py+disy
return Arr
def searchnum(limit, top=10, error=0.025, r=0):
'''
Searches for achiral nanotubes with a number of atoms <= "limit", up
to n,m < top. With an error = "error". If r=0, it prints the results,
if r=1 it returns an array with the results.
Parameters
----------
limit: int
Max number of atoms that will be searched for
top: int
Maximum value for n and m while searching for NTs. Defaults to 10
error: float
Error used for getting the x, y pairs. Defaults to 0.025.
r: int
If r=0, results are printed. If r=1, results are returned.
Returns
-------
nRes: list of float
List containing the results: No. of atoms, n,m indices,
x,y indices, error and percentage error.
'''
nRes=[]
for n in range(1,top):
for m in range(1,top):
t, Res = robtainxy(n,m, error=error)
x,y,err,perr=Res[0]
x=round(x)
y=round(y)
Arr, disy,disx,rad =do_everything(n,m,x,y)
if len(Arr)<=limit:
nRes.append((len(Arr),n,m,x,y,err,perr))
if r==0:
print("no., n, m, X, Y, Error, Percentage error")
for ele in nRes:
num, n, m, x, y, err, por=ele
print("{}, {}, {}, {}, {}, {}, {}".format(num, n, m, x,y, err, por))
if r==1:
return nRes
def printcoords(Arr, file_name, comment):
'''
Prints the coordinates of an array of point3d to a file "file_name",
with a comment line "comment". Output is in a simple .xyz format.
Mainly for testing purposes.
Parameters
----------
Arr: list of point3d
Array of atoms to print in a file
file_name: string
Name of output file
comment: string
Comment to write in a designated comment line on the output file.
'''
with open(file_name, mode = 'w') as outfile:
print(len(Arr), file=outfile)
print(comment, file=outfile)
for ele in Arr:
print("{} {} {} {}".format(ele.ele, ele.x, ele.y, ele.z), file=outfile)
def read_arch(arch, filetype=False):
'''
Reads the input file in .in, .xyz (special), or POSCAR VASP formats.
Returns the number of atoms in the unit cell, the three unit vectors in
an array and the atoms in the unit cell as an array.
Parameters
----------
arch: string
Name of input file
filetype:
If set to 'VASP', it will read the file as a POSCAR VASP file.
Otherwise it detects if its a .xyz or a .in file
Returns
-------
nat: int
Number of atoms in unit cell
A: list of point3d
List with the three unit vectors of the unit cell
UnitCell: list of point3d
List containing the atoms with their respective positions in the
unit cell.
Examples
--------
>>> nat, A, UnitCell = read_arch("./MoS2UnitCell.in")
>>> nat, A, UnitCell = read_arch("./POSCAR", filetype='VASP')
'''
A=[]
UnitCell=[]
if filetype=='VASP':
with open(arch, mode='r') as infile:
infile.readline()
scale = float(infile.readline().strip())
for _ in range(3):
valores= infile.readline().strip().split()
A.append(point3d(float(valores[0])*scale, float(valores[1])*scale, float(valores[2])*scale, 'Vector'))
line = infile.readline().strip()
element_numbers = []
try:
for number in line.split():
tmp = int(number)
print("\n***ERROR*** No line with species names. Please specify the species.\n")
sys.exit()
except ValueError:
element_names = line.split()
for number in infile.readline().strip().split():
element_numbers.append(int(number))
nat=sum(element_numbers)
line=infile.readline().strip()
if (line.startswith("s") or line.startswith("S")):
line = infile.readline().strip()
if line == 'Direct' or line=='direct':
for ele_no in range(len(element_names)):
for _ in range(element_numbers[ele_no]):
valores = infile.readline().strip().split()
position = a1*valores[0] + a2*valores[1] + a3*valores[2]
UnitCell.append(point3d(position.x, position.y, position.z, element_names[ele_no]))
elif line == 'Cartesian' or line=='cartesian':
for ele_no in range(len(element_names)):
for _ in range(element_numbers[ele_no]):
valores = infile.readline().strip().split()
UnitCell.append(point3d(float(valores[0]), float(valores[1]), float(valores[2]), element_names[ele_no]))
else:
print("\n***ERROR*** Coordinates can only be in 'Direct' format or 'Cartesian' format.\n {} format is not recognized\n".format(line))
sys.exit()