PredictSatellitePeaks.cpp 18.1 KB
Newer Older
1
2
3
4
5
6
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 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 +
7
/*
Lynch, Vickie's avatar
Lynch, Vickie committed
8
9
10
11
12
 * PredictSatellitePeaks.cpp
 *
 *  Created on: July 15, 2018
 *      Author: Vickie Lynch
 */
13
#include "MantidCrystal/PredictSatellitePeaks.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
14
#include "MantidAPI/OrientedLatticeValidator.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
15
#include "MantidAPI/Run.h"
16
17
#include "MantidAPI/Sample.h"
#include "MantidAPI/WorkspaceFactory.h"
18
#include "MantidCrystal/PeakAlgorithmHelpers.h"
19
#include "MantidDataObjects/PeaksWorkspace.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
20
21
#include "MantidGeometry/Crystal/BasicHKLFilters.h"
#include "MantidGeometry/Crystal/HKLGenerator.h"
22
23
24
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Objects/InstrumentRayTracer.h"
#include "MantidKernel/ArrayLengthValidator.h"
Lynch, Vickie's avatar
Lynch, Vickie committed
25
#include "MantidKernel/ArrayProperty.h"
26
27
28
29
#include "MantidKernel/EnabledWhenProperty.h"
#include <boost/math/special_functions/round.hpp>

namespace Mantid {
Lynch, Vickie's avatar
Lynch, Vickie committed
30
31
32
33
34
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace std;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
35

Lynch, Vickie's avatar
Lynch, Vickie committed
36
37
namespace Crystal {

38
39
40
41
// handy shortcuts
using Mantid::Geometry::IPeak_uptr;
using Mantid::DataObjects::Peak_uptr;

Lynch, Vickie's avatar
Lynch, Vickie committed
42
43
DECLARE_ALGORITHM(PredictSatellitePeaks)

44
45
46
47
/** Constructor
 */

PredictSatellitePeaks::PredictSatellitePeaks()
48
    : m_qConventionFactor(qConventionFactor()) {}
49

Lynch, Vickie's avatar
Lynch, Vickie committed
50
51
/// Initialise the properties
void PredictSatellitePeaks::init() {
52
  auto latticeValidator = std::make_shared<OrientedLatticeValidator>();
Lynch, Vickie's avatar
Lynch, Vickie committed
53
  declareProperty(
54
      std::make_unique<WorkspaceProperty<PeaksWorkspace>>(
55
          "Peaks", "", Direction::Input, latticeValidator),
Lynch, Vickie's avatar
Lynch, Vickie committed
56
57
58
59
      "Workspace of Peaks with orientation matrix that indexed the peaks and "
      "instrument loaded");

  declareProperty(
60
      std::make_unique<WorkspaceProperty<PeaksWorkspace>>("SatellitePeaks", "",
Sam Jenkins's avatar
Sam Jenkins committed
61
                                                          Direction::Output),
Lynch, Vickie's avatar
Lynch, Vickie committed
62
      "Workspace of Peaks with peaks with fractional h,k, and/or l values");
63
  declareProperty(std::make_unique<Kernel::ArrayProperty<double>>(
Lynch, Vickie's avatar
Lynch, Vickie committed
64
65
                      string("ModVector1"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
66
  declareProperty(std::make_unique<Kernel::ArrayProperty<double>>(
Lynch, Vickie's avatar
Lynch, Vickie committed
67
68
                      string("ModVector2"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
69
  declareProperty(std::make_unique<Kernel::ArrayProperty<double>>(
Lynch, Vickie's avatar
Lynch, Vickie committed
70
71
72
                      string("ModVector3"), "0.0,0.0,0.0"),
                  "Offsets for h, k, l directions ");
  declareProperty(
73
      std::make_unique<PropertyWithValue<int>>("MaxOrder", 0, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
74
75
      "Maximum order to apply ModVectors. Default = 0");

Lynch, Vickie's avatar
Lynch, Vickie committed
76
  declareProperty("GetModVectorsFromUB", false,
Lynch, Vickie's avatar
Lynch, Vickie committed
77
78
                  "If false Modulation Vectors will be read from input");

79
  declareProperty(std::make_unique<PropertyWithValue<bool>>("CrossTerms", false,
Sam Jenkins's avatar
Sam Jenkins committed
80
                                                            Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
81
82
83
84
85
86
                  "Include cross terms (false)");

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

Lynch, Vickie's avatar
Lynch, Vickie committed
87
88
89
90
  declareProperty("IncludeAllPeaksInRange", false,
                  "If false only offsets from "
                  "peaks from Peaks workspace "
                  "in input are used");
Lynch, Vickie's avatar
Lynch, Vickie committed
91

Sam Jenkins's avatar
Sam Jenkins committed
92
93
  declareProperty(std::make_unique<PropertyWithValue<double>>(
                      "WavelengthMin", 0.1, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
94
95
                  "Minimum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
Sam Jenkins's avatar
Sam Jenkins committed
96
97
  declareProperty(std::make_unique<PropertyWithValue<double>>(
                      "WavelengthMax", 100.0, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
98
99
                  "Maximum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
Sam Jenkins's avatar
Sam Jenkins committed
100
101
  declareProperty(std::make_unique<PropertyWithValue<double>>(
                      "MinDSpacing", 0.1, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
102
                  "Minimum d-spacing of peaks to consider. Default = 1.0");
Sam Jenkins's avatar
Sam Jenkins committed
103
104
  declareProperty(std::make_unique<PropertyWithValue<double>>(
                      "MaxDSpacing", 100.0, Direction::Input),
Lynch, Vickie's avatar
Lynch, Vickie committed
105
106
107
108
                  "Maximum d-spacing of peaks to consider");

  setPropertySettings(
      "WavelengthMin",
109
      std::make_unique<Kernel::EnabledWhenProperty>(
Lynch, Vickie's avatar
Lynch, Vickie committed
110
111
112
113
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));

  setPropertySettings(
      "WavelengthMax",
114
      std::make_unique<Kernel::EnabledWhenProperty>(
Lynch, Vickie's avatar
Lynch, Vickie committed
115
116
117
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
  setPropertySettings(
      "MinDSpacing",
118
      std::make_unique<Kernel::EnabledWhenProperty>(
Lynch, Vickie's avatar
Lynch, Vickie committed
119
120
121
122
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));

  setPropertySettings(
      "MaxDSpacing",
123
      std::make_unique<Kernel::EnabledWhenProperty>(
Lynch, Vickie's avatar
Lynch, Vickie committed
124
125
126
127
128
129
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
}

/// Run the algorithm
void PredictSatellitePeaks::exec() {
  bool includePeaksInRange = getProperty("IncludeAllPeaksInRange");
130
131
132
133
  Peaks = getProperty("Peaks");
  if (!Peaks)
    throw std::invalid_argument(
        "Input workspace is not a PeaksWorkspace. Type=" + Peaks->id());
Lynch, Vickie's avatar
Lynch, Vickie committed
134
135
136
137
138
139
140
141
142
143
144
  if (!includePeaksInRange) {
    exec_peaks();
    return;
  }

  V3D offsets1 = getOffsetVector("ModVector1");
  V3D offsets2 = getOffsetVector("ModVector2");
  V3D offsets3 = getOffsetVector("ModVector3");
  int maxOrder = getProperty("MaxOrder");
  bool crossTerms = getProperty("CrossTerms");
  bool includeOrderZero = getProperty("IncludeIntegerHKL");
145
146
  // boolean for only including order zero once
  bool notOrderZero = false;
Lynch, Vickie's avatar
Lynch, Vickie committed
147
148
149
150
151
152
153

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

  API::Sample sample = Peaks->mutableSample();
154
  auto lattice = std::make_unique<OrientedLattice>(sample.getOrientedLattice());
Lynch, Vickie's avatar
Lynch, Vickie committed
155

156
157
  bool fromUB = getProperty("GetModVectorsFromUB");
  if (fromUB) {
158
159
160
    offsets1 = lattice->getModVec(0);
    offsets2 = lattice->getModVec(1);
    offsets3 = lattice->getModVec(2);
Lynch, Vickie's avatar
Lynch, Vickie committed
161
    if (maxOrder == 0)
162
163
      maxOrder = lattice->getMaxOrder();
    crossTerms = lattice->getCrossTerm();
164
  } else {
165
166
167
168
169
    lattice->setModVec1(offsets1);
    lattice->setModVec2(offsets2);
    lattice->setModVec3(offsets3);
    lattice->setMaxOrder(maxOrder);
    lattice->setCrossTerm(crossTerms);
Lynch, Vickie's avatar
Lynch, Vickie committed
170
171
172
173
  }

  const auto instrument = Peaks->getInstrument();

174
  outPeaks = std::dynamic_pointer_cast<IPeaksWorkspace>(
Lynch, Vickie's avatar
Lynch, Vickie committed
175
      WorkspaceFactory::Instance().createPeaks());
176
  outPeaks->setInstrument(instrument);
177
  outPeaks->mutableSample().setOrientedLattice(std::move(lattice));
Lynch, Vickie's avatar
Lynch, Vickie committed
178
179
180
181
182
183
184
185
186
187
188

  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");
189
  Geometry::HKLGenerator gen(outPeaks->sample().getOrientedLattice(), dMin);
190
  auto dSpacingFilter = std::make_shared<HKLFilterDRange>(
191
      outPeaks->sample().getOrientedLattice(), dMin, dMax);
Lynch, Vickie's avatar
Lynch, Vickie committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

  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);
208
  const auto &UB = outPeaks->sample().getOrientedLattice().getUB();
Lynch, Vickie's avatar
Lynch, Vickie committed
209
210
  goniometer = peak0.getGoniometerMatrix();
  Progress prog(this, 0.0, 1.0, N);
Lynch, Vickie's avatar
Lynch, Vickie committed
211
  vector<vector<int>> AlreadyDonePeaks;
Lynch, Vickie's avatar
Lynch, Vickie committed
212
213
  auto orientedUB = goniometer * UB;
  HKLFilterWavelength lambdaFilter(orientedUB, lambdaMin, lambdaMax);
Lynch, Vickie's avatar
Lynch, Vickie committed
214
215
216
217
218
219
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset1", offsets1,
                                                          true);
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset2", offsets2,
                                                          true);
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset3", offsets3,
                                                          true);
Lynch, Vickie's avatar
Lynch, Vickie committed
220
221
222
  for (auto it = possibleHKLs.begin(); it != possibleHKLs.end(); ++it) {
    V3D hkl = *it;
    if (crossTerms) {
Lynch, Vickie's avatar
Lynch, Vickie committed
223
224
225
      predictOffsetsWithCrossTerms(offsets1, offsets2, offsets3, maxOrder, hkl,
                                   lambdaFilter, includePeaksInRange,
                                   includeOrderZero, AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
226
    } else {
227
      predictOffsets(0, offsets1, maxOrder, hkl, lambdaFilter,
Lynch, Vickie's avatar
Lynch, Vickie committed
228
                     includePeaksInRange, includeOrderZero, AlreadyDonePeaks);
229
      predictOffsets(1, offsets2, maxOrder, hkl, lambdaFilter,
230
                     includePeaksInRange, notOrderZero, AlreadyDonePeaks);
231
      predictOffsets(2, offsets3, maxOrder, hkl, lambdaFilter,
232
                     includePeaksInRange, notOrderZero, AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
233
234
    }
  }
235
236
237
  // Sort peaks by run number so that peaks with equal goniometer matrices are
  // adjacent
  std::vector<std::pair<std::string, bool>> criteria;
238
239
240
241
242
  criteria.emplace_back("RunNumber", true);
  criteria.emplace_back("BankName", true);
  criteria.emplace_back("h", true);
  criteria.emplace_back("k", true);
  criteria.emplace_back("l", true);
243
244
245
246
247
248
  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
249
250
251
252
253
254
255
256
257
258
}

void PredictSatellitePeaks::exec_peaks() {

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

Lynch, Vickie's avatar
Lynch, Vickie committed
259
260
  API::Sample sample = Peaks->mutableSample();

261
  auto lattice = std::make_unique<OrientedLattice>(sample.getOrientedLattice());
Lynch, Vickie's avatar
Lynch, Vickie committed
262

263
264
  bool fromUB = getProperty("GetModVectorsFromUB");
  if (fromUB) {
265
266
267
    offsets1 = lattice->getModVec(0);
    offsets2 = lattice->getModVec(1);
    offsets3 = lattice->getModVec(2);
Lynch, Vickie's avatar
Lynch, Vickie committed
268
    if (maxOrder == 0)
269
270
      maxOrder = lattice->getMaxOrder();
    crossTerms = lattice->getCrossTerm();
271
  } else {
272
273
274
275
276
    lattice->setModVec1(offsets1);
    lattice->setModVec2(offsets2);
    lattice->setModVec3(offsets3);
    lattice->setMaxOrder(maxOrder);
    lattice->setCrossTerm(crossTerms);
Lynch, Vickie's avatar
Lynch, Vickie committed
277
278
  }

Lynch, Vickie's avatar
Lynch, Vickie committed
279
280
  bool includePeaksInRange = false;
  bool includeOrderZero = getProperty("IncludeIntegerHKL");
281
282
  // boolean for only including order zero once
  bool notOrderZero = false;
Lynch, Vickie's avatar
Lynch, Vickie committed
283
284
285
286
287
288
289
290

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

  const auto instrument = Peaks->getInstrument();

291
  outPeaks = std::dynamic_pointer_cast<IPeaksWorkspace>(
Lynch, Vickie's avatar
Lynch, Vickie committed
292
      WorkspaceFactory::Instance().createPeaks());
293
  outPeaks->setInstrument(instrument);
294
  outPeaks->mutableSample().setOrientedLattice(std::move(lattice));
Lynch, Vickie's avatar
Lynch, Vickie committed
295

Lynch, Vickie's avatar
Lynch, Vickie committed
296
  vector<vector<int>> AlreadyDonePeaks;
Lynch, Vickie's avatar
Lynch, Vickie committed
297
  HKLFilterWavelength lambdaFilter(DblMatrix(3, 3, true), 0.1, 100.);
Lynch, Vickie's avatar
Lynch, Vickie committed
298
299
300
301
302
303
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset1", offsets1,
                                                          true);
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset2", offsets2,
                                                          true);
  outPeaks->mutableRun().addProperty<std::vector<double>>("Offset3", offsets3,
                                                          true);
Lynch, Vickie's avatar
Lynch, Vickie committed
304
305
306
307
308
  std::vector<Peak> peaks = Peaks->getPeaks();
  for (auto it = peaks.begin(); it != peaks.end(); ++it) {
    auto peak = *it;
    V3D hkl = peak.getHKL();
    if (crossTerms) {
Lynch, Vickie's avatar
Lynch, Vickie committed
309
310
311
      predictOffsetsWithCrossTerms(offsets1, offsets2, offsets3, maxOrder, hkl,
                                   lambdaFilter, includePeaksInRange,
                                   includeOrderZero, AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
312
    } else {
313
      predictOffsets(0, offsets1, maxOrder, hkl, lambdaFilter,
Lynch, Vickie's avatar
Lynch, Vickie committed
314
                     includePeaksInRange, includeOrderZero, AlreadyDonePeaks);
315
316
317
318
      predictOffsets(1, offsets2, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, notOrderZero, AlreadyDonePeaks);
      predictOffsets(2, offsets3, maxOrder, hkl, lambdaFilter,
                     includePeaksInRange, notOrderZero, AlreadyDonePeaks);
Lynch, Vickie's avatar
Lynch, Vickie committed
319
320
    }
  }
321
322
323
  // Sort peaks by run number so that peaks with equal goniometer matrices are
  // adjacent
  std::vector<std::pair<std::string, bool>> criteria;
324
325
326
327
328
  criteria.emplace_back("RunNumber", true);
  criteria.emplace_back("BankName", true);
  criteria.emplace_back("h", true);
  criteria.emplace_back("k", true);
  criteria.emplace_back("l", true);
329
330
331
332
333
334
335
  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
336
337
338
}

void PredictSatellitePeaks::predictOffsets(
Lynch, Vickie's avatar
Lynch, Vickie committed
339
340
341
    int indexModulatedVector, V3D offsets, int &maxOrder, V3D &hkl,
    HKLFilterWavelength &lambdaFilter, bool &includePeaksInRange,
    bool &includeOrderZero, vector<vector<int>> &AlreadyDonePeaks) {
342
  if (offsets == V3D(0, 0, 0) && !includeOrderZero)
Lynch, Vickie's avatar
Lynch, Vickie committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    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;

Lynch, Vickie's avatar
Lynch, Vickie committed
358
359
    Kernel::V3D Qs =
        goniometer * UB * satelliteHKL * 2.0 * M_PI * m_qConventionFactor;
Lynch, Vickie's avatar
Lynch, Vickie committed
360
361

    // Check if Q is non-physical
362
    if (Qs.Z() * m_qConventionFactor <= 0)
Lynch, Vickie's avatar
Lynch, Vickie committed
363
      continue;
Lynch, Vickie's avatar
Lynch, Vickie committed
364

365
366
    IPeak_uptr iPeak = Peaks->createPeak(Qs, 1);
    Peak_uptr peak(static_cast<DataObjects::Peak*>(iPeak.release()));
Lynch, Vickie's avatar
Lynch, Vickie committed
367
368
369
370
371

    peak->setGoniometerMatrix(goniometer);

    if (!peak->findDetector(tracer))
      continue;
Lynch, Vickie's avatar
Lynch, Vickie committed
372
373
374
    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
375
376
377
378
379

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

    if (!foundPeak) {
380
      AlreadyDonePeaks.emplace_back(SavPk);
Lynch, Vickie's avatar
Lynch, Vickie committed
381
382
383
384
    } else {
      continue;
    }

385
386
    peak->setHKL(satelliteHKL * m_qConventionFactor);
    peak->setIntHKL(hkl * m_qConventionFactor);
Lynch, Vickie's avatar
Lynch, Vickie committed
387
388
    peak->setRunNumber(RunNumber);
    V3D mnp;
389
    mnp[indexModulatedVector] = order;
390
    peak->setIntMNP(mnp * m_qConventionFactor);
391
    outPeaks->addPeak(*peak);
Lynch, Vickie's avatar
Lynch, Vickie committed
392
393
394
395
  }
}

void PredictSatellitePeaks::predictOffsetsWithCrossTerms(
Lynch, Vickie's avatar
Lynch, Vickie committed
396
    V3D offsets1, V3D offsets2, V3D offsets3, int &maxOrder, V3D &hkl,
Lynch, Vickie's avatar
Lynch, Vickie committed
397
    HKLFilterWavelength &lambdaFilter, bool &includePeaksInRange,
Lynch, Vickie's avatar
Lynch, Vickie committed
398
    bool &includeOrderZero, vector<vector<int>> &AlreadyDonePeaks) {
Lynch, Vickie's avatar
Lynch, Vickie committed
399
  if (offsets1 == V3D(0, 0, 0) && offsets2 == V3D(0, 0, 0) &&
400
      offsets3 == V3D(0, 0, 0) && !includeOrderZero)
Lynch, Vickie's avatar
Lynch, Vickie committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    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;

Lynch, Vickie's avatar
Lynch, Vickie committed
431
432
        Kernel::V3D Qs =
            goniometer * UB * satelliteHKL * 2.0 * M_PI * m_qConventionFactor;
Lynch, Vickie's avatar
Lynch, Vickie committed
433
434

        // Check if Q is non-physical
435
        if (Qs.Z() * m_qConventionFactor <= 0)
Lynch, Vickie's avatar
Lynch, Vickie committed
436
437
          continue;

438
439
        IPeak_uptr iPeak(Peaks->createPeak(Qs, 1));
        Peak_uptr peak(static_cast<DataObjects::Peak*>(iPeak.release()));
Lynch, Vickie's avatar
Lynch, Vickie committed
440
441
442
443
444

        peak->setGoniometerMatrix(goniometer);

        if (!peak->findDetector(tracer))
          continue;
Lynch, Vickie's avatar
Lynch, Vickie committed
445
446
447
448
        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
449
450
451
452
453

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

        if (!foundPeak) {
454
          AlreadyDonePeaks.emplace_back(SavPk);
Lynch, Vickie's avatar
Lynch, Vickie committed
455
456
        } else {
          continue;
457
458
        }

459
460
        peak->setHKL(satelliteHKL * m_qConventionFactor);
        peak->setIntHKL(hkl * m_qConventionFactor);
Lynch, Vickie's avatar
Lynch, Vickie committed
461
        peak->setRunNumber(RunNumber);
462
        peak->setIntMNP(V3D(m, n, p) * m_qConventionFactor);
463
        outPeaks->addPeak(*peak);
Lynch, Vickie's avatar
Lynch, Vickie committed
464
465
466
467
468
469
      }
}

V3D PredictSatellitePeaks::getOffsetVector(const std::string &label) {
  vector<double> offsets = getProperty(label);
  if (offsets.empty()) {
470
471
472
    offsets.emplace_back(0.0);
    offsets.emplace_back(0.0);
    offsets.emplace_back(0.0);
Lynch, Vickie's avatar
Lynch, Vickie committed
473
474
475
476
477
478
479
  }
  V3D offsets1 = V3D(offsets[0], offsets[1], offsets[2]);
  return offsets1;
}

} // namespace Crystal
} // namespace Mantid