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

Class interface to fit powder spectra #174

Merged
merged 44 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1cdd36b
Add fitpow method for fitting powder data
mducle Jan 19, 2024
e7ed167
Add intensity and background to fitpow
mducle Jan 21, 2024
3189c02
Add mex file to calculate q-convolution loop
mducle Jan 21, 2024
e2f4abe
Fix variables not assigned bug when maxiter=0 in lm3
RichardWaiteSTFC Mar 7, 2024
29d9c01
Add new class to sw_files
RichardWaiteSTFC Mar 7, 2024
bb876c1
Add dE option of sw_egrid as input argument to powspec
RichardWaiteSTFC Mar 19, 2024
47848ac
Class interface to fit 2D and 1D powder spectra
RichardWaiteSTFC Mar 19, 2024
1410321
Change to support user supplied minimizer (assumed from ndbase)
RichardWaiteSTFC Mar 20, 2024
b94e944
Add default fbg for 1D independent cuts and fix bug in bg subtraction
RichardWaiteSTFC Mar 20, 2024
6a4de70
Fix bug in en bins passed to powspec when data cropped
RichardWaiteSTFC Mar 20, 2024
90ee9cf
Fix bug with estimating scale with planar bg and 1D cuts
RichardWaiteSTFC Apr 18, 2024
094eca9
Pass varargin to optimizer
RichardWaiteSTFC Apr 18, 2024
738038e
Reshape parameters and bounds to work with all ndbase optmisers
RichardWaiteSTFC Apr 18, 2024
a769f7c
Add methods to plot results
RichardWaiteSTFC Apr 18, 2024
13117e3
Implement caching of spinwave calc if params unchanged
RichardWaiteSTFC Apr 26, 2024
8e86112
Add seperate setters for model, bg and scale parameters and bounds
RichardWaiteSTFC Apr 26, 2024
6622e09
Add docs string to class
RichardWaiteSTFC Apr 27, 2024
c7e8704
Set some properties to be private
RichardWaiteSTFC Apr 27, 2024
b1fb802
Fix bug in getting bg param index for planar bg
RichardWaiteSTFC Apr 29, 2024
5596ea8
Remove Name-Val pairs from constructor and add bg estimation method
RichardWaiteSTFC Apr 29, 2024
4a3542f
Support sqw, d1d and d2d objects and add validation
RichardWaiteSTFC Apr 29, 2024
068672c
Add crop |Q| method for 2D data
RichardWaiteSTFC Apr 30, 2024
908e7cc
Fix bug in modQ bin cens for adjacent cuts so no overlap
RichardWaiteSTFC Apr 30, 2024
9e2b47b
Add option to specify nQ for 1D cuts in init
RichardWaiteSTFC Apr 30, 2024
32c4047
Add unit tests for sw_fitpowder
RichardWaiteSTFC Apr 30, 2024
3cf2890
Add unit tests for estimating bg and scale
RichardWaiteSTFC May 1, 2024
c3fae34
Add tests for calc_cost_func
RichardWaiteSTFC May 1, 2024
83606d9
Update doc strings/comments
RichardWaiteSTFC May 1, 2024
2a00cc0
Fix typo calling static method without obj.
RichardWaiteSTFC May 1, 2024
0835cdb
Set appropriate axes limits
RichardWaiteSTFC May 1, 2024
3a18555
Delete fitpow
RichardWaiteSTFC May 1, 2024
feb6cb3
Add live plot - code form @mducle
RichardWaiteSTFC May 1, 2024
ed634a9
Add liveplot_interval
RichardWaiteSTFC May 2, 2024
f5088ec
Make dE soft parameter in powspec
RichardWaiteSTFC May 2, 2024
135ca59
Add fastmode and neutorn_output to powspec
RichardWaiteSTFC May 2, 2024
589d70d
Fix bugs loading horace objects
RichardWaiteSTFC May 2, 2024
bd31eb1
Fix bug where not running sw_neutron if neutron_output=false
RichardWaiteSTFC May 2, 2024
fe34ab4
Merge branch 'master' into fitpow_sw_egrid_resolution_merged
RichardWaiteSTFC May 17, 2024
eefb777
Add function to exclude energy range ref #185
RichardWaiteSTFC May 20, 2024
1fadf9c
Fix bug with empty plot created when liveplot_interval=0
RichardWaiteSTFC May 20, 2024
4c9a49e
Remove duplicate code setting parameter bounds
RichardWaiteSTFC May 20, 2024
599d534
Fix typo - stop printing variables in lm3 if not converged
RichardWaiteSTFC May 20, 2024
6136a14
Clear cache on changing data
RichardWaiteSTFC May 20, 2024
d72280c
Fix bug with lm3 not fixing parameters - thanks @mducle for code
RichardWaiteSTFC May 20, 2024
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
85 changes: 85 additions & 0 deletions external/sw_qconv/sw_qconv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "mex.h"
#include <stdexcept>
#include <complex>
#include <thread>
#include <vector>

template <typename T>
void loop(T *swOut, const mwSize *d1, const double stdG, const double *Qconv, const T *swConv, size_t i0, size_t i1) {
for (size_t ii=i0; ii<i1; ii++) {
double sumfG = 0.0;
std::vector<double> fG(d1[1], 0.0);
for (size_t jj=0; jj<d1[1]; jj++) {
fG[jj] = exp(-pow((Qconv[jj] - Qconv[ii]) / stdG, 2) / 2);
sumfG += fG[jj];
}
for (size_t jj=0; jj<d1[1]; jj++) {
fG[jj] /= sumfG;
for (size_t kk=0; kk<d1[0]; kk++) {
swOut[kk+d1[0]*jj] += swConv[kk+d1[0]*ii] * fG[jj];
}
}
}
}

template <typename T>
void do_calc(T *swOut, const mwSize *d1, const double stdG, const double *Qconv, const T *swConv, size_t nThread) {
if (d1[1] > 10*nThread) {
size_t nBlock = d1[1] / nThread;
size_t i0 = 0, i1 = nBlock;
std::vector<T*> swv(nThread);
std::vector<std::thread> threads;
for (size_t ii=0; ii<nThread; ii++) {
swv[ii] = new T[d1[0] * d1[1]];
threads.push_back(
std::thread(loop<T>, std::ref(swv[ii]), std::ref(d1), stdG, std::ref(Qconv), std::ref(swConv), i0, i1)
);
i0 = i1;
i1 += nBlock;
if (i1 > d1[1] || ii == (nThread - 2)) {
i1 = d1[1]; }
}
for (size_t ii=0; ii<nThread; ii++) {
if (threads[ii].joinable()) {
threads[ii].join(); }
for (size_t jj=0; jj<d1[0]; jj++) {
for (size_t kk=0; kk<d1[1]; kk++) {
swOut[jj+kk*d1[0]] += swv[ii][jj+kk*d1[0]];
}
}
delete[](swv[ii]);
}
} else {
loop<T>(swOut, d1, stdG, Qconv, swConv, 0, d1[1]);
}
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
if (nrhs < 3) {
throw std::runtime_error("sw_qconv: Requires 3 arguments");
}
size_t nThreads = 8;
if (nrhs == 4) {
nThreads = (size_t)(*mxGetDoubles(prhs[3]));
}
if (mxIsComplex(prhs[1])) { throw std::runtime_error("Arg 2 is complex\n"); }
if (mxIsComplex(prhs[2])) { throw std::runtime_error("Arg 3 is complex\n"); }
const mwSize *d1 = mxGetDimensions(prhs[0]);
const mwSize *d2 = mxGetDimensions(prhs[1]);
if (d1[1] != d2[1]) { throw std::runtime_error("Arg 1 and 2 size mismatch\n"); }
if (mxGetNumberOfElements(prhs[2]) > 1) { throw std::runtime_error("Arg 3 should be scalar\n"); }
double *Qconv = mxGetDoubles(prhs[1]);
double stdG = *(mxGetDoubles(prhs[2]));
if (mxIsComplex(prhs[0])) {
std::complex<double> *swConv = reinterpret_cast<std::complex<double>*>(mxGetComplexDoubles(prhs[0]));
plhs[0] = mxCreateDoubleMatrix(d1[0], d1[1], mxCOMPLEX);
std::complex<double> *swOut = reinterpret_cast<std::complex<double>*>(mxGetComplexDoubles(plhs[0]));
do_calc(swOut, d1, stdG, Qconv, swConv, nThreads);
} else {
double *swConv = mxGetDoubles(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(d1[0], d1[1], mxREAL);
double *swOut = mxGetDoubles(plhs[0]);
do_calc(swOut, d1, stdG, Qconv, swConv, nThreads);
}
}
4 changes: 3 additions & 1 deletion swfiles/+ndbase/lm3.m
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@

% function values at start
yBest = yCalc(:);

converged=false;
if Niter > 0
% optimisation

Expand Down Expand Up @@ -357,6 +357,8 @@
tmp = repmat(1./sqrt(diag(cov)),[1,NpFree]);
cor = tmp.*cov.*tmp';
else
sigP = []
iter=0
chisqr = chi2Best/nnorm;
ok = true;
warning('WARNING: Convergence not achieved')
Expand Down
Loading
Loading