Mantid
Loading...
Searching...
No Matches
HallRossSQE.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10#include "MantidAPI/Jacobian.h"
11#include "MantidKernel/Logger.h"
12#include <cmath>
13#include <limits>
14
15namespace {
16Mantid::Kernel::Logger g_log("HallRossSQE");
17}
18
20
21DECLARE_FUNCTION(HallRossSQE)
22
23
26void HallRossSQE::declareParameters() {
27 this->declareParameter("Height", 1.0, "scaling factor");
28 this->declareParameter("Centre", 0.0, "Shift along the X-axis");
29 this->declareParameter("Tau", 1.25, "Residence time");
30 this->declareParameter("L", 1.25, "Jump length");
31}
32
39void HallRossSQE::function1D(double *out, const double *xValues, const size_t nData) const {
40 double hbar(0.658211626); // ps*meV
41 auto H = this->getParameter("Height");
42 auto C = this->getParameter("Centre");
43 auto Q = this->getAttribute("Q").asDouble();
44 auto T = this->getParameter("Tau");
45 auto L = this->getParameter("L");
46
47 // Penalize negative parameters, just in case they show up
48 // when calculating the numeric derivative
49 if (H < std::numeric_limits<double>::epsilon() || T < std::numeric_limits<double>::epsilon()) {
50 for (size_t j = 0; j < nData; j++) {
51 out[j] = std::numeric_limits<double>::infinity();
52 }
53 return;
54 }
55
56 // Lorentzian intensities and HWHM
57 auto G = hbar * (1 - exp(-(Q * Q * L * L) / 2.0)) / T;
58 for (size_t j = 0; j < nData; j++) {
59 auto E = xValues[j] - C;
60 out[j] += H * G / (G * G + E * E) / M_PI;
61 }
62}
63
70void HallRossSQE::functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) {
71 const double deltaF{0.1}; // increase parameter by this fraction
72 const size_t nParam = this->nParams();
73 // cutoff defines the smallest change in the parameter when calculating the
74 // numerical derivative
75 std::map<std::string, double> cutoff;
76 cutoff["Tau"] = 0.2; // 0.2ps
77 cutoff["Centre"] = 0.0001; // 0.1micro-eV
78 std::vector<double> out(nData);
79 this->applyTies();
80 this->function1D(out.data(), xValues, nData);
81
82 for (size_t iP = 0; iP < nParam; iP++) {
83 if (this->isActive(iP)) {
84 std::vector<double> derivative(nData);
85 const double pVal = this->activeParameter(iP);
86 const std::string pName = this->parameterName(iP);
87 if (pName == "Height") {
88 // exact derivative
89 this->setActiveParameter(iP, 1.0);
90 this->applyTies();
91 this->function1D(derivative.data(), xValues, nData);
92 } else {
93 // numerical derivative
94 double delta = cutoff[pName] > fabs(pVal * deltaF) ? cutoff[pName] : pVal * deltaF;
95 this->setActiveParameter(iP, pVal + delta);
96 this->applyTies();
97 this->function1D(derivative.data(), xValues, nData);
98 for (size_t i = 0; i < nData; i++) {
99 derivative[i] = (derivative[i] - out[i]) / delta;
100 }
101 }
102 this->setActiveParameter(iP, pVal); // restore the value of the parameter
103 // fill the jacobian for this parameter
104 for (size_t i = 0; i < nData; i++) {
105 jacobian->set(i, iP, derivative[i]);
106 }
107 }
108 }
109}
110
111} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
#define fabs(x)
Definition Matrix.cpp:22
virtual void derivative(const FunctionDomain &domain, FunctionValues &values, const size_t order=1) const
double asDouble() const
Returns double value if attribute is a double, throws exception otherwise.
bool isActive(size_t i) const
Check if an active parameter i is actually active.
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
virtual void applyTies()
Apply the ties.
virtual double activeParameter(size_t i) const
Value of i-th active parameter.
virtual void setActiveParameter(size_t i, double value)
Set new value of i-th active parameter.
Represents the Jacobian in IFitFunction::functionDeriv.
Definition Jacobian.h:22
virtual void set(size_t iY, size_t iP, double value)=0
Set a value to a Jacobian matrix element.
std::string parameterName(size_t i) const override
Returns the name of parameter i.
size_t nParams() const override
Total number of parameters.
double getParameter(size_t i) const override
Get i-th parameter.
Hall-Ross jump diffusion model.
Definition HallRossSQE.h:24
void functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) override
analytical/numerical derivative with respect to fitting parameters We carry out analytical derivative...
void function1D(double *out, const double *xValues, const size_t nData) const override
Calculate function values on an energy domain.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
Kernel::Logger g_log("DetermineSpinStateOrder")