/
PredictFractionalPeaks.cpp
258 lines (220 loc) · 8.25 KB
/
PredictFractionalPeaks.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
/*
* PredictFractionalPeaks.cpp
*
* Created on: Dec 5, 2012
* Author: ruth
*/
#include "MantidCrystal/PredictFractionalPeaks.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ArrayLengthValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include <boost/math/special_functions/round.hpp>
namespace Mantid {
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace std;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
namespace Crystal {
DECLARE_ALGORITHM(PredictFractionalPeaks)
PredictFractionalPeaks::~PredictFractionalPeaks() {}
PredictFractionalPeaks::PredictFractionalPeaks() : Algorithm() {}
/// Initialise the properties
void PredictFractionalPeaks::init() {
declareProperty(
make_unique<WorkspaceProperty<IPeaksWorkspace>>("Peaks", "",
Direction::Input),
"Workspace of Peaks with orientation matrix that indexed the peaks and "
"instrument loaded");
declareProperty(
make_unique<WorkspaceProperty<IPeaksWorkspace>>("FracPeaks", "",
Direction::Output),
"Workspace of Peaks with peaks with fractional h,k, and/or l values");
declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
string("HOffset"), "-0.5,0.0,0.5"),
"Offset in the h direction");
declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
string("KOffset"), "0"),
"Offset in the h direction");
declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
string("LOffset"), "-0.5,0.5"),
"Offset in the h direction");
declareProperty("IncludeAllPeaksInRange", false,
"If false only offsets from peaks from Peaks are used");
declareProperty(
make_unique<PropertyWithValue<double>>("Hmin", -8.0, Direction::Input),
"Minimum H value to use");
declareProperty(
make_unique<PropertyWithValue<double>>("Hmax", 8.0, Direction::Input),
"Maximum H value to use");
declareProperty(
make_unique<PropertyWithValue<double>>("Kmin", -8.0, Direction::Input),
"Minimum K value to use");
declareProperty(
make_unique<PropertyWithValue<double>>("Kmax", 8.0, Direction::Input),
"Maximum K value to use");
declareProperty(
make_unique<PropertyWithValue<double>>("Lmin", -8.0, Direction::Input),
"Minimum L value to use");
declareProperty(
make_unique<PropertyWithValue<double>>("Lmax", 8.0, Direction::Input),
"Maximum L value to use");
setPropertySettings(
"Hmin", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
setPropertySettings(
"Hmax", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
setPropertySettings(
"Kmin", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
setPropertySettings(
"Kmax", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
setPropertySettings(
"Lmin", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
setPropertySettings(
"Lmax", Kernel::make_unique<Kernel::EnabledWhenProperty>(
string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
}
/// Run the algorithm
void PredictFractionalPeaks::exec() {
IPeaksWorkspace_sptr ipeaks = getProperty("Peaks");
auto Peaks = boost::dynamic_pointer_cast<PeaksWorkspace>(ipeaks);
if (!Peaks)
throw std::invalid_argument(
"Input workspace is not a PeaksWorkspace. Type=" + ipeaks->id());
vector<double> hOffsets = getProperty("HOffset");
vector<double> kOffsets = getProperty("KOffset");
vector<double> lOffsets = getProperty("LOffset");
if (hOffsets.empty())
hOffsets.push_back(0.0);
if (kOffsets.empty())
kOffsets.push_back(0.0);
if (lOffsets.empty())
lOffsets.push_back(0.0);
bool includePeaksInRange = getProperty("IncludeAllPeaksInRange");
if (Peaks->getNumberPeaks() <= 0) {
g_log.error() << "There are No peaks in the input PeaksWorkspace\n";
return;
}
API::Sample samp = Peaks->sample();
Geometry::OrientedLattice &ol = samp.getOrientedLattice();
Geometry::Instrument_const_sptr Instr = Peaks->getInstrument();
boost::shared_ptr<IPeaksWorkspace> OutPeaks =
WorkspaceFactory::Instance().createPeaks();
OutPeaks->setInstrument(Instr);
V3D hkl;
int peakNum = 0;
int NPeaks = Peaks->getNumberPeaks();
Kernel::Matrix<double> Gon;
Gon.identityMatrix();
double Hmin = getProperty("Hmin");
double Hmax = getProperty("Hmax");
double Kmin = getProperty("Kmin");
double Kmax = getProperty("Kmax");
double Lmin = getProperty("Lmin");
double Lmax = getProperty("Lmax");
int N = NPeaks;
if (includePeaksInRange) {
N = boost::math::iround((Hmax - Hmin + 1) * (Kmax - Kmin + 1) *
(Lmax - Lmin + 1));
N = max<int>(100, N);
}
IPeak &peak0 = Peaks->getPeak(0);
int RunNumber = peak0.getRunNumber();
Gon = peak0.getGoniometerMatrix();
Progress prog(this, 0, 1, N);
if (includePeaksInRange) {
hkl[0] = Hmin;
hkl[1] = Kmin;
hkl[2] = Lmin;
} else {
hkl[0] = peak0.getH();
hkl[1] = peak0.getK();
hkl[2] = peak0.getL();
}
Kernel::DblMatrix UB = ol.getUB();
vector<vector<int>> AlreadyDonePeaks;
bool done = false;
int ErrPos = 1; // Used to determine position in code of a throw
while (!done) {
for (double hOffset : hOffsets) {
for (double kOffset : kOffsets) {
for (double lOffset : lOffsets) {
try {
V3D hkl1(hkl);
ErrPos = 0;
hkl1[0] += hOffset;
hkl1[1] += kOffset;
hkl1[2] += lOffset;
Kernel::V3D Qs = UB * hkl1;
Qs *= 2.0;
Qs *= M_PI;
Qs = Gon * Qs;
if (Qs[2] <= 0)
continue;
ErrPos = 1;
boost::shared_ptr<IPeak> peak(Peaks->createPeak(Qs, 1));
peak->setGoniometerMatrix(Gon);
if (Qs[2] > 0 && peak->findDetector()) {
ErrPos = 2;
vector<int> SavPk{RunNumber,
boost::math::iround(1000.0 * hkl1[0]),
boost::math::iround(1000.0 * hkl1[1]),
boost::math::iround(1000.0 * hkl1[2])};
// TODO keep list sorted so searching is faster?
auto it =
find(AlreadyDonePeaks.begin(), AlreadyDonePeaks.end(), SavPk);
if (it == AlreadyDonePeaks.end())
AlreadyDonePeaks.push_back(SavPk);
else
continue;
peak->setHKL(hkl1);
peak->setRunNumber(RunNumber);
OutPeaks->addPeak(*peak);
}
} catch (...) {
if (ErrPos != 1) // setQLabFrame in createPeak throws exception
throw new std::invalid_argument("Invalid data at this point");
}
}
}
}
if (includePeaksInRange) {
hkl[0]++;
if (hkl[0] > Hmax) {
hkl[0] = Hmin;
hkl[1]++;
if (hkl[1] > Kmax) {
hkl[1] = Kmin;
hkl[2]++;
if (hkl[2] > Lmax)
done = true;
}
}
} else {
peakNum++;
if (peakNum >= NPeaks)
done = true;
else { // peak0= Peaks->getPeak(peakNum);
IPeak &peak1 = Peaks->getPeak(peakNum);
//??? could not assign to peak0 above. Did not work
// the peak that peak0 was associated with did NOT change
hkl[0] = peak1.getH();
hkl[1] = peak1.getK();
hkl[2] = peak1.getL();
Gon = peak1.getGoniometerMatrix();
RunNumber = peak1.getRunNumber();
}
}
prog.report();
}
setProperty("FracPeaks", OutPeaks);
}
} // namespace Crystal
} // namespace Mantid