Mantid
Loading...
Searching...
No Matches
RebinParamsValidator.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 <cmath>
10
11namespace Mantid::Kernel {
12RebinParamsValidator::RebinParamsValidator(bool allowEmpty, bool allowRange)
13 : m_allowEmpty(allowEmpty), m_allowRange(allowRange) {}
14
15IValidator_sptr RebinParamsValidator::clone() const { return std::make_shared<RebinParamsValidator>(*this); }
16
21std::string RebinParamsValidator::checkValidity(const std::vector<double> &value) const {
22 // Rebin Parameters allow three forms of input:
23 // 1) a single value, the bin width, in which case the bin boundaries will be calculated from the input workspace
24 // 2) a pair of values (a range), the lower and upper limits of the binning
25 // 3) a list of values in the form x1,dx1,x2,dx2,...,xn where the x's are bin boundaries and the dx's are bin widths
26
27 std::string error = "";
28 size_t numParams = value.size();
29 switch (numParams) {
30 case 0: {
31 if (!m_allowEmpty) { // unless allowed in the constructor
32 error = "Enter values for this property";
33 }
34 } break;
35 case 1: {
36 if (value[0] == 0.0) {
37 error = "Cannot have a zero bin width";
38 }
39 } break;
40 case 2: {
41 if (m_allowRange) { // if we allow ranges, must be in order
42 if (value[0] >= value[1]) {
43 error = "When giving a range the second value must be larger than the first";
44 }
45 } else { // if we don't allow ranges, then this is wrong
46 error = "The number of bin boundary parameters provided must be odd";
47 }
48 } break;
49 // three or more parameters must be in the form x1,dx1,x2,dx2,...,xn
50 default: {
51 if (value.size() % 2 == 0) { // even
52 error = "The number of bin boundary parameters provided must be odd";
53 } else { // odd
54 // for checking memory allocation
55 size_t numBins = 0;
56 // ensure all bin widths are non-zero
57 double previousBoundary = value[0];
58 for (size_t i = 1; i < value.size() - 1; i += 2) {
59 double binWidth = value[i];
60 double nextBoundary = value[i + 1];
61 // first validate the bins and boundaries
62 if (binWidth == 0.0) {
63 error = "Cannot have a zero bin width";
64 break;
65 } else {
66 if (nextBoundary <= previousBoundary) { // check bin boundaries are in increasing order
67 error = "Bin boundary values must be given in order of increasing value";
68 break;
69 } else if (binWidth < 0.0 &&
70 previousBoundary <=
71 0.0) { // if bin width is negative (log binning) check boundaries are positive-definite
72 error = "Bin boundaries must be positive for logarithmic binning";
73 break;
74 }
75 }
76 // the bins and bounds are okay, accumulate memory
77 if (binWidth < 0.0) {
78 // logarithmic binning
79 numBins += static_cast<size_t>(std::log(nextBoundary / previousBoundary) / std::log(1. - binWidth));
80 } else {
81 // linear binning
82 numBins += static_cast<size_t>((nextBoundary - previousBoundary) / binWidth);
83 }
84 previousBoundary = nextBoundary;
85 } // end for checking bins/boundaries
86 double memInBytes = static_cast<double>(MemoryStats().availMem() * 1024);
87 double binSpaceInBytes = static_cast<double>(numBins * sizeof(double));
88 if (binSpaceInBytes > memInBytes) {
89 double bytesInGB = 1e9;
90 error = "The number of bins requested is expected to exceed available memory. "
91 "This binning requires approximately " +
92 std::to_string(binSpaceInBytes / bytesInGB) + " GB of memory, but only " +
93 std::to_string(memInBytes / bytesInGB) + " GB is available.";
94 }
95 } // end else odd
96 } // end default case
97 }
98
99 // return string, which contains any error messages
100 return error;
101}
102
103} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
double error
This class is responsible for memory statistics.
Definition Memory.h:28
std::size_t availMem() const
Returns the available memory of the system in kiB.
Definition Memory.cpp:402
IValidator_sptr clone() const override
std::string checkValidity(const std::vector< double > &value) const override
Check on the inputed bin boundaries and widths.
RebinParamsValidator(bool allowEmpty=false, bool allowRange=false)
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26
std::string to_string(const wide_integer< Bits, Signed > &n)