Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
granrothge committed Sep 8, 2023
1 parent 32cb8b7 commit 99adabc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
55 changes: 41 additions & 14 deletions +sw_tests/+unit_tests/unittest_spinw_spec2MDHisto.m
Original file line number Diff line number Diff line change
@@ -1,39 +1,66 @@
classdef unittest_spinw_spec2MDHisto < sw_tests.unit_tests.unittest_super
properties

properties(TestParameter)
nsteps={100};
end
methods (Test)
function test_1_0_0(testCase)
q0 = [0 0 0];
qdir = [1 0 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir nsteps}));
proj = [qdir(:) [1 -1 0]' [0 0 1]'];
dproj = [norm((qdir-q0))/nsteps, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'test100mdh.nxs');
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [0 1 0]' [0 0 1]'];
dproj = [norm((qdir-q0))/testCase.nsteps{1}, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test100mdh.nxs');
end

function test_1_1_0(testCase)
q0 = [0 0 0];
qdir = [1 1 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir nsteps}));
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [1 -1 0]' [0 0 1]'];
dproj = [norm((qdir-q0))/nsteps, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'test110mdh.nxs');
dproj = [norm((qdir-q0))/testCase.nsteps{1}, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test110mdh.nxs');
end

function test_1_1_1(testCase)
q0 = [0 0 0];
qdir = [1 1 1];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir nsteps}));
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [1 -1 0]' [1 1 -2]'];
dproj = [norm((qdir-q0))/nsteps, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'test111mdh.nxs');
dproj = [norm((qdir-q0))/testCase.nsteps{1}, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test111mdh.nxs');
end
function test_1_1_2(testCase)
q0 = [0 0 2];
qdir = [1 1 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir nsteps}));
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [1 -1 0]' [0 0 1]'];
dproj = [norm((qdir-q0))/nsteps, 1e-6, 1e-6];
dproj = [norm((qdir-q0))/testCase.nsteps{1}, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test112mdh.nxs');
end
end
function test_1_1_2_2(testCase)
q0 = [0 0 2];
qdir = [1 1 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [[1 -1 0]' qdir(:) [0 0 1]'];
dproj = [1e-6, norm((qdir-q0))/testCase.nsteps{1}, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test112_2mdh.nxs');
end
function test_2_2_2(testCase)
q0 = [2 2 2];
qdir = [1 1 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [1 -1 0]' [0 0 1]'];
dproj = [norm((qdir-q0))/testCase.nsteps{1}, 1e-6, 1e-6];
sw_spec2MDHisto(spec, proj, dproj, 'tmp/test222mdh.nxs');
end
function test_non_ortho(testCase)
q0 = [0 0 2];
qdir = [1 1 0];
spec = sw_egrid(spinwave(sw_model('triAF', 1), {q0 q0+qdir testCase.nsteps{1}}));
proj = [qdir(:) [1 0 0]' [0 0 1]'];
dproj = [1e-6, norm((qdir-q0))/testCase.nsteps{1}, 1e-6];
verifyError(testCase,@() sw_spec2MDHisto(spec, proj, dproj, 'tmp/test_blank.nxs'), "read_struct:nonorthogonal")
end
end
end
39 changes: 29 additions & 10 deletions swfiles/sw_spec2MDHisto.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ function sw_spec2MDHisto(spectra,proj, dproj, filename)
%
% proj: a 3x3 matrix defining an orthogonal coordinate system
% where each column is a vector defining the orientation
% of the view. One of the vectors must be along a q axis
% of the view. One of the vectors must be identical to the Q axis
% defined by the direction of the calculation.
%
% dproj: is a 3 vector that is the bin size in each of the
% directions defined in proj. For the direction of the
% calculation, this should be the step size.
% calculation, the value used is internally calcualted from the spectrum.
% It is wise to enter the step size for clarity.
%
% filename: is the name of the nexus file. It will overwrite the existing
% file if one already exists
Expand All @@ -44,7 +45,7 @@ function sw_spec2MDHisto(spectra,proj, dproj, filename)
swhelp sw_spec2MDHisto
return
end
[unit_cell,Bmat,proj_out,D,dat,name] = read_struct(spectra,proj,dproj);
[unit_cell,Bmat,proj_out,D,dat,proj,name] = read_struct(spectra,proj,dproj);
%check if hdf file exists and delete if it does.
if exist(filename,'file')
delete(filename)
Expand Down Expand Up @@ -217,13 +218,15 @@ function writeNXlog(filename,log_pth,log_nm,value,units)
h5writeatt(filename,pth,'units',units)

end
function [latt_parms,Bmat,proj_out,D,signal,name] = read_struct(dstruct,proj,dproj)
function [latt_parms,Bmat,proj_out,D,signal,proj,name] = read_struct(dstruct,proj,dproj)

% ### Input Arguments
% struct: is the spinw structure
% proj: is the viewing projection matrix
% dstruct: is the spinw structure
% proj: is the viewing projection matrix each column is a different axis.
% One axis must be parallel to the drection of propogation in the
% dstruct
% drproj: is the size of the bin in each direction (ignored for the direction
% of the cut.
% of the cut).
%
% ### Output Arguments
% latt_parms: the lattice parameters from the spinw structure
Expand All @@ -232,22 +235,22 @@ function writeNXlog(filename,log_pth,log_nm,value,units)
% D: a cell array of the number of steps in each direction
% signal: the signal array from the spinw spec strcuture
% name : the chemical formula from the spinW file

check_ortho(proj)
fnames=fieldnames(dstruct);
objnum = find(strcmp(fnames,'obj'));
swobj = dstruct.(subsref(fnames,substruct('{}',{objnum})));
latt_parms = abc(swobj);
M = basisvector(swobj);
Bmat = inv(M);
name =formula(swobj).chemform;
proj_out = proj(:);
%proj_out = proj(:);
hkls = dstruct.hkl;
hkls_sz = size(hkls);
dir_vec = hkls(:,hkls_sz(2))-hkls(:,1);
%qout = hkls'/dir_vec';
D={};
for idx=1:3
procjv = proj(idx,:)/norm(proj(idx,:));
procjv = proj(:,idx)/norm(proj(:,idx));

if abs(norm(cross(dir_vec,procjv)))> 1e-6
dtmp = dot(hkls(:,2),procjv);
Expand All @@ -259,9 +262,25 @@ function writeNXlog(filename,log_pth,log_nm,value,units)
D{idx} = zeros([1,length(hkl_proj)+1]);
D{idx}(1:length(hkl_proj)) = hkl_proj-dhkl/2;
D{idx}(length(D{idx})) = hkl_proj(length(hkl_proj))+dhkl/2;% change to bin boundaries
proj(:,idx) = dir_vec; %set varying projection vector to spectra object
end
end
D{4}=dstruct.Evect;

signal = dstruct.swConv;
proj_out = proj(:);
end

function check_ortho(mat)
% check if the three column vectors in proj are orthogonal to each other
sm = size(mat);
for idx = 1:sm(2)
for idx2 = 1:sm(2)
if idx~=idx2
if norm(dot(mat(:,idx),mat(:,idx2))) >1e-6
error("read_struct:nonorthogonal","the 3 vectors in proj must form an orthogonal basis set")
end
end
end
end
end

0 comments on commit 99adabc

Please sign in to comment.