-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecip_2Dlattice.m
449 lines (385 loc) · 15.3 KB
/
recip_2Dlattice.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
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
classdef recip_2Dlattice < handle
%reciprocal 2D lattice object
%Dependencies eDiff_Wavenumber
% eDiff_ScatteringFactor
% phase2color
properties (SetAccess = public, GetAccess = public)
spotcut = 2; % radial cut off
kzval = 0; % default kz value
keV = 80; % incident electron beam energy (keV)
rnd = 8; % position tolerance (# decimal points)
kzMode = 'ewald'; %
intcut = 0; % Intensity cutoff (Normalized)
killZero = 1; % 0-> show 0, 1-> hide 0, 2-> gray 0
includeScat= true;
% Lattice Parameters
bs
b1
b2
as
a1
a2
area % Unit Cell Area
lambda % Inter-vdw spacing
stacking
title_str =''; % Title for figures
intensityFactor = 1; % Scaling factor for ploting bragg peaks
% For Tilt Series
tilt_start
tilt_end
tilt_n = 2.^8;
tilt_angle
tilt_axis = 0;
% 3D kz extent
kzExtent;
end
methods
function setTitle(self,val)
self.title_str=val;
end
function setSpotcut(self,val)
self.spotcut=val;
end
function setKzVal(self,val)
self.kzval=val;
end
function setkeV(self,val)
self.keV=val;
end
function setrnd(self,val)
self.rnd=val;
end
function setKzMode(self,val)
self.kzMode=val;
end
function setIntcut(self,val)
self.intcut=val;
end
function setKillZero(self,val)
self.killZero=val;
end
function setIncludeScat(self,val)
self.includeScat=val;
end
function setArea(self,val)
self.area = val;
end
function setKzExtent(self,val)
self.kzExtent = val;
end
function setLambda(self,val)
self.lambda=val;
end
function setIntensityFactor(self,val)
self.intensityFactor = val;
end
function setTiltStart(self,val)
self.tilt_start = val;
end
function setTiltEnd(self,val)
self.tilt_end = val;
end
function setTiltAxis(self,val)
self.tilt_axis = val;
end
function setTiltAngle(self,val)
self.tilt_angle = val;
end
function setTiltN(self,val)
self.tilt_n = val;
end
function [pos,h,k] = recip2DMeshGrid(self)
[pos,h,k] = indexedMeshGrid(self.spotcut,self.b1,self.b2);
% Get rid of overlapping peaks
pos = round(pos,self.rnd);
[~,ia,~] = unique(pos,'rows');
pos = pos(ia,:);
h = h(ia);
k = k(ia);
end
function [ kz ] = kzProvider(self,kx,ky)
switch self.kzMode
case 'constant'
kz = self.kzval*ones(length(kx),1);
case 'ewald'
K = eDiff_Wavenumber(self.keV);
k_rho_sq = (kx.^2+ky.^2);
kz = K-sqrt(K^2 -k_rho_sq);
case 'tilted_ewald'
k = eDiff_Wavenumber(self.keV);
phi = self.tilt_angle;
theta = self.tilt_axis;
qzo = k*cos(phi);
qxo = k*sin(phi)*cos(theta);
qyo = k*sin(phi)*sin(theta);
kz = qzo - sqrt(k^2 - (kx-qxo).^2 - (ky-qyo).^2);
otherwise
error('invalid mode')
end
end
function [pos] = rotPos(self,pos,theta)
% rotate positioin matrix in xy plane
% pos: n x 3 position matrix [x0,y0,z0;x1,y1,z1;..]
% Written by Suk Hyun Sung, [email protected]
% Jan. 05 2018
% construct tilt_axis matrix
rotmatrix = [cos(theta),-sin(theta);sin(theta),cos(theta)];
% extract xy coordinates only
xy = pos(:,1:2);
% rotate xy coordinates
xy_rot = xy*rotmatrix;
% assign output
pos(:,1:2) = xy_rot;
end
% Generic function for drawing scatter plot with color
% corresponding to phase and size corresponding to magnitude
function posmagDrawPhase(self,pos,mag,fig)
figure(fig);
% load global variables
% handle empty variables
% suppress zero beam
if self.killZero ==1
mag(round(pos(:,1),2) == 0 & round(pos(:,2),2) ==0) = 0;
end
% calculate intensity and normalize
int = mag.*conj(mag);
int = int/max(int);
% remove intensities below cutoff
pos(int<=self.intcut,:) =[];
mag(int<=self.intcut,:) =[];
int(int<=self.intcut) = [];
phase = angle(mag);
rgb = phase2color(phase);
if self.killZero == 2
rgb(round(pos(:,1),2) == 0 & round(pos(:,2),2) ==0,:) = .8275; %setting center beam to gray
end
% scatter point area is proportional to int
int = self.intensityFactor .* sqrt(int);
scatter3(pos(:,1),pos(:,2),pos(:,3),700*int,rgb,'.')
end
% Draw 2D recip structure according to current kzmode and kzval
function [pos, mag] = draw2D(self,fig)
figure(fig);
[pos,mag] = self.calculate();
self.posmagDrawPhase(pos,mag,fig);
title(self.title_str)
end
% Draw full 3D recip structure
function [pos, mag] = draw3D(self,drawUnitCell,fig)
figure(fig);
self.setKzMode('constant');
kzs = linspace(-self.kzExtent,self.kzExtent,2^8);
intensityScal = 5;
self.intensityFactor = intensityScal * self.intensityFactor;
pos =[]; mag = [];
for kz = kzs
self.setKzVal(kz);
[pos_cur,mag_cur] = self.calculate;
pos = [pos;pos_cur];
mag = [mag;mag_cur];
end
self.posmagDrawPhase(pos,mag,fig);
if drawUnitCell
% Draw a hexagon around first order Bragg peak
hold on
[x, y] = self.unitCellOutline();
plot(x,y,'k')
end
title(self.title_str)
axis equal off
view([17,36])
self.intensityFactor = self.intensityFactor/intensityScal;
end
% Draw full 3D recip structure
function [pos, mag] = draw3DHK(self,hs,ks,drawUnitCell,fig)
figure(fig);
self.setKzMode('constant');
kzs = linspace(-self.kzExtent,self.kzExtent,2^8);
intensityScal = 5;
self.intensityFactor = intensityScal * self.intensityFactor;
pos =[]; mag = [];
for kz = kzs
self.setKzVal(kz);
[pos_cur,mag_cur] = self.calculateHK(hs,ks);
pos = [pos;pos_cur];
mag = [mag;mag_cur];
end
self.posmagDrawPhase(pos,mag,fig);
if drawUnitCell
% Draw a hexagon around first order Bragg peak
hold on
[x, y] = self.unitCellOutline();
plot(x,y,'k')
end
title(self.title_str)
axis equal off
view([17,36])
self.intensityFactor = self.intensityFactor/intensityScal;
end
% Draw side view of structure given hk indices
function [pos, mag] = drawSideView(self,hs,ks,xpos,fig)
self.setKzMode('constant')
self.setIntcut(0)
intensityScal = 1;
self.intensityFactor = intensityScal * self.intensityFactor;
kzs = linspace(-self.kzExtent,self.kzExtent,2^8);
pos = []; mag = [];
for kz = kzs
self.setKzVal(kz);
[pos_cur,mag_cur] = self.calculateHK(hs,ks);
pos_cur(:,1) = xpos;
pos_cur(:,2) = zeros(size(xpos));
pos = [pos;pos_cur];
mag = [mag;mag_cur];
end
self.posmagDrawPhase(pos,mag,fig);
title(self.title_str)
view([0 0])
self.intensityFactor = self.intensityFactor/intensityScal;
end
% Used by getTiltSeries to draw tilt axis on a 2D diffraction
% pattern with colors corresponding to position NOT PHASE
function drawTiltAxis(self,pos,mag,fig)
% load global variables
% handle empty variables
% suppress zero beam
if self.killZero == 1
mag(round(pos(:,1),2) == 0 & round(pos(:,2),2) ==0) = 0;
end
% calculate intensity and normalize
int = mag.*conj(mag);
int = int/max(int);
% remove intensities below cutoff
pos(int<=self.intcut,:) =[];
int(int<=self.intcut,:) =[];
rgb = pos2color(pos);
% Draw
ax = axes('Parent',fig);
hold(ax,'on')
axis(ax,'equal')
scatter(pos(:,1),pos(:,2),1000*int,rgb,'.')
phi = self.tilt_axis+pi/2;
plot( self.bs(1)*linspace(-cos(phi),cos(phi)),self.bs(1)*linspace(-sin(phi),sin(phi)));
if isempty(self.title_str)
self.setTitle('')
end
title(self.title_str)
end
function [tiltrange, I] = getTiltSeries(self, kzmode, displaymode, displaypattern, fig)
figure(fig);
hold on;
curkeV = self.keV;
self.setKzMode('tilted_ewald');
if strcmp(kzmode, 'constant')
% Set arbitrary high beam energy to simulate flat ewald
self.setkeV(10^8);
end
tiltrange = linspace(self.tilt_start,self.tilt_end,self.tilt_n);
I = [];
kz_holder = [];
for tilt = tiltrange
self.tilt_angle = tilt;
[pos, mag] = self.calculate;
if self.killZero ==1
mag(round(pos(:,1),2) == 0 & round(pos(:,2),2) ==0) = 0;
end
int = mag.*conj(mag);
int = int./max(int);
pos(int<=self.intcut,:) =[];
mag(int<=self.intcut,:) = [];
%%%%%%% applying cosine correction baked in
mag = mag./(cos(tilt)); %before multiplying by conjugate -> cos^2
kz_holder = [kz_holder, pos(:,3)];
I = [I, mag.*conj(mag)];
end
I = I./max(I(:));
pos0 = pos;
mag0 = ones(size(pos0));
rgb = pos2color(pos0);
if(strcmp('angle',displaymode))
for i = length(rgb):-1:1
plot(tiltrange(:).*180/pi, I(i,:),'LineWidth',2,'Color',rgb(i,:));
end
elseif(strcmp('kz',displaymode))
for i = 1:length(rgb)
plot(kz_holder(i,:), I(i,:),'LineWidth',2,'Color',rgb(i,:));
end
else
error('Incorrect Display Mode');
end
if isempty(self.title_str)
self.setTitle('')
end
title(self.title_str)
if displaypattern
self.drawTiltAxis(pos0,mag0,figure);
end
self.setkeV(curkeV);
end
function [tiltrange, I] = getTiltSeriesHK(self, hs,ks,kzmode, displaymode, displaypattern, fig)
figure(fig);
hold on;
curkeV = self.keV;
self.setKzMode('tilted_ewald');
if strcmp(kzmode, 'constant')
% Set arbitrary high beam energy to simulate flat ewald
self.setkeV(10^8);
end
tiltrange = linspace(self.tilt_start,self.tilt_end,self.tilt_n);
I = [];
kz_holder = [];
for tilt = tiltrange
self.tilt_angle = tilt;
[pos, mag] = self.calculateHK(hs,ks);
if self.killZero ==1
mag(round(pos(:,1),2) == 0 & round(pos(:,2),2) ==0) = 0;
end
int = mag.*conj(mag);
int = int./max(int);
pos(int<=self.intcut,:) =[];
mag(int<=self.intcut,:) = [];
%%%%%%% applying cosine correction baked in
mag = mag./(cos(tilt)); %before multiplying by conjugate -> cos^2
kz_holder = [kz_holder, pos(:,3)];
I = [I, mag.*conj(mag)];
end
I = I./max(I(:));
pos0 = pos;
mag0 = ones(size(pos0));
rgb = pos2color(pos0);
if(strcmp('angle',displaymode))
for i = length(rgb):-1:1
plot(tiltrange(:).*180/pi, I(i,:),'LineWidth',2,'Color',rgb(i,:));
end
elseif(strcmp('kz',displaymode))
for i = 1:length(rgb)
plot(kz_holder(i,:), I(i,:),'LineWidth',2,'Color',rgb(i,:));
end
else
error('Incorrect Display Mode');
end
if isempty(self.title_str)
self.setTitle('')
end
title(self.title_str)
if displaypattern
self.drawTiltAxis(pos0,mag0,figure);
end
self.setkeV(curkeV);
end
function mag = applyScat(self,pos,mag,element)
% Calculate scattering factor and apply to recip-space magnitude vector
% Utilizes eDiff_ScatteringFactor by R Hovden.
% Written by Suk Hyun Sung, [email protected]
% Jan. 05 2018
r = sqrt(pos(:,1).^2+pos(:,2).^2 +pos(:,3).^2);
fe = eDiff_ScatteringFactor(element,r/(2*pi));
mag = mag.*fe;
end
end
methods (Abstract)
[pos,mag] = calculate(self);
mag = calculateHK(self,h,k);
[x, y] = unitCellOutline(self);
end
end