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

Add DynamicKuboToyabe fitting function #236

Merged
merged 26 commits into from
Feb 20, 2015
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e7765fb
Re #9556 DKT base implementation
raquelalvarezbanos Jan 28, 2015
fb9f1dc
Re #9556 DKT updating parameter description
raquelalvarezbanos Jan 28, 2015
a5fab0c
Re #9556 DKT documentation
raquelalvarezbanos Jan 28, 2015
5868103
Re #9556 DKT updating setActiveParameter
raquelalvarezbanos Jan 28, 2015
5079fc9
Re #9556 DKT unit test
raquelalvarezbanos Jan 28, 2015
b977abd
Re #9556 Unit test testing function not fitting
raquelalvarezbanos Jan 29, 2015
06a5cb2
Re #9556 Fitting function improvements
raquelalvarezbanos Jan 29, 2015
55d48c5
Re #9556 Removing unnecessary includes
raquelalvarezbanos Jan 30, 2015
fb46f0e
Re #9556 removing math
raquelalvarezbanos Jan 30, 2015
257ba6d
Re #9556 fixing cpp checks
raquelalvarezbanos Jan 30, 2015
2383727
Re #9556 remove unit test temporarily
raquelalvarezbanos Jan 30, 2015
f4d713c
Re #9556 Enabling some unit tests
raquelalvarezbanos Jan 30, 2015
1fa60d8
Re #9556 Some bugs fixed and unit test updated
raquelalvarezbanos Jan 30, 2015
96e3a4c
Re #9556 Fixing title overline in rst
raquelalvarezbanos Feb 2, 2015
c7e744a
Re #9556 Splitting gz into ZFKT and HKT
raquelalvarezbanos Feb 9, 2015
1b5a920
Re #9556 Updating function according to fortran code provided
raquelalvarezbanos Feb 12, 2015
6751422
Re #9556 Updating some values and temporarily disabling some tests
raquelalvarezbanos Feb 12, 2015
59d368a
Re #9556 Being more strict with test results
raquelalvarezbanos Feb 12, 2015
e69074a
Re #9556 Updating documentation
raquelalvarezbanos Feb 12, 2015
a04fcde
Re #9556 updating notation, array indexing, bin width
raquelalvarezbanos Feb 12, 2015
cdaed80
Re #9556 code refactoring
raquelalvarezbanos Feb 13, 2015
b9d7582
Re #9556 Removing unused macros
raquelalvarezbanos Feb 13, 2015
e96d4de
Re #9556 Commenting unused HKT function
raquelalvarezbanos Feb 18, 2015
5ae7c88
Re #9556 Fixing size_t to double conversion
raquelalvarezbanos Feb 18, 2015
5608f05
Re #9556 Code cleaning
raquelalvarezbanos Feb 18, 2015
9be647a
Re #9556 Removing more dead code
raquelalvarezbanos Feb 19, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set ( SRC_FILES
src/DerivMinimizer.cpp
src/DiffRotDiscreteCircle.cpp
src/DiffSphere.cpp
src/DynamicKuboToyabe.cpp
src/EndErfc.cpp
src/ExpDecay.cpp
src/ExpDecayMuon.cpp
Expand Down Expand Up @@ -135,6 +136,7 @@ set ( INC_FILES
inc/MantidCurveFitting/DiffRotDiscreteCircle.h
inc/MantidCurveFitting/DiffSphere.h
inc/MantidCurveFitting/DllConfig.h
inc/MantidCurveFitting/DynamicKuboToyabe.h
inc/MantidCurveFitting/EndErfc.h
inc/MantidCurveFitting/ExpDecay.h
inc/MantidCurveFitting/ExpDecayMuon.h
Expand Down Expand Up @@ -237,6 +239,7 @@ set ( TEST_FILES
DeltaFunctionTest.h
DiffRotDiscreteCircleTest.h
DiffSphereTest.h
DynamicKuboToyabeTest.h
EndErfcTest.h
ExpDecayMuonTest.h
ExpDecayOscTest.h
Expand Down
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.
Copy link
Contributor

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


@author Karl Palmen, ISIS, RAL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add your name here

@date 21/03/2012

Copyright &copy; 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_*/
170 changes: 170 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/DynamicKuboToyabe.cpp
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 **
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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