forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeanElasticPeaksWorkspaceTest.h
274 lines (236 loc) · 10.3 KB
/
LeanElasticPeaksWorkspaceTest.h
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
// 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 +
#pragma once
#include "MantidAPI/Run.h"
#include "MantidAPI/Sample.h"
#include "MantidDataObjects/LeanElasticPeaksWorkspace.h"
#include "MantidFrameworkTestHelpers/NexusTestHelper.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Instrument/Goniometer.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
namespace {
/// static logger object
Mantid::Kernel::Logger g_log("LeanElasticPeaksWorkspaceTest");
} // namespace
class LeanElasticPeaksWorkspaceTest : public CxxTest::TestSuite {
private:
class TestableLeanElasticPeaksWorkspace : public LeanElasticPeaksWorkspace {
public:
TestableLeanElasticPeaksWorkspace(const LeanElasticPeaksWorkspace &other) : LeanElasticPeaksWorkspace(other) {}
};
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static LeanElasticPeaksWorkspaceTest *createSuite() { return new LeanElasticPeaksWorkspaceTest(); }
static void destroySuite(LeanElasticPeaksWorkspaceTest *suite) { delete suite; }
void test_defaultConstructor() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
LeanElasticPeak p(V3D(1, 0, 0), 3.0);
pw->addPeak(p);
TS_ASSERT_EQUALS(pw->columnCount(), 14);
TS_ASSERT_EQUALS(pw->rowCount(), 1);
TS_ASSERT_EQUALS(pw->getNumberPeaks(), 1);
TS_ASSERT_DELTA(pw->getPeak(0).getWavelength(), 3.0, 1e-9);
// check column printing
std::ostringstream s;
for (int n = 0; n < 14; n++) {
pw->getColumn(n)->print(0, s);
s << " ";
}
TS_ASSERT_EQUALS(s.str(), "0 0.00 0.00 0.00 3 9.1 6.3 0 0 0 0 [1,0,0] [1,0,0] 0 ");
// check some cell values
TS_ASSERT_EQUALS(pw->cell<int>(0, 0), 0);
TS_ASSERT_EQUALS(pw->cell<double>(0, 1), 0);
TS_ASSERT_EQUALS(pw->cell<double>(0, 2), 0);
TS_ASSERT_EQUALS(pw->cell<double>(0, 3), 0);
TS_ASSERT_EQUALS(pw->cell<double>(0, 4), 3);
TS_ASSERT_DELTA(pw->cell<double>(0, 5), 9.0893558317, 1e-7);
TS_ASSERT_DELTA(pw->cell<double>(0, 6), 6.2831853017, 1e-7);
}
void test_copyConstructor() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
LeanElasticPeak p(V3D(1, 0, 0), 3.0);
pw->addPeak(p);
auto pw2 = std::make_shared<TestableLeanElasticPeaksWorkspace>(*pw);
TS_ASSERT_EQUALS(pw2->rowCount(), 1);
TS_ASSERT_EQUALS(pw2->getNumberPeaks(), 1);
TS_ASSERT_DELTA(pw2->getPeak(0).getWavelength(), 3.0, 1e-9);
}
void test_clone() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
LeanElasticPeak p(V3D(1, 0, 0), 3.0);
pw->addPeak(p);
auto pw2 = pw->clone();
TS_ASSERT_EQUALS(pw2->rowCount(), 1);
TS_ASSERT_EQUALS(pw2->getNumberPeaks(), 1);
TS_ASSERT_DELTA(pw2->getPeak(0).getWavelength(), 3.0, 1e-9);
}
void test_createPeak() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
Mantid::Geometry::Goniometer goniometer;
goniometer.pushAxis("axis1", 0, 1, 0);
goniometer.setRotationAngle(0, 90);
pw->mutableRun().setGoniometer(goniometer, false);
// cannot create peak using q-lab
TS_ASSERT_THROWS(pw->createPeak(V3D(1, 1, 0)), const Exception::NotImplementedError &)
auto peak = pw->createPeakQSample(V3D(1, 1, 0));
auto qSample = peak->getQSampleFrame();
TS_ASSERT_DELTA(qSample.X(), 1, 1e-7)
TS_ASSERT_DELTA(qSample.Y(), 1, 1e-7)
TS_ASSERT_DELTA(qSample.Z(), 0, 1e-7)
auto qLab = peak->getQLabFrame();
TS_ASSERT_DELTA(qLab.X(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Y(), 1, 1e-7)
TS_ASSERT_DELTA(qLab.Z(), -1, 1e-7)
}
void test_createPeakHKL() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
Mantid::Geometry::Goniometer goniometer;
goniometer.pushAxis("axis1", 0, 1, 0);
goniometer.setRotationAngle(0, 90);
pw->mutableSample().setOrientedLattice(std::make_unique<OrientedLattice>(5, 5, 5, 90, 90, 90));
pw->mutableRun().setGoniometer(goniometer, false);
auto peak = pw->createPeakHKL(V3D(1, 0, 0));
TS_ASSERT_EQUALS(peak->getH(), 1)
TS_ASSERT_EQUALS(peak->getK(), 0)
TS_ASSERT_EQUALS(peak->getL(), 0)
auto qSample = peak->getQSampleFrame();
TS_ASSERT_DELTA(qSample.X(), 2 * M_PI / 5, 1e-7)
TS_ASSERT_DELTA(qSample.Y(), 0, 1e-7)
TS_ASSERT_DELTA(qSample.Z(), 0, 1e-7)
auto qLab = peak->getQLabFrame();
TS_ASSERT_DELTA(qLab.X(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Y(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Z(), -2 * M_PI / 5, 1e-7)
}
void test_addPeakSpecialCoordinate() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
Mantid::Geometry::Goniometer goniometer;
goniometer.pushAxis("axis1", 0, 1, 0);
goniometer.setRotationAngle(0, 90);
pw->mutableSample().setOrientedLattice(std::make_unique<OrientedLattice>(5, 5, 5, 90, 90, 90));
pw->mutableRun().setGoniometer(goniometer, false);
TS_ASSERT_THROWS(pw->addPeak(V3D(1, 0, 0), Mantid::Kernel::QLab), const Exception::NotImplementedError &)
pw->addPeak(V3D(1, 1, 0), Mantid::Kernel::QSample);
pw->addPeak(V3D(1, 0, 0), Mantid::Kernel::HKL);
TS_ASSERT_EQUALS(pw->getNumberPeaks(), 2);
auto peak = pw->getPeak(0);
TS_ASSERT_EQUALS(peak.getH(), 0)
TS_ASSERT_EQUALS(peak.getK(), 0)
TS_ASSERT_EQUALS(peak.getL(), 0)
auto qSample = peak.getQSampleFrame();
TS_ASSERT_DELTA(qSample.X(), 1, 1e-7)
TS_ASSERT_DELTA(qSample.Y(), 1, 1e-7)
TS_ASSERT_DELTA(qSample.Z(), 0, 1e-7)
auto qLab = peak.getQLabFrame();
TS_ASSERT_DELTA(qLab.X(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Y(), 1, 1e-7)
TS_ASSERT_DELTA(qLab.Z(), -1, 1e-7)
peak = pw->getPeak(1);
TS_ASSERT_EQUALS(peak.getH(), 1)
TS_ASSERT_EQUALS(peak.getK(), 0)
TS_ASSERT_EQUALS(peak.getL(), 0)
qSample = peak.getQSampleFrame();
TS_ASSERT_DELTA(qSample.X(), 2 * M_PI / 5, 1e-7)
TS_ASSERT_DELTA(qSample.Y(), 0, 1e-7)
TS_ASSERT_DELTA(qSample.Z(), 0, 1e-7)
qLab = peak.getQLabFrame();
TS_ASSERT_DELTA(qLab.X(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Y(), 0, 1e-7)
TS_ASSERT_DELTA(qLab.Z(), -2 * M_PI / 5, 1e-7)
}
void test_addRemovePeaks() {
// build peaksworkspace (note number of peaks = 1)
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
// add peaks
LeanElasticPeak p(V3D(1, 0, 0));
LeanElasticPeak p2(V3D(0, 1, 0));
LeanElasticPeak p3(V3D(0, 0, 1));
pw->addPeak(p);
pw->addPeak(p2);
pw->addPeak(p3);
TS_ASSERT_EQUALS(pw->getNumberPeaks(), 3);
// number of peaks = 4, now remove 3
std::vector<int> badPeaks{0, 2, 3};
pw->removePeaks(std::move(badPeaks));
TS_ASSERT_EQUALS(pw->getNumberPeaks(), 1);
}
void test_sort() {
auto pw = std::make_shared<LeanElasticPeaksWorkspace>();
LeanElasticPeak p0(V3D(1, 0, 0), 3.0);
LeanElasticPeak p1(V3D(1, 0, 0), 4.0);
LeanElasticPeak p2(V3D(1, 0, 0), 5.0);
LeanElasticPeak p3(V3D(1, 1, 0), 3.0);
LeanElasticPeak p4(V3D(2, 0, 0), 3.0);
pw->addPeak(p0);
pw->addPeak(p1);
pw->addPeak(p2);
pw->addPeak(p3);
pw->addPeak(p4);
std::vector<std::pair<std::string, bool>> criteria;
// Sort by decending wavelength then decending dspacing
criteria.emplace_back(std::pair<std::string, bool>("wavelength", false));
criteria.emplace_back(std::pair<std::string, bool>("dspacing", false));
pw->sort(criteria);
TS_ASSERT_DELTA(pw->getPeak(0).getWavelength(), 5.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(0).getDSpacing(), 2 * M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(1).getWavelength(), 4.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(1).getDSpacing(), 2 * M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(2).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(2).getDSpacing(), 2 * M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(3).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(3).getDSpacing(), M_PI * M_SQRT2, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(4).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(4).getDSpacing(), M_PI, 1e-5);
// Sort by wavelength ascending then decending dspacing
criteria.clear();
criteria.emplace_back(std::pair<std::string, bool>("wavelength", true));
criteria.emplace_back(std::pair<std::string, bool>("dspacing", false));
pw->sort(criteria);
TS_ASSERT_DELTA(pw->getPeak(0).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(0).getDSpacing(), 2 * M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(1).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(1).getDSpacing(), M_PI * M_SQRT2, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(2).getWavelength(), 3.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(2).getDSpacing(), M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(3).getWavelength(), 4.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(3).getDSpacing(), 2 * M_PI, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(4).getWavelength(), 5.0, 1e-5);
TS_ASSERT_DELTA(pw->getPeak(4).getDSpacing(), 2 * M_PI, 1e-5);
}
void test_saveToNexus() {
// build peaksworkspace (note number of peaks = 1)
auto lpws = std::make_shared<LeanElasticPeaksWorkspace>();
// add peaks
LeanElasticPeak p(V3D(1, 0, 0), 3.0);
LeanElasticPeak p2(V3D(0, 1, 0), 4.0);
LeanElasticPeak p3(V3D(0, 0, 1), 5.0);
lpws->addPeak(p);
lpws->addPeak(p2);
lpws->addPeak(p3);
// save to nexus
NexusTestHelper nexusHelper(true);
nexusHelper.createFile("testSaveLeanElasticPeaksWorkspace.nxs");
lpws->saveNexus(nexusHelper.file.get());
nexusHelper.reopenFile();
// Verify that this test_entry has a peaks_workspace entry
TS_ASSERT_THROWS_NOTHING(nexusHelper.file->openGroup("peaks_workspace", "NXentry"));
// Check wavelengths
TS_ASSERT_THROWS_NOTHING(nexusHelper.file->openData("column_7"));
std::vector<double> waveLengths;
TS_ASSERT_THROWS_NOTHING(nexusHelper.file->getData(waveLengths));
nexusHelper.file->closeData();
TS_ASSERT_EQUALS(waveLengths.size(), 3);
TS_ASSERT_DELTA(waveLengths[0], 3.0, 1e-5);
TS_ASSERT_DELTA(waveLengths[1], 4.0, 1e-5);
TS_ASSERT_DELTA(waveLengths[2], 5.0, 1e-5);
}
};