Debian Codegraph @

packages/rol/example/PDE-OPT/flow-opt/3dim/models/darcy/vtkwritecell.m

raw
1 function vtkwritecell(filename, t, p, u, c, varname)
2 % vtkwrite
3 % creates a vtk file filename.vtk containing mesh and vertex data
4 % supported cell types: 'Line'
5 % 'Triangle'
6 % 'Quadrilateral'
7 % 'Tetrahedron'
8 % 'Hexahedron'
9 %
10 % input: filename vtk file destination
11 % (string)
12 % t mesh connectivity array
13 % (M x nvpc array, where M denotes the number of cells
14 % and nvpc is the number of vertices per cell )
15 % p coordinates of the mesh vertices
16 % (N x d array, where N is the number of vertices and d the dimension)
17 % u scalar values associated with each vertex
18 % (N x 1 array)
19 % c cell type
20 % (string)
21 % varname variable name
22 %
23 % example usage:
24 % 2d: [X,Y] = meshgrid(0:0.01:1, 0:0.01:1);
25 % p = [reshape(X,prod(size(X)),1), reshape(Y,prod(size(Y)),1)];
26 % t = delaunayn(p);
27 % u = peaks(6*p(:,1)-3,6*p(:,2)-3);
28 % vtkwrite('test2d', t, p, u, 'Triangle');
29 % 3d: p = rand(10, 3);
30 % t = delaunayn(p);
31 % u = sum(p.^2, 2);
32 % vtkwrite('test3d', t, p, u, 'Tetrahedron');
33 %
34
35 % Extract array sizes.
36 [np, dim] = size(p); % number of mesh vertices, dimension
37 [nc, nvpc] = size(t); % number of cells, number of vertices per cell
38
39 % These are VTK-specific mappings of topologies to integer designators.
40 ctm = containers.Map();
41 ctm('Line') = 3;
42 ctm('Triangle') = 5;
43 ctm('Quadrilateral') = 9;
44 ctm('Tetrahedron') = 10;
45 ctm('Hexahedron') = 12;
46
47 % Write preamble.
48 FID = fopen(strcat(filename, '.vtk'), 'w+');
49 fprintf(FID, '# vtk DataFile Version 2.0\nUnstructured Grid Example\nASCII\n');
50 fprintf(FID, 'DATASET UNSTRUCTURED_GRID\n');
51
52 % Write points -- always pad for 3D.
53 fprintf(FID, 'POINTS %d float\n', np);
54 s = '%18.16e %18.16e %18.16e \n';
55 P = [p zeros(np,3-dim)];
56 fprintf(FID, s, P');
57
58 % Write number of vertices per cell + full cell connectivity.
59 fprintf(FID, 'CELLS %d %d\n', nc, nc*(nvpc+1));
60 s = '%d ';
61 for k = 1:nvpc
62 s = horzcat(s, {' %d'});
63 end
64 s = cell2mat(horzcat(s, {' \n'}));
65 fprintf(FID, s, [nvpc*ones(nc,1) t-1]');
66
67 % Write cell types.
68 fprintf(FID, 'CELL_TYPES %d\n', nc);
69 s = '%d\n';
70 fprintf(FID, s, ctm(c)*ones(nc,1));
71
72 % Write data.
73 if (size(u,2) == 1)
74 fprintf(FID, 'CELL_DATA %s\nSCALARS ', num2str(nc));
75 fprintf(FID, varname);
76 fprintf(FID, ' float 1\nLOOKUP_TABLE default\n');
77 s = '%18.16e\n';
78 fprintf(FID, s, u');
79 end
80
81 if (size(u,2) == 3)
82 fprintf(FID, 'CELL_DATA %s\nVECTORS ', num2str(nc));
83 fprintf(FID, varname);
84 fprintf(FID, ' float\n');
85 s = '%18.16e %18.16e %18.16e\n';
86 fprintf(FID, s, u');
87 end
88
89 % Done.
90 fclose(FID);