Skip to content

Commit 249824a

Browse files
committed
Updated tutorials and bug fixes
1 parent d46839a commit 249824a

12 files changed

Lines changed: 104 additions & 46 deletions

File tree

install_spinw.m

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function install_spinw()
1+
function install_spinw(varargin)
22
% installs SpinW
33
%
44
% INSTALL_SPINW()
@@ -13,6 +13,15 @@ function install_spinw()
1313
% return
1414
% end
1515

16+
silent = false;
17+
if nargin == 2
18+
if strcmpi(varargin{1},'silent')
19+
if isa(varargin{2},'logical')
20+
silent = varargin{2};
21+
end
22+
end
23+
end
24+
1625
newline = char(10); %#ok<CHARTEN>
1726

1827
% remove old SpinW installation from path
@@ -91,20 +100,26 @@ function install_spinw()
91100

92101
% create new startup.m file
93102
if isempty(sfLoc)
103+
if ~silent
94104
answer = getinput(sprintf(['You don''t have a Matlab startup.m file,\n'...
95105
'do you want it to be created at %s? (y/n)'],esc(uPath)),'yn');
106+
else
107+
answer = 'n';
108+
end
96109
if answer == 'y'
97110
fclose(fopen(uPath,'w'));
98111
sfLoc = uPath;
99112
end
100113
end
101114

102115
if ~isempty(sfLoc)
103-
116+
if ~silent
104117
answer = getinput(['Would you like to add the following line:' newline...
105118
'addpath(genpath(''' esc(folName) '''));' newline 'to the end of '...
106119
'your Matlab startup file (' esc(sfLoc) ')? (y/n)'],'yn');
107-
120+
else
121+
answer = 'n';
122+
end
108123
if answer == 'y'
109124
fid = fopen(sfLoc,'a');
110125
fprintf(fid,['\n%%###SW_UPDATE\n%% Path to the SpinW installation\n'...
@@ -113,14 +128,17 @@ function install_spinw()
113128
end
114129
end
115130

131+
if ~silent
116132
answer = getinput(...
117133
['\nIn order to refresh the internal class definitions of Matlab (to\n'...
118134
'access the new SpinW version), issuing the "clear classes" command\n'...
119135
'is necessary. However this command will also clear all your variables\n'...
120136
'in the Matlab internal memory. Would you like the updater to issue\n'...
121137
'the command now, otherwise you can do it manually later.\n'...
122138
'Do you want to issue the command "clear classes" now? (y/n)'],'yn');
123-
139+
else
140+
answer = 'n';
141+
end
124142
if answer == 'y'
125143
clear('classes'); %#ok<CLCLS>
126144
disp('Matlab class memory is refreshed!')

swfiles/+swplot/plotmag.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@
208208
setappdata(hFigure,'base',obj.basisvector);
209209
end
210210

211+
if obj.symbolic
212+
warning('plotmag:symbolic','A symbolic magnetic structure can not be plotted.')
213+
varargout = cell(1:nargout);
214+
return
215+
end
216+
217+
211218
% the basis vectors in columns.
212219
BV = obj.basisvector;
213220

@@ -325,8 +332,14 @@
325332
end
326333

327334
% normalize the longest moment vector to scale*(shortest bond length)
328-
M = M/sqrt(max(sum(M.^2,1)))*param.scale*lBond;
329-
335+
if isa(M,'sym')
336+
warning('sw_plotmag:symcalc','This is a symbolic calculaiton, normalisation can''t be achieved.')
337+
temp = cellfun(@(x) M./sqrt(sum(x,1).^2),mat2cell(M,3,[1 1 1 1]),'UniformOutput',false);
338+
[m, ind] = min(cellfun(@(x) sum(x(:)),temp));
339+
M = temp{ind}*param.scale*lBond;
340+
else
341+
M = M/sqrt(max(sum(M.^2,1)))*param.scale*lBond;
342+
end
330343
if param.centered
331344
% double the length for centered moments
332345
M = 2*M;
@@ -350,7 +363,7 @@
350363
end
351364

352365
% shift positions
353-
vpos = bsxfun(@plus,vpos,BV\param.shift);
366+
vpos = bsxfunsym(@plus,vpos,BV\param.shift);
354367

355368
% prepare legend labels
356369
mAtom.name = obj.unit_cell.label(mAtom.idx);

swfiles/@spinw/energy.m

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
function Eout = energy(obj, varargin)
22
% calculates the ground state energy
3-
%
3+
%
44
% ### Syntax
5-
%
5+
%
66
% `E = energy(obj,Name,Value)`
7-
%
7+
%
88
% ### Description
9-
%
9+
%
1010
% `E = energy(obj,Name,Value)` calculates the classical ground state energy
1111
% per spin. The calculation correctly takes into account the magnetic
1212
% supercell. The function gives correct results on single-k magnetic
1313
% structures even defined on magnetic supercells. For multi-k magnetic
1414
% structures first a definition of a larger supercell is necessary where an
1515
% effective $k=0$ representation is possible.
16-
%
16+
%
1717
% ### Examples
18-
%
18+
%
1919
% After optimising the magnetic structure (by minimizing the ground state
2020
% energy), the energy per spin is calculated. This can be compared to
2121
% different ground state structures to decide which is the right classical
@@ -31,20 +31,20 @@
3131
% >>cryst.optmagsteep('nRun',10)
3232
% >>cryst.energy>>
3333
% ```
34-
%
34+
%
3535
% ### Input Arguments
36-
%
36+
%
3737
% `obj`
3838
% : [spinw] object.
39-
%
39+
%
4040
% ### Name-Value Pair Arguments
41-
%
41+
%
4242
% `'epsilon'`
43-
% : The smallest value of incommensurability that is tolerated
43+
% : The smallest value of incommensurability that is tolerated
4444
% without warning. Default is $10^{-5}$.
45-
%
45+
%
4646
% ### Output Arguments
47-
%
47+
%
4848
% `E`
4949
% : Energy per moment (anisotropy + exchange + Zeeman energy).
5050
%
@@ -59,9 +59,9 @@
5959
% ion anisotropy one has to be carefull! In the triangular case one has to
6060
% extend the unit cell to `nExt = [3 3 1]` (in the hexagonal setting), in
6161
% this case the energy will be correct.}}
62-
%
62+
%
6363
% ### See Also
64-
%
64+
%
6565
% [spinw] \| [spinw.anneal] \| [spinw.newcell]
6666
%
6767

@@ -103,7 +103,7 @@
103103
end
104104

105105
if nMagExt>0
106-
106+
107107
dR = [SS.all(1:3,:) zeros(3,nMagExt)];
108108
atom1 = [SS.all(4,:) 1:nMagExt];
109109
atom2 = [SS.all(5,:) 1:nMagExt];
@@ -127,8 +127,19 @@
127127

128128
Ml = repmat(permute(M1,[1 3 2]),[1 3 1]);
129129
Mr = repmat(permute(M2,[3 1 2]),[3 1 1]);
130-
131-
exchE = sum(sum(sum(Ml.*JJ.*Mr,3),2),1);
130+
131+
Q = Ml.*JJ.*Mr;
132+
if isa(Ml,'sym') || isa(Ml,'sym') || isa(Ml,'sym')
133+
[n1, n2, n3] = size(Q);
134+
sum_3 = 0;
135+
for k = 1:n3
136+
sum_3 = sum_3 + Q(:,:,k);
137+
end
138+
else
139+
sum_3 = sum(Q,3);
140+
end
141+
exchE = sum(sum(sum_3,2),1);
142+
132143
% TODO
133144
% correct energy for twins
134145
ZeemanE = -sum(SI.field*permute(mmat(SI.g,permute(M0,[1 3 2])),[1 3 2]),2)*obj.unit.muB;

swfiles/@spinw/genmagstr.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function genmagstr(obj, varargin)
341341
% Create random spin directions and use a single k-vector
342342
S = randn(nMagExt,3);
343343
S = bsxfun(@rdivide,S,sqrt(sum(S.^2,2)));
344-
S = bsxfun(@times,S,mAtom.Sext')';
344+
S = bsxfunsym(@times,S,mAtom.Sext')';
345345
if noK
346346
k = [0 0 0];
347347
end
@@ -387,11 +387,11 @@ function genmagstr(obj, varargin)
387387
if addPhase
388388
% additional phase for each spin in the magnetic supercell
389389
%phi = sum(bsxfun(@times,2*pi*kExt',r),1);
390-
phi = sum(bsxfun(@times,2*pi*permute(kExt,[2 3 1]),r),1);
390+
phi = sum(bsxfunsym(@times,2*pi*permute(kExt,[2 3 1]),r),1);
391391

392392
% add the extra phase for each spin in the unit cell
393393
% TODO check
394-
S = bsxfun(@times,S0(:,mod(0:(nMagExt-1),nSpin)+1,:),exp(-1i*phi));
394+
S = bsxfunsym(@times,S0(:,mod(0:(nMagExt-1),nSpin)+1,:),exp(-1i*phi));
395395
else
396396
S = S0;
397397
end

swfiles/sw_cartesian.m

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
function [vyOut, vzOut, vxOut] = sw_cartesian(n)
22
% creates a right handed Cartesian coordinate system
3-
%
3+
%
44
% ### Syntax
5-
%
5+
%
66
% `[vy, vz, vx] = sw_cartesian(n)`
77
%
88
% `V = sw_cartesian(n)`
9-
%
9+
%
1010
% ### Description
11-
%
11+
%
1212
% `[vy, vz, vx] = sw_cartesian(n)` creates an $(x,y,z)$ right handed
1313
% Cartesian coordinate system with $v_x$, $v_y$ and $v_z$ defining the
14-
% basis vectors.
14+
% basis vectors.
1515
%
1616
% `V = sw_cartesian(n)` the generated basis vectors are stored in the `V`
1717
% matrix: `V = [vx vy vz]` as column vectors.
18-
%
18+
%
1919
% ### Input Arguments
20-
%
20+
%
2121
% `n`
2222
% : Either a 3 element row/column vector or a $[3\times 3]$ matrix with
2323
% columns defining 3 vectors.
24-
%
24+
%
2525
% ### Output Arguments
26-
%
26+
%
2727
% `vy,vz,vx`
2828
% : Vectors defining the right handed coordinate system. They are
2929
% either column of row vectors depending on the shape of the
@@ -43,7 +43,14 @@
4343
z = [0; 0;-1];
4444
y = [0;-1; 0];
4545

46-
if any(cross(n,z))
46+
c = cross(n,z);
47+
if isa(n,'sym')
48+
opt = any(sw_always(c));
49+
else
50+
opt = any(c);
51+
end
52+
53+
if opt
4754
vy = cross(n,z);
4855
else
4956
vy = cross(n,y);
@@ -54,12 +61,12 @@
5461
if det(n) == 0
5562
error('sw_cartesian:WrongInput','The input vectors are not linearly independent!')
5663
end
57-
64+
5865
nShape = [3 1];
5966
vz = cross(n(:,1),n(:,2));
6067
vy = cross(vz,n(:,1));
6168
n = n(:,1);
62-
69+
6370
else
6471
error('sw_cartesian:WrongInput','Wrong size of n!')
6572
end
@@ -73,7 +80,6 @@
7380
vyOut = reshape(vy/norm(vy),nShape);
7481
vzOut = reshape(vz/norm(vz),nShape);
7582
vxOut = reshape(n/norm(n),nShape);
76-
7783
end
7884

7985
end

tutorials/publish/tutorial1.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
% value is dinamically calculated at every call.
5656

5757
FMchain.energy
58+
assert(FMchain.energy == -1)
5859

5960
%% Calculate spin wave dispersion and spin-spin correlation function
6061
% We calculate spin wave dispersion and correlation function along the

tutorials/publish/tutorial10.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@
7777
% ranges in momentum and energy. Second we call Horace to fill up the empty
7878
% d3d object with the simulated spin wave data and finally we plot a
7979
% constant energy cut.
80+
if ~exist('sqw','file')
81+
fprintf('Horace is not installed. Exiting....\n');
82+
return
83+
end
8084

8185
Ebin = [0,0.01,5];
8286
fwhm0 = 0.1;

tutorials/publish/tutorial12.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
% Horace is installed and setup. Calculate spectra using disp2sqw_eval
7373
% Horace function.
7474

75+
if ~exist('sqw','file')
76+
fprintf('Horace is not installed. Exiting....\n');
77+
return
78+
end
79+
7580
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]);
7681
horaceObj = disp2sqw_eval(horaceObj,@tri.horace,{'component','Sperp'},dE);
7782
cut1 = cut(horaceObj,[],[],[3.0 3.5]);

tutorials/publish/tutorial17.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
% also created using symbolic input variables, for example incommensurate
4545
% k-vector, etc.
4646

47-
FMchain.genmagstr('mode','helical','S',[0 1 0])
47+
FMchain.genmagstr('mode','helical','S',[0; 1; 0])
4848
FMchain.magtable.M
4949
plot(FMchain,'range',[3 0.5 0.5],'zoom',1)
5050

tutorials/publish/tutorial31.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
% Check spinw.formula() to see how much the new cell is smaller
7979

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

0 commit comments

Comments
 (0)