-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadStag3D_Lea.m
386 lines (321 loc) · 13.1 KB
/
ReadStag3D_Lea.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
function [varargout] = ReadStag3D_Lea(directory, fname_input, fname_number,Type)% Reads a STAG3D output file and transforms it into a MATLAB file
%
% Tested for 3D cartesian
%
%
% Syntax:
% For scalar fields:
%
% [X_3D, Y_3D, Z_3D, DATA_3D] = ReadStag3D(fname_input, fname_number, {'viscosity','temperature'});
%
% For vector fields:
%
% [X_3D, Y_3D, Z_3D, VX_3D, VY_3D, VZ_3D, P_3D] = ReadStag3D(fname_input, fname_number, {'viscosity','temperature'});
%
%
% Boris Kaus
%
% Modifications by Paul Tackley August 2010:
% - extra fields: topography, topography self-grav, crustal
% thickness, age, strain rate, geoid
% - unimplemented 2Dfield stuff deleted: 3D routine works for 2D fields
% - velocities & p now read into 4D arrays: yy split is done on output
% - correction to the storage order of v* and p
% - checks magic whether #components matches nval
% - compatibility with magic=9 storing extra x & y points for vectors
% - added missing scaling factor read for vectors
%
%
% Adapted by Lea Bello and Marie Bocher to be used in Error_Temp (for forecasting time calculation)
%
start_dir = pwd;
cd(directory);
FileFormat = 'n'; % native - default
%FileFormat = 'l'; % Little Endian
%FileFormat = 'b'; % Big Endian
if fname_number<10000
number_string = num2str(10000+fname_number);
number_string(1)='0';
else
number_string = num2str(10000+fname_number);
end
switch Type
case 'velocity'
fname = [fname_input,'_vp',number_string];
scalardata = false;
case 'residue'
fname = [fname_input,'_rs',number_string];
scalardata = false;
case 'temperature'
fname = [fname_input,'_t',number_string];
scalardata = true;
case 'viscosity'
fname = [fname_input,'_eta',number_string];
scalardata = true;
case 'composition'
fname = [fname_input,'_comp',number_string];
scalardata = true;
case 'cont root'
fname = [fname_input,'_c',number_string];
scalardata = true;
case 'cont crust'
fname = [fname_input,'_cc',number_string];
scalardata = true;
case 'cont belt'
fname = [fname_input,'_ccc',number_string];
scalardata = true;
case 'cont suture'
fname = [fname_input,'_cccc',number_string];
scalardata = true;
case 'cont plot'
fname = [fname_input,'_cpl',number_string];
scalardata = true;
case 'continent'
fname = [fname_input,'_nrc',number_string];
scalardata = true;
case 'melt fraction'
fname = [fname_input,'_f',number_string];
scalardata = true;
case 'topography'
fname = [fname_input,'_cs',number_string];
scalardata = true;
case 'topography self-grav'
fname = [fname_input,'_csg',number_string];
scalardata = true;
case 'crustal thickness'
fname = [fname_input,'_cr',number_string];
scalardata = true;
case 'age'
fname = [fname_input,'_age',number_string];
scalardata = true;
case 'strain rate'
fname = [fname_input,'_ed',number_string];
scalardata = true;
case 'geoid'
fname = [fname_input,'_g',number_string];
scalardata = true;
case 'stress'
fname = [fname_input,'_str',number_string];
scalardata = true;
case 'damage'
fname = [fname_input,'_d',number_string];
scalardata = true;
case 'heat flux'
fname = [fname_input,'_hf',number_string];
scalardata = true;
otherwise
error('Unknown property')
end
if ~exist(fname)
% The file does not exist and we should stop processing data
cd(start_dir);
for i=1:10
varargout{i} = -999;
end
return
end
if scalardata
nval = 1; % temperature has only one value
else
nval = 4; % assumed that we have a velocity-pressure file
end
fid = fopen(fname,'r',FileFormat); % Open File
%==========================================================================
% READ HEADER
%==========================================================================
magic = fread(fid,1,'int32'); % Version
if (magic<100 && nval>1) || (magic>300 && nval==1) % check #components
error('wrong number of components in field')
end
magic = mod(magic,100);
if magic>=9 && nval==4
xyp = 1; % extra ghost point in x & y direction
else
xyp = 0;
end
nxtot = fread(fid,1,'int32'); % nx total
nytot = fread(fid,1,'int32'); % ny total
nztot = fread(fid,1,'int32'); % nz total
nblocks = fread(fid,1,'int32'); % # of blocks, 2 for yinyang
Aspect = fread(fid,2,'single'); % Aspect ratio
nnx = fread(fid,1,'int32'); % Number of parallel subdomains
nny = fread(fid,1,'int32'); % in the x,y,z and b directions
nnz = fread(fid,1,'int32'); %
nnb = fread(fid,1,'int32'); %
nz2 = nztot*2 + 1;
zg = fread(fid,nz2,'single'); % z-coordinates
% compute nx, ny, nz and nb PER CPU
nx = nxtot/nnx;
ny = nytot/nny;
nz = nztot/nnz;
nb = nblocks/nnb;
npi = (nx+xyp)*(ny+xyp)*nz*nb*nval; % the number of values per 'read' block
rcmb = fread(fid,1,'single');
istep = fread(fid,1,'int32');
time = fread(fid,1,'single');
erupta_total= fread(fid,1,'single');
botT_val = fread(fid,1,'single');
x = fread(fid,nxtot,'single'); % x-coordinates
y = fread(fid,nytot,'single'); % y-coordinates
z = fread(fid,nztot,'single'); % z-coordinates
% read the parallel blocks
if scalardata
DATA_3D = zeros(nxtot,nytot,nztot);
else
scalefac= fread(fid,1,'single'); % scale factor
VX_3D = zeros(nxtot,nytot,nztot); % Vx
VY_3D = zeros(nxtot,nytot,nztot); % Vy
VZ_3D = zeros(nxtot,nytot,nztot); % Vz
P_3D = zeros(nxtot,nytot,nztot); % Pressure
end
for ibc=1:nnb % loop over parallel subdomains
for izc =1:nnz
for iyc =1:nny
for ixc =1:nnx
data_CPU = fread(fid,npi,'single'); % read the data for this CPU
% Create a 3D matrix from these data
if scalardata
data_CPU_3D = reshape(data_CPU, [nx ny nz nb]) ;
else
data_CPU_3D = reshape(data_CPU*scalefac, [nval nx+xyp ny+xyp nz nb]);
end
% Add local 3D matrix to global matrix
if scalardata
% Scalar data
DATA_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz),(ibc-1)*nb + (1:nb)) = data_CPU_3D;
else
% velocity-pressure data
VX_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(1,1:nx,1:ny,:,:));
VY_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(2,1:nx,1:ny,:,:));
VZ_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(3,1:nx,1:ny,:,:));
P_3D( (ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(4,1:nx,1:ny,:,:));
RX_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(1,1:nx,1:ny,:,:));
RY_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(2,1:nx,1:ny,:,:));
RZ_3D((ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(3,1:nx,1:ny,:,:));
RP_3D( (ixc-1)*nx + (1:nx), (iyc-1)*ny + (1:ny), (izc-1)*nz + (1:nz), (ibc-1)*nb + (1:nb)) = squeeze(data_CPU_3D(4,1:nx,1:ny,:,:));
end
end
end
end
end
fclose(fid); % close file
[Y_3D, X_3D, Z_3D] = meshgrid(y,x,z);
%% Transform coordinates for Yin & Yang grids
RYin = Z_3D+rcmb;
ThYin = pi/4-X_3D;
PhiYin = Y_3D-3*pi/4;
XYin=RYin.*cos(ThYin).*cos(PhiYin);
YYin=RYin.*cos(ThYin).*sin(PhiYin);
ZYin = RYin.*sin(ThYin);
XYang=-XYin;
YYang=ZYin;
ZYang=YYin;
%% erase overlapping points
RedFlagYin=zeros(size(XYin));
RedFlagYang=zeros(size(XYang));
for kray=1:nztot
minYYang=min(min(YYang(:,:,kray)));
maxYYang=max(max(YYang(:,:,kray)));
YinDel=XYin(:,:,kray)<=0&YYin(:,:,kray)<=maxYYang&YYin(:,:,kray)>=minYYang;
RedFlagYin(:,:,kray)=YinDel;
YangDel=XYang(:,:,kray)>=0&ZYang(:,:,kray)<=max(max(ZYin(:,:,kray)))&ZYang(:,:,kray)>=min(min(ZYin(:,:,kray)));
RedFlagYang(:,:,kray)=YangDel;
end
RedFlag=[RedFlagYin RedFlagYang];
RYinYang=[RYin RYin];
CoordX=[XYin XYang];
CoordY=[YYin YYang];
CoordZ=[ZYin ZYang];
%% volume
VolYin=zeros(nxtot,nytot,nztot);
Vol=zeros(nxtot,2*nytot,nztot);
for i=1:nxtot
for k=1:nztot
dz(k)= zg(2*k+1)-zg(2*k-1);
VolYin(i,:,k)= dz(k) .* sin(pi/4+X_3D(i,:,k)).*(pi/(2*nxtot))^2 .* (rcmb+zg(2*k)).^2;
end
end
Vol=[VolYin VolYin]; %same evolution of the volume on yin and yang, decrasing with colatitude=pi/4+X_3D(i,:,k)
%Vol=Vol(~logical(RedFlag));
%% prepare output data
varargout{1} = Vol; % old version: varargout{1} = nblocks;
varargout{2} = ThYin; % old version: varargout{2} = X_3D etc
varargout{3} = PhiYin;
varargout{4} = RYinYang;
if nblocks==1
% no ying-yang
switch Type
case 'velocity'
varargout{5} = VX_3D;
varargout{6} = VY_3D;
varargout{7} = VZ_3D;
varargout{8} = P_3D;
varargout{9} = time;
varargout{10}= rcmb;
otherwise
varargout{5} = DATA_3D;
varargout{6} = time;
varargout{7} = rcmb;
end
else
% ying-yang grid
switch Type
case 'velocity'
varargout{5} = VX_3D(:,:,:,1);
varargout{6} = VY_3D(:,:,:,1);
varargout{7} = VZ_3D(:,:,:,1);
varargout{8} = RedFlag; %previous version: P_3D (:,:,:,1);
varargout{9} = VX_3D(:,:,:,2);
varargout{10} = VY_3D(:,:,:,2);
varargout{11} = VZ_3D(:,:,:,2);
varargout{12} = z; %previous version: P_3D (:,:,:,2);
varargout{13} = time;
varargout{14} = rcmb;
varargout{15} = zg;
%% calcul of vsurf
RedFlagsurf=[RedFlagYin(:,:,size(RedFlagYin,3)) RedFlagYang(:,:,size(RedFlagYang,3))];
VXsurf = [VX_3D(:,:,size(VX_3D,3),1) VX_3D(:,:,size(VX_3D,3),2)];
VYsurf = [VY_3D(:,:,size(VY_3D,3),1) VY_3D(:,:,size(VY_3D,3),2)];
VZsurf = [VZ_3D(:,:,size(VZ_3D,3),1) VZ_3D(:,:,size(VZ_3D,3),2)];
vsurf_tot=(VXsurf.^2 + VYsurf.^2 + VZsurf.^2);
vsurf_tot_real=vsurf_tot(~logical(RedFlagsurf));
%bottom:
%RedFlagsurf=[RedFlagYin(:,:,1) RedFlagYang(:,:,1)];
%VXsurf = [VX_3D(:,:,1,1) VX_3D(:,:,1,2)];
%VYsurf = [VY_3D(:,:,1,1) VY_3D(:,:,1,2)];
%vsurf_tot=(VXsurf.^2 + VYsurf.^2).^(1/2);
%vsurf_tot_real=vsurf_tot(~logical(RedFlagsurf));
varargout{16} = mean(vsurf_tot_real).^(1/2);
%% calcul of v
VX = [VX_3D(:,:,:,1) VX_3D(:,:,:,2)];
VY = [VY_3D(:,:,:,1) VY_3D(:,:,:,2)];
VZ = [VZ_3D(:,:,:,1) VZ_3D(:,:,:,2)];
v_tot=(VX.^2 + VY.^2 + VZ.^2);
varargout{17} = v_tot;
case 'residue'
varargout{5} = RX_3D(:,:,:,1);
varargout{6} = RY_3D(:,:,:,1);
varargout{7} = RZ_3D(:,:,:,1);
varargout{8} = RP_3D (:,:,:,1);
varargout{9} = RX_3D(:,:,:,2);
varargout{10} = RY_3D(:,:,:,2);
varargout{11} = RZ_3D(:,:,:,2);
varargout{12} = RP_3D (:,:,:,2);
varargout{13} = time;
varargout{14} = zg;
otherwise
varargout{5} = DATA_3D(:,:,:,1);
varargout{6} = DATA_3D(:,:,:,2);
DATA_tot = [DATA_3D(:,:,:,1) DATA_3D(:,:,:,2)];
varargout{7} = DATA_tot(~logical(RedFlag));
varargout{8} = RedFlag;
varargout{9} = XYin;
varargout{10} = YYin;
varargout{11} = ZYin;
varargout{12} = z; %previous version: P_3D (:,:,:,2);
varargout{13} = time;
varargout{14} = rcmb;
varargout{15} = zg;
end
end
cd(start_dir);