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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
77 changes: 75 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# [v3.2.0](https://github.com/pace-neutrons/libpymcr/compare/0.0.1...v3.2.0)
# [Unreleased](https://github.com/spinw/spinw/compare/v3.2.0...master)

## New Features

- Add a function to output Mantid MDHistogramWorkspaces (`sw_spec2MDHisto`)
- Add Python plotting of magnetic structure using [vispy](https://vispy.org/)
- Add mex files to compute main loop in `spinwave()` enabling a 2x - 4x speed up depending on system size

## Improvements

- Replace `spinwavefast()` method with a new `fastmode` option in the main `spinwave()` method to reduce confusion
- Adds a `neutron_output` option to `spinwave()` to compute only the neutron (`Sperp`) cross-section and not the full spin-spin correlation tensor, saving memory

## Bug Fixes

- Corrects equation for Q-range from `thetaMin` in `sw_instrument` and add `thetaMax` option
- Fixes `sw_issymspec` to recognise powder spectra
- Fixes a parsing error in the `spinw.fourier` method if no sublat option given.
- Fixes several bugs in `sw_plotspec` where it ignores user options in `'auto'` mode, and where it inverts user supplied colormaps.
- Fixes several bugs in `.fitspec()` for handling twins and where it only outputs the final chi^2 values.


# [v3.2.0](https://github.com/spinw/spinw/compare/0.0.1...v3.2.0)

## Initial public beta of PySpinW

Expand Down Expand Up @@ -34,7 +56,6 @@ plt.show()

On Windows and Linux systems, as long as you're running PySpinW locally, Matlab plotting commands like `m.plot(swobj)` will work. This is not the case on MacOS (a known bug) and on remote systems (e.g. via JupyterHub).


# [v0.0.1](https://github.com/spinw/spinw/compare/v3.1.2...0.0.1)

## pySpinW
Expand Down Expand Up @@ -82,3 +103,55 @@ spec = m.spinwave(s, [q_start, q_end, pts])
### Known limitations

At the moment graphics will not work on macOS systems and is disabled.


# [v3.1.2](https://github.com/spinw/spinw/compare/v3.1.0...v3.1.2)

## Improvements

- Change to preallocation of output energies in `spinwave` to reduce memory usage and improve calculation speed
- Use a mex function (if mex is enabled) for matrix multiplication in `spinwave` with `hermit=false` that reduces memory usage and improves calculation speed for large magnetic cells (in an example with 216 magnetic atoms the execution time was reduced by ~65%)


## Bug Fixes

- Fix generation of lattice from basis vectors in `genlattice`, see issue [#28](https://github.com/SpinW/spinw/issues/28)
- `sortMode` in `spinwave` now correctly sorts the spin wave modes within each twin
- A `spinw` object can now be correctly created from a structure figure
- `.cif` files with a mixture of tabs and spaces or containing a `?` in the comments can now be read correctly
- Rotation matrix `rotC` in `addtwin` is now required to be a valid rotation or reflection matrix.
- Spin of atom in `addatom` must have `S>=0`.
- Anisotropic g-tensor in `addg` must be physically valid - i.e. :math:`g^\dagger.g` must be a symmetric positive definite matrix.
- Fix bug in addcoupling that did not allow user to supply 'atom' with numeric array of atom indices (previously only worked for string or cell of strings corresponding to atom labels).
- Renamed undocumented `gencoupling` parameter `tol` to `tolMaxDist` (see doc string of `gencoupling` for more details).
- Added validation to `gencoupling` to ensure `maxDistance > dMin`.
- Fixed uncaught error in `gencoupling` by checking if any bonds have length < `maxSym`
- A warning will now be emitted if `saveSabp` is requested in `spinwave` for a commensurate structure
- Fix bug in definition of rotation matrix transforming to spinw coordinate system when left-handed set of basis vectors supplied to `genlattice`, see issue [#57](https://github.com/SpinW/spinw/issues/57)
- Validation added for `perm` and `origin` arguments supplied to `genlattice` (and warn users that these will be ignored if no symmetry/spacegroup is supplied in the same function call).
- Deprecated `spgr` argument to `genlattice` (users should use `sym` instead).
- Fix `MATLAB:nonLogicalConditional` error raised when using multiple k in `genmagstr` with `helical` mode
- Raise error if invalid shape `S` or `k` is provided to `genmagstr`, previously they would be silently set to zero
- Raise error if wrong number of spins `S` is provided to `genmagstr` in `helical` mode. Previously the structure would be silently initialised to a random structure.
- Raise error if a complex spin `S` is provided to `genmagstr` in `helical` mode. Previously this meant it would silently ignore the `n` option, and behave exactly like `fourier` mode.
- Raise error if `rotate` mode is used without first initialising a magnetic structure
- Emit deprecation warning if the undocumented `extend` mode is used in `genmagstr`
- Raise error if the first spin is parallel to `n` and no rotation angle is provided in `rotate` mode in `genmagstr`. Previously this would silently result in `NaN`
- Raise error if `phi` or `phid` is not real in `rotate` mode in `genmagstr`. This was an undocumented feature which has been removed.
- Emit warning that the spin amplitude will be moderated if components of `S` are parallel to `n` in `helical` mode in `genmagstr`
- Emit warning if `nExt` is unnecessarily large compared to `k` in `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 is provided for `n` in `genmagstr`. Previously this would've caused a crash.
- Fix error when plotting progress of `optmagsteep` without existing figure
- Correctly report magnetic moments in each iteration of `optmagsteep`.
- Fix errors when calling `intmatrix` with dipolar bonds and symbolic spinw object with fitmode true and false
- Ensure biquadratic exchange interactions are isotropic in `addcoupling` (previously checked in `intmatrix`)
- Raise error if invalid shape `kbase` is provided to `optmagk`, previously it would be silently set to empty
- Ensure varargin is correctly passed through to `ndbase.pso` from `optmagk`. Previously user provided `TolFun`, `TolX` and `MaxIter` would be overwritten by the defaults.
- Warn users that that the results of `spinwave` have not been scientifically validated for supercell structures with an incommensurate modulation.
- Emit warning if wrong length `xmin`, `xmax` or `x0` is passed to `optmagstr`. Previously they would be silently ignored.
- No longer require a magnetic structure be initialised with `genmagstr` before using `optmagstr`. If not intialised, a default `nExt` of `[1 1 1]` is used. This has also been clarified in the docstring.
- Fix bug where powder spectra was not recognised in `sw_plotspec`, introduced by a previous update to provide more helpful error messages.
- `sw_instrument` now calculates the limits for thetaMax, before it was using the continuation of the thetaMin line to high Q which is incorrect.
- Fixes a parsing error in the `spinw.fourier` method if no sublat option given.

Loading
Loading