Mantid
Loading...
Searching...
No Matches
FindUBUsingMinMaxD.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include "MantidAPI/Sample.h"
15
16namespace Mantid::Crystal {
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(FindUBUsingMinMaxD)
19
20using namespace Mantid::Kernel;
21using namespace Mantid::API;
22using namespace Mantid::DataObjects;
23using namespace Mantid::Geometry;
24
28 useAlgorithm("FindUBUsingFFT");
29 deprecatedDate("2013-06-03");
30}
31
32const std::string FindUBUsingMinMaxD::name() const { return "FindUBUsingMinMaxD"; }
33
34int FindUBUsingMinMaxD::version() const { return 1; }
35
36const std::string FindUBUsingMinMaxD::category() const { return "Crystal\\UBMatrix"; }
37
41 this->declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("PeaksWorkspace", "", Direction::InOut),
42 "Input Peaks Workspace");
43
44 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
45 mustBePositive->setLower(0.0);
46
47 auto atLeast3Int = std::make_shared<BoundedValidator<int>>();
48 atLeast3Int->setLower(3);
49
50 // use negative values, force user to input all parameters
51 this->declareProperty(std::make_unique<PropertyWithValue<double>>("MinD", -1.0, mustBePositive, Direction::Input),
52 "Lower Bound on Lattice Parameters a, b, c");
53
54 this->declareProperty(std::make_unique<PropertyWithValue<double>>("MaxD", -1.0, mustBePositive, Direction::Input),
55 "Upper Bound on Lattice Parameters a, b, c");
56
57 this->declareProperty(std::make_unique<PropertyWithValue<int>>("NumInitial", 20, atLeast3Int, Direction::Input),
58 "Number of Peaks to Use on First Pass(20)");
59
60 this->declareProperty(
61 std::make_unique<PropertyWithValue<double>>("Tolerance", 0.15, mustBePositive, Direction::Input),
62 "Indexing Tolerance (0.15)");
63}
64
65//--------------------------------------------------------------------------
69 double min_d = this->getProperty("MinD");
70 double max_d = this->getProperty("MaxD");
71 int num_initial = this->getProperty("NumInitial");
72 double tolerance = this->getProperty("Tolerance");
73
74 int base_index = -1; // these "could" be properties if need be
75 double degrees_per_step = 1;
76
77 IPeaksWorkspace_sptr ws = this->getProperty("PeaksWorkspace");
78 if (!ws)
79 throw std::runtime_error("Could not read the peaks workspace");
80
81 int n_peaks = ws->getNumberPeaks();
82
83 std::vector<V3D> q_vectors;
84 q_vectors.reserve(n_peaks);
85
86 for (int i = 0; i < n_peaks; i++)
87 q_vectors.emplace_back(ws->getPeak(i).getQSampleFrame());
88
89 Matrix<double> UB(3, 3, false);
90 double error =
91 IndexingUtils::Find_UB(UB, q_vectors, min_d, max_d, tolerance, base_index, num_initial, degrees_per_step);
92
93 std::cout << "Error = " << error << '\n';
94 std::cout << "UB = " << UB << '\n';
95
96 if (!IndexingUtils::CheckUB(UB)) // UB not found correctly
97 {
98 g_log.notice(std::string("Found Invalid UB...peaks used might not be linearly independent"));
99 g_log.notice(std::string("UB NOT SAVED."));
100 } else // tell user how many would be indexed
101 { // and save the UB in the sample
102
103 std::vector<double> sigabc(7);
104 std::vector<V3D> miller_ind;
105 std::vector<V3D> indexed_qs;
106 double fit_error;
107 miller_ind.reserve(q_vectors.size());
108 indexed_qs.reserve(q_vectors.size());
109 IndexingUtils::GetIndexedPeaks(UB, q_vectors, tolerance, miller_ind, indexed_qs, fit_error);
110
111 IndexingUtils::Optimize_UB(UB, miller_ind, indexed_qs, sigabc);
112 char logInfo[200];
113 int num_indexed = IndexingUtils::NumberIndexed(UB, q_vectors, tolerance);
114 sprintf(logInfo, std::string("New UB will index %1d Peaks out of %1d with tolerance %5.3f").c_str(), num_indexed,
115 n_peaks, tolerance);
116 g_log.notice(std::string(logInfo));
117
118 auto lattice = std::make_unique<OrientedLattice>();
119 lattice->setUB(UB);
120 lattice->setError(sigabc[0], sigabc[1], sigabc[2], sigabc[3], sigabc[4], sigabc[5]);
121 // Show the modified lattice parameters
122 g_log.notice() << *lattice << "\n";
123 ws->mutableSample().setOrientedLattice(std::move(lattice));
124 }
125}
126
127} // namespace Mantid::Crystal
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double error
Definition: IndexPeaks.cpp:133
double tolerance
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void deprecatedDate(const std::string &)
The date the algorithm was deprecated on.
void useAlgorithm(const std::string &, const int version=-1)
The algorithm to use instead of this one.
A property class for workspaces.
const std::string category() const override
Algorithm's category for identification.
void init() override
Initialise the properties.
int version() const override
Algorithm's version for identification.
const std::string name() const override
Algorithm's name for identification.
void exec() override
Run the algorithm.
static int GetIndexedPeaks(const Kernel::DblMatrix &UB, const std::vector< Kernel::V3D > &q_vectors, double required_tolerance, std::vector< Kernel::V3D > &miller_indices, std::vector< Kernel::V3D > &indexed_qs, double &fit_error)
Get lists of indices and Qs for peaks indexed by the specified UB matrix.
static double Find_UB(Kernel::DblMatrix &UB, const std::vector< Kernel::V3D > &q_vectors, OrientedLattice &lattice, double required_tolerance, int base_index, size_t num_initial, double degrees_per_step, bool fixAll=false, int iterations=1)
Find the UB matrix that most nearly indexes the specified qxyz values given the lattice parameters.
static bool CheckUB(const Kernel::DblMatrix &UB)
Check that the specified UB is reasonable for an orientation matrix.
static int NumberIndexed(const Kernel::DblMatrix &UB, const std::vector< Kernel::V3D > &q_vectors, double tolerance)
Calculate the number of Q vectors that are mapped to integer indices by UB.
static double Optimize_UB(Kernel::DblMatrix &UB, const std::vector< Kernel::V3D > &hkl_vectors, const std::vector< Kernel::V3D > &q_vectors, std::vector< double > &sigabc)
Find the UB matrix that most nearly maps hkl to qxyz for 3 or more peaks.
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
Numerical Matrix class.
Definition: Matrix.h:42
The concrete, templated class for properties.
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53