-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriteStag3D_VTK_Cartesian_290611.m
584 lines (500 loc) · 19.9 KB
/
WriteStag3D_VTK_Cartesian_290611.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
% Write STAG_3D format from matlab in XML-VTK format, using ASCII or BINARY file
% format
%
%
%
% Boris Kaus, 21 Feb. 2008
ASCII = logical(0); % ASCII or BINARY?
% Change to correct directory
start_dir = pwd;
cd(directory);
% Generate input data in correct format
Points = single([X_3D(:) Y_3D(:) Z_3D(:)]);
T_3D = single(T_3D);
% Transform the arrays in vector-format, and change them to the appropriate
% precision. This example file assumes that data comes in single precision
%
% Double precision requires changing in ths code below (Float32 into Float64)
T = single(T_3D(:)); % Temperature
Points = [single(X_3D(:)) single(Y_3D(:)) single(Z_3D(:)) ]; % Coordinates
% Define which arrays to write
if WriteVelocity
P = single(P_3D(:)); % Pressure
Velocity = [single(VX_3D(:)) single(VY_3D(:)) single(VZ_3D(:)) ]; % Velocity % Velocity @ every point
end
if WriteViscosity
Eta = single(ETA_3D(:)); % Viscosity
end
if WriteComposition
Composition = single(C_3D(:)); % Composition
end
if WriteContRoot
Composition1 = single(C1_3D(:)); % Continental Root
end
if WriteContCrust
Composition2 = single(C2_3D(:)); % Composition
end
if WriteContBelt
Composition3 = single(C3_3D(:)); % Composition
end
if WriteContSuture
Composition4 = single(C4_3D(:)); % Composition
end
if WriteContPlot
ContPlot = single(CPL_3D(:)); % Composition
end
if WriteContinent
Continent = single(NRC_3D(:)); % Composition
end
if WriteTopography
Topography = single(TP_3D(:));
end
if WriteTopoSG
TopoSG = single(TPSG_3D(:));
end
if WriteGeoid
Geoid = single(G_3D(:));
end
if WriteStress
Stress = single(S_3D(:)); % Stress
end
if WriteEdot
Strainrate = single(E_3D(:)); % Strainrate
end
if WriteDamage
Damage = single(D_3D(:)); % Strainrate
end
if WriteAge
Age = single(AGE_3D(:)); % Strainrate
end
%==========================================================================
% Definitions and initialization
sizeof_Float32 = 4;
sizeof_Float64 = 4;
sizeof_UInt32 = 4;
Offset = 0; % Initial offset
%--------------------------------------------------------------------------
% Write the header for a structured grid:
%--------------------------------------------------------------------------
fname_vtk = [fname,'_',num2str(1000000+fname_number),'.vts'];
fid = fopen(fname_vtk,'w','b'); % note the 'b': not doing BigEndian does not work with MATLAB!
fprintf(fid,'<?xml version="1.0"?> \n');
fprintf(fid,'<VTKFile type="StructuredGrid" version="0.1" byte_order="BigEndian" >\n');
fprintf(fid,' <StructuredGrid WholeExtent="%i %i %i %i %i %i">\n', [0 size(X_3D,1)-1 0 size(X_3D,2)-1 0 size(X_3D,3)-1]);
fprintf(fid,' <Piece Extent="%i %i %i %i %i %i">\n', [0 size(X_3D,1)-1 0 size(X_3D,2)-1 0 size(X_3D,3)-1]);
%--------------------------------------------------------------------------
% Add point-wise data
%--------------------------------------------------------------------------
fprintf(fid,' <PointData Scalars="T" Vectors="Velocity" >\n');
% TEMPERATURE -----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="T" format="ascii">\n');
for i=1:length(T)
fprintf(fid,' %g \n',single(T(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="T" format="appended" offset="%i">\n', int32(Offset));
Offset = Offset + length(T(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
if WriteViscosity
% VISCOSITY---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Viscosity" format="ascii">\n');
for i=1:length(Eta)
fprintf(fid,' %g \n',single(Eta(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Viscosity" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Eta(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteComposition
% COMPOSITION---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Composition" format="ascii">\n');
for i=1:length(Composition)
fprintf(fid,' %g \n',single(Composition(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Composition" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Composition(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContRoot
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Cont Root" format="ascii">\n');
for i=1:length(Composition1)
fprintf(fid,' %g \n',single(Composition1(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Cont Root" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Composition1(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContCrust
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Cont Crust" format="ascii">\n');
for i=1:length(Composition2)
fprintf(fid,' %g \n',single(Composition2(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Cont Crust" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Composition2(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContBelt
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Cont Belt" format="ascii">\n');
for i=1:length(Composition3)
fprintf(fid,' %g \n',single(Composition3(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Cont Belt" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Composition3(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContSuture
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Cont Suture" format="ascii">\n');
for i=1:length(Composition4)
fprintf(fid,' %g \n',single(Composition4(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Cont Suture" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Composition4(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContPlot
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="cont plot" format="ascii">\n');
for i=1:length(ContPlot)
fprintf(fid,' %g \n',single(ContPlot(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="cont plot" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(ContPlot(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteContinent
% COMPOSITION1-----------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Continent" format="ascii">\n');
for i=1:length(Continent)
fprintf(fid,' %g \n',single(Continent(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Continent" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Continent(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteTopography
% Topography---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Topography" format="ascii">\n');
for i=1:length(Topography)
fprintf(fid,' %g \n',single(Topography(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Topography" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Topography(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteTopoSG
% Topography self-grav ---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Topo self-grav" format="ascii">\n');
for i=1:length(TopoSG)
fprintf(fid,' %g \n',single(TopoSG(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Topo self-grav" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(TopoSG(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteGeoid
% Geoid---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Geoid" format="ascii">\n');
for i=1:length(Geoid)
fprintf(fid,' %g \n',single(Geoid(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Geoid" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Geoid(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteStress
% STRESS---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Stress" format="ascii">\n');
for i=1:length(Stress)
fprintf(fid,' %g \n',single(Stress(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Stress" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Stress(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteEdot
% STRAIN RATE---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Strain rate" format="ascii">\n');
for i=1:length(Strainrate)
fprintf(fid,' %g \n',single(Strainrate(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Strainrate" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Strainrate(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteDamage
% Damage---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Damage" format="ascii">\n');
for i=1:length(Damage)
fprintf(fid,' %g \n',single(Damage(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Damage" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Damage(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteAge
% Damage---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Age" format="ascii">\n');
for i=1:length(Age)
fprintf(fid,' %g \n',single(Age(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Age" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Age(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
if WriteVelocity
% PRESSURE---------------
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="P" format="ascii">\n');
for i=1:length(P)
fprintf(fid,' %g \n',single(P(i)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="P" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(P(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
% VELOCITY--------------- : VELOCITY IS A 3-component vector
if ASCII
% ASCII:
fprintf(fid,' <DataArray type="Float32" Name="Velocity" NumberOfComponents="3" format="ascii">\n');
for i=1:length(T)
fprintf(fid,' %g %g %g \n',single(Velocity(i,:)));
end
else
% BINARY:
fprintf(fid,' <DataArray type="Float32" Name="Velocity" NumberOfComponents="3" format="appended" offset="%i">\n',int32(Offset));
Offset = Offset + length(Velocity(:))*sizeof_Float32 + 1*sizeof_UInt32;
end
fprintf(fid,' </DataArray>\n');
% -----------------------
end
fprintf(fid,' </PointData>\n');
%--------------------------------------------------------------------------
%
%--------------------------------------------------------------------------
fprintf(fid,' <Celldata>\n');
fprintf(fid,' </Celldata>\n');
%--------------------------------------------------------------------------
% Add coordinates of structured grid
%--------------------------------------------------------------------------
fprintf(fid,' <Points>\n');
% ASCII
if ASCII
fprintf(fid,' <DataArray type="Float32" Name="Array" NumberOfComponents="3" format="ascii">\n');
for i=1:size(Points,1)
fprintf(fid,' %g %g %g \n',Points(i,:));
end
else
fprintf(fid,' <DataArray type="Float32" Name="Array" NumberOfComponents="3" format="appended" offset="%i" >\n',int32(Offset));
end
fprintf(fid,' </DataArray>\n');
fprintf(fid,' </Points>\n');
%--------------------------------------------------------------------------
fprintf(fid,' </Piece> \n');
fprintf(fid,' </StructuredGrid> \n');
if ~ASCII
% Append binary data in raw format: the order in which data arrays are
% added should be the same as how they are defined above
fprintf(fid,' <AppendedData encoding="raw"> \n');
fprintf(fid,'_');
% Add Temperature in binary format
fwrite(fid,uint32(length(T)*sizeof_Float32),'uint32');
fwrite(fid,single(T).' , 'float32');
if WriteViscosity
% Add Viscosity in binary format
fwrite(fid,uint32(length(Eta)*sizeof_Float32),'uint32');
fwrite(fid,single(Eta).' , 'float32');
end
if WriteComposition
% Add Composition in binary format
fwrite(fid,uint32(length(Composition)*sizeof_Float32),'uint32');
fwrite(fid,single(Composition).' , 'float32');
end
if WriteContRoot
% Add Composition in binary format
fwrite(fid,uint32(length(Composition1)*sizeof_Float32),'uint32');
fwrite(fid,single(Composition1).' , 'float32');
end
if WriteContCrust
% Add Composition in binary format
fwrite(fid,uint32(length(Composition2)*sizeof_Float32),'uint32');
fwrite(fid,single(Composition2).' , 'float32');
end
if WriteContBelt
% Add Composition in binary format
fwrite(fid,uint32(length(Composition3)*sizeof_Float32),'uint32');
fwrite(fid,single(Composition3).' , 'float32');
end
if WriteContSuture
% Add Composition in binary format
fwrite(fid,uint32(length(Composition4)*sizeof_Float32),'uint32');
fwrite(fid,single(Composition4).' , 'float32');
end
if WriteContPlot
% Add Composition in binary format
fwrite(fid,uint32(length(ContPlot)*sizeof_Float32),'uint32');
fwrite(fid,single(ContPlot).' , 'float32');
end
if WriteContinent
% Add Composition in binary format
fwrite(fid,uint32(length(Continent)*sizeof_Float32),'uint32');
fwrite(fid,single(Continent).' , 'float32');
end
if WriteTopography
% Add Topography in binary format
fwrite(fid,uint32(length(Topography)*sizeof_Float32),'uint32');
fwrite(fid,single(Topography).' , 'float32');
end
if WriteTopoSG
% Add TopoSG in binary format
fwrite(fid,uint32(length(TopoSG)*sizeof_Float32),'uint32');
fwrite(fid,single(TopoSG).' , 'float32');
end
if WriteGeoid
% Add Geoid in binary format
fwrite(fid,uint32(length(Geoid)*sizeof_Float32),'uint32');
fwrite(fid,single(Geoid).' , 'float32');
end
if WriteStress
% Add Stress in binary format
fwrite(fid,uint32(length(Stress)*sizeof_Float32),'uint32');
fwrite(fid,single(Stress).' , 'float32');
end
if WriteEdot
% Add Strainrate in binary format
fwrite(fid,uint32(length(Strainrate)*sizeof_Float32),'uint32');
fwrite(fid,single(Strainrate).' , 'float32');
end
if WriteDamage
% Add Strainrate in binary format
fwrite(fid,uint32(length(Damage)*sizeof_Float32),'uint32');
fwrite(fid,single(Damage).' , 'float32');
end
if WriteAge
% Add Age in binary format
fwrite(fid,uint32(length(Age)*sizeof_Float32),'uint32');
fwrite(fid,single(Age).' , 'float32');
end
if WriteVelocity
% Add Pressure in binary format
fwrite(fid,uint32(length(P)*sizeof_Float32),'uint32');
fwrite(fid,single(P).' , 'float32');
% Add Velocity in binary format
fwrite(fid,uint32(length(Velocity(:))*sizeof_Float32),'uint32');
fwrite(fid,single(Velocity).' , 'float32');
end
% Add Coordinates in binary format
fwrite(fid,uint32(length(Points(:))*sizeof_Float32),'uint32');
fwrite(fid,single(Points).' , 'float32');
fprintf(fid,' </AppendedData> \n');
end
fprintf(fid,'</VTKFile>\n');
fclose(fid);
cd(start_dir)
if ~ASCII
% disp(['Created Binary XML-VTK output file ',fname_vtk]);
else
% disp(['Created ASCII XML-VTK output file ',fname_vtk]);
end