Skip to content

Commit 02c1d81

Browse files
committed
Add support for Model repo
User supplied models can now be obtained through the new repo: https://www.github.com/spinw/Models Or on the MATLAB search path with files starting in 'swm_*'. Former-commit-id: 39f2331
1 parent d1f04fb commit 02c1d81

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

swfiles/sw_model.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
% 120\\deg angle and optimised magnetic structure.
2222
% * `'squareAF'` Square lattice antiferromagnet.
2323
% * `'chain'` Chain with further neighbor interactions.
24-
%
24+
% * `swm_*` Custom models which are in the matlab path can be
25+
% evaluated. Checkout:
26+
% https://www.github.com/spinw/Models for pre-made models.
27+
%
2528
% `param`
2629
% : Input parameters of the model, row vector which gives the values of the
2730
% Heisenberg exchange for first, second, thirs etc. neighbor bonds stored
@@ -49,6 +52,7 @@
4952
end
5053

5154
fprintf0(fid,'Preparing ''%s'' model ...\n',model);
55+
modelSearch = 'swm_';
5256

5357
obj = spinw;
5458

@@ -108,7 +112,26 @@
108112
end
109113
end
110114
otherwise
111-
error('sw_model:WrongINput','Model does not exists!')
115+
% All paths
116+
allPaths = strsplit(path,':');
117+
% Remove the builtin functions
118+
allPaths = allPaths(~strncmp(allPaths,fullfile(matlabroot, 'toolbox'), length(fullfile(matlabroot, 'toolbox'))));
119+
relPaths = allPaths(~strncmp(allPaths,fullfile(matlabroot, 'example'), length(fullfile(matlabroot, 'example'))));
120+
% Get all files
121+
allFiles = cellfun(@(x) dir(x), relPaths, 'UniformOutput', false);
122+
allFiles = cellfun(@(x) {x.name}, allFiles, 'UniformOutput', false);
123+
allFiles = [allFiles{:}];
124+
% Search for files which are models
125+
relFiles = cellfun(@(x) x(1:end-2) ,allFiles(strncmp(allFiles, modelSearch, 3)),'UniformOutput',false);
126+
if ~any(strcmp(model, relFiles))
127+
error('sw_model:WrongINput','Model does not exists!')
128+
end
129+
% Evaluate the model
130+
try
131+
obj = feval(model, param);
132+
catch ME
133+
error('sw_model:WrongINput','This model has an error!')
134+
end
112135
end
113136

114137
fprintf0(fid,'... ready!\n');

0 commit comments

Comments
 (0)