ucftools/matlab/convert_particles_hdf2ucf.m

88 lines
2.4 KiB
Matlab

function [] = convert_particles_hdf2ucf(fhdf,fucf)
% [] = convert_particles_hdf2ucf(fhdf,fucf)
% Converts a H5Part file containing particle data to a file in UCF format.
% Input
% fhdf file path to HDF file (input file)
% fucf file path to UCF file (output file)
% Check if file contains DEM data
try
h5info(fhdf,'/Parameters/DEM');
isDEM = true;
catch
isDEM = false;
end
% Create column maps
colml = ucf.partColmapFromFlags(false,true,isDEM,false);
colpv = convert_colmap_matlab2paraview(colml);
% Determine final length of set
ncol = colpv.length;
% Open HDF file ro and UCF file w+
id_file = H5F.open(fhdf,'H5F_ACC_RDONLY','H5P_DEFAULT');
obj = ucf(fucf,'create');
obj.setFileHeader('particle');
% Read number of steps from HDF5 file
id_dset = H5D.open(id_file,'/NumberOfSteps');
nstep = H5D.read(id_dset);
H5D.close(id_dset);
% Loop through timesteps
fprintf('[HDF read] %s\n',fhdf);
fprintf('[UCF write] %s\n',fucf)
for istep=1:nstep
% Open group
groupname = sprintf('Step#%d',istep-1);
id_group = H5G.open(id_file,groupname);
% Read simulation time from HDF and create UCF timestep
id_att = H5A.open(id_group,'TimeValue');
time = H5A.read(id_att);
H5A.close(id_att);
obj.appendStep(time);
% Read number of particles from attribute
id_att = H5A.open(id_group,'NumberOfParticles');
np = H5A.read(id_att);
H5A.close(id_att);
% Prepare array to store current timestep data
id_dset = H5D.open(id_group,'Radius');
id_type = H5D.get_type(id_dset);
type_size = H5T.get_size(id_type);
if type_size==4
pp = zeros(ncol,np,'single');
else
pp = zeros(ncol,np,'double');
end
H5T.close(id_type);
H5D.close(id_dset);
colval = cell2mat(colpv.values);
[colval,sortidx] = sort(colval);
colkey = colpv.keys;
colkey = colkey(sortidx);
% Read data
for icol=1:ncol
id_dset = H5D.open(id_group,colkey{icol});
tmp = H5D.read(id_dset);
H5D.close(id_dset);
pp(colval(icol),:) = tmp;
end
% Add data to UCF file
obj.appendParticle(pp,colml);
% Close current timestep
H5G.close(id_group);
end
% Close files
H5F.close(id_file);
obj.close();
end