-
Notifications
You must be signed in to change notification settings - Fork 1
/
sheet.m
161 lines (135 loc) · 6.5 KB
/
sheet.m
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
classdef sheet
properties
Lattice
Sheet_Size % max sheet sizes (1,2) vector
Basis_Size
Basis_Positions
Node_Positions
Atom_Positions
Origin % this is considered the origin when taking input.
index % (i,j,b) coordinates to natural numbers coordinates
index_inv % opposite of above
Ratio % used in neighbor calculations
Origin_Index
r_cut % circular cut-out of sheet used for neighbors function
end
methods
function obj = sheet(L,Sheet_Size,basis_positions,r_cut,shift) % constructor
if ~exist('shift','var')
shift = [0;0];
end
obj.r_cut = r_cut;
obj.Lattice = L;
obj.Sheet_Size = Sheet_Size;
obj.Basis_Positions = basis_positions;
obj.Basis_Size = size(basis_positions,2);
obj.Node_Positions = zeros(2,Sheet_Size(1,1)*Sheet_Size(1,2));
obj.Atom_Positions = zeros(2,Sheet_Size(1,1)*Sheet_Size(1,2)*obj.Basis_Size);
obj.Origin_Index = zeros(obj.Basis_Size,1);
%s_size = Sheet_Size(1,1)*Sheet_Size(1,2)*obj.Basis_Size;
obj.Origin = L*(Sheet_Size'-1)*.5 + shift;
obj.index = zeros(Sheet_Size(1,1),Sheet_Size(1,2),2);
obj.index_inv = zeros(Sheet_Size(1,1)*Sheet_Size(1,2)*2,3);
obj.Ratio = abs(dot(obj.Lattice(:,1),obj.Lattice(:,2)))/(norm(obj.Lattice(:,1))*norm(obj.Lattice(:,2)));
obj.Ratio = min(obj.Ratio,1-obj.Ratio)^(-1);
for i = 1:Sheet_Size(1,1) % build sheet
for j = 1:Sheet_Size(1,2)
for k = 1:obj.Basis_Size
obj.index(i,j,k) = (i-1)*Sheet_Size(1,2)*obj.Basis_Size + (j-1)*obj.Basis_Size + k;
obj.index_inv(obj.index(i,j,k),1) = i;
obj.index_inv(obj.index(i,j,k),2) = j;
obj.index_inv(obj.index(i,j,k),3) = k;
% if i == (Sheet_Size(1,1)-mod(Sheet_Size(1,1),2))/2 && j == (Sheet_Size(1,2)-mod(Sheet_Size(1,2),2))/2
% obj.Origin_Index(k,1) = obj.index(i,j,k);
% end
obj.Node_Positions(:,(i-1)*Sheet_Size(1,2) + j) = L*[i-1;j-1];
obj.Atom_Positions(:,obj.index(i,j,k)) = L*[i-1;j-1]+basis_positions(:,k);
if norm(L*[i-1;j-1]-obj.Origin) < .5*max(norm(obj.Lattice(:,1)),norm(obj.Lattice(:,2)))
obj.Origin_Index(k,1) = obj.index(i,j,k);
end
end
end
end
end
function Neighbors = neighbors(obj,Position, Cutoff)
% noP = 0; % we assume there is a P
% if ~exist('P','var')
% noP = 1;
% P = ones(size(obj.Atom_Positions,2),1);
% end
Cutoff_Adj = Cutoff*obj.Ratio;
Cutoff_Squared = Cutoff^2;
Position_Adj = Position + obj.Origin;
coord = floor(obj.Lattice^(-1)*Position_Adj);
if coord(1,1) <= 0
coord(1,1) = 1;
end
if coord(2,1) <= 0
coord(2,1) = 1;
end
% not taking into account basis!
min1 = floor(max(1,coord(1,1)-Cutoff_Adj*norm(obj.Lattice(:,1))^(-1)-1));
max1 = ceil(min(obj.Sheet_Size(1,1),coord(1,1)+Cutoff_Adj*norm(obj.Lattice(:,1))^(-1)+1));
min2 = floor(max(1,coord(2,1)-Cutoff_Adj*norm(obj.Lattice(:,2))^(-1)-1));
max2 = ceil(min(obj.Sheet_Size(1,2),coord(2,1)+Cutoff_Adj*norm(obj.Lattice(:,2))^(-1)+1));
% count = 0;
%s_size = size(obj.Atom_Positions,2);
%Position_Squared = zeros(s_size,1);
% for j = 1:s_size
% Position_Squared(j,1) = (obj.Atom_Positions(1,j)-Position_Adj(1,1))^2 ...
% + (obj.Atom_Positions(2,j)-Position_Adj(2,1))^2;
% end
% [i,j,k] = meshgrid(min1:max1,min2:max2, 1:obj.Basis_Size);
% i = permute(i,[2 1 3]);
% j = permute(j,[2 1 3]);
l = obj.index(min1:max1,min2:max2, 1:obj.Basis_Size);
l = l(:);
Position_Squared = (obj.Atom_Positions(1,l)-Position_Adj(1,1)).^2 ...
+ (obj.Atom_Positions(2,l)-Position_Adj(2,1)).^2;
X = obj.Atom_Positions(:,l)-obj.Origin(:)*ones(1,size(l,1));
Neighbors = l(Position_Squared(:) < Cutoff_Squared & (X(1,:).^2 + X(2,:).^2 < obj.r_cut^2)');
% if Position_Squared(l,1)<Cutoff_Squared
% if noP == 0
% if P(l,1) == 0
% count = count - 1;
% end
% end
% count = count + 1;
% end
% Neighbors = zeros(count,1);
% count_pos = 1;
% for i = min1:max1
% for j = min2:max2
% for k = 1:obj.Basis_Size
% l = obj.index(i,j,k);
%
% if Position_Squared(l,1)<Cutoff_Squared
% if noP == 1
% Neighbors(count_pos,1) = l;
% count_pos = count_pos+1;
% end
% if noP == 0
% if P(l,1) == 1
% Neighbors(count_pos,1) = l;
% count_pos = count_pos + 1;
% end
% end
% end
% end
% end
%end
end
function view(obj,color,extra_command,color2)
if ~exist('extra_command','var')
extra_command = 'nothing';
end
scatter(obj.Atom_Positions(1,:)-obj.Origin(1,1),obj.Atom_Positions(2,:)-obj.Origin(2,1),color);
if strcmp(extra_command,'highlight_center')
hold on
for k = 1:obj.Basis_Size
scatter(obj.Atom_Positions(1,obj.Origin_Index(k,1))-obj.Origin(1,1),obj.Atom_Positions(2,obj.Origin_Index(k,1))-obj.Origin(2,1),color2);
end
end
end
end
end