From f507d20eb1dc677ee348db0befc86002473e742a Mon Sep 17 00:00:00 2001 From: Thorsten Liebig Date: Thu, 2 Feb 2012 12:23:50 +0100 Subject: [PATCH] matlab interface to new nf2ff application --- matlab/CalcNF2FF.m | 85 ++++++++++++++++++++++++++++++++++++++++++++++ matlab/ReadNF2FF.m | 41 ++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 matlab/CalcNF2FF.m create mode 100644 matlab/ReadNF2FF.m diff --git a/matlab/CalcNF2FF.m b/matlab/CalcNF2FF.m new file mode 100644 index 0000000..8e2df8d --- /dev/null +++ b/matlab/CalcNF2FF.m @@ -0,0 +1,85 @@ +function nf2ff = CalcNF2FF(nf2ff, Sim_Path, freq, theta, phi, varargin) +% function nf2ff = CalcNF2FF(nf2ff, Sim_Path, freq, theta, phi, varargin) +% +% Calculate the near-field to far-field transformation created by +% CreateNF2FFBox +% +% parameter: +% nf2ff: data structure created by CreateNF2FFBox +% Sim_Path: path to simulation data +% freq: array of frequencies to analyse +% theta,phi: spherical coordinates to evaluate the far-field on +% +% optional paramater: +% 'Mode': 'Mode', 0 -> read only, if data already exist (default) +% 'Mode', 1 -> calculate anyway, overwrite existing +% 'Mode', 2 -> read only, fail if not existing +% 'Outfile': alternative nf2ff result hdf5 file name +% default is: .h5 +% 'Verbose': set verbose level for the nf2ff calculation 0-2 supported +% +% See also: CreateNF2FFBox, ReadNF2FF +% +% openEMS matlab interface +% ----------------------- +% author: Thorsten Liebig, 2012 + +mode = 0; + +filename = nf2ff.name; +nf2ff_xml.Planes = {}; + +for (n=1:numel(nf2ff.filenames_E)) + if (nf2ff.directions(n)~=0) + nf2ff_xml.Planes{end+1}.ATTRIBUTE.E_Field = [nf2ff.filenames_E{n} '.h5']; + nf2ff_xml.Planes{end}.ATTRIBUTE.H_Field = [nf2ff.filenames_H{n} '.h5']; + end +end +nf2ff_xml.ATTRIBUTE.freq = freq; +nf2ff_xml.theta = theta; +nf2ff_xml.phi = phi; +nf2ff_xml.ATTRIBUTE.Outfile = [filename '.h5']; + +for n=1:2:numel(varargin)-1 + if (strcmp(varargin{n},'Mode')) + mode = varargin{n+1}; + else + nf2ff_xml.ATTRIBUTE.(varargin{n})=varargin{n+1}; + end +end + +nf2ff.xml = [Sim_Path '/' filename '.xml']; +nf2ff.hdf5 = [Sim_Path '/' nf2ff_xml.ATTRIBUTE.Outfile]; + +% create nf2ff structure +struct_2_xml(nf2ff.xml,nf2ff_xml,'nf2ff'); + +m_filename = mfilename('fullpath'); +dir = fileparts( m_filename ); +openEMS_Path = [dir filesep '..' filesep]; + +if ((exist(nf2ff.hdf5,'file') && (mode==0)) || (mode==2)) + disp('CalcNF2FF: Reading nf2ff data only...') + nf2ff = ReadNF2FF(nf2ff); + return; +end + +savePath = pwd; +cd(Sim_Path); + +try + if isunix + % remove LD_LIBRARY_PATH set by matlab + system(['export LD_LIBRARY_PATH=; ' openEMS_Path 'nf2ff/nf2ff ' filename '.xml']); + else + system([openEMS_Path 'nf2ff/nf2ff ' filename '.xml']); + end + + nf2ff.hdf5; + cd(savePath); +catch + cd(savePath); + error 'CalcNF2FF: failed' +end + +nf2ff = ReadNF2FF(nf2ff); diff --git a/matlab/ReadNF2FF.m b/matlab/ReadNF2FF.m new file mode 100644 index 0000000..ce6ca98 --- /dev/null +++ b/matlab/ReadNF2FF.m @@ -0,0 +1,41 @@ +function nf2ff = ReadNF2FF(nf2ff) +% function nf2ff = ReadNF2FF(nf2ff) +% +% internal function to read calculated nf2ff data, use CalcNF2FF to read +% existing nf2ff data +% +% See also: CalcNF2FF, CreateNF2FFBox +% +% openEMS matlab interface +% ----------------------- +% author: Thorsten Liebig, 2012 + +file = nf2ff.hdf5; + +hdf_mesh = ReadHDF5Mesh(file); + +nf2ff.r = double(hdf_mesh.lines{1}); +nf2ff.theta = double(hdf_mesh.lines{2}); +nf2ff.phi = double(hdf_mesh.lines{3}); + +if isOctave + nf2ff.freq = double(h5readatt_octave(file,'/nf2ff','Frequency')); + nf2ff.Prad = double(h5readatt_octave(file,'/nf2ff','Prad')); + nf2ff.Dmax = double(h5readatt_octave(file,'/nf2ff','Dmax')); + hdf = load( '-hdf5', file ); + for n=1:numel(nf2ff.freq) + nf2ff.E_theta{n} = double(hdf.nf2ff.E_theta.FD.(['f' int2str(n-1) '_real']) +1i*hdf.nf2ff.E_theta.FD.(['f' int2str(n-1) '_imag']) ); + nf2ff.E_phi{n} = double(hdf.nf2ff.E_phi.FD.(['f' int2str(n-1) '_real']) +1i*hdf.nf2ff.E_phi.FD.(['f' int2str(n-1) '_imag']) ); + nf2ff.E_norm{n} = double(sqrt(abs(nf2ff.E_theta{n}).^2+abs(nf2ff.E_phi{n}).^2)); + end +else + nf2ff.freq = double(h5readatt(file,'/nf2ff','Frequency')); + nf2ff.Prad = double(h5readatt(file,'/nf2ff','Prad')); + nf2ff.Dmax = double(h5readatt(file,'/nf2ff','Dmax')); + + for n=1:numel(nf2ff.freq) + nf2ff.E_theta{n} = double(h5read(file,['/nf2ff/E_theta/FD/f' int2str(n-1) '_real']) + 1i*h5read(file,['/nf2ff/E_theta/FD/f' int2str(n-1) '_imag'])); + nf2ff.E_phi{n} = double(h5read(file,['/nf2ff/E_phi/FD/f' int2str(n-1) '_real']) + 1i*h5read(file,['/nf2ff/E_phi/FD/f' int2str(n-1) '_imag'])); + nf2ff.E_norm{n} = double(sqrt(abs(nf2ff.E_theta{n}).^2+abs(nf2ff.E_phi{n}).^2)); + end +end