2010-09-28 08:33:42 +00:00
|
|
|
function Dump2VTK(filename, fields, mesh, fieldname)
|
|
|
|
% Dump2VTK(filename, fields, mesh, fieldname)
|
|
|
|
%
|
|
|
|
% Dump fields extraced from an hdf5 file to a vtk file format
|
|
|
|
%
|
|
|
|
% example:
|
|
|
|
%
|
|
|
|
%
|
|
|
|
% openEMS matlab interface
|
|
|
|
% -----------------------
|
|
|
|
% author: Thorsten Liebig
|
|
|
|
%
|
|
|
|
% See also ReadHDF5FieldData ReadHDF5Mesh GetField_TD2FD GetField_Interpolation
|
|
|
|
|
|
|
|
x = mesh.lines{1};
|
|
|
|
y = mesh.lines{2};
|
|
|
|
z = mesh.lines{3};
|
|
|
|
|
|
|
|
fid = fopen(filename,'w+');
|
2011-08-16 09:23:44 +00:00
|
|
|
|
|
|
|
% set nan values to zero
|
|
|
|
ind = find(isnan(fields));
|
|
|
|
if (~isempty(ind))
|
|
|
|
warning('openEMS:Dump2VTK','field contains nan, setting to zero');
|
|
|
|
fields(ind)=0;
|
|
|
|
end
|
|
|
|
|
|
|
|
% set inf values to zero
|
|
|
|
ind = find(isinf(fields));
|
|
|
|
if (~isempty(ind))
|
|
|
|
warning('openEMS:Dump2VTK','field contains inf, setting to zero');
|
|
|
|
fields(ind)=0;
|
|
|
|
end
|
|
|
|
|
2010-09-28 08:33:42 +00:00
|
|
|
if (mesh.type==0) %write cartesian mesh to vtk
|
|
|
|
fprintf(fid,'# vtk DataFile Version 2.0\n');
|
|
|
|
fprintf(fid,'Rectilinear Grid by matlab-interface of openEMS\n');
|
|
|
|
fprintf(fid,'ASCII\n');
|
|
|
|
fprintf(fid,'DATASET RECTILINEAR_GRID\n');
|
|
|
|
|
|
|
|
fprintf(fid,'DIMENSIONS %d %d %d\n',numel(x),numel(y),numel(z));
|
|
|
|
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'X_COORDINATES %d double\n',numel(x));
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,'%e',x(1));
|
2010-09-28 08:33:42 +00:00
|
|
|
for n=2:numel(x)
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,' %e',x(n));
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
fprintf(fid,'\n');
|
|
|
|
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'Y_COORDINATES %d double\n',numel(y));
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,'%e',y(1));
|
2010-09-28 08:33:42 +00:00
|
|
|
for n=2:numel(y)
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,' %e',y(n));
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
fprintf(fid,'\n');
|
|
|
|
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'Z_COORDINATES %d double\n',numel(z));
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,'%e',z(1));
|
2010-09-28 08:33:42 +00:00
|
|
|
for n=2:numel(z)
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,' %e',z(n));
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
elseif (mesh.type==1) %write cylindrical mesh to vtk
|
|
|
|
fprintf(fid,'# vtk DataFile Version 3.0\n');
|
|
|
|
fprintf(fid,'Structured Grid by matlab-interface of openEMS\n');
|
|
|
|
fprintf(fid,'ASCII\n');
|
|
|
|
fprintf(fid,'DATASET STRUCTURED_GRID\n');
|
|
|
|
|
|
|
|
fprintf(fid,'DIMENSIONS %d %d %d\n',numel(x),numel(y),numel(z));
|
|
|
|
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'POINTS %d double\n',numel(x)*numel(y)*numel(z));
|
2010-09-28 08:33:42 +00:00
|
|
|
|
|
|
|
for nz=1:numel(z)
|
|
|
|
for ny=1:numel(y)
|
|
|
|
for nx=1:numel(x)
|
2010-10-14 10:20:19 +00:00
|
|
|
fprintf(fid,'%e %e %e\n',x(nx)*cos(y(ny)),x(nx)*sin(y(ny)),z(nz));
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-26 07:23:49 +00:00
|
|
|
if (ndims(fields)==4)
|
|
|
|
[R A Z] = ndgrid(x,y,z);
|
|
|
|
sinA = sin(A);
|
|
|
|
cosA = cos(A);
|
|
|
|
field_CC(:,:,:,1) = fields(:,:,:,1) .* cosA - fields(:,:,:,2) .* sinA;
|
|
|
|
field_CC(:,:,:,2) = fields(:,:,:,1) .* sinA + fields(:,:,:,2) .* cosA;
|
|
|
|
field_CC(:,:,:,3) = fields(:,:,:,3);
|
|
|
|
fields = field_CC;
|
|
|
|
clear R A Z sinA cosA field_CC
|
|
|
|
end
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
fprintf(fid,'\n\n');
|
|
|
|
|
|
|
|
fprintf(fid,'POINT_DATA %d\n',numel(x)*numel(y)*numel(z));
|
2010-11-26 13:50:49 +00:00
|
|
|
% dump vector field data
|
2011-07-22 08:02:09 +00:00
|
|
|
if (size(fields,4)>1)
|
2010-11-26 13:50:49 +00:00
|
|
|
if (nargin>3)
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,['VECTORS ' fieldname ' double\n']);
|
2010-11-26 13:50:49 +00:00
|
|
|
else
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'VECTORS field double\n');
|
2010-11-26 13:50:49 +00:00
|
|
|
end
|
|
|
|
fclose(fid);
|
|
|
|
field_x = fields(:,:,:,1);
|
|
|
|
field_y = fields(:,:,:,2);
|
|
|
|
field_z = fields(:,:,:,3);
|
|
|
|
clear fields
|
|
|
|
dumpField(:,1) = field_x(:);
|
|
|
|
dumpField(:,2) = field_y(:);
|
|
|
|
dumpField(:,3) = field_z(:);
|
|
|
|
save('-ascii','-append',filename,'dumpField')
|
|
|
|
return
|
2011-07-22 08:02:09 +00:00
|
|
|
elseif (size(fields,4)==1) % scalar field
|
2010-11-26 13:50:49 +00:00
|
|
|
if (nargin>3)
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,['SCALARS ' fieldname ' double 1\nLOOKUP_TABLE default\n']);
|
2010-11-26 13:50:49 +00:00
|
|
|
else
|
2011-04-11 09:43:43 +00:00
|
|
|
fprintf(fid,'SCALARS field double 1\nLOOKUP_TABLE default\n');
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
2010-11-26 13:50:49 +00:00
|
|
|
fclose(fid);
|
|
|
|
dumpField = fields(:);
|
|
|
|
save('-ascii','-append',filename,'dumpField')
|
|
|
|
return
|
2010-09-28 08:33:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fclose(fid);
|