forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeanElasticPeak.cpp
260 lines (223 loc) · 10.9 KB
/
LeanElasticPeak.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
// 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 "MantidDataObjects/LeanElasticPeak.h"
#include "MantidDataObjects/NoShape.h"
#include "MantidGeometry/Objects/InstrumentRayTracer.h"
#include "MantidGeometry/Surfaces/LineIntersectVisit.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Strings.h"
#include "boost/make_shared.hpp"
#include <algorithm>
#include <cctype>
#include <string>
#include <utility>
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
namespace Mantid {
namespace DataObjects {
//----------------------------------------------------------------------------------------------
/** Default constructor */
LeanElasticPeak::LeanElasticPeak() : BasePeak(), m_Qsample(V3D(0, 0, 0)), m_wavelength(0.) {}
//----------------------------------------------------------------------------------------------
/** Constructor that uses the Q position of the peak (in the sample frame)
* and a goniometer rotation matrix.
* No detector ID is set.
*
* @param QSampleFrame :: Q of the center of the peak, in reciprocal space, in
*the sample frame (goniometer rotation accounted for).
*/
LeanElasticPeak::LeanElasticPeak(const Mantid::Kernel::V3D &QSampleFrame)
: BasePeak(), m_Qsample(QSampleFrame), m_wavelength(0.) {}
//----------------------------------------------------------------------------------------------
/** Constructor that uses the Q position of the peak (in the lab frame).
* No detector ID is set.
*
* @param QSampleFrame :: Q of the center of the peak, in reciprocal space
* @param goniometer :: a 3x3 rotation matrix
* @param refFrame :: optional reference frame, will default to beam along +Z
*/
LeanElasticPeak::LeanElasticPeak(const Mantid::Kernel::V3D &QSampleFrame,
const Mantid::Kernel::Matrix<double> &goniometer,
boost::optional<std::shared_ptr<const Geometry::ReferenceFrame>> refFrame)
: BasePeak() {
if (refFrame.is_initialized())
setReferenceFrame(refFrame.get());
setQSampleFrame(QSampleFrame, goniometer);
}
//----------------------------------------------------------------------------------------------
/** Constructor that uses the Q position of the peak (in the sample frame)
* and a goniometer rotation matrix.
* No detector ID is set.
*
* @param QSampleFrame :: Q of the center of the peak, in reciprocal space, in
*the sample frame (goniometer rotation accounted for).
* @param wavelength :: wavelength in Angstroms.
*/
LeanElasticPeak::LeanElasticPeak(const Mantid::Kernel::V3D &QSampleFrame, double wavelength)
: BasePeak(), m_Qsample(QSampleFrame), m_wavelength(wavelength) {}
/**
* @brief Copy constructor
* @param other : Source
*/
LeanElasticPeak::LeanElasticPeak(const LeanElasticPeak &other)
: BasePeak(other), m_Qsample(other.m_Qsample), m_wavelength(other.m_wavelength), m_refFrame(other.m_refFrame) {}
//----------------------------------------------------------------------------------------------
/** Constructor making a LeanElasticPeak from IPeak interface
*
* @param ipeak :: const reference to an IPeak object
*/
LeanElasticPeak::LeanElasticPeak(const Geometry::IPeak &ipeak)
: BasePeak(ipeak), m_Qsample(ipeak.getQSampleFrame()), m_wavelength(ipeak.getWavelength()) {}
//----------------------------------------------------------------------------------------------
/** Set the wavelength of the neutron. Assumes elastic scattering.
*
* @param wavelength :: wavelength in Angstroms.
*/
void LeanElasticPeak::setWavelength(double wavelength) { m_wavelength = wavelength; }
/** Return a shared ptr to the reference frame for this peak. */
std::shared_ptr<const Geometry::ReferenceFrame> LeanElasticPeak::getReferenceFrame() const {
return (m_refFrame) ? m_refFrame : std::make_shared<const ReferenceFrame>();
}
/**
Setter for the reference frame.
@param frame : reference frame object to use.
*/
void LeanElasticPeak::setReferenceFrame(std::shared_ptr<const ReferenceFrame> frame) { m_refFrame = std::move(frame); }
// -------------------------------------------------------------------------------------
/** Return the neutron wavelength (in angstroms) */
double LeanElasticPeak::getWavelength() const { return m_wavelength; }
// -------------------------------------------------------------------------------------
/** Calculate the time of flight (in microseconds) of the neutrons for this
* peak,
* using the geometry of the detector */
double LeanElasticPeak::getTOF() const {
throw Exception::NotImplementedError("LeanElasticPeak::getTOF(): no detector infomation in LeanElasticPeak");
}
/// returns the row (y) of the pixel of the detector, throws NotImplementedError for LeanElasticPeak
int LeanElasticPeak::getRow() const {
throw Exception::NotImplementedError("LeanElasticPeak::getRow(): no detector infomation in LeanElasticPeak");
}
/// returns the column (x) of the pixel of the detector, throws NotImplementedError for LeanElasticPeak
int LeanElasticPeak::getCol() const {
throw Exception::NotImplementedError("LeanElasticPeak::getCol(): no detector infomation in LeanElasticPeak");
}
// -------------------------------------------------------------------------------------
/** Calculate the scattering angle of the peak */
double LeanElasticPeak::getScattering() const { return asin(getWavelength() / (2 * getDSpacing())) * 2; }
// -------------------------------------------------------------------------------------
/** Calculate the azimuthal angle of the peak */
double LeanElasticPeak::getAzimuthal() const {
const V3D qLab = getQLabFrame();
std::shared_ptr<const ReferenceFrame> refFrame = getReferenceFrame();
const double qSign = (convention != "Crystallography") ? 1.0 : -1.0;
const V3D detectorDir = -qLab * qSign;
if (refFrame)
return atan2(detectorDir[refFrame->pointingUp()], detectorDir[refFrame->pointingHorizontal()]);
else
return atan2(detectorDir[1], detectorDir[0]);
}
// -------------------------------------------------------------------------------------
/** Calculate the d-spacing of the peak, in 1/Angstroms */
double LeanElasticPeak::getDSpacing() const { return 2 * M_PI / m_Qsample.norm(); }
//----------------------------------------------------------------------------------------------
/** Return the Q change (of the lattice, k_i - k_f) for this peak.
* The Q is in the Lab frame: the goniometer rotation was NOT taken out.
*
* Note: There is a 2*pi factor used, so |Q| = 2*pi/wavelength.
* */
Mantid::Kernel::V3D LeanElasticPeak::getQLabFrame() const { return getGoniometerMatrix() * m_Qsample; }
//----------------------------------------------------------------------------------------------
/** Return the Q change (of the lattice, k_i - k_f) for this peak.
* The Q is in the Sample frame: the goniometer rotation WAS taken out. */
Mantid::Kernel::V3D LeanElasticPeak::getQSampleFrame() const { return m_Qsample; }
//----------------------------------------------------------------------------------------------
/** Set the peak using the peak's position in reciprocal space, in the sample
*frame.
*
* @param QSampleFrame :: Q of the center of the peak, in reciprocal space
* This is in inelastic convention: momentum transfer of the LATTICE!
* Also, q does NOT have a 2pi factor = it is equal to 1/wavelength.
*/
void LeanElasticPeak::setQSampleFrame(const Mantid::Kernel::V3D &QSampleFrame, boost::optional<double>) {
m_Qsample = QSampleFrame;
}
void LeanElasticPeak::setQSampleFrame(const Mantid::Kernel::V3D &QSampleFrame,
const Mantid::Kernel::Matrix<double> &goniometer) {
m_Qsample = QSampleFrame;
setGoniometerMatrix(goniometer);
const V3D qLab = getQLabFrame();
try {
double wl = calculateWavelengthFromQLab(qLab);
setWavelength(wl);
} catch (std::exception &e) {
g_log.information() << "Unable to automatically determine wavelength from q-lab\n"
<< e.what() << ", goniometer is likely not correct\n";
}
}
//----------------------------------------------------------------------------------------------
/** Set the peak using the peak's position in reciprocal space, in the lab
*frame.
*
* @param qLab :: Q of the center of the peak, in reciprocal space.
* This is in inelastic convention: momentum transfer of the LATTICE!
* Also, q does have a 2pi factor = it is equal to 2pi/wavelength (in
* Angstroms).
*/
void LeanElasticPeak::setQLabFrame(const Mantid::Kernel::V3D &qLab, boost::optional<double>) {
this->setQSampleFrame(getInverseGoniometerMatrix() * qLab);
}
//----------------------------------------------------------------------------------------------
/** Get the final neutron energy in meV */
double LeanElasticPeak::getFinalEnergy() const {
// Velocity of the neutron (non-relativistic)
const double velocity = PhysicalConstants::h / (m_wavelength * 1e-10 * PhysicalConstants::NeutronMass);
// Energy in J of the neutron
const double energy = PhysicalConstants::NeutronMass * velocity * velocity / 2.0;
// Convert to meV
return energy / PhysicalConstants::meV;
}
/** Get the initial (incident) neutron energy in meV */
double LeanElasticPeak::getInitialEnergy() const { return getFinalEnergy(); }
/** Get the difference between the initial and final neutron energy in meV,
* elastic so always 0 */
double LeanElasticPeak::getEnergyTransfer() const { return 0.; }
/** Set the final energy */
void LeanElasticPeak::setFinalEnergy(double) {
throw Exception::NotImplementedError("Use LeanElasticPeak::setWavelength");
}
/** Set the initial energy */
void LeanElasticPeak::setInitialEnergy(double) {
throw Exception::NotImplementedError("Use LeanElasticPeak::setWavelength");
}
// -------------------------------------------------------------------------------------
/** Return the L1 flight path length (source to sample), in meters. */
double LeanElasticPeak::getL1() const {
throw Exception::NotImplementedError("LeanElasticPeak has no detector information");
}
// -------------------------------------------------------------------------------------
/** Return the L2 flight path length (sample to detector), in meters. */
double LeanElasticPeak::getL2() const {
throw Exception::NotImplementedError("LeanElasticPeak has no detector information");
}
/**
* @brief Assignement operator overload
* @param other : Other peak object to assign from
* @return this
*/
LeanElasticPeak &LeanElasticPeak::operator=(const LeanElasticPeak &other) {
if (&other != this) {
BasePeak::operator=(other);
m_Qsample = other.m_Qsample;
m_wavelength = other.m_wavelength;
}
return *this;
}
Mantid::Kernel::Logger LeanElasticPeak::g_log("PeakLogger");
} // namespace DataObjects
} // namespace Mantid