-
Notifications
You must be signed in to change notification settings - Fork 122
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
Add DynamicKuboToyabe fitting function #236
Changes from 24 commits
e7765fb
fb9f1dc
a5fab0c
5868103
5079fc9
b977abd
06a5cb2
55d48c5
fb46f0e
257ba6d
2383727
f4d713c
1fa60d8
96e3a4c
c7e744a
1b5a920
6751422
59d368a
e69074a
a04fcde
cdaed80
b9d7582
e96d4de
5ae7c88
5608f05
9be647a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#ifndef MANTID_CURVEFITTING_DYNAMICKUBOTOYABE_H_ | ||
#define MANTID_CURVEFITTING_DYNAMICKUBOTOYABE_H_ | ||
|
||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidAPI/IPeakFunction.h" | ||
#include "MantidAPI/IFunctionMW.h" | ||
#include "MantidAPI/IFunctionWithLocation.h" | ||
#include <cmath> | ||
|
||
//#include "MantidAPI/ParamFunction.h" | ||
//#include "MantidAPI/IPeakFunction.h" | ||
|
||
//#include "MantidAPI/IFunctionMW.h" | ||
//#include "MantidAPI/IFunction1D.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
/** | ||
Provide Dynamic Kubo Toyabe function interface to IPeakFunction for muon scientists. | ||
|
||
@author Karl Palmen, ISIS, RAL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and add your name here |
||
@date 21/03/2012 | ||
|
||
Copyright © 2007-2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
|
||
This file is part of Mantid. | ||
|
||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
File change history is stored at: <https://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
|
||
class DLLExport DynamicKuboToyabe : public API::ParamFunction, public API::IFunction1D | ||
{ | ||
public: | ||
|
||
/// Destructor | ||
virtual ~DynamicKuboToyabe() {} | ||
|
||
/// overwrite base class methods | ||
std::string name()const{return "DynamicKuboToyabe";} | ||
virtual const std::string category() const { return "Muon";} | ||
|
||
protected: | ||
virtual void function1D(double* out, const double* xValues, const size_t nData)const; | ||
virtual void functionDeriv1D(API::Jacobian* out, const double* xValues, const size_t nData); | ||
virtual void functionDeriv(const API::FunctionDomain& domain, API::Jacobian& jacobian); | ||
virtual void init(); | ||
virtual void setActiveParameter(size_t i, double value); | ||
|
||
}; | ||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid | ||
|
||
#endif /*MANTID_CURVEFITTING_DYNAMICKUBOTOYABE_H_*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
//---------------------------------------------------------------------- | ||
// Includes | ||
//---------------------------------------------------------------------- | ||
#include "MantidCurveFitting/DynamicKuboToyabe.h" | ||
#include "MantidAPI/Jacobian.h" | ||
#include "MantidAPI/FunctionFactory.h" | ||
#include <vector> | ||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
|
||
using namespace Kernel; | ||
using namespace API; | ||
|
||
DECLARE_FUNCTION(DynamicKuboToyabe) | ||
|
||
// ** MODIFY THIS ** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments need removing |
||
// Here specify/declare the parameters of your Fit Function | ||
// | ||
// declareParameter takes three arguments: | ||
// | ||
// 1st: The name of the parameter | ||
// 2nd: The default (initial) value of the parameter | ||
// 3rd: A description of the parameter (optional) | ||
// | ||
void DynamicKuboToyabe::init() | ||
{ | ||
declareParameter("Asym", 0.2, "Amplitude at time 0"); | ||
declareParameter("Delta", 0.2, "Local field"); | ||
declareParameter("Field", 0.0, "External field"); | ||
declareParameter("Nu", 0.0, "Hopping rate"); | ||
} | ||
|
||
// Static Zero Field Kubo Toyabe relaxation function | ||
double ZFKT (const double x, const double G){ | ||
|
||
const double q = G*G*x*x; | ||
return (0.3333333333 + 0.6666666667*exp(-0.5*q)*(1-q)); | ||
} | ||
|
||
// Static Non-Zero field Kubo Toyabe relaxation function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Take away the dead code |
||
//double HKT (const double x, const double G, const double F) | ||
//{ | ||
// throw std::runtime_error("HKT not implemented yet"); | ||
//} | ||
|
||
// Dynamic Kubo-Toyabe | ||
double getDKT (double t, double G, double v){ | ||
|
||
const int tsmax = 656; // Length of the time axis, 32 us of valid data | ||
const double eps = 0.05; // Bin width for calculations | ||
// const int stk = 25; // Not used for the moment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless there is a good reason for keeping this it should be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason for keeping it is that the HKT function will be used in the future (trac issue #11086), once I know how to implement it (there are some details that need to be discussed with muon scientists). But it is true that it is not used now, so I will be removing it for this ticket. |
||
|
||
static double oldG=-1., oldV=-1.; | ||
static std::vector<double> gStat(tsmax), gDyn(tsmax); | ||
|
||
|
||
if ( (G != oldG) || (v != oldV) ){ | ||
|
||
// If G or v have changed with respect to the | ||
// previous call, we need to re-do the computations | ||
|
||
|
||
if ( G != oldG ){ | ||
|
||
// But we only need to | ||
// re-compute gStat if G has changed | ||
|
||
// Generate static Kubo-Toyabe | ||
for (int k=0; k<tsmax; k++){ | ||
gStat[k]= ZFKT(k*eps,G); | ||
} | ||
// Store new G value | ||
oldG =G; | ||
} | ||
|
||
// Store new v value | ||
oldV =v; | ||
|
||
double hop = v*eps; | ||
|
||
// Generate dynamic Kubo Toyabe | ||
for (int k=0; k<tsmax; k++){ | ||
double y=gStat[k]; | ||
for (int j=k-1; j>0; j--){ | ||
y=y*(1-hop)+hop*gDyn[k-j]*gStat[j]; | ||
} | ||
gDyn[k]=y; | ||
} | ||
} | ||
|
||
// Interpolate table | ||
// If beyond end, extrapolate | ||
int x=int(fabs(t)/eps); | ||
if (x>tsmax-2) | ||
x = tsmax-2; | ||
double xe=(fabs(t)/eps)-x; | ||
return gDyn[x]*(1-xe)+xe*gDyn[x+1]; | ||
|
||
} | ||
|
||
// Dynamic Kubo Toyabe function | ||
void DynamicKuboToyabe::function1D(double* out, const double* xValues, const size_t nData)const | ||
{ | ||
const double& A = getParameter("Asym"); | ||
const double& G = fabs(getParameter("Delta")); | ||
const double& F = fabs(getParameter("Field")); | ||
const double& v = fabs(getParameter("Nu")); | ||
|
||
|
||
// Zero hopping rate | ||
if (v == 0.0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very confusing indentation here on this line and the ones below, could you try running clang on this? or fix it yourself |
||
|
||
// Zero external field | ||
if ( F == 0.0 ){ | ||
for (size_t i = 0; i < nData; i++) { | ||
out[i] = A*ZFKT(xValues[i],G); | ||
} | ||
} | ||
// Non-zero external field | ||
else{ | ||
//for (size_t i = 0; i < nData; i++) { | ||
// out[i] = A*HKT(xValues[i],G,F); | ||
//} | ||
throw std::runtime_error("HKT() not implemented yet"); | ||
} | ||
} | ||
|
||
// Non-zero hopping rate | ||
else { | ||
|
||
if ( F==0.0 ) { | ||
|
||
for (size_t i = 0; i<nData; i++){ | ||
out[i] = A*getDKT(xValues[i],G,v); | ||
} | ||
|
||
} else { | ||
|
||
// Non-zero field | ||
throw std::runtime_error("HKT() not implemented yet"); | ||
} | ||
|
||
} // else hopping rate != 0 | ||
|
||
|
||
} | ||
|
||
|
||
|
||
void DynamicKuboToyabe::functionDeriv(const API::FunctionDomain& domain, API::Jacobian& jacobian) | ||
{ | ||
calNumericalDeriv(domain, jacobian); | ||
} | ||
|
||
void DynamicKuboToyabe::functionDeriv1D(API::Jacobian* , const double* , const size_t ) | ||
{ | ||
throw Mantid::Kernel::Exception::NotImplementedError("functionDerivLocal is not implemented for DynamicKuboToyabe."); | ||
} | ||
|
||
void DynamicKuboToyabe::setActiveParameter(size_t i, double value) { | ||
|
||
setParameter( i, fabs(value), false); | ||
|
||
} | ||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't an IPeakFunction, could you correct the comment