-
Notifications
You must be signed in to change notification settings - Fork 122
/
Bk2BkExpConvPV.cpp
281 lines (235 loc) · 8.27 KB
/
Bk2BkExpConvPV.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include <cmath>
#include "MantidAPI/Axis.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidCurveFitting/Functions/Bk2BkExpConvPV.h"
#include "MantidKernel/System.h"
#include "MantidKernel/UnitFactory.h"
#include <gsl/gsl_sf_erf.h>
using namespace Mantid::Kernel;
using namespace Mantid::API;
namespace Mantid {
namespace CurveFitting {
namespace Functions {
using namespace CurveFitting;
namespace {
/// static logger
Kernel::Logger g_log("Bk2BkExpConvPV");
} // namespace
DECLARE_FUNCTION(Bk2BkExpConvPV)
// ----------------------------
/** Constructor and Desctructor
*/
Bk2BkExpConvPV::Bk2BkExpConvPV() : mFWHM(0.0) {}
/** Initialize: declare paraemters
*/
void Bk2BkExpConvPV::init() {
declareParameter("X0", -0.0, "Location of the peak");
declareParameter("Intensity", 0.0, "Integrated intensity");
declareParameter("Alpha", 1.0, "Exponential rise");
declareParameter("Beta", 1.0, "Exponential decay");
declareParameter("Sigma2", 1.0, "Sigma squared");
declareParameter("Gamma", 0.0);
}
/** Set peak height
*/
void Bk2BkExpConvPV::setHeight(const double h) {
setParameter("Intensity", 1);
double h0 = height();
// avoid divide by zero
double minCutOff = 100.0 * std::numeric_limits<double>::min();
if (h0 >= 0 && h0 < minCutOff)
h0 = minCutOff;
else if (h0 < 0 && h0 > -minCutOff)
h0 = -minCutOff;
setParameter("Intensity", h / h0);
}
/** Get peak height
*/
double Bk2BkExpConvPV::height() const {
double height[1];
double peakCentre[1];
peakCentre[0] = this->getParameter("X0");
this->functionLocal(height, peakCentre, 1);
return height[0];
}
/** Get peak's FWHM
*/
double Bk2BkExpConvPV::fwhm() const {
double sigma2 = this->getParameter("Sigma2");
double gamma = this->getParameter("Gamma");
double H, eta;
calHandEta(sigma2, gamma, H, eta);
return mFWHM;
}
/** Set FWHM
* This cannot be set directly so we assume Gamma is 0 and set Sigma by doing
* the reverse of calHandEta
*/
void Bk2BkExpConvPV::setFwhm(const double w) {
this->setParameter("Gamma", 0);
auto sigma2 = std::pow(w, 2) / (8.0 * M_LN2);
this->setParameter("Sigma2", sigma2);
}
/** Set peak center
*/
void Bk2BkExpConvPV::setCentre(const double c) { setParameter("X0", c); }
/** Center
*/
double Bk2BkExpConvPV::centre() const { return getParameter("X0"); }
void Bk2BkExpConvPV::setMatrixWorkspace(
std::shared_ptr<const API::MatrixWorkspace> workspace, size_t wi,
double startX, double endX) {
if (workspace) {
// convert alpha and beta to correct units so inital guess is resonable
auto tof = Mantid::Kernel::UnitFactory::Instance().create("TOF");
const auto centre = getParameter("X0");
const auto scaleFactor = centre / convertValue(centre, tof, workspace, wi);
if (scaleFactor != 0) {
if (isActive(parameterIndex("Alpha")))
setParameter("Alpha", getParameter("Alpha") / scaleFactor);
if (isActive(parameterIndex("Beta")))
setParameter("Beta", getParameter("Beta") / scaleFactor);
}
}
IFunctionMW::setMatrixWorkspace(workspace, wi, startX, endX);
}
/** Implement the peak calculating formula
*/
void Bk2BkExpConvPV::functionLocal(double *out, const double *xValues,
const size_t nData) const {
// 1. Prepare constants
const double alpha = this->getParameter("Alpha");
const double beta = this->getParameter("Beta");
const double sigma2 = this->getParameter("Sigma2");
const double gamma = this->getParameter("Gamma");
const double intensity = this->getParameter("Intensity");
const double x0 = this->getParameter("X0");
double invert_sqrt2sigma = 1.0 / sqrt(2.0 * sigma2);
double N = alpha * beta * 0.5 / (alpha + beta);
double H, eta;
calHandEta(sigma2, gamma, H, eta);
g_log.debug() << "DB1143: nData = " << nData << " From " << xValues[0]
<< " To " << xValues[nData - 1] << " X0 = " << x0
<< " Intensity = " << intensity << " alpha = " << alpha
<< " beta = " << beta << " H = " << H << " eta = " << eta
<< '\n';
// 2. Do calculation for each data point
for (size_t id = 0; id < nData; ++id) {
double dT = xValues[id] - x0;
double omega =
calOmega(dT, eta, N, alpha, beta, H, sigma2, invert_sqrt2sigma);
out[id] = intensity * omega;
}
}
/** Local derivative
*/
void Bk2BkExpConvPV::functionDerivLocal(API::Jacobian * /*jacobian*/,
const double * /*xValues*/,
const size_t /*nData*/) {
throw Mantid::Kernel::Exception::NotImplementedError(
"functionDerivLocal is not implemented for Bk2BkExpConvPV.");
}
/** Numerical derivative
*/
void Bk2BkExpConvPV::functionDeriv(const API::FunctionDomain &domain,
API::Jacobian &jacobian) {
calNumericalDeriv(domain, jacobian);
}
/** Calculate Omega(x) = ... ...
*/
double Bk2BkExpConvPV::calOmega(double x, double eta, double N, double alpha,
double beta, double H, double sigma2,
double invert_sqrt2sigma) const {
// 1. Prepare
std::complex<double> p(alpha * x, alpha * H * 0.5);
std::complex<double> q(-beta * x, beta * H * 0.5);
double u = 0.5 * alpha * (alpha * sigma2 + 2 * x);
double y = (alpha * sigma2 + x) * invert_sqrt2sigma;
double v = 0.5 * beta * (beta * sigma2 - 2 * x);
double z = (beta * sigma2 - x) * invert_sqrt2sigma;
// 2. Calculate
double omega1 = (1 - eta) * N *
(exp(u + gsl_sf_log_erfc(y)) + exp(v + gsl_sf_log_erfc(z)));
double omega2;
if (eta < 1.0E-8) {
omega2 = 0.0;
} else {
omega2 = 2 * N * eta / M_PI * (imag(exp(p) * E1(p)) + imag(exp(q) * E1(q)));
}
double omega = omega1 - omega2;
return omega;
}
/** Implementation of complex integral E_1
*/
std::complex<double> Bk2BkExpConvPV::E1(std::complex<double> z) const {
std::complex<double> e1;
double rz = real(z);
double az = abs(z);
if (fabs(az) < 1.0E-8) {
// If z = 0, then the result is infinity... diverge!
std::complex<double> r(1.0E300, 0.0);
e1 = r;
} else if (az <= 10.0 || (rz < 0.0 && az < 20.0)) {
// Some interesting region, equal to integrate to infinity, converged
std::complex<double> r(1.0, 0.0);
e1 = r;
std::complex<double> cr = r;
for (size_t k = 0; k < 150; ++k) {
auto dk = double(k);
cr = -cr * dk * z / ((dk + 2.0) * (dk + 2.0));
e1 += cr;
if (abs(cr) < abs(e1) * 1.0E-15) {
// cr is converged to zero
break;
}
} // ENDFOR k
e1 = -e1 - log(z) + (z * e1);
} else {
std::complex<double> ct0(0.0, 0.0);
for (int k = 120; k > 0; --k) {
std::complex<double> dk(double(k), 0.0);
ct0 = dk / (10.0 + dk / (z + ct0));
} // ENDFOR k
e1 = 1.0 / (z + ct0);
e1 = e1 * exp(-z);
if (rz < 0.0 && fabs(imag(z)) < 1.0E-10) {
std::complex<double> u(0.0, 1.0);
e1 = e1 - (M_PI * u);
}
}
return e1;
}
void Bk2BkExpConvPV::geneatePeak(double *out, const double *xValues,
const size_t nData) {
this->functionLocal(out, xValues, nData);
}
void Bk2BkExpConvPV::calHandEta(double sigma2, double gamma, double &H,
double &eta) const {
// 1. Calculate H
double H_G = sqrt(8.0 * sigma2 * M_LN2);
double H_L = gamma;
double temp1 = std::pow(H_L, 5) + 0.07842 * H_G * std::pow(H_L, 4) +
4.47163 * std::pow(H_G, 2) * std::pow(H_L, 3) +
2.42843 * std::pow(H_G, 3) * std::pow(H_L, 2) +
2.69269 * std::pow(H_G, 4) * H_L + std::pow(H_G, 5);
H = std::pow(temp1, 0.2);
mFWHM = H;
// 2. Calculate eta
double gam_pv = H_L / H;
eta = 1.36603 * gam_pv - 0.47719 * std::pow(gam_pv, 2) +
0.11116 * std::pow(gam_pv, 3);
if (eta > 1 || eta < 0) {
g_log.error() << "Bk2BkExpConvPV: Calculated eta = " << eta
<< " is out of range [0, 1].\n";
}
}
} // namespace Functions
} // namespace CurveFitting
} // namespace Mantid