LeanElasticPeakTest.h 9.87 KB
Newer Older
Whitfield, Ross's avatar
Whitfield, Ross committed
1
2
3
4
5
6
7
8
9
10
11
12
// 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 "MantidKernel/V3D.h"

#include <cxxtest/TestSuite.h>

13
#include "MantidDataObjects/LeanElasticPeak.h"
Whitfield, Ross's avatar
Whitfield, Ross committed
14

15
16
17
#include "MantidDataObjects/Peak.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"

Whitfield, Ross's avatar
Whitfield, Ross committed
18
19
20
21
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;

22
class LeanElasticPeakTest : public CxxTest::TestSuite {
Whitfield, Ross's avatar
Whitfield, Ross committed
23
24
25
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
26
27
28
29
  static LeanElasticPeakTest *createSuite() {
    return new LeanElasticPeakTest();
  }
  static void destroySuite(LeanElasticPeakTest *suite) { delete suite; }
Whitfield, Ross's avatar
Whitfield, Ross committed
30
31

  void test_default_constructor() {
32
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
33
34
35
36
37
38
39
40
    TS_ASSERT_EQUALS(p.getH(), 0.0)
    TS_ASSERT_EQUALS(p.getK(), 0.0)
    TS_ASSERT_EQUALS(p.getL(), 0.0)
    TS_ASSERT_EQUALS(p.getInitialEnergy(), 0.0)
    TS_ASSERT_EQUALS(p.getFinalEnergy(), 0.0)
    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(0, 0, 0))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D())

41
    TS_ASSERT_EQUALS(p.getDetectorID(), -1)
42
43
44
45
46
47
48
49
50
    TS_ASSERT_THROWS(p.getDetector(), const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getInstrument(), const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.findDetector(), const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getDetectorPosition(),
                     const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getDetectorPositionNoCheck(),
                     const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getDetPos(), const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getSamplePos(), const Exception::NotImplementedError &)
51
52
53
    TS_ASSERT(std::isnan(p.getTOF()))
    TS_ASSERT(std::isnan(p.getScattering()))
    TS_ASSERT(std::isnan(p.getAzimuthal()))
54
55
    TS_ASSERT_THROWS(p.getL1(), const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(p.getL2(), const Exception::NotImplementedError &)
Whitfield, Ross's avatar
Whitfield, Ross committed
56
57
58
  }

  void test_Qsample_constructor() {
59
    LeanElasticPeak p(V3D(1, 2, 3));
60
61
    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(1, 2, 3))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D(1, 2, 3))
Whitfield, Ross's avatar
Whitfield, Ross committed
62
63
64
65
66
67
68

    // This goniometer should just swap x and y of q
    Mantid::Kernel::Matrix<double> gon(3, 3);
    gon[0][1] = 1;
    gon[1][0] = 1;
    gon[2][2] = 1;
    p.setGoniometerMatrix(gon);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(1, 2, 3))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D(2, 1, 3))

    p.setWavelength(1.);
    TS_ASSERT_DELTA(p.getInitialEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getFinalEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getWavelength(), 1., 1e-9)
  }

  void test_Qsample_gon_constructor() {

    // This goniometer should just swap x and y of q
    Mantid::Kernel::Matrix<double> gon(3, 3);
    gon[0][1] = 1;
    gon[1][0] = 1;
    gon[2][2] = 1;

86
    LeanElasticPeak p(V3D(1, 2, 3), gon);
87
88
89
90
91
92

    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(1, 2, 3))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D(2, 1, 3))
  }

  void test_Qsample_wavelength_constructor() {
93
    LeanElasticPeak p(V3D(1, 2, 3), 1.);
94
95
96
97
98
99
    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(1, 2, 3))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D(1, 2, 3))

    TS_ASSERT_DELTA(p.getInitialEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getFinalEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getWavelength(), 1., 1e-9)
100
101
    TS_ASSERT_DELTA(p.getDSpacing(), 1.679251908362714, 1e-9)
    TS_ASSERT_DELTA(p.getScattering(), 0.6046731932, 1e-9)
102
103
104
105
106
107
108
109
110
111
  }

  void test_Qsample_gon_wavelength_constructor() {

    // This goniometer should just swap x and y of q
    Mantid::Kernel::Matrix<double> gon(3, 3);
    gon[0][1] = 1;
    gon[1][0] = 1;
    gon[2][2] = 1;

112
    LeanElasticPeak p(V3D(1, 2, 3), gon, 1.);
Whitfield, Ross's avatar
Whitfield, Ross committed
113
114
115
116
117
118
119

    TS_ASSERT_EQUALS(p.getQSampleFrame(), V3D(1, 2, 3))
    TS_ASSERT_EQUALS(p.getQLabFrame(), V3D(2, 1, 3))
    TS_ASSERT_DELTA(p.getInitialEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getFinalEnergy(), 81.8042024359, 1e-5)
    TS_ASSERT_DELTA(p.getWavelength(), 1., 1e-9)
  }
120
121
122
123
124
125
126
127

  void test_copyConstructor() {
    // This goniometer should just swap x and y of q
    Mantid::Kernel::Matrix<double> gon(3, 3);
    gon[0][1] = 1;
    gon[1][0] = 1;
    gon[2][2] = 1;

128
    LeanElasticPeak p(V3D(1, 2, 3), gon, 1.);
129
    // Default (not-explicit) copy constructor
130
    LeanElasticPeak p2(p);
131
132
133
134
    TS_ASSERT_EQUALS(p.getQSampleFrame(), p2.getQSampleFrame());
    TS_ASSERT_EQUALS(p.getQLabFrame(), p2.getQLabFrame());
    TS_ASSERT_EQUALS(p.getGoniometerMatrix(), p2.getGoniometerMatrix());
  }
135
136
137
138
139
140
141
142

  void test_ConstructorFromIPeakInterface() {
    // This goniometer should just swap x and y of q
    Mantid::Kernel::Matrix<double> gon(3, 3);
    gon[0][1] = 1;
    gon[1][0] = 1;
    gon[2][2] = 1;

143
    LeanElasticPeak p(V3D(1, 2, 3), gon, 1.);
144
145

    const Mantid::Geometry::IPeak &ipeak = p;
146
    LeanElasticPeak p2(ipeak);
147
148
149
150
    TS_ASSERT_EQUALS(p.getQSampleFrame(), p2.getQSampleFrame());
    TS_ASSERT_EQUALS(p.getQLabFrame(), p2.getQLabFrame());
    TS_ASSERT_EQUALS(p.getGoniometerMatrix(), p2.getGoniometerMatrix());
  }
Whitfield, Ross's avatar
Whitfield, Ross committed
151
152

  void test_HKL() {
153
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    p.setHKL(1.0, 2.0, 3.0);
    TS_ASSERT_EQUALS(p.getH(), 1.0);
    TS_ASSERT_EQUALS(p.getK(), 2.0);
    TS_ASSERT_EQUALS(p.getL(), 3.0);
    p.setH(5);
    p.setK(6);
    p.setL(7);
    TS_ASSERT_EQUALS(p.getH(), 5.0);
    TS_ASSERT_EQUALS(p.getK(), 6.0);
    TS_ASSERT_EQUALS(p.getL(), 7.0);
    p.setHKL(V3D(1.0, 2.0, 3.0));
    TS_ASSERT_EQUALS(p.getH(), 1.0);
    TS_ASSERT_EQUALS(p.getK(), 2.0);
    TS_ASSERT_EQUALS(p.getL(), 3.0);
    TS_ASSERT_EQUALS(p.getHKL(), V3D(1.0, 2.0, 3.0));
  }

  void test_isIndexed() {
172
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
173
174
175
176
177
178
179
180
    TS_ASSERT_EQUALS(false, p.isIndexed());
    p.setHKL(1, 2, 3);
    TS_ASSERT_EQUALS(true, p.isIndexed());
  }

  void test_get_intensity_over_sigma() {
    const double intensity{100};
    const double sigma{10};
181
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
182
183
184
185
186
187
188
189
190
191

    p.setIntensity(intensity);
    p.setSigmaIntensity(sigma);

    TS_ASSERT_EQUALS(p.getIntensityOverSigma(), intensity / sigma);
  }

  void test_get_intensity_over_sigma_empty_sigma() {
    const double intensity{10};
    const double sigma{0};
192
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
193
194
195
196
197
198
199
200
201
202
203
204

    p.setIntensity(intensity);
    p.setSigmaIntensity(sigma);

    const double expectedResult{0.0};
    const double tolerance{1e-10};
    TS_ASSERT_DELTA(p.getIntensityOverSigma(), expectedResult, tolerance);
  }

  void test_get_energy() {
    const double initialEnergy{100};
    const double finalEnergy{110};
205
    LeanElasticPeak p;
Whitfield, Ross's avatar
Whitfield, Ross committed
206
207
208
209
210
211

    p.setInitialEnergy(initialEnergy);
    p.setFinalEnergy(finalEnergy);

    TS_ASSERT_EQUALS(p.getEnergyTransfer(), initialEnergy - finalEnergy);
  }
212

213
  void test_Peak_to_LeanElasticPeak_through_IPeak() {
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    Instrument_sptr inst(
        ComponentCreationHelper::createTestInstrumentRectangular(5, 100));

    // Peak 3 is phi,chi,omega of 90,0,0; giving this matrix:
    Matrix<double> r(3, 3, false);
    r[0][2] = 1;
    r[1][1] = 1;
    r[2][0] = -1;

    Peak peak(inst, 19999, 2.0, V3D(1, 2, 3), r);
    peak.setRunNumber(1234);
    peak.setPeakNumber(42);
    peak.setIntensity(900);
    peak.setSigmaIntensity(30);
    peak.setBinCount(90);

    const IPeak &ipeak = peak;

232
    LeanElasticPeak leanpeak(ipeak);
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

    TS_ASSERT_EQUALS(leanpeak.getQSampleFrame(), peak.getQSampleFrame());
    V3D qsample = leanpeak.getQSampleFrame();
    TS_ASSERT_DELTA(qsample[0], -0.0759765444, 1e-7);
    TS_ASSERT_DELTA(qsample[1], -0.4855935910, 1e-7);
    TS_ASSERT_DELTA(qsample[2], -0.4855935910, 1e-7);

    TS_ASSERT_EQUALS(leanpeak.getQLabFrame(), peak.getQLabFrame());
    V3D qlab = leanpeak.getQLabFrame();
    TS_ASSERT_DELTA(qlab[0], -0.4855935910, 1e-7);
    TS_ASSERT_DELTA(qlab[1], -0.4855935910, 1e-7);
    TS_ASSERT_DELTA(qlab[2], 0.0759765444, 1e-7);

    TS_ASSERT_EQUALS(leanpeak.getHKL(), peak.getHKL());
    TS_ASSERT_EQUALS(leanpeak.getH(), 1);
    TS_ASSERT_EQUALS(leanpeak.getK(), 2);
    TS_ASSERT_EQUALS(leanpeak.getL(), 3);

    TS_ASSERT_EQUALS(leanpeak.getGoniometerMatrix(),
                     peak.getGoniometerMatrix());
    TS_ASSERT_EQUALS(leanpeak.getGoniometerMatrix(), r);

    TS_ASSERT_DELTA(leanpeak.getInitialEnergy(), peak.getInitialEnergy(), 1e-7);
    TS_ASSERT_DELTA(leanpeak.getInitialEnergy(), 20.4510506207, 1e-7);

    TS_ASSERT_DELTA(leanpeak.getFinalEnergy(), peak.getFinalEnergy(), 1e-7);
    TS_ASSERT_DELTA(leanpeak.getFinalEnergy(), 20.4510506207, 1e-7);

    TS_ASSERT_DELTA(leanpeak.getWavelength(), peak.getWavelength(), 1e-7);
    TS_ASSERT_DELTA(leanpeak.getWavelength(), 2.0, 1e-7);

    TS_ASSERT_DELTA(leanpeak.getDSpacing(), peak.getDSpacing(), 1e-7);
    TS_ASSERT_DELTA(leanpeak.getDSpacing(), 9.0938998166, 1e-7);

    TS_ASSERT_DELTA(leanpeak.getScattering(), peak.getScattering(), 1e-7);
    TS_ASSERT_DELTA(leanpeak.getScattering(), 0.2203733065, 1e-7);

    TS_ASSERT_EQUALS(leanpeak.getRunNumber(), peak.getRunNumber());
    TS_ASSERT_EQUALS(leanpeak.getRunNumber(), 1234);

    TS_ASSERT_EQUALS(leanpeak.getPeakNumber(), peak.getPeakNumber());
    TS_ASSERT_EQUALS(leanpeak.getPeakNumber(), 42);

    TS_ASSERT_EQUALS(leanpeak.getIntensity(), peak.getIntensity());
    TS_ASSERT_EQUALS(leanpeak.getIntensity(), 900);

    TS_ASSERT_EQUALS(leanpeak.getSigmaIntensity(), peak.getSigmaIntensity());
    TS_ASSERT_EQUALS(leanpeak.getSigmaIntensity(), 30);
    TS_ASSERT_EQUALS(leanpeak.getIntensityOverSigma(), 30);

    TS_ASSERT_EQUALS(leanpeak.getBinCount(), peak.getBinCount());
    TS_ASSERT_EQUALS(leanpeak.getBinCount(), 90);
285
286
287
288
289

    TS_ASSERT_THROWS(leanpeak.getDetector(),
                     const Exception::NotImplementedError &)
    TS_ASSERT_THROWS(leanpeak.getInstrument(),
                     const Exception::NotImplementedError &)
290
  }
Whitfield, Ross's avatar
Whitfield, Ross committed
291
};