Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Bugfixes #180

Merged
merged 10 commits into from
May 20, 2024
2 changes: 1 addition & 1 deletion +sw_tests/+system_tests/systemtest_spinwave_pcsmo.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function test_pcsmo(testCase, usehorace)
spec = testCase.swobj.spinwave(qln, 'formfact', true, 'saveV', true, 'saveH', true, 'optmem', 2);
spec = sw_egrid(spec, 'Evect', linspace(0, 100, 200));
spec = sw_neutron(spec);
testCase.generate_or_verify(spec, {}, struct('V', spec.V, 'H', spec.H), 'approxSab', 0.1);
testCase.generate_or_verify(spec, {}, struct('V', spec.V, 'H', spec.H), 'approxSab', 0.11);
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions +sw_tests/+unit_tests/unittest_spinw_fitspec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
classdef unittest_spinw_fitspec < sw_tests.unit_tests.unittest_super
% Tests for fitspec - not strictly a unit test, but make sure it runs

properties
datafile = '';
swobj = [];
fitpar = struct();
end

methods (TestClassSetup)
function setup_model_and_fitpars(testCase)
% Writes out the mode data
testCase.datafile = fullfile(tempdir, 'triAF_modes.txt');
fid = fopen(testCase.datafile, 'w');
fprintf(fid, ' QH QK QL Elim1 Elim2 I1 EN1 sig1 I2 EN2 sig2\n');
fprintf(fid, ' 1 0.2 1 0.5 5.0 22.6 2.94 0.056 0 0 0\n');
fprintf(fid, ' 1 0.4 1 0.5 5.0 65.2 2.48 0.053 13.8 3.23 0.061\n');
fprintf(fid, ' 1 0.6 1 0.5 5.0 69.5 2.52 0.058 16.3 3.15 0.054\n');
fprintf(fid, ' 1 0.8 1 0.5 5.0 22.6 2.83 0.057 0 0 0\n');
fclose(fid);
testCase.swobj = sw_model('triAF', 0.7);
testCase.fitpar = struct('datapath', testCase.datafile, ...
'Evect', linspace(0, 5, 51), ...
'func', @(obj,p)matparser(obj,'param',p,'mat',{'J_1'},'init',1),...
'xmin', 0, 'xmax', 2, 'x0', 0.7, ...
'plot', false, 'hermit', true, ...
'optimizer', 'simplex', ...
'maxiter', 1, 'maxfunevals', 1, 'nMax', 1, 'nrun', 1);
end
end

methods (TestClassTeardown)
function del_mode_file(testCase)
try
delete(testCase.datafile); % Ignores errors if file already deleted
end
end
end

methods (Test)
function test_fitspec(testCase)
fitout = testCase.swobj.fitspec(testCase.fitpar);
testCase.verify_val(fitout.x, 1.0, 'abs_tol', 0.25);
testCase.verify_val(fitout.redX2, 0.0, 'abs_tol', 10);
end
function test_fitspec_twin(testCase)
% Checks that twins are handled correctly
swobj = copy(testCase.swobj);
% Adds a twin with very small volume so it doesn't affect original fit
% If twins not handled correctly, the fit will be bad.
swobj.addtwin('axis', [1 1 1], 'phid', 54, 'vol', 0.01);
fitout = swobj.fitspec(testCase.fitpar);
testCase.verify_val(fitout.x, 1.0, 'abs_tol', 0.25);
testCase.verify_val(fitout.redX2, 0.0, 'abs_tol', 10);
end
end
end
3 changes: 3 additions & 0 deletions +sw_tests/+unit_tests/unittest_spinw_spinwave.m
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ function test_sw_qh5_fitmode(testCase, mex)
% fitmode automatically turns off sortMode
expected_sw = testCase.swobj.spinwave(qpts, 'sortMode', false);
expected_sw = rmfield(expected_sw, {'obj', 'datestart', 'dateend'});
obj = testCase.swobj;
expected_sw.obj = struct('single_ion', obj.single_ion, 'twin', obj.twin, ...
'unit', obj.unit, 'basisvector', obj.basisvector, 'nmagext', obj.nmagext);
testCase.verify_spinwave(sw_out, expected_sw);
end
function test_incommensurate(testCase, mex)
Expand Down
38 changes: 22 additions & 16 deletions .github/workflows/build_pyspinw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Check out SpinW
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Dependencies
run: ${{ matrix.INSTALL_DEPS }}
- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1 # v1.1.0 required for Windows/MacOS support
uses: matlab-actions/setup-matlab@v2
with:
release: ${{ matrix.matlab_version }}

Expand All @@ -41,13 +43,13 @@ jobs:
rm external/mtimesx/sw_mtimesx.mexmaci64
rm external/mtimesx/sw_mtimesx.mexw64
- name: Run MEXing
uses: matlab-actions/run-command@v1
uses: matlab-actions/run-command@v2
with:
command: "addpath(genpath('swfiles')); addpath(genpath('external')); sw_mex('compile', true, 'test', false, 'swtest', false);"
- name: Upload MEX results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: MEX
name: mex-${{ matrix.os }}
path: ${{ github.workspace }}/external/**/*.mex*

build_ctfs:
Expand All @@ -58,22 +60,22 @@ jobs:
runs-on: self-hosted
steps:
- name: Check out SpinW
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download MEX artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: MEX
pattern: mex-*
path: ${{ github.workspace }}/external
- name: Build ctf
run: |
cd python
/Applications/MATLAB_${{ matrix.matlab_version }}.app/bin/matlab -nodisplay -r "build_ctf; exit"
- name: Upload CTF results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: CTF
name: ctf-${{ matrix.matlab_version }}
path: ${{ github.workspace }}/python/ctf/*.ctf

build_wheel:
Expand All @@ -83,11 +85,11 @@ jobs:
contents: write
steps:
- name: Checkout SpinW
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Download CTF artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: CTF
pattern: ctf-*
path: python/ctf
- name: Set up Python environment
uses: actions/setup-python@v4
Expand All @@ -98,15 +100,16 @@ jobs:
cd python
echo "PYSPINW_VERSION=$( cat pyproject.toml | grep "version = \"" | awk -F'"' '$0=$2' | sed 's/ //g' )" >> $GITHUB_ENV
mkdir pyspinw/ctfs
mv ctf/*.ctf pyspinw/ctfs
find ctf/ -name "*.ctf" -exec mv '{}' pyspinw/ctfs \;
- name: Build Wheel
run: |
cd ${{ github.workspace }}/python
python -m pip wheel --no-deps --wheel-dir build .
- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1 # v1.1.0 required for Windows/MacOS support
uses: matlab-actions/setup-matlab@v2
with:
release: R2023a
products: MATLAB_Compiler_SDK
- name: Run python test
run: |
pip install scipy
Expand All @@ -115,7 +118,7 @@ jobs:
cd tests
python -m unittest
- name: Create wheel artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: pySpinW Wheel
path: ${{ github.workspace }}/python/build/*.whl
Expand All @@ -124,3 +127,6 @@ jobs:
run: |
pip3 install requests
python3 release.py --notest --github --token=${{ secrets.GH_TOKEN }}
- name: Setup tmate
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
2 changes: 1 addition & 1 deletion .github/workflows/publish_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# IMPORTANT: this permission is mandatory for trusted publishing
id-token: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download wheels
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: Creates a SpinW release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- if: |
Expand Down
21 changes: 13 additions & 8 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,45 @@ jobs:
matlab_version: [latest]
include:
- os: ubuntu-latest
matlab_version: R2020a
matlab_version: R2021a
- os: macos-latest
INSTALL_DEPS: brew install llvm libomp
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- name: Check out SpinW
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Dependencies
run: ${{ matrix.INSTALL_DEPS }}
- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1 # v1.1.0 required for Windows/MacOS support
uses: matlab-actions/setup-matlab@v2
with:
release: ${{ matrix.matlab_version }}
- name: Run tests
uses: matlab-actions/run-command@v1
uses: matlab-actions/run-command@v2
with:
command: "run run_tests.m"
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
if: ${{ always() && matrix.os == 'ubuntu-latest' && matrix.matlab_version == 'latest' }}
with:
files: coverage*.xml
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Unit test results ${{ matrix.os }}
name: Unit test results ${{ matrix.os }}-${{ matrix.matlab_version }}
path: junit_report*.xml
#- name: Setup tmate
# if: ${{ failure() }}
# uses: mxschmitt/action-tmate@v3
publish-test-results:
needs: test
runs-on: ubuntu-latest
if: success() || failure()
steps:
- name: Download Artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Publish test results
Expand Down
6 changes: 3 additions & 3 deletions swfiles/@spinw/fitspec.m
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@
[x(idx,:),~, output(idx)] = ndbase.pso(dat,@(x,p)spec_fitfun(obj, data, param.func, p, param0),x0,'lb',param.xmin,'ub',param.xmax,...
'TolX',param.tolx,'TolFun',param.tolfun,'MaxIter',param.maxiter);

redX2(idx) = output.redX2;
redX2(idx) = output(idx).redX2;

case 'simplex'
[x(idx,:),~, output(idx)] = ndbase.simplex(dat,@(x,p)spec_fitfun(obj, data, param.func, p, param0),x0,'lb',param.xmin,'ub',param.xmax,...
'TolX',param.tolx,'TolFun',param.tolfun,'MaxIter',param.maxiter);

redX2(idx) = output.redX2;
redX2(idx) = output(idx).redX2;

case 'lm'
% does not work due to the binning of the spectrum
Expand Down Expand Up @@ -448,7 +448,7 @@
if param.plot
text(0.05,0.9,['x = [' sprintf('%6.4f ',x) sprintf(']\nRw = %6.4f',sqrt(R))],'Units','normalized','fontsize',12);
axis([0.5 Qc+0.5 param.Evect(1) param.Evect(end)]);
legend(pHandle(1:2),'simulation','data')
legend(pHandle([1, length(simE)+2]),'simulation','data')
xlabel('Scan index')
ylabel('Energy transfer (meV)')
title('Spin wave dispersion fit')
Expand Down
11 changes: 10 additions & 1 deletion swfiles/@spinw/spinwave.m
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@
'structure is commensurate so this will have no effect.']);
end

% If only one hkl value, convert to column vector
if numel(hkl) == 3
hkl = hkl(:);
end

% Transform the momentum values to the new lattice coordinate system
hkl = obj.unit.qmat*hkl;

Expand Down Expand Up @@ -1265,7 +1270,11 @@
spectra.title = param.title;
spectra.gtensor = param.gtensor;

if ~param.fitmode
if param.fitmode
% Copies only fields needed by downstream functions (sw_egrid, sw_neutron, sw_plotspec)
spectra.obj = struct('single_ion', obj.single_ion, 'twin', obj.twin, ...
'unit', obj.unit, 'basisvector', obj.basisvector, 'nmagext', obj.nmagext);
else
spectra.dateend = datestr(now);
spectra.obj = copy(obj);
end
Expand Down
5 changes: 3 additions & 2 deletions swfiles/sw_egrid.m
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@
if param.T==0
nBose = double(ebin_cens>=0);
else
nBose = 1./(exp(abs(ebin_cens)./(spectra.obj.unit.kB*param.T))-1)+double(ebin_cens>=0);
kB = 0.086173324;
nBose = 1./(exp(abs(ebin_cens)./(kB*param.T))-1)+double(ebin_cens>=0);
end

% Sums up the intensities in DSF into swConv.
Expand Down Expand Up @@ -555,7 +556,7 @@
if isfield(spectra,'obj')
vol = spectra.obj.twin.vol/sum(spectra.obj.twin.vol);
else
vol = 1;
vol = ones(1,nTwin);
end
swConvT = cell(nConv,1);
DSFT = cell(nConv,1);
Expand Down
24 changes: 18 additions & 6 deletions swfiles/sw_mex.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,30 @@ function sw_mex(varargin)
mex('-R2018a',['COMPFLAGS= /I eigen-' eigen_ver],'swloop.cpp')
elseif ismac
% add =libiomp5 after -fopenmp?
if strcmp(mexext, 'mexmaca64')
mwlinklib = '-L$MATLABROOT/bin/maca64';
else
mwlinklib = '-L$MATLABROOT/sys/os/maci64';
end
if exist('/opt/homebrew/opt/libomp/include') == 7
omp_inc = '-I/opt/homebrew/opt/libomp/include';
omp_lib = '-lomp';
else
omp_inc = '-I/usr/local/opt/libomp/include';
omp_lib = '-liomp5';
end
cd(eig_omp_dir);
mex('-v','-largeArrayDims','eig_omp.cpp', 'CXX_FLAGS="-Xclang -fopenmp -pthread"', ...
'LDFLAGS="$LDFLAGS -L$MATLABROOT/sys/os/maci64 -liomp5 -lmwblas -lmwlapack"', ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', "-I/usr/local/opt/libomp/include");
['LDFLAGS="$LDFLAGS ' mwlinklib ' ' omp_lib ' -lmwblas -lmwlapack"'], ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', omp_inc);
cd(chol_omp_dir);
mex('-v','-largeArrayDims','chol_omp.cpp', 'CXX_FLAGS="-Xclang -fopenmp -pthread"', ...
'LDFLAGS="$LDFLAGS -L$MATLABROOT/sys/os/maci64 -liomp5 -lmwblas -lmwlapack"', ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', "-I/usr/local/opt/libomp/include");
['LDFLAGS="$LDFLAGS ' mwlinklib ' ' omp_lib ' -lmwblas -lmwlapack"'], ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', omp_inc);
cd(mtimesx_dir);
mex('-v','-largeArrayDims','sw_mtimesx.c', 'CXX_FLAGS="-Xclang -fopenmp -pthread"', ...
'LDFLAGS="$LDFLAGS -L$MATLABROOT/sys/os/maci64 -liomp5 -lmwblas"', ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', "-I/usr/local/opt/libomp/include");
['LDFLAGS="$LDFLAGS ' mwlinklib ' ' omp_lib ' -lmwblas"'], ...
'CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp"', omp_inc);
cd(swloop_dir);
mex('-v','-R2018a',['-Ieigen-' eigen_ver],'swloop.cpp')
else
Expand Down
Loading
Loading