forked from uafgeotools/capuaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_MT.m
294 lines (274 loc) · 9.62 KB
/
convert_MT.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
function [Mout,T] = convert_MT(i1,i2,M,boption)
%CONVERT_MT convert moment tensor matrices among different bases
%
% This program converts between different moment tensor conventions.
% All conventions are associated with a local coordinate system.
%
% M = [M11 M22 M33 M12 M13 M23]
%
% INPUT
% i1 index of input moment tensor basis (see convert_getbasis.m)
% i2 index of output moment tensor basis (see convert_getbasis.m)
% M 6 x n set of moment tensors, M = [M11 M22 M33 M12 M13 M23]
% boption choice of how to calculate the change of basis
%
% OUTPUT
% Mout 6 x n set of moment tensors in basis of i2
% T transformation matrix to change basis of M from i1 to i2: Mout = T*M*T'
%
% See convert_getbasis.m for details
% Convention 1: up-south-east (GCMT)
% Convention 2: north-east-down (Aki and Richards, 1980)
% Convention 3: north-west-down (Stein and Wysession (2003)
% Convention 4: east-north-up
% Convention 5: south-east-up
%
% See also convertv.m, the vector version of this function.
%
% calls convert_getbasis.m
%
% Carl Tape, 11/2010
%
% two options to implement the transformation
% true: perform the linear algebra operation Mout = U*Min*U'
% false: swap entries and flip signs to achieve the correct transformation
% The 'true' option is mathematically cleanest but also has some
% possibility of introducing numerical errors.
if nargin < 4, boption = false; end
T = convert_getbasis(i1,i2);
if nargin==2
disp('returning transformation matrix only, as requested');
Mout = T;
return
end
% make sure M is 6 x n
[M,n] = Mdim(M);
if i1==i2
%error('i1 must differ from i2');
disp('warning: i1 = i2, so no change');
Mout = M;
return
end
Mout = []; % initialize
if boption % transformation matrix T is from convert_getbasis.m
% convert from 6 x n to 3 x 3 x n
Mmat = Mvec2Mmat(M,1);
% loop over each matrix and apply Mout = T*Min*T'
Moutmat = NaN(size(Mmat));
for ii=1:n
Moutmat(:,:,ii) = T*Mmat(:,:,ii)*T';
end
% convert back to 6 x n (note: this assumes that Mout is symmetric)
Mout = Mvec2Mmat(Moutmat,0);
else % WARNING: THESE FORMULAS ARE TIED TO SPECIFIC BASES
if i1==1
if i2==2 % up-south-east (GCMT) to north-east-down (AkiRichards) (AR, 1980, p. 118)
Mout(1,:) = M(2,:);
Mout(2,:) = M(3,:);
Mout(3,:) = M(1,:);
Mout(4,:) = -M(6,:);
Mout(5,:) = M(4,:);
Mout(6,:) = -M(5,:);
elseif i2==3 % up-south-east (GCMT) to north-west-up (/opt/seismo-util/bin/faultpar2cmtsol.pl)
Mout(1,:) = M(2,:);
Mout(2,:) = M(3,:);
Mout(3,:) = M(1,:);
Mout(4,:) = M(6,:);
Mout(5,:) = -M(4,:);
Mout(6,:) = -M(5,:);
elseif i2==4 % up-south-east (GCMT) to east-north-up
Mout(1,:) = M(3,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(1,:);
Mout(4,:) = -M(6,:);
Mout(5,:) = M(5,:);
Mout(6,:) = -M(4,:);
elseif i2==5 % up-south-east (GCMT) to south-east-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(3,:);
Mout(3,:) = M(1,:);
Mout(4,:) = M(6,:);
Mout(5,:) = M(4,:);
Mout(6,:) = M(5,:);
end
elseif i1==2
if i2==1 % north-east-down (AkiRichards) to up-south-east (GCMT) (AR, 1980, p. 118)
Mout(1,:) = M(3,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(2,:);
Mout(4,:) = M(5,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = -M(4,:);
elseif i2==3 % north-east-down (AkiRichards) to north-west-up
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = -M(5,:);
Mout(6,:) = M(6,:);
elseif i2==4 % north-east-down (AkiRichards) to east-north-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = M(4,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = -M(5,:);
elseif i2==5 % north-east-down (AkiRichards) to south-east-up
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = M(5,:);
Mout(6,:) = -M(6,:);
end
elseif i1==3
if i2==1 % north-west-up to up-south-east (GCMT)
Mout(1,:) = M(3,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(2,:);
Mout(4,:) = -M(5,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = M(4,:);
elseif i2==2 % north-west-up to north-east-down (AkiRichards)
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = -M(5,:);
Mout(6,:) = M(6,:);
elseif i2==4 % north-west-up to east-north-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = M(5,:);
elseif i2==5 % north-west-up to south-east-up
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = M(4,:);
Mout(5,:) = -M(5,:);
Mout(6,:) = -M(6,:);
end
elseif i1==4
if i2==1 % east-north-up to up-south-east (GCMT)
Mout(1,:) = M(3,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(1,:);
Mout(4,:) = -M(6,:);
Mout(5,:) = M(5,:);
Mout(6,:) = -M(4,:);
elseif i2==2 % east-north-up to north-east-down (AkiRichards)
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = M(4,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = -M(5,:);
elseif i2==3 % east-north-up to north-west-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = M(6,:);
Mout(6,:) = -M(5,:);
elseif i2==5 % east-north-up to south-east-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = -M(6,:);
Mout(6,:) = M(5,:);
end
elseif i1==5 % south-east-up to up-south-east (GCMT)
if i2==1
Mout(1,:) = M(3,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(2,:);
Mout(4,:) = M(5,:);
Mout(5,:) = M(6,:);
Mout(6,:) = M(4,:);
elseif i2==2 % south-east-up to north-east-down (AkiRichards)
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = M(5,:);
Mout(6,:) = -M(6,:);
elseif i2==3 % south-east-up to north-west-up
Mout(1,:) = M(1,:);
Mout(2,:) = M(2,:);
Mout(3,:) = M(3,:);
Mout(4,:) = M(4,:);
Mout(5,:) = -M(5,:);
Mout(6,:) = -M(6,:);
elseif i2==4 % south-east-up to east-north-up
Mout(1,:) = M(2,:);
Mout(2,:) = M(1,:);
Mout(3,:) = M(3,:);
Mout(4,:) = -M(4,:);
Mout(5,:) = M(6,:);
Mout(6,:) = -M(5,:);
end
end
end % boption
%==========================================================================
% EXAMPLES
if 0==1
% transformation matrix only
i1 = 1; i2 = 2;
T = convert_MT(i1,i2)
% simple example
i1 = 1; i2 = 2;
A = [1:6]'
M1 = convert_MT(i1,i2,A) % convert from i1 to i2
M2 = convert_MT(i2,i1,M1) % convert back from i2 to i1
% checking the transformation matrix
i1 = 1; i2 = 5;
M1 = rand(6,1);
[M2,T] = convert_MT(i1,i2,M1);
Mvec2Mmat(M1,1) % up-south-east
Mcheck = T*Mvec2Mmat(M1,1)*T' % south-east-up
Mvec2Mmat(M2,1) % (check)
% example vector v
v1 = rand(3,1) % up-south-east
v2 = T*v1 % south-east-up
% example symmetric matrix X1
X = randi(10,3); X1=X'*X, X2=T*X1*T'
% check the two different implementations (boption = true/false) for
% all possible change of bases
M1 = rand(6,1); % single symmetric matrix with random entries
NTYPE = 5;
for i1=1:NTYPE
for i2=2:NTYPE
if i2==i1, continue; end
M2true = convert_MT(i1,i2,M1,true);
M2false = convert_MT(i1,i2,M1,false);
[M2true M2false M2true-M2false]
if norm(M2true-M2false) > 1e-6
error('from %i to %i',i1,i2);
end
end
end
% % check all possible transformations
% M1 = rand(6,1); % single symmetric matrix with random entries
% NTYPE = 5;
% for i1=1:NTYPE
% for i2=2:NTYPE
% if i2==i1, continue; end
% [M2,T] = convert_MT(i1,i2,M1);
% if and(~isempty(T),~isempty(M2))
% Mcheck = T*Mvec2Mmat(M1,1)*T';
% ncheck = norm(Mvec2Mmat(M2,1)-Mcheck);
% % display info if the numerical check fails
% if ncheck > 1e-6
% disp(sprintf('from %i to %i',i1,i2));
% Mvec2Mmat(M2,1),Mcheck
% error('check')
% end
% end
% end
end
end
%==========================================================================