Mantid
Loading...
Searching...
No Matches
TeixeiraWaterSQE.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("TeixeiraWaterSQE");
17}
18
20
21DECLARE_FUNCTION(TeixeiraWaterSQE)
22
23
26void TeixeiraWaterSQE::declareParameters() {
27 this->declareParameter("Height", 1.0, "scaling factor");
28 this->declareParameter("DiffCoeff", 2.3, "Diffusion coefficient (10^(-5)cm^2/s)");
29 this->declareParameter("Tau", 1.25, "Residence time (ps)");
30 this->declareParameter("Centre", 0.0, "Shift along the X-axis");
31}
32
39void TeixeiraWaterSQE::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 D = this->getParameter("DiffCoeff");
43 auto T = this->getParameter("Tau");
44 auto C = this->getParameter("Centre");
45 auto Q = this->getAttribute("Q").asDouble();
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() || D < std::numeric_limits<double>::epsilon() ||
50 T < std::numeric_limits<double>::epsilon()) {
51 for (size_t j = 0; j < nData; j++) {
52 out[j] = std::numeric_limits<double>::infinity();
53 }
54 return;
55 }
56
57 // Lorentzian intensities and HWHM
58 D *= 0.10; // conversion from 10^{-5}cm^2/s to Angstrom^2/ps, the internal
59 // units used
60 auto G = hbar * D * Q * Q / (1 + D * Q * Q * T);
61 for (size_t j = 0; j < nData; j++) {
62 auto E = xValues[j] - C;
63 out[j] += H * G / (G * G + E * E) / M_PI;
64 }
65}
66
73void TeixeiraWaterSQE::functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) {
74 const double deltaF{0.1}; // increase parameter by this fraction
75 const size_t nParam = this->nParams();
76 // cutoff defines the smallest change in the parameter when calculating the
77 // numerical derivative
78 std::map<std::string, double> cutoff;
79 cutoff["DiffCoeff"] = 0.2; // 0.2x10^(-5)cm^2/s
80 cutoff["Tau"] = 0.2; // 0.2ps
81 cutoff["Centre"] = 0.0001; // 0.1micro-eV
82 std::vector<double> out(nData);
83 this->applyTies();
84 this->function1D(out.data(), xValues, nData);
85
86 for (size_t iP = 0; iP < nParam; iP++) {
87 if (this->isActive(iP)) {
88 std::vector<double> derivative(nData);
89 const double pVal = this->activeParameter(iP);
90 const std::string pName = this->parameterName(iP);
91 if (pName == "Height") {
92 // exact derivative
93 this->setActiveParameter(iP, 1.0);
94 this->applyTies();
95 this->function1D(derivative.data(), xValues, nData);
96 } else {
97 // numerical derivative
98 double delta = cutoff[pName] > fabs(pVal * deltaF) ? cutoff[pName] : pVal * deltaF;
99 this->setActiveParameter(iP, pVal + delta);
100 this->applyTies();
101 this->function1D(derivative.data(), xValues, nData);
102 for (size_t i = 0; i < nData; i++) {
103 derivative[i] = (derivative[i] - out[i]) / delta;
104 }
105 }
106 this->setActiveParameter(iP, pVal); // restore the value of the parameter
107 // fill the jacobian for this parameter
108 for (size_t i = 0; i < nData; i++) {
109 jacobian->set(i, iP, derivative[i]);
110 }
111 }
112 }
113}
114
115} // 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.
Teixeira's model to describe the translational diffusion of water.
void function1D(double *out, const double *xValues, const size_t nData) const override
Calculate function values on an energy domain.
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...
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")