summaryrefslogtreecommitdiff
path: root/openEMS/matlab/ReadHDF5Mesh.m
blob: b2763919a81a2a3881fc5c930ce7decf713682ec (plain)
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
function hdf_mesh = ReadHDF5Mesh(file)
% function hdf_mesh = ReadHDF5Mesh(file)
%
%   Get the raw mesh data stored in the hdf5 dump file created by openEMS
%
% returns:
% hdf_mesh.type     (0-> cartesian, 1-> cylindrical mesh type)
% hdf_mesh.names    (e.g. 'Mesh/y')
% hdf_mesh.lines    (e.g. [0,1,2,3,4])
%
% openEMS matlab interface
% -----------------------
% author: Thorsten Liebig
%
% See also ReadHDF5FieldData

isOctave = exist('OCTAVE_VERSION','builtin') ~= 0;
if isOctave
    hdf_mesh = ReadHDF5Mesh_octave(file);
    return
end

info = hdf5info(file);

for n=1:numel(info.GroupHierarchy.Groups)
    if strcmp(info.GroupHierarchy.Groups(n).Name,'/Mesh')
        for m=1:numel(info.GroupHierarchy.Groups(n).Datasets)
            names{m} = info.GroupHierarchy.Groups(n).Datasets(m).Name;
        end
    end
end

hdf_mesh.names = names;
for n=1:numel(names)
    hdf_mesh.lines{n} = double(hdf5read(file,names{n}));
end

if (strcmp(names{1},'/Mesh/alpha'))
    % alpha and rho are in the wrong order, flip to have rho, alpha, z
    hdf_mesh.names(1:2) = fliplr(hdf_mesh.names(1:2));
    hdf_mesh.lines(1:2) = fliplr(hdf_mesh.lines(1:2));
    hdf_mesh.type=1;
    return
end
if (strcmp(names{1},'/Mesh/phi'))
    % reorder coordinates
    hdf_mesh.names = hdf_mesh.names([2 3 1]);
    hdf_mesh.lines = hdf_mesh.lines([2 3 1]);
    hdf_mesh.type=2;
    return
end

hdf_mesh.type=0;


function hdf_mesh = ReadHDF5Mesh_octave(file)
hdf = load( '-hdf5', file );
hdf_mesh.names = fieldnames(hdf.Mesh);
hdf_mesh.type = 0; % cartesian mesh
for n=1:numel(hdf_mesh.names)
    hdf_mesh.lines{n} = hdf.Mesh.(hdf_mesh.names{n});
    hdf_mesh.names{n} = ['/Mesh/' hdf_mesh.names{n}];
    if strcmp(hdf_mesh.names{n},'/Mesh/alpha')
        hdf_mesh.type = 1; % cylindrical mesh
    end
    if strcmp(hdf_mesh.names{n},'/Mesh/phi')
        hdf_mesh.type = 2; % cylindrical mesh
    end
end

if (hdf_mesh.type==1)
    % alpha and rho are in the wrong order, flip to have rho, alpha, z
    hdf_mesh.names(1:2) = fliplr(hdf_mesh.names(1:2));
    hdf_mesh.lines(1:2) = fliplr(hdf_mesh.lines(1:2));
end
if (hdf_mesh.type==2)
    % alpha and rho are in the wrong order, flip to have rho, alpha, z
    hdf_mesh.names = hdf_mesh.names([2 3 1]);
    hdf_mesh.lines = hdf_mesh.lines([2 3 1]);
end