Mantid
Loading...
Searching...
No Matches
ConvertToDiffractionMDWorkspace3.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 +
8
10
14
18
22
23#include <boost/math/special_functions/sign.hpp>
24
25#include <algorithm>
26#include <limits>
27
28using namespace Mantid::API;
29using namespace Mantid::Kernel;
30using namespace Mantid::DataObjects;
31using namespace Mantid::Geometry;
32
33namespace Mantid::MDAlgorithms {
34
35// Register the algorithm into the AlgorithmFactory
36DECLARE_ALGORITHM(ConvertToDiffractionMDWorkspace3)
37
38//----------------------------------------------------------------------------------------------
42 // initilise common properties between versions
44
45 std::vector<double> extents;
46 declareProperty(std::make_unique<ArrayProperty<double>>("Extents", std::move(extents)),
47 "A comma separated list of min, max for each dimension,\n"
48 "specifying the extents of each dimension. Optional, default "
49 "will use ConvertToMDMinMaxLocal to calculate extents for each "
50 "dimension.");
51 setPropertyGroup("Extents", getBoxSettingsGroupName());
52}
53
61void ConvertToDiffractionMDWorkspace3::convertExtents(const std::vector<double> &Extents, std::vector<double> &minVal,
62 std::vector<double> &maxVal) {
63 minVal.resize(3);
64 maxVal.resize(3);
65 if (Extents.size() == 2) {
66 for (size_t d = 0; d < 3; d++) {
67 minVal[d] = Extents[0];
68 maxVal[d] = Extents[1];
69 }
70 } else if (Extents.size() == 6) {
71 for (size_t d = 0; d < 3; d++) {
72 minVal[d] = Extents[2 * d + 0];
73 maxVal[d] = Extents[2 * d + 1];
74 }
75 } else if (Extents.empty()) {
76 calculateExtentsFromData(minVal, maxVal);
77 } else
78 throw std::invalid_argument("You must specify either 2 or 6 extents (min,max).");
79}
80
87 std::vector<double> &maxVal) {
88 auto alg = createChildAlgorithm("ConvertToMDMinMaxLocal");
89 alg->initialize();
90 alg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", getProperty("InputWorkspace"));
91 alg->setPropertyValue("QDimensions", "Q3D");
92 std::vector<std::string> dE_modes = Kernel::DeltaEMode::availableTypes();
93 alg->setPropertyValue("dEAnalysisMode", dE_modes[Kernel::DeltaEMode::Elastic]);
94
95 std::string TargetFrame, Scaling;
96 convertFramePropertyNames(getPropertyValue("OutputDimensions"), TargetFrame, Scaling);
97 alg->setProperty("Q3DFrames", TargetFrame);
98 alg->setProperty("QConversionScales", Scaling);
99
100 alg->execute();
101
102 minVal = alg->getProperty("MinValues");
103 maxVal = alg->getProperty("MaxValues");
104
105 // If the calculation produced +/- infinity as one of the extents
106 // replace this with a more reasonable value.
107 const auto INF = std::numeric_limits<double>::infinity();
108 const auto MAX_DBL = std::numeric_limits<double>::max();
109 const auto DEFAULT_BOUND = 50.0;
110
111 std::replace(minVal.begin(), minVal.end(), -INF, -DEFAULT_BOUND);
112 std::replace(maxVal.begin(), maxVal.end(), INF, DEFAULT_BOUND);
113 std::replace(minVal.begin(), minVal.end(), MAX_DBL, -DEFAULT_BOUND);
114 std::replace(maxVal.begin(), maxVal.end(), -MAX_DBL, DEFAULT_BOUND);
115
116 // Increases bounds by a small margin and ensures they aren't too close to
117 // zero. This is to prevent events from being incorrectly discarded as out
118 // of bounds by calcMatrixCoord functions (or, if fixed there, later causing
119 // further issues in MDGridBox::calculateChildIndex due to events sitting
120 // on maximum boundaries)
121 for (size_t i = 0; i < maxVal.size(); ++i) {
122 minVal[i] *= (1 - 1.e-5 * boost::math::sign(minVal[i]));
123 if (fabs(minVal[i]) < 1.e-5)
124 minVal[i] = -1.e-5;
125 maxVal[i] *= (1 + 1.e-5 * boost::math::sign(maxVal[i]));
126 if (fabs(maxVal[i]) < 1.e-5)
127 maxVal[i] = 1.e-5;
128 }
129}
130
131} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define fabs(x)
Definition Matrix.cpp:22
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Support for a property that holds an array of values.
void convertFramePropertyNames(const std::string &TargFrame, std::string &TargFrameName, std::string &ScalingName)
method to convert the value of the target frame specified for the ConvertToDiffractionMDWorksapce int...
ConvertToDiffractionMDWorkspace3 : Create a MDEventWorkspace with events in reciprocal space (Qx,...
void convertExtents(const std::vector< double > &Extents, std::vector< double > &minVal, std::vector< double > &maxVal) override
Splits extents accepted by convertToDiffreactionMD workspace in the form min1,max1 or min1,...
void calculateExtentsFromData(std::vector< double > &minVal, std::vector< double > &maxVal)
Calculate the extents to use for the conversion from the input workspace.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
TargetFrame
enum describes availible target coordinate systems for Q3D mode
static const std::vector< std::string > availableTypes()
Returns the string list of available modes.