Skip to content

Commit

Permalink
Raise error if nonsensical complex values are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccafair committed Aug 17, 2022
1 parent d8d54f3 commit 5887b8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
11 changes: 11 additions & 0 deletions +sw_tests/+unit_tests/unittest_spinw_genmagstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
{'mode', 'func', 'x0', [pi/2 -pi/4 0 0 [0 0 1/3] pi pi/2], 'unit', 'lu'}, ...
{'mode', 'fourier', 'S',[1; i; 0], 'n', [1 0 0]}
};
complex_inputs = {
{'n', [1 i 0]}, ...
{'k', [i 0 0]}, ...
{'nExt', [1 1 i]}, ...
{'epsilon', complex(1)}, ...
{'phid', i}};
end
methods (TestMethodSetup)
function setup_chain_model(testCase)
Expand Down Expand Up @@ -88,6 +94,11 @@ function test_no_magnetic_atoms_raises_error(testCase)
@() swobj.genmagstr(), ...
'spinw:genmagstr:NoMagAtom')
end
function test_complex_input_raises_error(testCase, complex_inputs)
testCase.verifyError(...
@() testCase.swobj.genmagstr('mode', 'helical', 'S', [1; 0; 0], complex_inputs{:}), ...
'spinw:genmagstr:WrongInput')
end
function test_tile_too_few_S_raises_error(testCase)
swobj = copy(testCase.swobj);
swobj.addatom('r', [0.5 0.5 0], 'S', 1);
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ Bug Fixes
``helical`` and ``fourier`` modes in ``genmagstr``
- Emit warning if arguments that will be ignored are passed to a particular
mode in ``genmagstr`` (e.g. ``S`` is passed to ``random``)
- Raise error if complex values are provided for ``k``, ``n``, ``nExt``,
``epsilon`` or ``phid`` in ``genmagstr``. Previously
this might result in hanging or other errors depending on the argument.
46 changes: 27 additions & 19 deletions swfiles/@spinw/genmagstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,33 @@ function genmagstr(obj, varargin)
'structure to be defined with another mode first'])
end


real_inputs = {"n", "k", "nExt", "epsilon", "phid"};
for i = 1:length(real_inputs)
if any(strcmp(varargin, real_inputs{i})) && ~isreal(param.(real_inputs{i}))
error('spinw:genmagstr:WrongInput', '%s should be real', real_inputs{i})
end
end

if isempty(param.k)
noK = true;
param.k = k0;
else
noK = false;
end

if prod(double(param.nExt)) == 0
error('spinw:genmagstr:WrongInput','''nExt'' has to be larger than 0!');
end

% input type for S, check whether it is complex type
cmplxS = ~isreal(param.S);
if strcmpi(param.mode, 'helical') && cmplxS
error('spinw:genmagstr:WrongInput', ...
['S must be real for helical mode. To specify complex basis ' ...
'vectors directly use fourier mode.'])
end

if strcmp(param.mode,'extend')
warning('spinw:genmagstr:DeprecationWarning',...
'extend mode is deprecated, please use tile mode instead');
Expand All @@ -272,25 +299,6 @@ function genmagstr(obj, varargin)
end
end

if isempty(param.k)
noK = true;
param.k = k0;
else
noK = false;
end

if prod(double(param.nExt)) == 0
error('spinw:genmagstr:WrongInput','''nExt'' has to be larger than 0!');
end

% input type for S, check whether it is complex type
cmplxS = ~isreal(param.S);
if strcmpi(param.mode, 'helical') && cmplxS
error('spinw:genmagstr:WrongInput', ...
['S must be real for helical mode. To specify complex basis ' ...
'vectors directly use fourier mode.'])
end

switch lower(param.unit)
case 'lu'
% convert the moments from lattice units to xyz
Expand Down

0 comments on commit 5887b8c

Please sign in to comment.