PredictSatellitePeaks.cpp 13.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* PredictSatellitePeaks.cpp
*
*  Created on: Dec 5, 2012
*      Author: ruth
*/
#include "MantidCrystal/PredictSatellitePeaks.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Objects/InstrumentRayTracer.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ArrayLengthValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
16
#include "MantidAPI/Run.h"
17
18
19
#include "MantidGeometry/Crystal/BasicHKLFilters.h"
#include "MantidGeometry/Crystal/HKLFilterWavelength.h"
#include "MantidGeometry/Crystal/HKLGenerator.h"
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
#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(PredictSatellitePeaks)

/// Initialise the properties
void PredictSatellitePeaks::init() {
  declareProperty(
      make_unique<WorkspaceProperty<PeaksWorkspace>>("Peaks", "",
                                                     Direction::Input),
      "Workspace of Peaks with orientation matrix that indexed the peaks and "
      "instrument loaded");

  declareProperty(
      make_unique<WorkspaceProperty<PeaksWorkspace>>("SatellitePeaks", "",
                                                     Direction::Output),
      "Workspace of Peaks with peaks with fractional h,k, and/or l values");
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
                      string("OffsetVector1"), "0.0,0.0,0.0,0"),
                  "Offsets for h, k, l directions and order");
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
                      string("OffsetVector2"), "0.0,0.0,0.0,0"),
                  "Offsets for h, k, l directions and order");
  declareProperty(Kernel::make_unique<Kernel::ArrayProperty<double>>(
                      string("OffsetVector3"), "0.0,0.0,0.0,0"),
                  "Offsets for h, k, l directions and order");

  declareProperty(
Lynch, Vickie's avatar
Lynch, Vickie committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
      "IncludeIntegerHKL", true,
      "If false order 0 peaks are not included in workspace (integer HKL)");

  declareProperty("IncludeAllPeaksInRange", false, "If false only offsets from "
                                                   "peaks from Peaks workspace "
                                                   "in input are used");

  declareProperty(make_unique<PropertyWithValue<double>>("WavelengthMin", 0.1,
                                                         Direction::Input),
                  "Minimum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
  declareProperty(make_unique<PropertyWithValue<double>>("WavelengthMax", 100.0,
                                                         Direction::Input),
                  "Maximum wavelength limit at which to start looking for "
                  "single-crystal peaks.");
  declareProperty(make_unique<PropertyWithValue<double>>("MinDSpacing", 0.1,
                                                         Direction::Input),
                  "Minimum d-spacing of peaks to consider. Default = 1.0");
  declareProperty(make_unique<PropertyWithValue<double>>("MaxDSpacing", 100.0,
                                                         Direction::Input),
                  "Maximum d-spacing of peaks to consider");
76
77

  setPropertySettings(
Lynch, Vickie's avatar
Lynch, Vickie committed
78
79
80
      "WavelengthMin",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
81
82

  setPropertySettings(
Lynch, Vickie's avatar
Lynch, Vickie committed
83
84
85
      "WavelengthMax",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
86
  setPropertySettings(
Lynch, Vickie's avatar
Lynch, Vickie committed
87
88
89
      "MinDSpacing",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
90
91

  setPropertySettings(
Lynch, Vickie's avatar
Lynch, Vickie committed
92
93
94
      "MaxDSpacing",
      Kernel::make_unique<Kernel::EnabledWhenProperty>(
          string("IncludeAllPeaksInRange"), Kernel::IS_EQUAL_TO, "1"));
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
}

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

  vector<double> offsets1 = getProperty("OffsetVector1");
  vector<double> offsets2 = getProperty("OffsetVector2");
  vector<double> offsets3 = getProperty("OffsetVector3");
  if (offsets1.empty()) {
    offsets1.push_back(0.0);
    offsets1.push_back(0.0);
    offsets1.push_back(0.0);
    offsets1.push_back(0.0);
  }
  if (offsets2.empty()) {
    offsets2.push_back(0.0);
    offsets2.push_back(0.0);
    offsets2.push_back(0.0);
    offsets2.push_back(0.0);
  }
  if (offsets3.empty()) {
    offsets3.push_back(0.0);
    offsets3.push_back(0.0);
    offsets3.push_back(0.0);
    offsets3.push_back(0.0);
Lynch, Vickie's avatar
Lynch, Vickie committed
124
  }
125
126

  bool includePeaksInRange = getProperty("IncludeAllPeaksInRange");
127
  bool includeOrderZero = getProperty("IncludeIntegerHKL");
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

  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();

  auto OutPeaks = boost::dynamic_pointer_cast<IPeaksWorkspace>(
      WorkspaceFactory::Instance().createPeaks());
  OutPeaks->setInstrument(Instr);
Lynch, Vickie's avatar
Lynch, Vickie committed
143
144
145
146
147
148
  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);
149
150
151
152
153
154
155

  V3D hkl;
  int peakNum = 0;
  const auto NPeaks = Peaks->getNumberPeaks();
  Kernel::Matrix<double> Gon;
  Gon.identityMatrix();

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

160
161
162
163
164
  std::vector<V3D> possibleHKLs;
  if (includePeaksInRange) {
    const double dMin = getProperty("MinDSpacing");
    const double dMax = getProperty("MaxDSpacing");
    Geometry::HKLGenerator gen(ol, dMin);
Lynch, Vickie's avatar
Lynch, Vickie committed
165
166
    auto filter = boost::make_shared<HKLFilterDRange>(ol, dMin, dMax);

167
168
    V3D hkl = *(gen.begin());
    g_log.information() << "HKL range for d_min of " << dMin << " to d_max of "
Lynch, Vickie's avatar
Lynch, Vickie committed
169
170
171
172
173
174
175
176
177
178
                        << dMax << " is from " << hkl << " to " << hkl * -1.0
                        << ", a total of " << gen.size() << " possible HKL's\n";
    if (gen.size() > 10000000000)
      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), (~filter)->fn());
179
180
181
182
183
184
  } else {
    hkl[0] = peak0.getH();
    hkl[1] = peak0.getK();
    hkl[2] = peak0.getL();
  }

Lynch, Vickie's avatar
Lynch, Vickie committed
185
186
  size_t N =
      NPeaks * (1 + int(offsets1[3] * 2 + offsets2[3] * 2 + offsets3[3] * 2));
187
188
189
190
191
  if (includePeaksInRange) {
    N = possibleHKLs.size();
    N = max<size_t>(100, N);
  }
  auto RunNumber = peak0.getRunNumber();
192
  const Kernel::DblMatrix &UB = ol.getUB();
193
194
  Gon = peak0.getGoniometerMatrix();
  Progress prog(this, 0.0, 1.0, N);
195
196
197
198
  vector<vector<int>> AlreadyDonePeaks;
  bool done = false;
  int ErrPos = 1; // Used to determine position in code of a throw
  Geometry::InstrumentRayTracer tracer(Peaks->getInstrument());
199
200
  DblMatrix orientedUB = Gon * UB;
  HKLFilterWavelength lambdaFilter(orientedUB, lambdaMin, lambdaMax);
Lynch, Vickie's avatar
Lynch, Vickie committed
201
  int seqNum = 0;
202
  size_t next = 0;
203
  while (!done) {
Lynch, Vickie's avatar
Lynch, Vickie committed
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
    for (int order = -static_cast<int>(offsets1[3]);
         order <= static_cast<int>(offsets1[3]); order++) {
      if (order == 0 && !includeOrderZero)
        continue; // exclude order 0
      try {
        V3D hkl1(hkl);
        ErrPos = 0;

        hkl1[0] += order * offsets1[0];
        hkl1[1] += order * offsets1[1];
        hkl1[2] += order * offsets1[2];
        if (!lambdaFilter.isAllowed(hkl1) && includePeaksInRange)
          continue;

        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(tracer)) {
          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->setPeakNumber(seqNum);
          seqNum++;
          peak->setRunNumber(RunNumber);
          peak->setModStru(V3D(order, 0, 0));
          OutPeaks->addPeak(*peak);
        }
      } catch (...) {
        if (ErrPos != 1) // setQLabFrame in createPeak throws exception
          throw std::invalid_argument("Invalid data at this point");
      }
257
    }
Lynch, Vickie's avatar
Lynch, Vickie committed
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
    for (int order = -static_cast<int>(offsets2[3]);
         order <= static_cast<int>(offsets2[3]); order++) {
      if (order == 0)
        continue; // already added with 1st vector
      try {
        V3D hkl1(hkl);
        ErrPos = 0;

        hkl1[0] += order * offsets2[0];
        hkl1[1] += order * offsets2[1];
        hkl1[2] += order * offsets2[2];

        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(tracer)) {
          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->setPeakNumber(seqNum);
          seqNum++;
          peak->setRunNumber(RunNumber);
          peak->setModStru(V3D(0, order, 0));
          OutPeaks->addPeak(*peak);
        }
      } catch (...) {
        if (ErrPos != 1) // setQLabFrame in createPeak throws exception
          throw std::invalid_argument("Invalid data at this point");
      }
309
    }
Lynch, Vickie's avatar
Lynch, Vickie committed
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
    for (int order = -static_cast<int>(offsets3[3]);
         order <= static_cast<int>(offsets3[3]); order++) {
      if (order == 0)
        continue; // already added with 1st vector
      try {
        V3D hkl1(hkl);
        ErrPos = 0;

        hkl1[0] += order * offsets3[0];
        hkl1[1] += order * offsets3[1];
        hkl1[2] += order * offsets3[2];

        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(tracer)) {
          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->setPeakNumber(seqNum);
          seqNum++;
          peak->setRunNumber(RunNumber);
          peak->setModStru(V3D(0, 0, order));
          OutPeaks->addPeak(*peak);
        }
      } catch (...) {
        if (ErrPos != 1) // setQLabFrame in createPeak throws exception
          throw std::invalid_argument("Invalid data at this point");
      }
361
362
    }
    if (includePeaksInRange) {
363
      next++;
Lynch, Vickie's avatar
Lynch, Vickie committed
364
365
      if (next == possibleHKLs.size())
        break;
366
      hkl = possibleHKLs[next];
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
    } 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("SatellitePeaks", OutPeaks);
}

} // namespace Crystal
} // namespace Mantid