Mantid
Loading...
Searching...
No Matches
InelasticDiffRotDiscreteCircle.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
11#include "MantidAPI/IFunction.h"
17#include "MantidKernel/Logger.h"
19
20#include "MantidTypes/SpectrumDefinition.h"
21
22#include <cmath>
23#include <limits>
24#include <sstream>
25
27
28namespace {
29Mantid::Kernel::Logger g_log("InelasticDiffDiffRotDiscreteCircle");
30}
31
33
34DECLARE_FUNCTION(InelasticDiffRotDiscreteCircle)
35
36
40 this->declareParameter("Intensity", 1.0, "scaling factor [no units]");
41 this->declareParameter("Radius", 1.0, "Circle radius [Angstroms]");
42 this->declareParameter("Decay", 1.0,
43 "Inverse of transition rate, in nanoseconds "
44 "if energy in micro-ev, or picoseconds if "
45 "energy in mili-eV");
46 this->declareParameter("Shift", 0.0, "Shift in the centre of the peak");
47
48 this->declareAttribute("N", API::IFunction::Attribute(3));
49 declareAttributes();
50}
51
56 // Ensure positive values for Intensity, Radius, and decay
57 auto IntensityConstraint =
58 std::make_unique<BConstraint>(this, "Intensity", std::numeric_limits<double>::epsilon(), true);
59 this->addConstraint(std::move(IntensityConstraint));
60
61 auto RadiusConstraint = std::make_unique<BConstraint>(this, "Radius", std::numeric_limits<double>::epsilon(), true);
62 this->addConstraint(std::move(RadiusConstraint));
63
64 auto DecayConstraint = std::make_unique<BConstraint>(this, "Decay", std::numeric_limits<double>::epsilon(), true);
65 this->addConstraint(std::move(DecayConstraint));
66}
67
75void InelasticDiffRotDiscreteCircle::function1D(double *out, const double *xValues, const size_t nData) const {
76
77 auto I = this->getParameter("Intensity");
78 auto R = this->getParameter("Radius");
79 auto rate = m_hbar / this->getParameter("Decay"); // micro-eV or mili-eV
80 auto N = this->getAttribute("N").asInt();
81
82 // Retrieve Q-value from the appropriate attribute
83 double Q;
84 if (this->getAttribute("Q").asDouble() == EMPTY_DBL()) {
85 if (m_qValueCache.empty()) {
86 throw std::runtime_error("No Q attribute provided and cannot retrieve from workspace.");
87 }
88 const int specIdx = this->getAttribute("WorkspaceIndex").asInt();
89 Q = m_qValueCache[specIdx];
90
91 g_log.debug() << "Get Q value for workspace index " << specIdx << ": " << Q << '\n';
92 } else {
93 Q = getAttribute("Q").asDouble();
94
95 g_log.debug() << "Using Q attribute: " << Q << '\n';
96 }
97
98 std::vector<double> sph(N);
99 for (int k = 1; k < N; k++) {
100 double x = 2 * Q * R * sin(M_PI * k / N);
101 // spherical Besell function of order zero 'j0' is sin(x)/x
102 sph[k] = sin(x) / x;
103 }
104
105 std::vector<double> ratel(N);
106 for (int l = 1; l < N; l++) {
107 // notice that 0 < l/N < 1
108 ratel[l] = rate * 4 * pow(sin(M_PI * l / N), 2);
109 }
110
111 const auto shift = this->getParameter("Shift");
112 for (size_t i = 0; i < nData; i++) {
113 double w = xValues[i] - shift;
114 double S = 0.0;
115 for (int l = 1; l < N; l++) {
116 double lorentzian = ratel[l] / (ratel[l] * ratel[l] + w * w);
117 double al = 0.0;
118 for (int k = 1; k < N; k++) { // case k==N after the loop
119 double y = 2 * M_PI * l * k / N;
120 al += cos(y) * sph[k];
121 }
122 al += 1; // limit for j0 when k==N, or x==0
123 al /= N;
124 S += al * lorentzian;
125 }
126 out[i] = I * S / M_PI;
127 }
128}
129
135void InelasticDiffRotDiscreteCircle::setWorkspace(std::shared_ptr<const API::Workspace> ws) {
136 m_qValueCache.clear();
137
138 auto workspace = std::dynamic_pointer_cast<const API::MatrixWorkspace>(ws);
139 if (!workspace)
140 return;
141
142 const auto &spectrumInfo = workspace->spectrumInfo();
143 const auto &detectorIDs = workspace->detectorInfo().detectorIDs();
144 for (size_t idx = 0; idx < spectrumInfo.size(); idx++) {
145 if (!spectrumInfo.hasDetectors(idx)) {
146 m_qValueCache.clear();
147 g_log.information("Cannot populate Q values from workspace - no detectors set.");
148 break;
149 }
150
151 const auto detectorIndex = spectrumInfo.spectrumDefinition(idx)[0].first;
152
153 try {
154 double efixed = workspace->getEFixed(detectorIDs[detectorIndex]);
155 double usingTheta = 0.5 * spectrumInfo.twoTheta(idx);
156
158
159 m_qValueCache.emplace_back(q);
160 } catch (std::runtime_error &) {
161 m_qValueCache.clear();
162 g_log.information("Cannot populate Q values from workspace - could not "
163 "find EFixed value.");
164 return;
165 }
166 }
167}
168
169} // namespace Mantid::CurveFitting::Functions
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
IPeaksWorkspace_sptr workspace
IntArray detectorIndex
Attribute is a non-fitting parameter.
Definition IFunction.h:286
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
double asDouble() const
Returns double value if attribute is a double, throws exception otherwise.
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
virtual void addConstraint(std::unique_ptr< IConstraint > ic)
Add a constraint to function.
double getParameter(size_t i) const override
Get i-th parameter.
A boundary constraint is designed to be used to set either upper or lower (or both) boundaries on a s...
void setWorkspace(std::shared_ptr< const API::Workspace > ws) override
Cache Q values from the workspace.
void function1D(double *out, const double *xValues, const size_t nData) const override
Calculate function values on an energy domain.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
Kernel::Logger g_log("ExperimentInfo")
static logger object
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42