Mantid
Loading...
Searching...
No Matches
SetUB.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"
14
15using namespace Mantid::Kernel;
16using namespace Mantid::API;
18
19namespace Mantid::Crystal {
20
21// Register the algorithm into the AlgorithmFactory
23
24
25const std::string SetUB::name() const { return "SetUB"; }
26
28int SetUB::version() const { return 1; }
29
31const std::string SetUB::category() const { return "Crystal\\UBMatrix"; }
32
36 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
37 mustBePositive->setLower(0.0);
38 auto reasonableAngle = std::make_shared<BoundedValidator<double>>();
39 reasonableAngle->setLower(5.0);
40 reasonableAngle->setUpper(175.0);
41 // clang-format off
42 auto mustBe3D = std::make_shared<ArrayLengthValidator<double> >(3);
43 auto threeVthree = std::make_shared<ArrayLengthValidator<double> >(9);
44 // clang-format on
45 std::vector<double> zeroes(9, 0.), u0(3, 0), v0(3, 0);
46 u0[0] = 1.;
47 v0[1] = 1.;
48 this->declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("Workspace", "", Direction::InOut),
49 "An input workspace.");
50 this->declareProperty(
51 std::make_unique<PropertyWithValue<double>>("a", 1.0, mustBePositive->clone(), Direction::Input),
52 "Lattice parameter a");
53 this->declareProperty(
54 std::make_unique<PropertyWithValue<double>>("b", 1.0, mustBePositive->clone(), Direction::Input),
55 "Lattice parameter b");
56 this->declareProperty(
57 std::make_unique<PropertyWithValue<double>>("c", 1.0, std::move(mustBePositive), Direction::Input),
58 "Lattice parameter c");
59 this->declareProperty(
60 std::make_unique<PropertyWithValue<double>>("alpha", 90.0, reasonableAngle->clone(), Direction::Input),
61 "Lattice parameter alpha (degrees)");
62 this->declareProperty(
63 std::make_unique<PropertyWithValue<double>>("beta", 90.0, reasonableAngle->clone(), Direction::Input),
64 "Lattice parameter beta (degrees)");
65 this->declareProperty(
66 std::make_unique<PropertyWithValue<double>>("gamma", 90.0, std::move(reasonableAngle), Direction::Input),
67 "Lattice parameter gamma(degrees) ");
68 this->declareProperty(std::make_unique<ArrayProperty<double>>("u", std::move(u0), mustBe3D->clone()),
69 "Vector along k_i, when goniometer is at 0");
70 this->declareProperty(std::make_unique<ArrayProperty<double>>("v", std::move(v0), std::move(mustBe3D)),
71 "In plane vector perpendicular to k_i, when goniometer is at 0");
72 this->declareProperty(std::make_unique<ArrayProperty<double>>("UB", std::move(zeroes), threeVthree), "UB Matrix");
73 this->declareProperty(std::make_unique<PropertyWithValue<int>>("MDSampleNumber", EMPTY_INT(), Direction::Input),
74 "For an MD workspace, the sample number to wich to "
75 "attach an oriented lattice (starting from 0). No "
76 "number, or negative number, means that it will copy "
77 "to all samples");
78}
79
84 std::unique_ptr<Mantid::Geometry::OrientedLattice> lattice;
85 std::vector<double> UBvec = getProperty("UB");
86 Mantid::Kernel::DblMatrix UBMatrix(UBvec), zeroMatrix(3, 3);
87 if (UBMatrix == zeroMatrix) {
88 double a, b, c, alpha, beta, gamma;
89 a = getProperty("a");
90 b = getProperty("b");
91 c = getProperty("c");
92 alpha = getProperty("alpha");
93 beta = getProperty("beta");
94 gamma = getProperty("gamma");
95 std::vector<double> u = getProperty("u");
96 std::vector<double> v = getProperty("v");
97
98 lattice = std::make_unique<OrientedLattice>(a, b, c, alpha, beta, gamma);
99 lattice->setUFromVectors(Mantid::Kernel::V3D(u[0], u[1], u[2]), Mantid::Kernel::V3D(v[0], v[1], v[2]));
100 } else {
101 if (UBMatrix.determinant() == 0)
102 throw std::invalid_argument("UB matrix determinant is 0");
103 else {
104 lattice = std::make_unique<OrientedLattice>();
105 lattice->setUB(UBMatrix);
106 }
107 }
108
109 // now attach the oriented lattice to the workspace
110 Workspace_sptr ws = this->getProperty("Workspace");
111
112 // Sample copy;
113 MultipleExperimentInfos_sptr mdws = std::dynamic_pointer_cast<MultipleExperimentInfos>(ws);
114 if (mdws != nullptr) {
115 int sampleNumber = getProperty("MDSampleNumber");
116 if ((sampleNumber == EMPTY_INT()) || (sampleNumber < 0)) // copy to all samples
117 {
118 for (uint16_t i = 0; i < mdws->getNumExperimentInfo(); i++) {
119 mdws->getExperimentInfo(i)->mutableSample().setOrientedLattice(std::make_unique<OrientedLattice>(*lattice));
120 }
121 } else // copy to a single sample
122 {
123 if (static_cast<uint16_t>(sampleNumber) > (mdws->getNumExperimentInfo() - 1)) {
124 g_log.warning() << "Number greater than the number of last sample in "
125 "the workspace ("
126 << (mdws->getNumExperimentInfo() - 1) << "). Will use sample number 0 instead\n";
127 sampleNumber = 0;
128 }
129 mdws->getExperimentInfo(static_cast<uint16_t>(sampleNumber))
130 ->mutableSample()
131 .setOrientedLattice(std::move(lattice));
132 }
133 } else // peaks workspace or matrix workspace
134 {
135 ExperimentInfo_sptr ei = std::dynamic_pointer_cast<ExperimentInfo>(ws);
136 if (!ei)
137 throw std::invalid_argument("Wrong type of workspace");
138 ei->mutableSample().setOrientedLattice(std::move(lattice));
139 }
140 this->setProperty("Workspace", ws);
141}
142
143} // namespace Mantid::Crystal
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
A property class for workspaces.
SetUB : Algorithm to set the UB matrix, given lattice parameters and u and v vectors as defined in: h...
Definition: SetUB.h:23
void init() override
Initialize the algorithm's properties.
Definition: SetUB.cpp:35
const std::string category() const override
Algorithm's category for identification.
Definition: SetUB.cpp:31
int version() const override
Algorithm's version for identification.
Definition: SetUB.cpp:28
void exec() override
Execute the algorithm.
Definition: SetUB.cpp:83
Class to implement UB matrix.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
The concrete, templated class for properties.
Class for 3D vectors.
Definition: V3D.h:34
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< ExperimentInfo > ExperimentInfo_sptr
Shared pointer to ExperimentInfo.
std::shared_ptr< MultipleExperimentInfos > MultipleExperimentInfos_sptr
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
STL namespace.
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53