IndexSXPeaks.h 6.54 KB
Newer Older
1
2
#ifndef MANTID_CRYSTAL_INDEX_SX_PEAKS_H_
#define MANTID_CRYSTAL_INDEX_SX_PEAKS_H_
3
4
5
6
7
8

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include <boost/tuple/tuple.hpp>
9
#include "MantidAPI/IPeaksWorkspace_fwd.h"
10
#include "MantidGeometry/Crystal/UnitCell.h"
11
#include "MantidKernel/V3D.h"
12
13
#include <set>

14
15
16
17
namespace Mantid {
namespace Crystal {
/** Peak indexing algorithm, which works by assigning multiple possible HKL
   values to each peak and then culling these options
18
    by comparison with neighbouring peaks.
19
20
21

    @author L C Chapon, ISIS, Rutherford Appleton Laboratory
    @date 11/08/2009
22
23
    Copyright &copy; 2007-2010 ISIS Rutherford Appleton Laboratory, NScD Oak
   Ridge National Laboratory & European Spallation Source
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

    This file is part of Mantid.

    Mantid is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Mantid is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

40
    File change history is stored at: <https://github.com/mantidproject/mantid>
41
42
43
    Code Documentation is available at: <http://doxygen.mantidproject.org>
 */

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
struct DLLExport index {
  index(int h, int k, int l) : _h(h), _k(k), _l(l) {}
  friend bool operator<(const index &i1, const index &i2) {
    if (i1._h < i2._h)
      return true;
    else if (i1._h > i2._h)
      return false;
    if (i1._k < i2._k)
      return true;
    else if (i1._k > i2._k)
      return false;
    if (i1._l < i2._l)
      return true;
    else if (i1._l > i2._l)
      return false;
    return false;
  }
  friend bool operator==(const index &i1, const index &i2) {
    if (i1._h != i2._h)
      return false;
    if (i1._k != i2._k)
      return false;
    if (i1._l != i2._l)
      return false;
    return true;
  }
  friend std::ostream &operator<<(std::ostream &os, const index &rhs) {
    os << rhs._h << "," << rhs._k << "," << rhs._l;
    return os;
  }
  int _h, _k, _l;
75
76
};

77
class DLLExport PeakCandidate {
78
public:
79
80
81
82
83
84
85
  PeakCandidate(double qx, double qy, double qz) : _Q(qx, qy, qz) {}
  double getdSpacing() const { return 1 / _Q.norm(); }
  void addHKL(int h, int k, int l) { _hkls.insert(index(h, k, l)); }
  Mantid::Kernel::V3D getHKL() const {
    if (_hkls.size() != 1) {
      throw std::logic_error(
          "Expecting a single HKL value for each peak. Refinement incomplete.");
86
    }
87
88
    Kernel::V3D result =
        Kernel::V3D(_hkls.begin()->_h, _hkls.begin()->_k, _hkls.begin()->_l);
89
    return result;
90
  }
91
92
  size_t candidateHKLSize() const { return _hkls.size(); }
  void delHKL(int h, int k, int l) {
Matthew D Jones's avatar
Matthew D Jones committed
93
    auto it = std::find(_hkls.cbegin(), _hkls.cend(), index(h, k, l));
Matthew D Jones's avatar
Matthew D Jones committed
94
    if (it != _hkls.cend())
95
      _hkls.erase(it);
96
  }
97
98
99
100
101
102
103
  const Mantid::Kernel::V3D &getQ() const { return _Q; }
  double angle(const PeakCandidate &rhs) { return rhs._Q.angle(_Q); }
  void setIndex(const std::set<index> &s) {
    _hkls.clear();
    _hkls = s;
  }
  void setFirst() {
104
    if (!_hkls.empty()) {
Matthew D Jones's avatar
Matthew D Jones committed
105
      auto it = _hkls.begin(); // Take the first possiblity
106
      it++;
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
      _hkls.erase(it, _hkls.end()); // Erase all others!
    }
  }
  void clean(PeakCandidate &rhs, const Mantid::Geometry::UnitCell &uc,
             double tolerance) {
    double measured_angle = this->angle(rhs);
    std::set<index>::iterator it1, it2;
    std::set<index> s1;
    std::set<index> s2;
    for (it1 = _hkls.begin(); it1 != _hkls.end();
         it1++) // All possible vectors for hkl on current instance
    {
      for (it2 = rhs._hkls.begin(); it2 != rhs._hkls.end();
           it2++) // All possible vectors for hkl on other
      {
        const index &index1 = *it1;
        const index &index2 = *it2;
        double angle =
            uc.recAngle(index1._h, index1._k, index1._l, index2._h, index2._k,
                        index2._l, 1); // calculate angle between each fictional
                                       // primative vector on both this and
                                       // other.
        if (std::abs(angle - measured_angle) <
            tolerance) // If peak angles are the same as the dspacing angles we
                       // can say that this peak corresponds to privatve vector
                       // hkl and the other corresponds to primative vector hkl
        {
          s1.insert(index1);
          s2.insert(index2);
        }
      }
138
    }
139
140
    setIndex(s1);
    rhs.setIndex(s2);
141
  }
142
143
144
145
146
147
148
  friend std::ostream &operator<<(std::ostream &os, const PeakCandidate &rhs) {
    os << "Peak" << rhs._Q[0] << "," << rhs._Q[1] << "," << rhs._Q[2] << "\n";
    std::copy(rhs._hkls.begin(), rhs._hkls.end(),
              std::ostream_iterator<index>(os, ":"));
    return os;
  }

149
private:
150
151
  Mantid::Kernel::V3D _Q;
  std::set<index> _hkls;
152
153
};

154
class DLLExport IndexSXPeaks : public API::Algorithm {
155
156
public:
  /// Default constructor
157
  IndexSXPeaks() : API::Algorithm(){};
158
  /// Destructor
159
  ~IndexSXPeaks() override{};
160
  /// Algorithm's name for identification overriding a virtual method
161
  const std::string name() const override { return "IndexSXPeaks"; }
162
  /// Summary of algorithms purpose
163
  const std::string summary() const override {
164
165
166
167
    return "Takes a PeaksWorkspace and a B-Matrix and determines the HKL "
           "values corresponding to each Single Crystal peak. Sets indexes on "
           "the input/output workspace.";
  }
168

169
  /// Algorithm's version for identification overriding a virtual method
170
  int version() const override { return (1); }
171
  /// Algorithm's category for identification overriding a virtual method
172
  const std::string category() const override { return "Crystal\\Peaks"; }
173
174

private:
175
176
177
178
179
  // Helper method to cull potential hkls off each peak.
  void cullHKLs(std::vector<PeakCandidate> &peaksCandidates,
                Mantid::Geometry::UnitCell &unitcell);
  // Helper method used to check that not all peaks are colinear.
  void validateNotColinear(std::vector<PeakCandidate> &peakCandidates) const;
180
  // Overridden Algorithm methods
181
  void init() override;
182
  //
183
  void exec() override;
184
185
186
187
188
189
  //
};

} // namespace Algorithm
} // namespace Mantid

190
#endif /*MANTID_ALGORITHMS_IndexSXPeaks_H_*/