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 <cmath>
12#include <limits>
13
14namespace {
15Mantid::Kernel::Logger g_log("TeixeiraWaterSQE");
16}
17
19
20DECLARE_FUNCTION(TeixeiraWaterSQE)
21
22
25void TeixeiraWaterSQE::declareParameters() {
26 this->declareParameter("Height", 1.0, "scaling factor");
27 this->declareParameter("DiffCoeff", 2.3, "Diffusion coefficient (10^(-5)cm^2/s)");
28 this->declareParameter("Tau", 1.25, "Residence time (ps)");
29 this->declareParameter("Centre", 0.0, "Shift along the X-axis");
30}
31
38void TeixeiraWaterSQE::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 D = this->getParameter("DiffCoeff");
42 auto T = this->getParameter("Tau");
43 auto C = this->getParameter("Centre");
44 auto Q = this->getAttribute("Q").asDouble();
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() || D < std::numeric_limits<double>::epsilon() ||
49 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 D *= 0.10; // conversion from 10^{-5}cm^2/s to Angstrom^2/ps, the internal
58 // units used
59 auto G = hbar * D * Q * Q / (1 + D * Q * Q * T);
60 for (size_t j = 0; j < nData; j++) {
61 auto E = xValues[j] - C;
62 out[j] += H * G / (G * G + E * E) / M_PI;
63 }
64}
65
72void TeixeiraWaterSQE::functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) {
73 const double deltaF{0.1}; // increase parameter by this fraction
74 const size_t nParam = this->nParams();
75 // cutoff defines the smallest change in the parameter when calculating the
76 // numerical derivative
77 std::map<std::string, double> cutoff;
78 cutoff["DiffCoeff"] = 0.2; // 0.2x10^(-5)cm^2/s
79 cutoff["Tau"] = 0.2; // 0.2ps
80 cutoff["Centre"] = 0.0001; // 0.1micro-eV
81 std::vector<double> out(nData);
82 this->applyTies();
83 this->function1D(out.data(), xValues, nData);
84
85 for (size_t iP = 0; iP < nParam; iP++) {
86 if (this->isActive(iP)) {
87 std::vector<double> derivative(nData);
88 const double pVal = this->activeParameter(iP);
89 const std::string pName = this->parameterName(iP);
90 if (pName == "Height") {
91 // exact derivative
92 this->setActiveParameter(iP, 1.0);
93 this->applyTies();
94 this->function1D(derivative.data(), xValues, nData);
95 } else {
96 // numerical derivative
97 double delta = cutoff[pName] > fabs(pVal * deltaF) ? cutoff[pName] : pVal * deltaF;
98 this->setActiveParameter(iP, pVal + delta);
99 this->applyTies();
100 this->function1D(derivative.data(), xValues, nData);
101 for (size_t i = 0; i < nData; i++) {
102 derivative[i] = (derivative[i] - out[i]) / delta;
103 }
104 }
105 this->setActiveParameter(iP, pVal); // restore the value of the parameter
106 // fill the jacobian for this parameter
107 for (size_t i = 0; i < nData; i++) {
108 jacobian->set(i, iP, derivative[i]);
109 }
110 }
111 }
112}
113
114} // 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")