-
Notifications
You must be signed in to change notification settings - Fork 122
/
DiffRotDiscreteCircle.cpp
253 lines (216 loc) · 8.35 KB
/
DiffRotDiscreteCircle.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/DiffRotDiscreteCircle.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/ParameterTie.h"
#include "MantidCurveFitting/BoundaryConstraint.h"
#include "MantidGeometry/IDetector.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/UnitConversion.h"
#include <cmath>
#include <limits>
namespace {
Mantid::Kernel::Logger g_log("DiffSphere");
}
namespace Mantid {
namespace CurveFitting {
using namespace API;
using namespace Geometry;
DECLARE_FUNCTION(ElasticDiffRotDiscreteCircle)
DECLARE_FUNCTION(InelasticDiffRotDiscreteCircle)
DECLARE_FUNCTION(DiffRotDiscreteCircle)
ElasticDiffRotDiscreteCircle::ElasticDiffRotDiscreteCircle() {
// declareParameter("Height", 1.0); //parameter "Height" already declared in
// constructor of base class DeltaFunction
declareParameter("Radius", 1.0, "Circle radius [Angstroms] ");
declareAttribute("Q", API::IFunction::Attribute(0.5));
declareAttribute("N", API::IFunction::Attribute(3));
}
void ElasticDiffRotDiscreteCircle::init() {
// Ensure positive values for Height and Radius
BoundaryConstraint *HeightConstraint = new BoundaryConstraint(
this, "Height", std::numeric_limits<double>::epsilon(), true);
addConstraint(HeightConstraint);
BoundaryConstraint *RadiusConstraint = new BoundaryConstraint(
this, "Radius", std::numeric_limits<double>::epsilon(), true);
addConstraint(RadiusConstraint);
}
double ElasticDiffRotDiscreteCircle::HeightPrefactor() const {
const double R = getParameter("Radius");
const double Q = getAttribute("Q").asDouble();
const int N = getAttribute("N").asInt();
double aN = 0;
for (int k = 1; k < N; k++) {
double x = 2 * Q * R * sin(M_PI * k / N);
aN += sin(x) / x; // spherical Besell function of order zero j0==sin(x)/x
}
aN += 1; // limit for j0 when k==N, or x==0
return aN / N;
}
InelasticDiffRotDiscreteCircle::InelasticDiffRotDiscreteCircle()
: m_hbar(0.658211626) {
declareParameter("Intensity", 1.0, "scaling factor [arbitrary units]");
declareParameter("Radius", 1.0, "Circle radius [Angstroms]");
declareParameter("Decay", 1.0, "Inverse of transition rate, in nanoseconds "
"if energy in micro-ev, or picoseconds if "
"energy in mili-eV");
declareParameter("Shift", 0.0, "Shift in domain");
declareAttribute("Q", API::IFunction::Attribute(EMPTY_DBL()));
declareAttribute("WorkspaceIndex", API::IFunction::Attribute(0));
declareAttribute("N", API::IFunction::Attribute(3));
}
void InelasticDiffRotDiscreteCircle::init() {
// Ensure positive values for Intensity, Radius, and decay
BoundaryConstraint *IntensityConstraint = new BoundaryConstraint(
this, "Intensity", std::numeric_limits<double>::epsilon(), true);
addConstraint(IntensityConstraint);
BoundaryConstraint *RadiusConstraint = new BoundaryConstraint(
this, "Radius", std::numeric_limits<double>::epsilon(), true);
addConstraint(RadiusConstraint);
BoundaryConstraint *DecayConstraint = new BoundaryConstraint(
this, "Decay", std::numeric_limits<double>::epsilon(), true);
addConstraint(DecayConstraint);
}
void InelasticDiffRotDiscreteCircle::function1D(double *out,
const double *xValues,
const size_t nData) const {
const double I = getParameter("Intensity");
const double R = getParameter("Radius");
const double rate = m_hbar / getParameter("Decay"); // micro-eV or mili-eV
const int N = getAttribute("N").asInt();
const double S = getParameter("Shift");
double Q;
if (getAttribute("Q").asDouble() == EMPTY_DBL()) {
if (m_qValueCache.size() == 0) {
throw std::runtime_error(
"No Q attribute provided and cannot retrieve from worksapce.");
}
const int specIdx = getAttribute("WorkspaceIndex").asInt();
Q = m_qValueCache[specIdx];
g_log.debug() << "Get Q value for workspace index " << specIdx << ": " << Q
<< std::endl;
} else {
Q = getAttribute("Q").asDouble();
g_log.debug() << "Using Q attribute: " << Q << std::endl;
}
std::vector<double> sph(N);
for (int k = 1; k < N; k++) {
double x = 2 * Q * R * sin(M_PI * k / N);
sph[k] =
sin(x) / x; // spherical Besell function of order zero 'j0' is sin(x)/x
}
std::vector<double> ratel(N);
for (int l = 1; l < N; l++) // l goes up to N-1
{
ratel[l] = rate * 4 * pow(sin(M_PI * l / N), 2); // notice that 0 < l/N < 1
}
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;
}
}
/**
* Handle seting fit workspace.
*
* 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 MatrixWorkspace>(ws);
if (!workspace)
return;
size_t numHist = workspace->getNumberHistograms();
for (size_t idx = 0; idx < numHist; idx++) {
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 = workspace->detectorTwoTheta(det) / 2.0;
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;
}
}
}
/* Propagate the attribute to its member functions.
* NOTE: we pass this->getAttribute(name) by reference, thus the same
* object is shared by the composite function and its members.
*/
void DiffRotDiscreteCircle::trickleDownAttribute(const std::string &name) {
for (size_t iFun = 0; iFun < nFunctions(); iFun++) {
API::IFunction_sptr fun = getFunction(iFun);
if (fun->hasAttribute(name))
fun->setAttribute(name, this->getAttribute(name));
}
}
/* Same as parent except we overwrite attributes of member functions
* having the same name
*/
void DiffRotDiscreteCircle::declareAttribute(
const std::string &name, const API::IFunction::Attribute &defaultValue) {
API::ImmutableCompositeFunction::declareAttribute(name, defaultValue);
trickleDownAttribute(name);
}
/* Same as parent except we overwrite attributes of member functions
* having the same name
*/
void DiffRotDiscreteCircle::setAttribute(const std::string &name,
const Attribute &att) {
API::ImmutableCompositeFunction::setAttribute(name, att);
trickleDownAttribute(name);
}
// DiffRotDiscreteCircle::DiffRotDiscreteCircle()
void DiffRotDiscreteCircle::init() {
m_elastic = boost::dynamic_pointer_cast<ElasticDiffRotDiscreteCircle>(
API::FunctionFactory::Instance().createFunction(
"ElasticDiffRotDiscreteCircle"));
addFunction(m_elastic);
m_inelastic = boost::dynamic_pointer_cast<InelasticDiffRotDiscreteCircle>(
API::FunctionFactory::Instance().createFunction(
"InelasticDiffRotDiscreteCircle"));
addFunction(m_inelastic);
setAttributeValue("NumDeriv", true);
declareAttribute("Q", API::IFunction::Attribute(0.5));
declareAttribute("N", API::IFunction::Attribute(3));
// Set the aliases
setAlias("f1.Intensity", "Intensity");
setAlias("f1.Radius", "Radius");
setAlias("f1.Decay", "Decay");
setAlias("f1.Shift", "Shift");
// Set the ties between Elastic and Inelastic parameters
addDefaultTies("f0.Height=f1.Intensity,f0.Radius=f1.Radius");
applyTies();
}
} // namespace CurveFitting
} // namespace Mantid