-
Notifications
You must be signed in to change notification settings - Fork 122
/
InelasticDiffRotDiscreteCircle.cpp
175 lines (151 loc) · 5.79 KB
/
InelasticDiffRotDiscreteCircle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Mantid Coding standards <http://www.mantidproject.org/Coding_Standards>
// Main Module Header
#include "MantidCurveFitting/Functions/InelasticDiffRotDiscreteCircle.h"
// Mantid Headers from the same project
#include "MantidCurveFitting/Constraints/BoundaryConstraint.h"
// Mantid headers from other projects
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/IDetector.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/make_unique.h"
#include "MantidKernel/UnitConversion.h"
// 3rd party library headers (N/A)
// standard library headers
#include <cmath>
#include <limits>
#include <sstream>
using BConstraint = Mantid::CurveFitting::Constraints::BoundaryConstraint;
namespace {
Mantid::Kernel::Logger g_log("InelasticDiffDiffRotDiscreteCircle");
}
namespace Mantid {
namespace CurveFitting {
namespace Functions {
DECLARE_FUNCTION(InelasticDiffRotDiscreteCircle)
/**
* @brief Constructor. Declare fitting parameters and attributes
*/
InelasticDiffRotDiscreteCircle::InelasticDiffRotDiscreteCircle()
: m_hbar(0.658211626) {
this->declareParameter("Intensity", 1.0, "scaling factor [no units]");
this->declareParameter("Radius", 1.0, "Circle radius [Angstroms]");
this->declareParameter("Decay", 1.0,
"Inverse of transition rate, in nanoseconds "
"if energy in micro-ev, or picoseconds if "
"energy in mili-eV");
this->declareParameter("Shift", 0.0, "Shift in the centre of the peak");
this->declareAttribute("Q", API::IFunction::Attribute(EMPTY_DBL()));
this->declareAttribute("WorkspaceIndex", API::IFunction::Attribute(0));
this->declareAttribute("N", API::IFunction::Attribute(3));
}
/**
* @brief Set constraints on fitting parameters
*/
void InelasticDiffRotDiscreteCircle::init() {
// Ensure positive values for Intensity, Radius, and decay
auto IntensityConstraint = Kernel::make_unique<BConstraint>(
this, "Intensity", std::numeric_limits<double>::epsilon(), true);
this->addConstraint(std::move(IntensityConstraint));
auto RadiusConstraint = Kernel::make_unique<BConstraint>(
this, "Radius", std::numeric_limits<double>::epsilon(), true);
this->addConstraint(std::move(RadiusConstraint));
auto DecayConstraint = Kernel::make_unique<BConstraint>(
this, "Decay", std::numeric_limits<double>::epsilon(), true);
this->addConstraint(std::move(DecayConstraint));
}
/**
* @brief Calculate function values on an energy domain
* @param out array to store function values
* @param xValues energy domain where function is evaluated
* @param nData size of the energy domain
* @exception No Q values can be found in associated attributes
*/
void InelasticDiffRotDiscreteCircle::function1D(double *out,
const double *xValues,
const size_t nData) const {
auto I = this->getParameter("Intensity");
auto R = this->getParameter("Radius");
auto rate = m_hbar / this->getParameter("Decay"); // micro-eV or mili-eV
auto N = this->getAttribute("N").asInt();
auto S = this->getParameter("Shift");
// Retrieve Q-value from the appropriate attribute
double Q;
if (this->getAttribute("Q").asDouble() == EMPTY_DBL()) {
if (m_qValueCache.empty()) {
throw std::runtime_error(
"No Q attribute provided and cannot retrieve from workspace.");
}
const int specIdx = this->getAttribute("WorkspaceIndex").asInt();
Q = m_qValueCache[specIdx];
g_log.debug() << "Get Q value for workspace index " << specIdx << ": " << Q
<< '\n';
} else {
Q = getAttribute("Q").asDouble();
g_log.debug() << "Using Q attribute: " << Q << '\n';
}
std::vector<double> sph(N);
for (int k = 1; k < N; k++) {
double x = 2 * Q * R * sin(M_PI * k / N);
// spherical Besell function of order zero 'j0' is sin(x)/x
sph[k] = sin(x) / x;
}
std::vector<double> ratel(N);
for (int l = 1; l < N; l++) { // l goes up to N-1
// notice that 0 < l/N < 1
ratel[l] = rate * 4 * pow(sin(M_PI * l / N), 2);
}
for (size_t i = 0; i < nData; i++) {
double w = xValues[i] - S;
double S = 0.0;
for (int l = 1; l < N; l++) { // l goes up to N-1
double lorentzian = ratel[l] / (ratel[l] * ratel[l] + w * w);
double al = 0.0;
for (int k = 1; k < N; k++) { // case k==N after the loop
double y = 2 * M_PI * l * k / N;
al += cos(y) * sph[k];
}
al += 1; // limit for j0 when k==N, or x==0
al /= N;
S += al * lorentzian;
}
out[i] = I * S / M_PI;
}
}
/**
* @brief Creates a list of Q values from each spectrum
* to be used with WorkspaceIndex attribute.
* @param ws Pointer to workspace
*/
void InelasticDiffRotDiscreteCircle::setWorkspace(
boost::shared_ptr<const API::Workspace> ws) {
m_qValueCache.clear();
auto workspace = boost::dynamic_pointer_cast<const API::MatrixWorkspace>(ws);
if (!workspace)
return;
size_t numHist = workspace->getNumberHistograms();
for (size_t idx = 0; idx < numHist; idx++) {
Mantid::Geometry::IDetector_const_sptr det;
try {
det = workspace->getDetector(idx);
} catch (Kernel::Exception::NotFoundError &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
break;
}
try {
double efixed = workspace->getEFixed(det);
double usignTheta = 0.5 * workspace->detectorTwoTheta(*det);
double q = Mantid::Kernel::UnitConversion::run(usignTheta, efixed);
m_qValueCache.push_back(q);
} catch (std::runtime_error &) {
m_qValueCache.clear();
g_log.information("Cannot populate Q values from workspace");
return;
}
}
}
} // namespace Functions
} // namespace CurveFitting
} // namespace Mantid