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