Skip to content

Commit

Permalink
swloop mex (part II) (#173)
Browse files Browse the repository at this point in the history
* Add fastmode and neutron_output options to spinwave

* Add twin and unittests to neutron_output and fastmode

* Add neutron_output option to swloop mex

* Fix neutron_output swloop option. Add unit test.

* Add fastmode to swloop mex.

* Remove spinwavefast; horace() uses spinwave('fastmode')

* Fix failing symbolic tests on R2024a

* Fix additional failing tests on R2024a

* Remove redundant comments
  • Loading branch information
mducle committed Apr 15, 2024
1 parent 0c2f294 commit 74422b7
Show file tree
Hide file tree
Showing 11 changed files with 520 additions and 1,298 deletions.
53 changes: 53 additions & 0 deletions +sw_tests/+unit_tests/unittest_spinw_spinwave.m
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,59 @@ function test_mex_prefs_nspinlarge(testCase)
swpref.setpref('usemex', mexpref.val);
swpref.setpref('nspinlarge', nslpref.val);
end
function test_neutron_output(testCase, mex)
swpref.setpref('usemex', mex);
objs = {testCase.swobj, testCase.swobj_tri};
hkl = {[0 0 0] [1 0 0] [0 1 0] 50};
for ii = 1:numel(objs)
swobj = copy(objs{ii});
spec0 = sw_neutron(swobj.spinwave(hkl, 'sortMode', false));
spec1 = swobj.spinwave(hkl, 'neutron_output', true);
testCase.verify_val(spec0.omega, spec1.omega, 'abs_tol', 1e-8);
testCase.verify_val(spec0.Sperp, spec1.Sperp, 'abs_tol', 1e-8);
end
end
function test_neutron_twin(testCase, mex)
swpref.setpref('usemex', mex);
swobj = copy(testCase.swobj);
swobj.addtwin('axis', [1 1 1], 'phid', 54);
swobj.unit.nformula = int32(2);
hkl = {[1 0 0] [0 1 0] [0 0 0] 50};
spec0 = sw_neutron(swobj.spinwave(hkl, 'sortMode', false));
spec1 = swobj.spinwave(hkl, 'neutron_output', true);
testCase.verify_val(spec0.omega{1}, spec1.omega{1}, 'abs_tol', 1e-8);
testCase.verify_val(spec0.omega{2}, spec1.omega{2}, 'abs_tol', 1e-8);
testCase.verify_val(spec0.Sperp{1}, spec1.Sperp{1}, 'abs_tol', 1e-8);
testCase.verify_val(spec0.Sperp{2}, spec1.Sperp{2}, 'abs_tol', 1e-8);
end
function test_fastmode(testCase, mex)
swpref.setpref('usemex', mex);
swobj = copy(testCase.swobj);
hkl = {[0 0 0] [1 0 0] [0 1 0] 50};
spec0 = sw_neutron(swobj.spinwave(hkl));
spec1 = swobj.spinwave(hkl, 'fastmode', true);
spec2 = swobj.spinwave(hkl, 'hermit', false, 'fastmode', true);
nMode = size(spec1.omega, 1);
testCase.verify_val(size(spec0.omega, 1), 2*nMode);
testCase.verify_val(spec0.omega(1:nMode,:), spec1.omega, 'abs_tol', 1e-4);
testCase.verify_val(spec0.Sperp(1:nMode,:), spec1.Sperp, 'abs_tol', 1e-8);
testCase.verify_val(spec0.Sperp(1:nMode,:), spec2.Sperp, 'abs_tol', 1e-8);
end
function test_fastmode_mex_nomex(testCase)
% Tests that fast mode gives same results for mex, no-mex and non-fastmode
swobj = copy(testCase.swobj);
hkl = {[0 0 0] [1 0 0] [0 1 0] 50};
swpref.setpref('usemex', 0);
spec0 = sw_neutron(swobj.spinwave(hkl));
spec1 = swobj.spinwave(hkl, 'fastmode', true);
swpref.setpref('usemex', 1);
spec2 = swobj.spinwave(hkl, 'fastmode', true);
nMode = size(spec1.omega, 1);
testCase.verify_val(size(spec0.omega, 1), 2*nMode);
testCase.verify_val(spec0.omega(1:nMode,:), spec2.omega, 'abs_tol', 1e-4);
testCase.verify_val(spec0.Sperp(1:nMode,:), spec1.Sperp, 'abs_tol', 1e-8);
testCase.verify_val(spec0.Sperp(1:nMode,:), spec2.Sperp, 'abs_tol', 1e-8);
end
end
methods (Test, TestTags = {'Symbolic'})
function test_sw_symbolic_no_qpts(testCase)
Expand Down
20 changes: 10 additions & 10 deletions +sw_tests/+unit_tests/unittest_sw_egrid.m
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,18 @@ function test_dE_single_number(testCase)
% use small dE so only intensity in a single ebin at each q
out = sw_egrid(testCase.spectrum, 'component', 'Sperp', 'dE', 0.001);
expected_out = testCase.sw_egrid_out_sperp;
expected_out.swConv(228,[2, 4]) = 6.29705585872502e-05;
expected_out.swConv(455,3) = 2.65255873676699;
testCase.verify_obj(out, expected_out);
expected_out.swConv(228,[2, 4]) = 0.00929494936601533;
expected_out.swConv(455,3) = 2.36709828275952;
testCase.verify_obj(out, expected_out, 'abs_tol', 1e-10);
end
function test_dE_matrix_correct_numel(testCase)
% use small dE so only intensity in a single ebin at each q
dE = 0.001*ones(1, numel(testCase.sw_egrid_out_sperp.Evect)-1);
out = sw_egrid(testCase.spectrum, 'component', 'Sperp', 'dE', dE);
expected_out = testCase.sw_egrid_out_sperp;
expected_out.swConv(228,[2, 4]) = 6.29705585872502e-05;
expected_out.swConv(455,3) = 2.65255873676699;
testCase.verify_obj(out, expected_out);
expected_out.swConv(228,[2, 4]) = 0.00929494936601533;
expected_out.swConv(455,3) = 2.36709828275952;
testCase.verify_obj(out, expected_out, 'abs_tol', 1e-10);
end
function test_dE_matrix_incorrect_numel(testCase)
% use small dE so only intensity in a single ebin at each q
Expand All @@ -397,10 +397,10 @@ function test_dE_callable_func(testCase)
dE_func = @(en) 0.001;
out = sw_egrid(testCase.spectrum, 'component', 'Sperp', 'dE', dE_func);
expected_out = testCase.sw_egrid_out_sperp;
expected_out.swConv(228,[2, 4]) = 6.29705585872502e-05;
expected_out.swConv(455,3) = 2.65255873676699;
testCase.verify_obj(out, expected_out);
expected_out.swConv(228,[2, 4]) = 0.00929494936601533;
expected_out.swConv(455,3) = 2.36709828275952;
testCase.verify_obj(out, expected_out, 'abs_tol', 1e-10);
end
end

end
end
Loading

0 comments on commit 74422b7

Please sign in to comment.