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 <cmath>
12#include <limits>
13
14namespace {
15Mantid::Kernel::Logger g_log("HallRossSQE");
16}
17
19
20DECLARE_FUNCTION(HallRossSQE)
21
22
25void HallRossSQE::declareParameters() {
26 this->declareParameter("Height", 1.0, "scaling factor");
27 this->declareParameter("Centre", 0.0, "Shift along the X-axis");
28 this->declareParameter("Tau", 1.25, "Residence time");
29 this->declareParameter("L", 1.25, "Jump length");
30}
31
38void HallRossSQE::function1D(double *out, const double *xValues, const size_t nData) const {
39 double hbar(0.658211626); // ps*meV
40 auto H = this->getParameter("Height");
41 auto C = this->getParameter("Centre");
42 auto Q = this->getAttribute("Q").asDouble();
43 auto T = this->getParameter("Tau");
44 auto L = this->getParameter("L");
45
46 // Penalize negative parameters, just in case they show up
47 // when calculating the numeric derivative
48 if (H < std::numeric_limits<double>::epsilon() || T < std::numeric_limits<double>::epsilon()) {
49 for (size_t j = 0; j < nData; j++) {
50 out[j] = std::numeric_limits<double>::infinity();
51 }
52 return;
53 }
54
55 // Lorentzian intensities and HWHM
56 auto G = hbar * (1 - exp(-(Q * Q * L * L) / 2.0)) / T;
57 for (size_t j = 0; j < nData; j++) {
58 auto E = xValues[j] - C;
59 out[j] += H * G / (G * G + E * E) / M_PI;
60 }
61}
62
69void HallRossSQE::functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) {
70 const double deltaF{0.1}; // increase parameter by this fraction
71 const size_t nParam = this->nParams();
72 // cutoff defines the smallest change in the parameter when calculating the
73 // numerical derivative
74 std::map<std::string, double> cutoff;
75 cutoff["Tau"] = 0.2; // 0.2ps
76 cutoff["Centre"] = 0.0001; // 0.1micro-eV
77 std::vector<double> out(nData);
78 this->applyTies();
79 this->function1D(out.data(), xValues, nData);
80
81 for (size_t iP = 0; iP < nParam; iP++) {
82 if (this->isActive(iP)) {
83 std::vector<double> derivative(nData);
84 const double pVal = this->activeParameter(iP);
85 const std::string pName = this->parameterName(iP);
86 if (pName == "Height") {
87 // exact derivative
88 this->setActiveParameter(iP, 1.0);
89 this->applyTies();
90 this->function1D(derivative.data(), xValues, nData);
91 } else {
92 // numerical derivative
93 double delta = cutoff[pName] > fabs(pVal * deltaF) ? cutoff[pName] : pVal * deltaF;
94 this->setActiveParameter(iP, pVal + delta);
95 this->applyTies();
96 this->function1D(derivative.data(), xValues, nData);
97 for (size_t i = 0; i < nData; i++) {
98 derivative[i] = (derivative[i] - out[i]) / delta;
99 }
100 }
101 this->setActiveParameter(iP, pVal); // restore the value of the parameter
102 // fill the jacobian for this parameter
103 for (size_t i = 0; i < nData; i++) {
104 jacobian->set(i, iP, derivative[i]);
105 }
106 }
107 }
108}
109
110} // 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")