Mantid
Loading...
Searching...
No Matches
FickDiffusionSQE.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("FickDiffusionSQE");
16}
17
19
20DECLARE_FUNCTION(FickDiffusionSQE)
21
22
25void FickDiffusionSQE::declareParameters() {
26 this->declareParameter("Height", 1.0, "scaling factor");
27 this->declareParameter("Centre", 0.0, "Shift along the X-axis");
28 this->declareParameter("DiffCoeff", 2.3, "Diffusion coefficient (10^(-5)cm^2/s)");
29}
30
37void FickDiffusionSQE::function1D(double *out, const double *xValues, const size_t nData) const {
38 auto H = this->getParameter("Height");
39 auto C = this->getParameter("Centre");
40 auto Q = this->getAttribute("Q").asDouble();
41 auto D = this->getParameter("DiffCoeff");
42
43 // Penalize negative parameters, just in case they show up
44 // when calculating the numeric derivative
45 if (H < std::numeric_limits<double>::epsilon() || D < std::numeric_limits<double>::epsilon()) {
46 for (size_t j = 0; j < nData; j++) {
47 out[j] = std::numeric_limits<double>::infinity();
48 }
49 return;
50 }
51
52 // Lorentzian intensities and HWHM
53 D *= 0.10; // conversion from 10^{-5}cm^2/s to Angstrom^2/ps, the internal
54 // units used
55 auto G = D * Q * Q;
56 for (size_t j = 0; j < nData; j++) {
57 auto E = xValues[j] - C;
58 out[j] += H * G / (G * G + E * E) / M_PI;
59 }
60}
61
68void FickDiffusionSQE::functionDeriv1D(Mantid::API::Jacobian *jacobian, const double *xValues, const size_t nData) {
69 const double deltaF{0.1}; // increase parameter by this fraction
70 const size_t nParam = this->nParams();
71 // cutoff defines the smallest change in the parameter when calculating the
72 // numerical derivative
73 std::map<std::string, double> cutoff;
74 cutoff["DiffCoeff"] = 0.2; // 0.2x10^(-5)cm^2/s
75 cutoff["Centre"] = 0.0001; // 0.1micro-eV
76 std::vector<double> out(nData);
77 this->applyTies();
78 this->function1D(out.data(), xValues, nData);
79
80 for (size_t iP = 0; iP < nParam; iP++) {
81 if (this->isActive(iP)) {
82 std::vector<double> derivative(nData);
83 const double pVal = this->activeParameter(iP);
84 const std::string pName = this->parameterName(iP);
85 if (pName == "Height") {
86 // exact derivative
87 this->setActiveParameter(iP, 1.0);
88 this->applyTies();
89 this->function1D(derivative.data(), xValues, nData);
90 } else {
91 // numerical derivative
92 double delta = cutoff[pName] > fabs(pVal * deltaF) ? cutoff[pName] : pVal * deltaF;
93 this->setActiveParameter(iP, pVal + delta);
94 this->applyTies();
95 this->function1D(derivative.data(), xValues, nData);
96 for (size_t i = 0; i < nData; i++) {
97 derivative[i] = (derivative[i] - out[i]) / delta;
98 }
99 }
100 this->setActiveParameter(iP, pVal); // restore the value of the parameter
101 // fill the jacobian for this parameter
102 for (size_t i = 0; i < nData; i++) {
103 jacobian->set(i, iP, derivative[i]);
104 }
105 }
106 }
107}
108
109} // 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.
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")