Mantid
Loading...
Searching...
No Matches
ShowPossibleCells.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#include "MantidJson/Json.h"
18
19#include <boost/algorithm/string/replace.hpp>
20#include <json/json.h>
21
22namespace Mantid::Crystal {
23// Register the algorithm into the AlgorithmFactory
24DECLARE_ALGORITHM(ShowPossibleCells)
25
26using namespace Mantid::Kernel;
27using namespace Mantid::API;
28using namespace Mantid::DataObjects;
29using namespace Mantid::Geometry;
30
34 this->declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("PeaksWorkspace", "", Direction::InOut),
35 "Input Peaks Workspace");
36
37 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
38 mustBePositive->setLower(0.0);
39
40 this->declareProperty(
41 std::make_unique<PropertyWithValue<double>>("MaxScalarError", 0.2, mustBePositive, Direction::Input),
42 "Max Scalar Error (0.2)");
43
44 this->declareProperty("BestOnly", true, "Show at most one for each Bravais Lattice");
45
46 this->declareProperty(std::make_unique<PropertyWithValue<int>>("NumberOfCells", 0, Direction::Output),
47 "Gets set with the number of possible cells.");
48
49 this->declareProperty("AllowPermutations", true, "Allow permutations of conventional cells");
50
52 "A list of the different cells");
53}
54
58 IPeaksWorkspace_const_sptr ws = this->getProperty("PeaksWorkspace");
59 if (!ws) {
60 throw std::runtime_error("Could not read the peaks workspace");
61 }
62
63 OrientedLattice o_lattice = ws->sample().getOrientedLattice();
64 const Matrix<double> &UB = o_lattice.getUB();
65
66 if (!IndexingUtils::CheckUB(UB)) {
67 throw std::runtime_error("ERROR: The stored UB is not a valid orientation matrix");
68 }
69
70 double max_scalar_error = this->getProperty("MaxScalarError");
71 bool best_only = this->getProperty("BestOnly");
72 bool allowPermutations = this->getProperty("AllowPermutations");
73
74 std::vector<ConventionalCell> list = ScalarUtils::GetCells(UB, best_only, allowPermutations);
75
76 ScalarUtils::RemoveHighErrorForms(list, max_scalar_error);
77
78 size_t num_cells = list.size();
79
80 // now tell the user the number of possible conventional cells:
81 g_log.notice() << "Num Cells : " << num_cells << '\n';
82
83 std::vector<std::string> cells;
84
85 for (size_t i = 0; i < num_cells; i++) {
86 DblMatrix newUB = list[i].GetNewUB();
87 std::string message = list[i].GetDescription() + " Lat Par:" + IndexingUtils::GetLatticeParameterString(newUB);
88
89 g_log.notice(std::string(message));
90
91 Json::Value root;
92 root["Error"] = list[i].GetError();
93 root["FormNumber"] = static_cast<uint32_t>(list[i].GetFormNum());
94 root["CellType"] = list[i].GetCellType();
95 root["Centering"] = list[i].GetCentering();
96 Json::Value outUB;
97 for (double x : newUB.getVector())
98 outUB.append(x);
99 root["UB"] = outUB;
100
101 std::vector<double> lattice_parameters;
102 IndexingUtils::GetLatticeParameters(newUB, lattice_parameters);
103 root["a"] = lattice_parameters[0];
104 root["b"] = lattice_parameters[1];
105 root["c"] = lattice_parameters[2];
106 root["alpha"] = lattice_parameters[3];
107 root["beta"] = lattice_parameters[4];
108 root["gamma"] = lattice_parameters[5];
109 root["volume"] = lattice_parameters[6];
110
111 cells.push_back(Mantid::JsonHelpers::jsonToString(root));
112 }
113
114 this->setProperty("NumberOfCells", static_cast<int>(num_cells));
115 this->setProperty("Cells", cells);
116}
117
118} // namespace Mantid::Crystal
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
A property class for workspaces.
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
static std::string GetLatticeParameterString(const Kernel::DblMatrix &UB)
Get a formatted string listing the lattice parameters and cell volume.
static bool CheckUB(const Kernel::DblMatrix &UB)
Check that the specified UB is reasonable for an orientation matrix.
static bool GetLatticeParameters(const Kernel::DblMatrix &UB, std::vector< double > &lattice_par)
Get the lattice parameters for the specified orientation matrix.
Class to implement UB matrix.
const Kernel::DblMatrix & getUB() const
Get the UB matrix.
static std::vector< ConventionalCell > GetCells(const Kernel::DblMatrix &UB, bool best_only, bool allowPermutations=false)
Get list of all possible conventional cells for UB, regardless of errors, using this UB,...
static void RemoveHighErrorForms(std::vector< ConventionalCell > &list, double level)
Remove cells from list that have scalar errors above the specified level.
Support for a property that holds an array of values.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition Logger.cpp:126
Numerical Matrix class.
Definition Matrix.h:42
std::vector< T > getVector() const
Definition Matrix.cpp:77
The concrete, templated class for properties.
std::shared_ptr< const IPeaksWorkspace > IPeaksWorkspace_const_sptr
shared pointer to Mantid::API::IPeaksWorkspace (const version)
@ InOut
Both an input & output workspace.
Definition Property.h:55
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54