matlab: some new hdf5 field processing routines

pull/1/head
Thorsten Liebig 2010-09-28 10:33:42 +02:00
parent 8623e32925
commit 70345c32f0
4 changed files with 205 additions and 1 deletions

94
matlab/Dump2VTK.m Normal file
View File

@ -0,0 +1,94 @@
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+');
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));
fprintf(fid,'X_COORDINATES %d float\n',numel(x));
fprintf(fid,'%f',x(1));
for n=2:numel(x)
fprintf(fid,' %f',x(n));
end
fprintf(fid,'\n');
fprintf(fid,'Y_COORDINATES %d float\n',numel(y));
fprintf(fid,'%f',y(1));
for n=2:numel(y)
fprintf(fid,' %f',y(n));
end
fprintf(fid,'\n');
fprintf(fid,'Z_COORDINATES %d float\n',numel(z));
fprintf(fid,'%f',z(1));
for n=2:numel(z)
fprintf(fid,' %f',z(n));
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));
fprintf(fid,'POINTS %d float\n',numel(x)*numel(y)*numel(z));
for nz=1:numel(z)
for ny=1:numel(y)
for nx=1:numel(x)
fprintf(fid,'%f %f %f\n',x(nx)*cos(y(ny)),x(nx)*sin(y(ny)),z(nz));
end
end
end
[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
fprintf(fid,'\n\n');
fprintf(fid,'POINT_DATA %d\n',numel(x)*numel(y)*numel(z));
if (nargin>3)
fprintf(fid,['VECTORS ' fieldname ' float\n']);
else
fprintf(fid,'VECTORS field float\n');
end
for nz=1:numel(z)
for ny=1:numel(y)
for nx=1:numel(x)
fprintf(fid,'%f %f %f\n',fields(nx,ny,nz,1),fields(nx,ny,nz,2),fields(nx,ny,nz,3));
end
end
end
fclose(fid);

View File

@ -0,0 +1,80 @@
function [field_i mesh_i] = GetField_Interpolation(field, mesh, numLines, varargin)
% [field_i mesh_i] = GetField_Interpolation(field, mesh, numLines, varargin)
%
% example:
% [field mesh] = ReadHDF5Dump('Et.h5');
% %interpolate on a mesh with 21x21x101 lines
% [field_i mesh_i] = GetField_Interpolation(field, mesh, [21 21 101]);
%
% %or both steps in one with the same result:
% [field_i mesh_i] = ReadHDF5Dump('Et.h5','Interpolation', [21 21 101]);
%
% openEMS matlab interface
% -----------------------
% author: Thorsten Liebig
%
% See also ReadHDF5Dump ReadHDF5FieldData ReadHDF5Mesh
x = mesh.lines{1};
y = mesh.lines{2};
z = mesh.lines{3};
x_i = linspace(x(1),x(end),numLines(1));
y_i = linspace(y(1),y(end),numLines(2));
z_i = linspace(z(1),z(end),numLines(3));
field_i = field;
mesh_i = mesh;
mesh_i.lines{1} = x_i;
mesh_i.lines{2} = y_i;
mesh_i.lines{3} = z_i;
NULL = zeros(numel(x_i),numel(y_i),numel(z_i),3);
for n=1:numel(field.values)
field_i.values{n} = NULL;
end
clear NULL;
% matlab cannot handle 3D data to be 2D data, workaround for these cases
if (numel(x)==1)
[Y Z] = ndgrid(y,z);
[Y_I Z_I] = ndgrid(y_i,z_i);
for n=1:numel(field.values)
field_i.values{n}(1,:,:,1) = interpn(Y,Z,squeeze(field.values{n}(1,:,:,1)),Y_I,Z_I);
field_i.values{n}(1,:,:,2) = interpn(Y,Z,squeeze(field.values{n}(1,:,:,2)),Y_I,Z_I);
field_i.values{n}(1,:,:,3) = interpn(Y,Z,squeeze(field.values{n}(1,:,:,3)),Y_I,Z_I);
end
return;
end
if (numel(y)==1)
[X Z] = ndgrid(x,z);
[X_I Z_I] = ndgrid(x_i,z_i);
for n=1:numel(field.values)
field_i.values{n}(:,1,:,1) = interpn(X,Z,squeeze(field.values{n}(:,1,:,1)),X_I,Z_I);
field_i.values{n}(:,1,:,2) = interpn(X,Z,squeeze(field.values{n}(:,1,:,2)),X_I,Z_I);
field_i.values{n}(:,1,:,3) = interpn(X,Z,squeeze(field.values{n}(:,1,:,3)),X_I,Z_I);
end
return;
end
if (numel(z)==1)
[X Y] = ndgrid(x,y);
[X_I Y_I] = ndgrid(x_i,y_i);
for n=1:numel(field.values)
field_i.values{n}(:,:,1,1) = interpn(X,Y,squeeze(field.values{n}(:,:,1,1)),X_I,Y_I);
field_i.values{n}(:,:,1,2) = interpn(X,Y,squeeze(field.values{n}(:,:,1,2)),X_I,Y_I);
field_i.values{n}(:,:,1,3) = interpn(X,Y,squeeze(field.values{n}(:,:,1,3)),X_I,Y_I);
end
return;
end
%real 3D case
[X Y Z] = ndgrid(x,y,z);
[X_I Y_I Z_I] = ndgrid(x_i,y_i,z_i);
for n=1:numel(field.values)
field_i.values{n}(:,:,:,1) = interpn(X,Y,Z,field.values{n}(:,:,:,1),X_I,Y_I,Z_I);
field_i.values{n}(:,:,:,2) = interpn(X,Y,Z,field.values{n}(:,:,:,2),X_I,Y_I,Z_I);
field_i.values{n}(:,:,:,3) = interpn(X,Y,Z,field.values{n}(:,:,:,3),X_I,Y_I,Z_I);
end

30
matlab/ReadHDF5Dump.m Normal file
View File

@ -0,0 +1,30 @@
function [field mesh] = ReadHDF5Dump(file, varargin)
%[field mesh] = ReadHDF5Dump(file, varargin)
%
% example:
% [field mesh] = ReadHDF5Dump('Et.h5');
% or
% [field mesh] = ReadHDF5Dump('Et.h5','Interpolation',[21 1 101],'Frequency',300e6);
%
% openEMS matlab interface
% -----------------------
% author: Thorsten Liebig
%
% See also ReadHDF5Mesh ReadHDF5FieldData GetField_Interpolation
field = ReadHDF5FieldData(file);
mesh = ReadHDF5Mesh(file);
if (nargin>1)
for n=1:2:(nargin-1)
if (strcmp(varargin{n},'Interpolation')==1);
[field mesh] = GetField_Interpolation(field,mesh,varargin{n+1});
end
end
for n=1:2:(nargin-1)
if (strcmp(varargin{n},'Frequency')==1);
field = GetField_TD2FD(field,varargin{n+1});
end
end
end

View File

@ -16,7 +16,7 @@ function hdf_fielddata = ReadHDF5FieldData(file)
% -----------------------
% author: Thorsten Liebig
%
% See also ReadHDF5Mesh
% See also ReadHDF5Mesh ReadHDF5Dump
info = hdf5info(file);