/
ComptonProfile.cpp
288 lines (251 loc) · 12 KB
/
ComptonProfile.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
282
283
284
285
286
287
288
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "MantidCurveFitting/ComptonProfile.h"
#include "MantidCurveFitting/ConvertToYSpace.h"
#include "MantidAPI/FunctionFactory.h"
#include <gsl/gsl_poly.h>
namespace Mantid
{
namespace CurveFitting
{
namespace
{
///@cond
const char * WSINDEX_NAME = "WorkspaceIndex";
const char * MASS_NAME = "Mass";
const double STDDEV_TO_HWHM = std::sqrt(std::log(4.0));
///@endcond
}
/**
*/
ComptonProfile::ComptonProfile() : API::ParamFunction(), API::IFunction1D(),
m_log(Kernel::Logger::get("ComptonProfile")),
m_wsIndex(0), m_mass(0.0), m_voigt(),
m_yspace(), m_modQ(), m_e0(),m_resolutionSigma(0.0), m_lorentzFWHM(0.0)
{}
//-------------------------------------- Function evaluation -----------------------------------------
/**
* Calculates the value of the function for each x value and stores in the given output array
* @param out An array of size nData to store the results
* @param xValues The input X data array of size nData. It is assumed to be times in microseconds
* @param nData The length of the out & xValues arrays
*/
void ComptonProfile::function1D(double* out, const double* xValues, const size_t nData) const
{
UNUSED_ARG(xValues); // Y-space values have already been pre-cached
this->massProfile(out, nData);
m_log.setEnabled(false);
}
/*
* Creates the internal caches
*/
void ComptonProfile::setUpForFit()
{
// Voigt
using namespace Mantid::API;
m_voigt = boost::dynamic_pointer_cast<IPeakFunction>(FunctionFactory::Instance().createFunction("Voigt"));
}
/**
* Also caches parameters from the instrument
* Throws if it is not a MatrixWorkspace
* @param ws The workspace set as input
*/
void ComptonProfile::setWorkspace(boost::shared_ptr<const API::Workspace> ws)
{
auto workspace = boost::dynamic_pointer_cast<const API::MatrixWorkspace>(ws);
if(!workspace)
{
throw std::invalid_argument("ComptonProfile expected an object of type MatrixWorkspace, type=" + ws->id());
}
auto inst = workspace->getInstrument();
auto sample = inst->getSample();
auto source = inst->getSource();
if(!sample || !source)
{
throw std::invalid_argument("ComptonProfile - Workspace has no source/sample.");
}
Geometry::IDetector_const_sptr det;
try
{
det = workspace->getDetector(m_wsIndex);
}
catch (Kernel::Exception::NotFoundError &)
{
throw std::invalid_argument("ComptonProfile - Workspace has no detector attached to histogram at index " + boost::lexical_cast<std::string>(m_wsIndex));
}
DetectorParams detpar = ConvertToYSpace::getDetectorParameters(workspace, m_wsIndex);
const auto & pmap = workspace->constInstrumentParameters();
ResolutionParams respar;
respar.dl1 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l1");
respar.dl2 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l2");
respar.dthe = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_theta"); //radians
respar.dEnLorentz = ConvertToYSpace::getComponentParameter(det, pmap, "hwhm_lorentz");
respar.dEnGauss = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_gauss");
this->cacheYSpaceValues(workspace->readX(m_wsIndex), workspace->isHistogramData(), detpar, respar);
}
/**
* @param tseconds A vector containing the time-of-flight values in seconds
* @param isHistogram True if histogram tof values have been passed in
* @param detpar Structure containing detector parameters
* @param respar Structure containing resolution parameters
*/
void ComptonProfile::cacheYSpaceValues(const std::vector<double> & tseconds, const bool isHistogram,
const DetectorParams & detpar, const ResolutionParams & respar)
{
// geometry
double theta = detpar.theta; //cache for frequent access
double hwhmLorentzE = respar.dEnLorentz;
double hwhmGaussE = STDDEV_TO_HWHM*respar.dEnGauss;
// ------ Fixed coefficients related to resolution & Y-space transforms ------------------
const double mn = PhysicalConstants::NeutronMassAMU;
const double mevToK = PhysicalConstants::E_mev_toNeutronWavenumberSq;
const double massToMeV = 0.5*PhysicalConstants::NeutronMass/PhysicalConstants::meV; // Includes factor of 1/2
const double v1 = std::sqrt(detpar.efixed/massToMeV);
const double k1 = std::sqrt(detpar.efixed/mevToK);
const double l2l1 = detpar.l2/detpar.l1;
// Resolution dependence
// Find K0/K1 at y=0 by taking the largest root of (M-1)s^2 + 2cos(theta)s - (M+1) = 0
// Quadratic if M != 1 but simple linear if it does
double k0k1(0.0);
if((m_mass-1.0) > DBL_EPSILON)
{
double x0(0.0),x1(0.0);
gsl_poly_solve_quadratic(m_mass-1.0, 2.0*std::cos(theta), -(m_mass+1.0), &x0, &x1);
k0k1 = std::max(x0,x1); // K0/K1 at y=0
}
else
{
// solution is simply s = 1/cos(theta)
k0k1 = 1.0/std::cos(theta);
}
double qy0(0.0), wgauss(0.0);
if(m_mass > 1.0)
{
qy0 = std::sqrt(k1*k1*m_mass*(k0k1*k0k1 - 1));
double k0k1p3 = std::pow(k0k1,3);
double r1 = -(1.0 + l2l1*k0k1p3);
double r2 = 1.0 - l2l1*k0k1p3 + l2l1*std::pow(k0k1,2)*std::cos(theta) - k0k1*std::cos(theta);
double factor = (0.2413/qy0)*((m_mass/mn)*r1 - r2);
m_lorentzFWHM = std::abs(factor*hwhmLorentzE*2);
wgauss = std::abs(factor*hwhmGaussE*2);
}
else
{
qy0 = k1*std::tan(theta);
double factor = (0.2413*2.0/k1)*std::abs((std::cos(theta) + l2l1)/std::sin(theta));
m_lorentzFWHM = hwhmLorentzE*factor;
wgauss = hwhmGaussE*factor;
}
double k0y0 = k1*k0k1; // k0_y0 = k0 value at y=0
double wtheta = 2.0*STDDEV_TO_HWHM*std::abs(k0y0*k1*std::sin(theta)/qy0)*respar.dthe;
double common = (m_mass/mn) - 1 + k1*std::cos(theta)/k0y0;
double wl1 = 2.0*STDDEV_TO_HWHM*std::abs((std::pow(k0y0,2)/(qy0*detpar.l1))*common)*respar.dl1;
double wl2 = 2.0*STDDEV_TO_HWHM*std::abs((std::pow(k0y0,3)/(k1*qy0*detpar.l1))*common)*respar.dl2;
m_resolutionSigma = std::sqrt(std::pow(wgauss,2) + std::pow(wtheta,2) + std::pow(wl1,2) + std::pow(wl2,2));
m_log.notice() << "--------------------- Mass=" << m_mass << " -----------------------" << std::endl;
m_log.notice() << "w_l1 (FWHM)=" << wl2 << std::endl;
m_log.notice() << "w_l0 (FWHM)=" << wl1 << std::endl;
m_log.notice() << "w_theta (FWHM)=" << wtheta << std::endl;
m_log.notice() << "w_foil_lorentz (FWHM)=" << m_lorentzFWHM << std::endl;
m_log.notice() << "w_foil_gauss (FWHM)=" << wgauss << std::endl;
// Calculate energy dependent factors and transform q to Y-space
const size_t nData = (isHistogram) ? tseconds.size() - 1 : tseconds.size();
m_e0.resize(nData);
m_modQ.resize(nData);
m_yspace.resize(nData);
for(size_t i = 0; i < nData; ++i)
{
const double tsec = (isHistogram) ? 0.5*(tseconds[i] + tseconds[i+1]) : tseconds[i];
ConvertToYSpace::calculateY(m_yspace[i], m_modQ[i],m_e0[i],
m_mass,tsec*1e06,k1,v1,detpar);
}
}
/**
*/
void ComptonProfile::declareAttributes()
{
declareAttribute(WSINDEX_NAME, IFunction::Attribute(static_cast<int>(m_wsIndex)));
declareAttribute(MASS_NAME, IFunction::Attribute(m_mass));
}
/**
* @param name The name of the attribute
* @param value The attribute's value
*/
void ComptonProfile::setAttribute(const std::string& name,const Attribute& value)
{
IFunction::setAttribute(name,value); // Make sure the base-class stores it
if(name == WSINDEX_NAME) m_wsIndex = static_cast<size_t>(value.asInt());
else if(name == MASS_NAME) m_mass = value.asDouble();
}
/**
* Transforms the input y coordinates using a difference if Voigt functions across the whole range
* @param voigtDiff [Out] Output values (vector is expected to be of the correct size)
* @param yspace Input y coordinates
* @param lorentzPos LorentzPos parameter
* @param lorentzAmp LorentzAmp parameter
* @param lorentzWidth LorentzFWHM parameter
* @param gaussWidth GaussianFWHM parameter
*/
void ComptonProfile::voigtApproxDiff(std::vector<double> & voigtDiff, const std::vector<double> & yspace, const double lorentzPos, const double lorentzAmp,
const double lorentzWidth, const double gaussWidth) const
{
double miny(DBL_MAX), maxy(-DBL_MAX);
auto iend = yspace.end();
for(auto itr = yspace.begin(); itr != iend; ++itr)
{
const double absy = std::abs(*itr);
if(absy < miny) miny = absy;
else if(absy > maxy) maxy = absy;
}
const double epsilon = (maxy - miny)/1000.0;
// Compute: V = (voigt(y+2eps,...) - voigt(y-2eps,...) - 2*voigt(y+eps,...) + 2*(voigt(y-eps,...))/(2eps^3)
std::vector<double> ypmEps(yspace.size());
// y+2eps
std::transform(yspace.begin(), yspace.end(), ypmEps.begin(), std::bind2nd(std::plus<double>(), 2.0*epsilon)); // Add 2 epsilon
voigtApprox(voigtDiff, ypmEps, lorentzPos, lorentzAmp, lorentzWidth, gaussWidth);
// y-2eps
std::transform(yspace.begin(), yspace.end(), ypmEps.begin(), std::bind2nd(std::minus<double>(), 2.0*epsilon)); // Subtract 2 epsilon
std::vector<double> tmpResult(yspace.size());
voigtApprox(tmpResult, ypmEps, lorentzPos, lorentzAmp, lorentzWidth, gaussWidth);
// Difference of first two terms - result is put back in voigtDiff
std::transform(voigtDiff.begin(), voigtDiff.end(), tmpResult.begin(), voigtDiff.begin(), std::minus<double>());
// y+eps
std::transform(yspace.begin(), yspace.end(), ypmEps.begin(), std::bind2nd(std::plus<double>(), epsilon)); // Add epsilon
voigtApprox(tmpResult, ypmEps, lorentzPos, lorentzAmp, lorentzWidth, gaussWidth);
std::transform(tmpResult.begin(), tmpResult.end(), tmpResult.begin(), std::bind2nd(std::multiplies<double>(), 2.0)); // times 2
// Difference with 3rd term - result is put back in voigtDiff
std::transform(voigtDiff.begin(), voigtDiff.end(), tmpResult.begin(), voigtDiff.begin(), std::minus<double>());
//y-eps
std::transform(yspace.begin(), yspace.end(), ypmEps.begin(), std::bind2nd(std::minus<double>(), epsilon)); // Subtract epsilon
voigtApprox(tmpResult, ypmEps, lorentzPos, lorentzAmp, lorentzWidth, gaussWidth);
std::transform(tmpResult.begin(), tmpResult.end(), tmpResult.begin(), std::bind2nd(std::multiplies<double>(), 2.0)); // times 2
// Sum final term
std::transform(voigtDiff.begin(), voigtDiff.end(), tmpResult.begin(), voigtDiff.begin(), std::plus<double>());
// Finally multiply by 2*eps^3
std::transform(voigtDiff.begin(), voigtDiff.end(), voigtDiff.begin(), std::bind2nd(std::divides<double>(), 2.0*std::pow(epsilon,3))); // divided by (2eps^3)
}
/**
* Transforms the input y coordinates using the Voigt function approximation. The area is normalized to lorentzAmp
* @param voigt [Out] Output values (vector is expected to be of the correct size
* @param yspace Input y coordinates
* @param lorentzPos LorentzPos parameter
* @param lorentzAmp LorentzAmp parameter
* @param lorentzWidth LorentzFWHM parameter
* @param gaussWidth GaussianFWHM parameter
*/
void ComptonProfile::voigtApprox(std::vector<double> & voigt, const std::vector<double> & yspace, const double lorentzPos,
const double lorentzAmp, const double lorentzWidth, const double gaussWidth) const
{
m_voigt->setParameter(0,lorentzAmp);
m_voigt->setParameter(1,lorentzPos);
m_voigt->setParameter(2,lorentzWidth);
m_voigt->setParameter(3,gaussWidth);
assert(voigt.size() == yspace.size());
m_voigt->functionLocal(voigt.data(), yspace.data(), yspace.size());
// Normalize so that integral of V=lorentzAmp
const double norm = 1.0/(0.5*M_PI*lorentzWidth);
std::transform(voigt.begin(), voigt.end(), voigt.begin(), std::bind2nd(std::multiplies<double>(), norm));
}
} // namespace CurveFitting
} // namespace Mantid