-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_Gt_match_fun.m
152 lines (143 loc) · 8.17 KB
/
calc_Gt_match_fun.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
function [Gt_matched_all,Nr_match,Gt_matched_all_mean,Gt_matched_all_median,NrSpotExpectAll]=calc_Gt_match_fun(U,pos,Spots,rot_angles,rot_start,rot_step,S,B,Ahkl,nrhkl,RotDet, ...
hkl_family,hkl_family_square,d_possible,Glen_possible,Lsam2sou,Lsam2det,dety00,detz00,P0y,P0z,RotAxisOffset, ...
pixelysize,pixelzsize,dety0,detz0,thetamax,lambda_min,lambda_max,detysize,detzsize,BeamStopY,BeamStopZ)
L=Lsam2sou+Lsam2det;
hkl = [Ahkl(1:nrhkl,1) Ahkl(1:nrhkl,2) Ahkl(1:nrhkl,3)]';
Gw = S*U*B*hkl;
Gt_matched_all=[];
NrSpotExpectAll=0;
% consider the detector tilts for calculating the diffraction vector
d_tr=RotDet'*[Lsam2det dety00-RotAxisOffset detz00]';
Lsam2det_tr=d_tr(1)/RotDet(1,1);
dety00_tr=RotDet(1,2)*Lsam2det_tr-d_tr(2);
detz00_tr=RotDet(1,3)*Lsam2det_tr-d_tr(3);
L_tr=Lsam2sou+Lsam2det_tr;
for rot=rot_angles
rot_number=(rot-rot_start)/rot_step+1;
dety=Spots{rot_number}(:,9);
detz=Spots{rot_number}(:,10);
omega=rot*pi/180; % [rad]
Omega=[cos(omega) -sin(omega) 0;sin(omega) cos(omega) 0;0 0 1];
SamposW=Omega*S*pos';
center = [L_tr, (SamposW(2)-P0y+RotAxisOffset)*L_tr/(Lsam2sou+SamposW(1)), ...
(SamposW(3)-P0z)*L_tr/(Lsam2sou+SamposW(1))]; % sample center projected to the position of the detector
alpha = atan(sqrt((SamposW(2)-P0y+RotAxisOffset)^2+(SamposW(3)-P0z)^2)/(Lsam2sou+SamposW(1)));
grainpos = [Lsam2sou+SamposW(1) SamposW(2)-P0y+RotAxisOffset SamposW(3)-P0z];
% unit vectors along incoming and diffracted beams
Kin_unit=[Lsam2sou+SamposW(1),SamposW(2)-P0y+RotAxisOffset,SamposW(3)-P0z]./ ...
norm([Lsam2sou+SamposW(1),SamposW(2)-P0y+RotAxisOffset,SamposW(3)-P0z]); % unit vector along the incoming beam
% dety22=(dety0-dety+0.5)*pixelysize; % [mm]
% detz22=(detz0-detz+0.5)*pixelzsize; % [mm]
dety22=(dety0-dety)*pixelysize; % [mm]
detz22=(detz0-detz)*pixelzsize; % [mm]
Kout_unit=[repmat(Lsam2det-SamposW(1),length(dety),1),dety22-SamposW(2)-dety00+RotAxisOffset,detz22-SamposW(3)-detz00]./ ...
sqrt((Lsam2det-SamposW(1)).^2+(dety22-SamposW(2)-dety00+RotAxisOffset).^2+(detz22-SamposW(3)-detz00).^2); % unit vector along the diffracted beam
% % consider the detector tilts for calculating the diffraction vector
% d_tr=RotDet'*[Lsam2det dety00 detz00]';
% Lsam2det_tr=d_tr(1)/RotDet(1,1);
% dety00_tr=RotDet(1,2)*Lsam2det_tr-d_tr(2);
% detz00_tr=RotDet(1,3)*Lsam2det_tr-d_tr(3);
dety22_tr=dety22-dety00_tr;
detz22_tr=detz22-detz00_tr;
Kout_unit_tr=[repmat(Lsam2det_tr-SamposW(1),length(dety),1),dety22_tr-SamposW(2),detz22_tr-SamposW(3)]./ ...
sqrt((Lsam2det_tr-SamposW(1)).^2+(dety22_tr-SamposW(2)).^2+(detz22_tr-SamposW(3)).^2);
ttheta_spot=acos(sum(repmat(Kin_unit,length(Kout_unit_tr(:,1)),1).*Kout_unit_tr,2))*180/pi; % 2-theta [deg]
lambdahkl_possible = 2 * d_possible *sind(ttheta_spot'/2); % [A]
Gt_spot_factor=Kout_unit_tr-Kin_unit; % [mm]
% Gt_spot{rot_number}=[];
% for j=1:length(hkl_family(:,1))
% Gt_spot{rot_number}(:,:,j)=(2*pi./lambdahkl_possible(j,:)').*Gt_spot_factor;
% end
for j=1:length(hkl_family(:,1))
Gt_spot{j}=(2*pi./lambdahkl_possible(j,:)').*Gt_spot_factor;
end
Gt=Omega*Gw;
Gt=Gt';
NrSpotExpect=calcExpectSpotNr(Gt',hkl,grainpos,center,alpha,RotDet, ...
SamposW,thetamax,lambda_min,lambda_max,Lsam2det,dety00,detz00,RotAxisOffset, ...
pixelysize,pixelzsize,dety0,detz0,detysize,detzsize,BeamStopY,BeamStopZ);
NrSpotExpectAll=NrSpotExpectAll+NrSpotExpect;
% find matched diffraction vectors
[~,Gt_matched]=find_Gt_match_fun(Gt_spot,Gt,hkl,hkl_family_square,Glen_possible,rot,dety,detz);
Gt_matched_all=[Gt_matched_all;Gt_matched];
end
if ~isempty(Gt_matched_all)
Nr_match=length(Gt_matched_all(:,1));
Gt_matched_all_mean=mean(Gt_matched_all(:,7));
Gt_matched_all_median=median(Gt_matched_all(:,7));
else
Nr_match=0;
Gt_matched_all_mean=NaN;
Gt_matched_all_median=NaN;
end
end
function [Gt_match,Gt_matched]=find_Gt_match_fun(Gt_spot,Gt,hkl,hkl_family_square,Glen_possible,rot,dety,detz)
% Gt_spot: list of Glab vectors calculated from the spot peaks;
% Gt: list of Glab vectors calculated from the forward simulation given a specific orientation
% Sep 24, 2021
for i=1:length(Gt_spot)
Gt_spot_norm{i}=Gt_spot{i}./sqrt((Gt_spot{i}(:,1)).^2+(Gt_spot{i}(:,2)).^2+(Gt_spot{i}(:,3)).^2);
end
Gt_match=zeros(length(Gt(:,1)),17);
Gt_match(:,1:3)=Gt./sqrt(Gt(:,1).^2+Gt(:,2).^2+Gt(:,3).^2);
Gt_match(:,4)=sqrt(Gt(:,1).^2+Gt(:,2).^2+Gt(:,3).^2);
% angle_threshold=2.7; % minimum angle to accept as correct match, should be consistent with the mis-angle of discretized OR
angle_threshold=1;
for i=1:length(Gt_match(:,1))
switch sum(hkl(:,i).^2)
case hkl_family_square(1)
ang=acos(sum(repmat(Gt_match(i,1:3),length(Gt_spot_norm{1}(:,1)),1).*Gt_spot_norm{1},2))*180/pi; % angle betwee [deg]
[ang_min,MatchSpotID]=min(ang);
if ang_min<angle_threshold && abs(Gt_match(i,4)-Glen_possible(1))<0.1
Gt_match(i,5)=1; % find a matched experimental diffraction vector
Gt_match(i,6)=MatchSpotID; % matched spot ID
Gt_match(i,7)=ang_min;
Gt_match(i,8:10)=Gt_spot_norm{1}(MatchSpotID,:); % Gt_spot
Gt_match(i,11)=Glen_possible(1); % Gt_spot length
Gt_match(i,12:14)=hkl(:,i)'; % (hkl)
Gt_match(i,15)=rot; % [deg]
Gt_match(i,16:17)=[dety(MatchSpotID) detz(MatchSpotID)]; % [dety, detz] [pixel]
end
case hkl_family_square(2)
ang=acos(sum(repmat(Gt_match(i,1:3),length(Gt_spot_norm{2}(:,1)),1).*Gt_spot_norm{2},2))*180/pi;
[ang_min,MatchSpotID]=min(ang);
if ang_min<angle_threshold && abs(Gt_match(i,4)-Glen_possible(2))<0.1
Gt_match(i,5)=1; % find a matched experimental diffraction vector
Gt_match(i,6)=MatchSpotID; % matched spot ID
Gt_match(i,7)=ang_min;
Gt_match(i,8:10)=Gt_spot_norm{2}(MatchSpotID,:); % Gt_spot
Gt_match(i,11)=Glen_possible(2); % Gt_spot length
Gt_match(i,12:14)=hkl(:,i)'; % (hkl)
Gt_match(i,15)=rot; % [deg]
Gt_match(i,16:17)=[dety(MatchSpotID) detz(MatchSpotID)]; % [dety, detz] [pixel]
end
case hkl_family_square(3)
ang=acos(sum(repmat(Gt_match(i,1:3),length(Gt_spot_norm{3}(:,1)),1).*Gt_spot_norm{3},2))*180/pi;
[ang_min,MatchSpotID]=min(ang);
if ang_min<angle_threshold && abs(Gt_match(i,4)-Glen_possible(3))<0.1
Gt_match(i,5)=1; % find a matched experimental diffraction vector
Gt_match(i,6)=MatchSpotID; % matched spot ID
Gt_match(i,7)=ang_min;
Gt_match(i,8:10)=Gt_spot_norm{3}(MatchSpotID,:); % Gt_spot
Gt_match(i,11)=Glen_possible(3); % Gt_spot length
Gt_match(i,12:14)=hkl(:,i)'; % (hkl)
Gt_match(i,15)=rot; % [deg]
Gt_match(i,16:17)=[dety(MatchSpotID) detz(MatchSpotID)]; % [dety, detz] [pixel]
end
case hkl_family_square(4)
ang=acos(sum(repmat(Gt_match(i,1:3),length(Gt_spot_norm{4}(:,1)),1).*Gt_spot_norm{4},2))*180/pi;
[ang_min,MatchSpotID]=min(ang);
if ang_min<angle_threshold && abs(Gt_match(i,4)-Glen_possible(4))<0.1
Gt_match(i,5)=1; % find a matched experimental diffraction vector
Gt_match(i,6)=MatchSpotID; % matched spot ID
Gt_match(i,7)=ang_min;
Gt_match(i,8:10)=Gt_spot_norm{4}(MatchSpotID,:); % Gt_spot
Gt_match(i,11)=Glen_possible(4); % Gt_spot length
Gt_match(i,12:14)=hkl(:,i)'; % (hkl)
Gt_match(i,15)=rot; % [deg]
Gt_match(i,16:17)=[dety(MatchSpotID) detz(MatchSpotID)]; % [dety, detz] [pixel]
end
end
end
Gt_matched=Gt_match(Gt_match(:,5)==1,:);
end