1 ## Copyright (C) 2008 Carlo de Falco
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 ## @deftypefn {Function File} {} FPL2vtkoutputdata ( @var{filename}, @var{p}, @var{t}, @var{nodedata}, @var{celldata}, @var{header}, @var{vtkver})
21 ## Save data in VTK ASCII format.
24 ## @item @var{filename} = name of file to save into
25 ## @item @var{p}, @var{t} = mesh node coordinates and connectivity
26 ## @item @var{name} = name of a mesh variable
27 ## @item @var{nodedata}/@var{celldata} = node/cell centered data
28 ## fields (2xNfields cell array), @var{*data}@{:,1@} = variable names;
29 ## @var{*data}@{:,2@} = variable data;
30 ## @item @var{header} comment to add in the file header
31 ## @item @var{vtkver} format version (default is 3.0)
34 ## @seealso{FPL2dxoutputdata}
37 function FPL2vtkoutputdata (filename, p, t, nodedata, celldata, header, vtkver)
39 fid = fopen (filename, "w");
47 fprintf (fid, "# vtk DataFile Version %d.%d\n", vtkver(1), vtkver(2));
52 elseif (length(header) > 255)
53 header (255:end) = [];
56 fprintf (fid, "%s\n", header);
58 ## File format: only ASCII supported for the moment
59 fprintf (fid, "ASCII\n");
61 ## Mesh: only triangles suported
62 fprintf (fid, "DATASET UNSTRUCTURED_GRID\n");
65 fprintf (fid,"POINTS %d double\n", Nnodes);
66 fprintf (fid,"%g %g 0\n", p);
71 T(2:4,:) = t(1:3,:) -1;
72 fprintf (fid,"CELLS %d %d\n", Nelem, Nelem*4);
73 fprintf (fid,"%d %d %d %d\n", T);
74 fprintf (fid,"CELL_TYPES %d \n", Nelem);
75 fprintf (fid,"%d\n", 5*ones(Nelem,1));
78 if (exist("nodedata"))
79 nfields = rows(nodedata);
81 fprintf (fid,"POINT_DATA %d\n", Nnodes);
82 for ifield = 1:nfields
83 V = nodedata {ifield, 2};
84 N = nodedata {ifield, 1};
86 fprintf (fid,"SCALARS %s double\nLOOKUP_TABLE default\n", N);
87 fprintf (fid,"%g\n", V);
90 fprintf (fid,"VECTORS %s double\nLOOKUP_TABLE default\n", N);
91 fprintf (fid,"%g %g %g\n", V);
98 if (exist("celldata"))
99 nfields = rows(celldata);
101 fprintf (fid,"CELL_DATA %d\n", Nelem);
102 for ifield = 1:nfields
103 V = celldata {ifield, 2};
104 N = celldata {ifield, 1};
106 fprintf (fid,"SCALARS %s double\nLOOKUP_TABLE default\n", N);
107 fprintf (fid,"%g\n", V);
110 fprintf (fid,"VECTORS %s double\nLOOKUP_TABLE default\n", N);
111 fprintf (fid,"%g %g %g\n", V);
120 error(["FPL2vtkoutputdata: could not open file " filename]);
125 %! msh.p =[ 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
126 %! 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3];
127 %! msh.e =[1 5 9 13 14 15 4 8 12 1 2 3
128 %! 5 9 13 14 15 16 8 12 16 2 3 4
129 %! 0 0 0 0 0 0 0 0 0 0 0 0
130 %! 0 0 0 0 0 0 0 0 0 0 0 0
131 %! 1 1 1 2 2 2 3 3 3 4 4 4
132 %! 0 0 0 0 0 0 0 0 0 0 0 0
133 %! 1 1 1 1 1 1 1 1 1 1 1 1];
134 %! msh.t =[ 1 2 3 5 6 7 9 10 11 1 2 3 5 6 7 9 10 11
135 %! 5 6 7 9 10 11 13 14 15 6 7 8 10 11 12 14 15 16
136 %! 6 7 8 10 11 12 14 15 16 2 3 4 6 7 8 10 11 12
137 %! 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
138 %! testfile = "# vtk DataFile Version 3.0\n\nASCII\nDATASET UNSTRUCTURED_GRID\nPOINTS 16 double\n0 0 0\n0 1 0\n0 2 0\n0 3 0\n1 0 0\n1 1 0\n1 2 0\n1 3 0\n2 0 0\n2 1 0\n2 2 0\n2 3 0\n3 0 0\n3 1 0\n3 2 0\n3 3 0\nCELLS 18 72\n3 0 4 5\n3 1 5 6\n3 2 6 7\n3 4 8 9\n3 5 9 10\n3 6 10 11\n3 8 12 13\n3 9 13 14\n3 10 14 15\n3 0 5 1\n3 1 6 2\n3 2 7 3\n3 4 9 5\n3 5 10 6\n3 6 11 7\n3 8 13 9\n3 9 14 10\n3 10 15 11\nCELL_TYPES 18 \n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\n5\nPOINT_DATA 16\nSCALARS u double\nLOOKUP_TABLE default\n0\n0\n0\n0\n1\n1\n1\n1\n2\n2\n2\n2\n3\n3\n3\n3\n";
139 %! FPL2vtkoutputdata ("__FPL2vtkoutputdata__test__.vtk", msh.p, msh.t, {"u", msh.p(1,:).'});
140 %! fid = fopen("__FPL2vtkoutputdata__test__.vtk","r");
143 %! assert (char(s'), testfile);
144 %! delete __FPL2vtkoutputdata__test__.vtk