Newer
Older
#ifndef MANTID_ALGORITHMS_REFLECTOMETRYSUMINQTEST_H_
#define MANTID_ALGORITHMS_REFLECTOMETRYSUMINQTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/ReflectometrySumInQ.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
using Mantid::Algorithms::ReflectometrySumInQ;
class ReflectometrySumInQTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ReflectometrySumInQTest *createSuite() { return new ReflectometrySumInQTest(); }
static void destroySuite( ReflectometrySumInQTest *suite ) { delete suite; }
static Mantid::API::MatrixWorkspace_sptr convertToWavelength(Mantid::API::MatrixWorkspace_sptr ws) {
using namespace Mantid;
auto toWavelength = API::AlgorithmManager::Instance().createUnmanaged("ConvertUnits");
toWavelength->initialize();
toWavelength->setChild(true);
toWavelength->setProperty("InputWorkspace", ws);
toWavelength->setProperty("OutputWorkspace", "_unused_for_child");
toWavelength->setProperty("Target", "Wavelength");
toWavelength->setProperty("EMode", "Elastic");
toWavelength->execute();
return toWavelength->getProperty("OutputWorkspace");
}
static Mantid::API::MatrixWorkspace_sptr detectorsOnly(Mantid::API::MatrixWorkspace_sptr ws) {
using namespace Mantid;
auto &specturmInfo = ws->spectrumInfo();
std::vector<size_t> detectorIndices;
for (size_t i = 0; i < ws->getNumberHistograms(); ++i) {
if (specturmInfo.isMonitor(i)) {
continue;
}
detectorIndices.emplace_back(i);
}
auto extractDetectors = API::AlgorithmManager::Instance().createUnmanaged("ExtractSpectra");
extractDetectors->initialize();
extractDetectors->setChild(true);
extractDetectors->setProperty("InputWorkspace", ws);
extractDetectors->setProperty("OutputWorkspace", "_unused_for_child");
extractDetectors->setProperty("WorkspaceIndexList", detectorIndices);
extractDetectors->execute();
return extractDetectors->getProperty("OutputWorkspace");
}
void test_init()
{
ReflectometrySumInQ alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
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
void test_sumSingleHistogram() {
using namespace Mantid;
auto inputWS = testWorkspace();
inputWS = detectorsOnly(inputWS);
inputWS = convertToWavelength(inputWS);
auto &Ys = inputWS->y(0);
const auto totalY = std::accumulate(Ys.cbegin(), Ys.cend(), 0.0);
const std::array<bool, 2> flatSampleOptions{{true, false}};
for (const auto isFlatSample : flatSampleOptions) {
for (size_t i = 0; i < inputWS->getNumberHistograms(); ++i) {
ReflectometrySumInQ alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", inputWS))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspaceIndexSet", std::to_string(i)))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "_unused_for_child"))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BeamCentre", static_cast<int>(i)))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMin", 0.1))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMax", 20.))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("FlatSample", isFlatSample))
TS_ASSERT_THROWS_NOTHING(alg.execute())
API::MatrixWorkspace_sptr outputWS = alg.getProperty("OutputWorkspace");
TS_ASSERT(outputWS);
TS_ASSERT_EQUALS(outputWS->getNumberHistograms(), 1)
auto &Ys = outputWS->y(0);
const auto totalYSummedInQ = std::accumulate(Ys.cbegin(), Ys.cend(), 0.0);
TS_ASSERT_DELTA(totalYSummedInQ, totalY, 1e-10)
}
}
}
void test_sumEntireWorkspace() {
using namespace Mantid;
auto inputWS = testWorkspace();
inputWS = detectorsOnly(inputWS);
inputWS = convertToWavelength(inputWS);
auto &Ys = inputWS->y(0);
double totalY{0.0};
for (size_t i = 0; i < inputWS->getNumberHistograms(); ++i) {
totalY += std::accumulate(Ys.cbegin(), Ys.cend(), 0.0);
}
const std::array<bool, 2> flatSampleOptions{{true, false}};
for (const auto isFlatSample : flatSampleOptions) {
// Loop over possible beam centres.
for (size_t beamCentre = 0; beamCentre < inputWS->getNumberHistograms(); ++beamCentre) {
ReflectometrySumInQ alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", inputWS))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspaceIndexSet", "0, 1, 2"))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "_unused_for_child"))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BeamCentre", static_cast<int>(beamCentre)))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMin", 0.1))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMax", 20.))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("FlatSample", isFlatSample))
TS_ASSERT_THROWS_NOTHING(alg.execute())
API::MatrixWorkspace_sptr outputWS = alg.getProperty("OutputWorkspace");
TS_ASSERT(outputWS);
TS_ASSERT_EQUALS(outputWS->getNumberHistograms(), 1)
auto &Ys = outputWS->y(0);
const auto totalYSummedInQ = std::accumulate(Ys.cbegin(), Ys.cend(), 0.0);
TS_ASSERT_DELTA(totalYSummedInQ, totalY, 1e-10)
}
}
}
void test_monitorNextToDetectorsThrows() {
auto inputWS = testWorkspace();
inputWS = convertToWavelength(inputWS);
ReflectometrySumInQ alg;
alg.setChild(true);
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
alg.setRethrows(true);
constexpr size_t monitorIdx{0};
constexpr size_t detectorIdx{1};
TS_ASSERT(inputWS->spectrumInfo().isMonitor(monitorIdx))
TS_ASSERT(!inputWS->spectrumInfo().isMonitor(detectorIdx))
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", inputWS))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspaceIndexSet", std::to_string(detectorIdx)))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "_unused_for_child"))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BeamCentre", static_cast<int>(detectorIdx)))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMin", 0.1))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMax", 15.))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("FlatSample", true))
TS_ASSERT_THROWS_EQUALS(alg.execute(), const std::runtime_error &e, e.what(), std::string("Some invalid Properties found"))
}
void test_monitorInIndexSetThrows() {
auto inputWS = testWorkspace();
inputWS = convertToWavelength(inputWS);
ReflectometrySumInQ alg;
alg.setChild(true);
alg.setRethrows(true);
const size_t monitorIdx{0};
TS_ASSERT(inputWS->spectrumInfo().isMonitor(monitorIdx))
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", inputWS))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspaceIndexSet", std::to_string(monitorIdx)))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "_unused_for_child"))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BeamCentre", static_cast<int>(monitorIdx)))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMin", 0.1))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMax", 15.))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("FlatSample", true))
TS_ASSERT_THROWS_EQUALS(alg.execute(), const std::runtime_error &e, e.what(), std::string("Some invalid Properties found"))
}
void test_BeamCentreNotInIndexSetThrows() {
auto inputWS = testWorkspace();
inputWS = convertToWavelength(inputWS);
inputWS = detectorsOnly(inputWS);
ReflectometrySumInQ alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", inputWS))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspaceIndexSet", "0, 1"))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "_unused_for_child"))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BeamCentre", 2))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMin", 0.1))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WavelengthMax", 15.))
TS_ASSERT_THROWS_NOTHING(alg.setProperty("FlatSample", true))
TS_ASSERT_THROWS_EQUALS(alg.execute(), const std::runtime_error &e, e.what(), std::string("Some invalid Properties found"))
private:
static Mantid::API::MatrixWorkspace_sptr testWorkspace() {
using namespace Mantid;
using namespace WorkspaceCreationHelper;
constexpr double startX{0.};
const Kernel::V3D slit1Pos{0., 0., -2.};
const Kernel::V3D slit2Pos{0., 0., -1.};
constexpr double vg1{0.5};
constexpr double vg2{1.};
const Kernel::V3D sourcePos{0., 0., -50.};
const Kernel::V3D monitorPos{0., 0., -0.5};
const Kernel::V3D samplePos{0., 0., 0.,};
constexpr double twoTheta{0.87 / 180. * M_PI};
constexpr double detectorHeight{0.001};
constexpr double l2{2.3};
const auto y = l2 * std::sin(twoTheta);
const auto z = l2 * std::cos(twoTheta);
const Kernel::V3D centrePos{0., y, z};
constexpr int nSpectra{4}; // One spectrum is monitor
constexpr int nBins{50};
return create2DWorkspaceWithReflectometryInstrumentMultiDetector(startX, detectorHeight, slit1Pos, slit2Pos, vg1, vg2, sourcePos, monitorPos, samplePos, centrePos, nSpectra, nBins);
}
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
class ReflectometrySumInQTestPerformance : public CxxTest::TestSuite {
public:
static ReflectometrySumInQTestPerformance *createSuite() {
return new ReflectometrySumInQTestPerformance();
}
static void destroySuite(ReflectometrySumInQTestPerformance *suite) { delete suite; }
ReflectometrySumInQTestPerformance() {
using namespace Mantid;
using namespace WorkspaceCreationHelper;
constexpr double startX{0.};
const Kernel::V3D slit1Pos{0., 0., -2.};
const Kernel::V3D slit2Pos{0., 0., -1.};
constexpr double vg1{0.5};
constexpr double vg2{1.};
const Kernel::V3D sourcePos{0., 0., -50.};
const Kernel::V3D monitorPos{0., 0., -0.5};
const Kernel::V3D samplePos{0., 0., 0.,};
constexpr double twoTheta{5.87 / 180. * M_PI};
constexpr double detectorHeight{0.001};
constexpr double l2{2.3};
const auto y = l2 * std::sin(twoTheta);
const auto z = l2 * std::cos(twoTheta);
const Kernel::V3D centrePos{0., y, z};
constexpr int nSpectra{101}; // One spectrum is monitor
constexpr int nBins{200};
constexpr double binWidth{1250.};
m_workspace = create2DWorkspaceWithReflectometryInstrumentMultiDetector(startX, detectorHeight, slit1Pos, slit2Pos, vg1, vg2, sourcePos, monitorPos, samplePos, centrePos, nSpectra, nBins, binWidth);
m_workspace = ReflectometrySumInQTest::convertToWavelength(m_workspace);
m_workspace = ReflectometrySumInQTest::detectorsOnly(m_workspace);
m_fullIndexSet.assign(m_workspace->getNumberHistograms(), 0);
std::iota(m_fullIndexSet.begin(), m_fullIndexSet.end(), 0);
}
void test_typical() {
ReflectometrySumInQ alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", m_workspace);
alg.setProperty("InputWorkspaceIndexSet", m_fullIndexSet);
alg.setPropertyValue("OutputWorkspace", "_unused_for_child");
alg.setProperty("BeamCentre", 49);
alg.setProperty("WavelengthMin", 0.1);
alg.setProperty("WavelengthMax", 20.);
alg.setProperty("FlatSample", true);
for (int repetitions = 0; repetitions < 1000; ++repetitions) {
alg.execute();
}
}
private:
Mantid::API::MatrixWorkspace_sptr m_workspace;
std::vector<int64_t> m_fullIndexSet;
};
#endif /* MANTID_ALGORITHMS_REFLECTOMETRYSUMINQTEST_H_ */