Skip to content

Commit

Permalink
Updated tutorials and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wardsimon committed Jan 15, 2018
1 parent d46839a commit 249824a
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 46 deletions.
26 changes: 22 additions & 4 deletions install_spinw.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function install_spinw()
function install_spinw(varargin)
% installs SpinW
%
% INSTALL_SPINW()
Expand All @@ -13,6 +13,15 @@ function install_spinw()
% return
% end

silent = false;
if nargin == 2
if strcmpi(varargin{1},'silent')
if isa(varargin{2},'logical')
silent = varargin{2};
end
end
end

newline = char(10); %#ok<CHARTEN>

% remove old SpinW installation from path
Expand Down Expand Up @@ -91,20 +100,26 @@ function install_spinw()

% create new startup.m file
if isempty(sfLoc)
if ~silent
answer = getinput(sprintf(['You don''t have a Matlab startup.m file,\n'...
'do you want it to be created at %s? (y/n)'],esc(uPath)),'yn');
else
answer = 'n';
end
if answer == 'y'
fclose(fopen(uPath,'w'));
sfLoc = uPath;
end
end

if ~isempty(sfLoc)

if ~silent
answer = getinput(['Would you like to add the following line:' newline...
'addpath(genpath(''' esc(folName) '''));' newline 'to the end of '...
'your Matlab startup file (' esc(sfLoc) ')? (y/n)'],'yn');

else
answer = 'n';
end
if answer == 'y'
fid = fopen(sfLoc,'a');
fprintf(fid,['\n%%###SW_UPDATE\n%% Path to the SpinW installation\n'...
Expand All @@ -113,14 +128,17 @@ function install_spinw()
end
end

if ~silent
answer = getinput(...
['\nIn order to refresh the internal class definitions of Matlab (to\n'...
'access the new SpinW version), issuing the "clear classes" command\n'...
'is necessary. However this command will also clear all your variables\n'...
'in the Matlab internal memory. Would you like the updater to issue\n'...
'the command now, otherwise you can do it manually later.\n'...
'Do you want to issue the command "clear classes" now? (y/n)'],'yn');

else
answer = 'n';
end
if answer == 'y'
clear('classes'); %#ok<CLCLS>
disp('Matlab class memory is refreshed!')
Expand Down
19 changes: 16 additions & 3 deletions swfiles/+swplot/plotmag.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@
setappdata(hFigure,'base',obj.basisvector);
end

if obj.symbolic
warning('plotmag:symbolic','A symbolic magnetic structure can not be plotted.')
varargout = cell(1:nargout);
return
end


% the basis vectors in columns.
BV = obj.basisvector;

Expand Down Expand Up @@ -325,8 +332,14 @@
end

% normalize the longest moment vector to scale*(shortest bond length)
M = M/sqrt(max(sum(M.^2,1)))*param.scale*lBond;

if isa(M,'sym')
warning('sw_plotmag:symcalc','This is a symbolic calculaiton, normalisation can''t be achieved.')
temp = cellfun(@(x) M./sqrt(sum(x,1).^2),mat2cell(M,3,[1 1 1 1]),'UniformOutput',false);
[m, ind] = min(cellfun(@(x) sum(x(:)),temp));
M = temp{ind}*param.scale*lBond;
else
M = M/sqrt(max(sum(M.^2,1)))*param.scale*lBond;
end
if param.centered
% double the length for centered moments
M = 2*M;
Expand All @@ -350,7 +363,7 @@
end

% shift positions
vpos = bsxfun(@plus,vpos,BV\param.shift);
vpos = bsxfunsym(@plus,vpos,BV\param.shift);

% prepare legend labels
mAtom.name = obj.unit_cell.label(mAtom.idx);
Expand Down
47 changes: 29 additions & 18 deletions swfiles/@spinw/energy.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
function Eout = energy(obj, varargin)
% calculates the ground state energy
%
%
% ### Syntax
%
%
% `E = energy(obj,Name,Value)`
%
%
% ### Description
%
%
% `E = energy(obj,Name,Value)` calculates the classical ground state energy
% per spin. The calculation correctly takes into account the magnetic
% supercell. The function gives correct results on single-k magnetic
% structures even defined on magnetic supercells. For multi-k magnetic
% structures first a definition of a larger supercell is necessary where an
% effective $k=0$ representation is possible.
%
%
% ### Examples
%
%
% After optimising the magnetic structure (by minimizing the ground state
% energy), the energy per spin is calculated. This can be compared to
% different ground state structures to decide which is the right classical
Expand All @@ -31,20 +31,20 @@
% >>cryst.optmagsteep('nRun',10)
% >>cryst.energy>>
% ```
%
%
% ### Input Arguments
%
%
% `obj`
% : [spinw] object.
%
%
% ### Name-Value Pair Arguments
%
%
% `'epsilon'`
% : The smallest value of incommensurability that is tolerated
% : The smallest value of incommensurability that is tolerated
% without warning. Default is $10^{-5}$.
%
%
% ### Output Arguments
%
%
% `E`
% : Energy per moment (anisotropy + exchange + Zeeman energy).
%
Expand All @@ -59,9 +59,9 @@
% ion anisotropy one has to be carefull! In the triangular case one has to
% extend the unit cell to `nExt = [3 3 1]` (in the hexagonal setting), in
% this case the energy will be correct.}}
%
%
% ### See Also
%
%
% [spinw] \| [spinw.anneal] \| [spinw.newcell]
%

Expand Down Expand Up @@ -103,7 +103,7 @@
end

if nMagExt>0

dR = [SS.all(1:3,:) zeros(3,nMagExt)];
atom1 = [SS.all(4,:) 1:nMagExt];
atom2 = [SS.all(5,:) 1:nMagExt];
Expand All @@ -127,8 +127,19 @@

Ml = repmat(permute(M1,[1 3 2]),[1 3 1]);
Mr = repmat(permute(M2,[3 1 2]),[3 1 1]);

exchE = sum(sum(sum(Ml.*JJ.*Mr,3),2),1);

Q = Ml.*JJ.*Mr;
if isa(Ml,'sym') || isa(Ml,'sym') || isa(Ml,'sym')
[n1, n2, n3] = size(Q);
sum_3 = 0;
for k = 1:n3
sum_3 = sum_3 + Q(:,:,k);
end
else
sum_3 = sum(Q,3);
end
exchE = sum(sum(sum_3,2),1);

% TODO
% correct energy for twins
ZeemanE = -sum(SI.field*permute(mmat(SI.g,permute(M0,[1 3 2])),[1 3 2]),2)*obj.unit.muB;
Expand Down
6 changes: 3 additions & 3 deletions swfiles/@spinw/genmagstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function genmagstr(obj, varargin)
% Create random spin directions and use a single k-vector
S = randn(nMagExt,3);
S = bsxfun(@rdivide,S,sqrt(sum(S.^2,2)));
S = bsxfun(@times,S,mAtom.Sext')';
S = bsxfunsym(@times,S,mAtom.Sext')';
if noK
k = [0 0 0];
end
Expand Down Expand Up @@ -387,11 +387,11 @@ function genmagstr(obj, varargin)
if addPhase
% additional phase for each spin in the magnetic supercell
%phi = sum(bsxfun(@times,2*pi*kExt',r),1);
phi = sum(bsxfun(@times,2*pi*permute(kExt,[2 3 1]),r),1);
phi = sum(bsxfunsym(@times,2*pi*permute(kExt,[2 3 1]),r),1);

% add the extra phase for each spin in the unit cell
% TODO check
S = bsxfun(@times,S0(:,mod(0:(nMagExt-1),nSpin)+1,:),exp(-1i*phi));
S = bsxfunsym(@times,S0(:,mod(0:(nMagExt-1),nSpin)+1,:),exp(-1i*phi));
else
S = S0;
end
Expand Down
32 changes: 19 additions & 13 deletions swfiles/sw_cartesian.m
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
function [vyOut, vzOut, vxOut] = sw_cartesian(n)
% creates a right handed Cartesian coordinate system
%
%
% ### Syntax
%
%
% `[vy, vz, vx] = sw_cartesian(n)`
%
% `V = sw_cartesian(n)`
%
%
% ### Description
%
%
% `[vy, vz, vx] = sw_cartesian(n)` creates an $(x,y,z)$ right handed
% Cartesian coordinate system with $v_x$, $v_y$ and $v_z$ defining the
% basis vectors.
% basis vectors.
%
% `V = sw_cartesian(n)` the generated basis vectors are stored in the `V`
% matrix: `V = [vx vy vz]` as column vectors.
%
%
% ### Input Arguments
%
%
% `n`
% : Either a 3 element row/column vector or a $[3\times 3]$ matrix with
% columns defining 3 vectors.
%
%
% ### Output Arguments
%
%
% `vy,vz,vx`
% : Vectors defining the right handed coordinate system. They are
% either column of row vectors depending on the shape of the
Expand All @@ -43,7 +43,14 @@
z = [0; 0;-1];
y = [0;-1; 0];

if any(cross(n,z))
c = cross(n,z);
if isa(n,'sym')
opt = any(sw_always(c));
else
opt = any(c);
end

if opt
vy = cross(n,z);
else
vy = cross(n,y);
Expand All @@ -54,12 +61,12 @@
if det(n) == 0
error('sw_cartesian:WrongInput','The input vectors are not linearly independent!')
end

nShape = [3 1];
vz = cross(n(:,1),n(:,2));
vy = cross(vz,n(:,1));
n = n(:,1);

else
error('sw_cartesian:WrongInput','Wrong size of n!')
end
Expand All @@ -73,7 +80,6 @@
vyOut = reshape(vy/norm(vy),nShape);
vzOut = reshape(vz/norm(vz),nShape);
vxOut = reshape(n/norm(n),nShape);

end

end
1 change: 1 addition & 0 deletions tutorials/publish/tutorial1.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
% value is dinamically calculated at every call.

FMchain.energy
assert(FMchain.energy == -1)

%% Calculate spin wave dispersion and spin-spin correlation function
% We calculate spin wave dispersion and correlation function along the
Expand Down
4 changes: 4 additions & 0 deletions tutorials/publish/tutorial10.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
% ranges in momentum and energy. Second we call Horace to fill up the empty
% d3d object with the simulated spin wave data and finally we plot a
% constant energy cut.
if ~exist('sqw','file')
fprintf('Horace is not installed. Exiting....\n');
return
end

Ebin = [0,0.01,5];
fwhm0 = 0.1;
Expand Down
5 changes: 5 additions & 0 deletions tutorials/publish/tutorial12.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
% Horace is installed and setup. Calculate spectra using disp2sqw_eval
% Horace function.

if ~exist('sqw','file')
fprintf('Horace is not installed. Exiting....\n');
return
end

horaceObj = d3d(tri.abc,[1 0 0 0],[0,0.005,1],[0 1 0 0],[0,0.005,1],[0 0 0 1],[0,0.1,10]);
horaceObj = disp2sqw_eval(horaceObj,@tri.horace,{'component','Sperp'},dE);
cut1 = cut(horaceObj,[],[],[3.0 3.5]);
Expand Down
2 changes: 1 addition & 1 deletion tutorials/publish/tutorial17.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
% also created using symbolic input variables, for example incommensurate
% k-vector, etc.

FMchain.genmagstr('mode','helical','S',[0 1 0])
FMchain.genmagstr('mode','helical','S',[0; 1; 0])
FMchain.magtable.M
plot(FMchain,'range',[3 0.5 0.5],'zoom',1)

Expand Down
2 changes: 1 addition & 1 deletion tutorials/publish/tutorial31.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
% Check spinw.formula() to see how much the new cell is smaller

licroR = copy(licro);
T = licroR.newcell({[-1 1 1]/3 [2 1 1]/3 [-1 -2 1]/3})
T = licroR.newcell('bvect',{[-1 1 1]/3 [2 1 1]/3 [-1 -2 1]/3})
plot(licroR,'cellMode','outside')
swplot.zoom(1.5)

Expand Down
2 changes: 1 addition & 1 deletion tutorials/publish/tutorial7.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
%% Powder spectrum

afkPow = AFkagome.powspec(linspace(0,2.5,150),'Evect',linspace(0,3,250),...
'nRand',1e3,'hermit',false);
'nRand',1e3,'hermit',false,'imagChk',false);

figure
sw_plotspec(afkPow,'axLim',[0 0.2])
Expand Down
4 changes: 2 additions & 2 deletions tutorials/publish/tutorial8.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
% magnetic ground state. After calculating the diagonal of the correlation
% function we can see that only a few modes have non-zero intensity.

kag33Spec = AF33kagome.spinwave({[-1/2 0 0] [0 0 0] [1/2 1/2 0] 250});
kag33Spec = sw_egrid(kag33Spec,'component','Sxx+Syy+Szz');
kag33Spec = AF33kagome.spinwave({[-1/2 0 0] [0 0 0] [1/2 1/2 0] 250},'hermit',false,'imagChk',false);
kag33Spec = sw_egrid(kag33Spec,'component','Sxx+Syy+Szz','imagChk',false);
figure
subplot(2,1,1)
sw_plotspec(kag33Spec,'mode',1,'axLim',[0 2.5],'colorbar',false',...
Expand Down

0 comments on commit 249824a

Please sign in to comment.