PredictSatellitePeaks.cpp 17.2 KB
Newer Older
1
/*
Lynch, Vickie's avatar
Lynch, Vickie committed
2
3
4
5
6
 * PredictSatellitePeaks.cpp
 *
 *  Created on: July 15, 2018
 *      Author: Vickie Lynch
 */
7
#include "MantidCrystal/PredictSatellitePeaks.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
8
#include "MantidAPI/Run.h"
9
#include "MantidAPI/Sample.h"
10
#include "MantidAPI/OrientedLatticeValidator.h"
11
12
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
13
14
#include "MantidGeometry/Crystal/BasicHKLFilters.h"
#include "MantidGeometry/Crystal/HKLGenerator.h"
15
16
17
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Objects/InstrumentRayTracer.h"
#include "MantidKernel/ArrayLengthValidator.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
18
#include "MantidKernel/ArrayProperty.h"
19
20
21
22
#include "MantidKernel/EnabledWhenProperty.h"
#include <boost/math/special_functions/round.hpp>

namespace Mantid {
Lynch, Vickie's avatar
Lynch, Vickie committed
23
24
25
26
27
28
29
30
31
32
33
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace std;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
namespace Crystal {

DECLARE_ALGORITHM(PredictSatellitePeaks)

/// Initialise the properties
void PredictSatellitePeaks::init() {
34
  auto latticeValidator = boost::make_shared<OrientedLatticeValidator>();
Lynch, Vickie's avatar
Lynch, Vickie committed
35
  declareProperty(
36
37
      make_unique<WorkspaceProperty<PeaksWorkspace> >(
          "Peaks", "", Direction::Input, latticeValidator),
Lynch, Vickie's avatar
Lynch, Vickie committed
38
39
40
41
      "Workspace of Peaks with orientation matrix that indexed the peaks and "
      "instrument loaded");

  declareProperty(
42
43
      make_unique<WorkspaceProperty<PeaksWorkspace> >("SatellitePeaks", "",
                                                      Direction::Output),
Lynch, Vickie's avatar
Lynch, Vickie committed
44
      "Workspace of Peaks with peaks with fractional h,k, and/or l values");
45
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double> >(
Lynch, Vickie's avatar
Lynch, Vickie committed
46
47
                      string("ModVector1"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
48
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double> >(
Lynch, Vickie's avatar
Lynch, Vickie committed
49
50
                      string("ModVector2"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
51
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double> >(
Lynch, Vickie's avatar
Lynch, Vickie committed
52
53
54
                      string("ModVector3"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
  declareProperty(
55
      make_unique<PropertyWithValue<int> >("MaxOrder", 0, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
56
57
58
59
60
      "Maximum order to apply ModVectors. Default = 0");

  declareProperty("GetModVectorsFromUB", true,
                  "If false Modulation Vectors will be read from input");

61
62
  declareProperty(make_unique<PropertyWithValue<bool> >("CrossTerms", false,
                                                        Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
63
64
65
66
67
68
                  "Include cross terms (false)");

  declareProperty(
      "IncludeIntegerHKL", true,
      "If false order 0 peaks are not included in workspace (integer HKL)");

69
70
71
  declareProperty("IncludeAllPeaksInRange", false, "If false only offsets from "
                                                   "peaks from Peaks workspace "
                                                   "in input are used");
Lynch, Vickie's avatar
Lynch, Vickie committed
72

73
74
  declareProperty(make_unique<PropertyWithValue<double> >("WavelengthMin", 0.1,
                                                          Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
75
76
                  "Minimum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
77
78
  declareProperty(make_unique<PropertyWithValue<double> >(
                      "WavelengthMax", 100.0, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
79
80
                  "Maximum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
81
82
  declareProperty(make_unique<PropertyWithValue<double> >("MinDSpacing", 0.1,
                                                          Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
83
                  "Minimum d-spacing of peaks to consider. Default = 1.0");
84
85
  declareProperty(make_unique<PropertyWithValue<double> >("MaxDSpacing", 100.0,
                                                          Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
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
                  "Maximum d-spacing of peaks to consider");

  setPropertySettings(
      "WavelengthMin",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));

  setPropertySettings(
      "WavelengthMax",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
  setPropertySettings(
      "MinDSpacing",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));

  setPropertySettings(
      "MaxDSpacing",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
}

/// Run the algorithm
void PredictSatellitePeaks::exec() {
  bool includePeaksInRange = getProperty("IncludeAllPeaksInRange");
  if (!includePeaksInRange) {
    exec_peaks();
    return;
  }
  PeaksWorkspace_sptr Peaks = getProperty("Peaks");
  if (!Peaks)
    throw std::invalid_argument(
        "Input workspace is not a PeaksWorkspace. Type=" + Peaks->id());

  V3D offsets1 = getOffsetVector("ModVector1");
  V3D offsets2 = getOffsetVector("ModVector2");
  V3D offsets3 = getOffsetVector("ModVector3");
  int maxOrder = getProperty("MaxOrder");
  bool crossTerms = getProperty("CrossTerms");
  bool includeOrderZero = getProperty("IncludeIntegerHKL");

  if (Peaks->getNumberPeaks() <= 0) {
    g_log.error() << "There are No peaks in the input PeaksWorkspace\n";
    return;
  }

  API::Sample sample = Peaks->mutableSample();

  OrientedLattice lattice = sample.getOrientedLattice();

  if (getProperty("GetModVectorsFromUB")) {
    offsets1 = lattice.getModVec(1);
    offsets2 = lattice.getModVec(2);
    offsets3 = lattice.getModVec(3);
    if (maxOrder == 0)
      maxOrder = lattice.getMaxOrder();
  }

  const auto instrument = Peaks->getInstrument();

146
  auto outPeaks = boost::dynamic_pointer_cast<IPeaksWorkspace>(
Lynch, Vickie's avatar
Lynch, Vickie committed
147
      WorkspaceFactory::Instance().createPeaks());
148
149
  outPeaks->setInstrument(instrument);
  outPeaks->mutableSample().setOrientedLattice(&lattice);
Lynch, Vickie's avatar
Lynch, Vickie committed
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

  Kernel::Matrix<double> goniometer;
  goniometer.identityMatrix();

  const double lambdaMin = getProperty("WavelengthMin");
  const double lambdaMax = getProperty("WavelengthMax");
  IPeak &peak0 = Peaks->getPeak(0);

  std::vector<V3D> possibleHKLs;
  const double dMin = getProperty("MinDSpacing");
  const double dMax = getProperty("MaxDSpacing");
  Geometry::HKLGenerator gen(lattice, dMin);
  auto dSpacingFilter =
      boost::make_shared<HKLFilterDRange>(lattice, dMin, dMax);

  V3D hkl = *(gen.begin());
  g_log.information() << "HKL range for d_min of " << dMin << " to d_max of "
                      << dMax << " is from " << hkl << " to " << hkl * -1.0
                      << ", a total of " << gen.size() << " possible HKL's\n";
  if (gen.size() > MAX_NUMBER_HKLS)
    throw std::invalid_argument("More than 10 billion HKLs to search. Is "
                                "your d_min value too small?");

  possibleHKLs.clear();
  possibleHKLs.reserve(gen.size());
  std::remove_copy_if(gen.begin(), gen.end(), std::back_inserter(possibleHKLs),
                      (~dSpacingFilter)->fn());

  size_t N = possibleHKLs.size();
  N = max<size_t>(100, N);
  auto &UB = lattice.getUB();
  goniometer = peak0.getGoniometerMatrix();
  Progress prog(this, 0.0, 1.0, N);
183
  vector<vector<int> > AlreadyDonePeaks;
Lynch, Vickie's avatar
Lynch, Vickie committed
184
185
  auto orientedUB = goniometer * UB;
  HKLFilterWavelength lambdaFilter(orientedUB, lambdaMin, lambdaMax);
186
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset1", offsets1,
187
                                                           true);
188
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset2", offsets2,
189
                                                           true);
190
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset3", offsets3,
191
                                                           true);
Lynch, Vickie's avatar
Lynch, Vickie committed
192
193
194
  for (auto it = possibleHKLs.begin(); it != possibleHKLs.end(); ++it) {
    V3D hkl = *it;
    if (crossTerms) {
195
      predictOffsetsWithCrossTerms(Peaks, outPeaks, offsets1, offsets2,
Lynch, Vickie's avatar
Lynch, Vickie committed
196
197
                                   offsets3, maxOrder, hkl, lambdaFilter,
                                   includePeaksInRange, includeOrderZero,
198
                                   AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
199
    } else {
200
201
      predictOffsets(Peaks, outPeaks, 0, offsets1, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, includeOrderZero,
Lynch, Vickie's avatar
Lynch, Vickie committed
202
                     AlreadyDonePeaks);
203
204
      predictOffsets(Peaks, outPeaks, 1, offsets2, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, includeOrderZero,
Lynch, Vickie's avatar
Lynch, Vickie committed
205
                     AlreadyDonePeaks);
206
207
      predictOffsets(Peaks, outPeaks, 2, offsets3, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, includeOrderZero,
Lynch, Vickie's avatar
Lynch, Vickie committed
208
209
210
                     AlreadyDonePeaks);
    }
  }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  // Sort peaks by run number so that peaks with equal goniometer matrices are
  // adjacent
  std::vector<std::pair<std::string, bool>> criteria;
  criteria.push_back(std::pair<std::string, bool>("RunNumber", true));
  criteria.push_back(std::pair<std::string, bool>("BankName", true));
  criteria.push_back(std::pair<std::string, bool>("h", true));
  criteria.push_back(std::pair<std::string, bool>("k", true));
  criteria.push_back(std::pair<std::string, bool>("l", true));
  outPeaks->sort(criteria);

  for (int i = 0; i < static_cast<int>(outPeaks->getNumberPeaks()); ++i) {
    outPeaks->getPeak(i).setPeakNumber(i);
  }
  setProperty("SatellitePeaks", outPeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
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
}

void PredictSatellitePeaks::exec_peaks() {
  PeaksWorkspace_sptr Peaks = getProperty("Peaks");
  if (!Peaks)
    throw std::invalid_argument(
        "Input workspace is not a PeaksWorkspace. Type=" + Peaks->id());

  V3D offsets1 = getOffsetVector("ModVector1");
  V3D offsets2 = getOffsetVector("ModVector2");
  V3D offsets3 = getOffsetVector("ModVector3");
  int maxOrder = getProperty("MaxOrder");
  bool crossTerms = getProperty("CrossTerms");

  bool includePeaksInRange = false;
  bool includeOrderZero = getProperty("IncludeIntegerHKL");

  if (Peaks->getNumberPeaks() <= 0) {
    g_log.error() << "There are No peaks in the input PeaksWorkspace\n";
    return;
  }

  API::Sample sample = Peaks->mutableSample();

  OrientedLattice lattice = sample.getOrientedLattice();

  const auto instrument = Peaks->getInstrument();

253
  auto outPeaks = boost::dynamic_pointer_cast<IPeaksWorkspace>(
Lynch, Vickie's avatar
Lynch, Vickie committed
254
      WorkspaceFactory::Instance().createPeaks());
255
256
  outPeaks->setInstrument(instrument);
  outPeaks->mutableSample().setOrientedLattice(&lattice);
Lynch, Vickie's avatar
Lynch, Vickie committed
257

258
  vector<vector<int> > AlreadyDonePeaks;
Lynch, Vickie's avatar
Lynch, Vickie committed
259
  HKLFilterWavelength lambdaFilter(DblMatrix(3, 3, true), 0.1, 100.);
260
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset1", offsets1,
261
                                                           true);
262
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset2", offsets2,
263
                                                           true);
264
  outPeaks->mutableRun().addProperty<std::vector<double> >("Offset3", offsets3,
265
                                                           true);
Lynch, Vickie's avatar
Lynch, Vickie committed
266
267
268
269
270
  std::vector<Peak> peaks = Peaks->getPeaks();
  for (auto it = peaks.begin(); it != peaks.end(); ++it) {
    auto peak = *it;
    V3D hkl = peak.getHKL();
    if (crossTerms) {
271
      predictOffsetsWithCrossTerms(Peaks, outPeaks, offsets1, offsets2,
Lynch, Vickie's avatar
Lynch, Vickie committed
272
273
                                   offsets3, maxOrder, hkl, lambdaFilter,
                                   includePeaksInRange, includeOrderZero,
274
                                   AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
275
    } else {
276
277
      predictOffsets(Peaks, outPeaks, 0, offsets1, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, includeOrderZero,
Lynch, Vickie's avatar
Lynch, Vickie committed
278
                     AlreadyDonePeaks);
279
280
281
282
      predictOffsets(Peaks, outPeaks, 1, offsets2, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, false, AlreadyDonePeaks);
      predictOffsets(Peaks, outPeaks, 2, offsets3, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, false, AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
283
284
    }
  }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  // Sort peaks by run number so that peaks with equal goniometer matrices are
  // adjacent
  std::vector<std::pair<std::string, bool>> criteria;
  criteria.push_back(std::pair<std::string, bool>("RunNumber", true));
  criteria.push_back(std::pair<std::string, bool>("BankName", true));
  criteria.push_back(std::pair<std::string, bool>("h", true));
  criteria.push_back(std::pair<std::string, bool>("k", true));
  criteria.push_back(std::pair<std::string, bool>("l", true));
  outPeaks->sort(criteria);

  for (int i = 0; i < static_cast<int>(outPeaks->getNumberPeaks()); ++i) {
    outPeaks->getPeak(i).setPeakNumber(i);
  }

  setProperty("SatellitePeaks", outPeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
300
301
302
303
}

void PredictSatellitePeaks::predictOffsets(
    DataObjects::PeaksWorkspace_sptr Peaks,
304
    boost::shared_ptr<Mantid::API::IPeaksWorkspace> &outPeaks, int iVector,
Lynch, Vickie's avatar
Lynch, Vickie committed
305
    V3D offsets, int &maxOrder, V3D &hkl, HKLFilterWavelength &lambdaFilter,
306
    bool &includePeaksInRange, bool includeOrderZero,
307
    vector<vector<int> > &AlreadyDonePeaks) {
Lynch, Vickie's avatar
Lynch, Vickie committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  if (offsets == V3D(0, 0, 0))
    return;
  const Kernel::DblMatrix &UB = Peaks->sample().getOrientedLattice().getUB();
  IPeak &peak1 = Peaks->getPeak(0);
  Kernel::Matrix<double> goniometer = peak1.getGoniometerMatrix();
  auto RunNumber = peak1.getRunNumber();
  Geometry::InstrumentRayTracer tracer(Peaks->getInstrument());
  for (int order = -maxOrder; order <= maxOrder; order++) {
    if (order == 0 && !includeOrderZero)
      continue; // exclude order 0
    V3D satelliteHKL(hkl);

    satelliteHKL += offsets * order;
    if (!lambdaFilter.isAllowed(satelliteHKL) && includePeaksInRange)
      continue;

    Kernel::V3D Qs = goniometer * UB * satelliteHKL * 2.0 * M_PI;

    // Check if Q is non-physical
    if (Qs[2] <= 0)
      continue;

    auto peak(Peaks->createPeak(Qs, 1));

    peak->setGoniometerMatrix(goniometer);

    if (!peak->findDetector(tracer))
      continue;
336
337
338
339
    vector<int> SavPk{ RunNumber,
                       boost::math::iround(1000.0 * satelliteHKL[0]),
                       boost::math::iround(1000.0 * satelliteHKL[1]),
                       boost::math::iround(1000.0 * satelliteHKL[2]) };
Lynch, Vickie's avatar
Lynch, Vickie committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

    bool foundPeak =
        binary_search(AlreadyDonePeaks.begin(), AlreadyDonePeaks.end(), SavPk);

    if (!foundPeak) {
      AlreadyDonePeaks.push_back(SavPk);
    } else {
      continue;
    }

    peak->setHKL(satelliteHKL);
    peak->setIntHKL(hkl);
    peak->setRunNumber(RunNumber);
    V3D mnp;
    mnp[iVector] = order;
    peak->setIntMNP(mnp);
356
    outPeaks->addPeak(*peak);
Lynch, Vickie's avatar
Lynch, Vickie committed
357
358
359
360
361
  }
}

void PredictSatellitePeaks::predictOffsetsWithCrossTerms(
    DataObjects::PeaksWorkspace_sptr Peaks,
362
    boost::shared_ptr<Mantid::API::IPeaksWorkspace> &outPeaks, V3D offsets1,
Lynch, Vickie's avatar
Lynch, Vickie committed
363
364
    V3D offsets2, V3D offsets3, int &maxOrder, V3D &hkl,
    HKLFilterWavelength &lambdaFilter, bool &includePeaksInRange,
365
    bool &includeOrderZero,
366
    vector<vector<int> > &AlreadyDonePeaks) {
Lynch, Vickie's avatar
Lynch, Vickie committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  if (offsets1 == V3D(0, 0, 0) && offsets2 == V3D(0, 0, 0) &&
      offsets3 == V3D(0, 0, 0))
    return;
  const Kernel::DblMatrix &UB = Peaks->sample().getOrientedLattice().getUB();
  IPeak &peak1 = Peaks->getPeak(0);
  Kernel::Matrix<double> goniometer = peak1.getGoniometerMatrix();
  auto RunNumber = peak1.getRunNumber();
  Geometry::InstrumentRayTracer tracer(Peaks->getInstrument());
  DblMatrix offsetsMat(3, 3);
  offsetsMat.setColumn(0, offsets1);
  offsetsMat.setColumn(1, offsets2);
  offsetsMat.setColumn(2, offsets3);
  int maxOrder1 = maxOrder;
  if (offsets1 == V3D(0, 0, 0))
    maxOrder1 = 0;
  int maxOrder2 = maxOrder;
  if (offsets2 == V3D(0, 0, 0))
    maxOrder2 = 0;
  int maxOrder3 = maxOrder;
  if (offsets3 == V3D(0, 0, 0))
    maxOrder3 = 0;
  for (int m = -maxOrder1; m <= maxOrder1; m++)
    for (int n = -maxOrder2; n <= maxOrder2; n++)
      for (int p = -maxOrder3; p <= maxOrder3; p++) {
        if (m == 0 && n == 0 && p == 0 && !includeOrderZero)
          continue; // exclude 0,0,0
        V3D satelliteHKL(hkl);
        V3D mnp = V3D(m, n, p);
        satelliteHKL -= offsetsMat * mnp;
        if (!lambdaFilter.isAllowed(satelliteHKL) && includePeaksInRange)
          continue;

        Kernel::V3D Qs = goniometer * UB * satelliteHKL * 2.0 * M_PI;

        // Check if Q is non-physical
        if (Qs[2] <= 0)
          continue;

        auto peak(Peaks->createPeak(Qs, 1));

        peak->setGoniometerMatrix(goniometer);

        if (!peak->findDetector(tracer))
          continue;
411
412
413
414
        vector<int> SavPk{ RunNumber,
                           boost::math::iround(1000.0 * satelliteHKL[0]),
                           boost::math::iround(1000.0 * satelliteHKL[1]),
                           boost::math::iround(1000.0 * satelliteHKL[2]) };
Lynch, Vickie's avatar
Lynch, Vickie committed
415
416
417
418
419
420
421
422

        bool foundPeak = binary_search(AlreadyDonePeaks.begin(),
                                       AlreadyDonePeaks.end(), SavPk);

        if (!foundPeak) {
          AlreadyDonePeaks.push_back(SavPk);
        } else {
          continue;
423
424
        }

Lynch, Vickie's avatar
Lynch, Vickie committed
425
426
427
428
        peak->setHKL(satelliteHKL);
        peak->setIntHKL(hkl);
        peak->setRunNumber(RunNumber);
        peak->setIntMNP(V3D(m, n, p));
429
        outPeaks->addPeak(*peak);
Lynch, Vickie's avatar
Lynch, Vickie committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
      }
}

V3D PredictSatellitePeaks::getOffsetVector(const std::string &label) {
  vector<double> offsets = getProperty(label);
  if (offsets.empty()) {
    offsets.push_back(0.0);
    offsets.push_back(0.0);
    offsets.push_back(0.0);
  }
  V3D offsets1 = V3D(offsets[0], offsets[1], offsets[2]);
  return offsets1;
}

} // namespace Crystal
} // namespace Mantid