Mantid
Loading...
Searching...
No Matches
DiscusMultipleScatteringCorrection.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 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 +
7#pragma once
8
9//------------------------------------------------------------------------------
10// Includes
11//------------------------------------------------------------------------------
12#include "MantidAPI/Algorithm.h"
13#include "MantidAlgorithms/DllConfig.h"
21#include <boost/container/small_vector.hpp>
22#include <shared_mutex>
23
24namespace Mantid {
25namespace API {
26class Sample;
27}
28namespace Geometry {
29class Instrument;
30}
31
32namespace Algorithms {
33
34// define some simple classes to store 2D datasets instead of using MatrixWorkspace internally
35// This couples the algorithm more loosely to Mantid and avoids some complexity in choosing whether
36// to call readX, dataX etc
38 // separate vectors of X and Y rather than vector of pairs to mirror Histogram class and support edges\points
39 std::vector<double> X;
40 std::vector<double> Y;
42 DiscusData1D(std::vector<double> X, std::vector<double> Y) : X(std::move(X)), Y(std::move(Y)) {}
43};
44
46public:
47 DiscusData2D() : m_data(std::vector<DiscusData1D>{}), m_specAxis(nullptr) {};
48 DiscusData2D(const std::vector<DiscusData1D> &data, const std::shared_ptr<std::vector<double>> &specAxis)
49 : m_data(data), m_specAxis(specAxis) {};
50 std::unique_ptr<DiscusData2D> createCopy(bool clearY = false);
51 size_t getNumberHistograms() { return m_data.size(); }
52 DiscusData1D &histogram(const size_t i) { return m_data[i]; }
53 std::vector<DiscusData1D> &histograms() { return m_data; }
54 const std::vector<double> &getSpecAxisValues();
55
56private:
57 std::vector<DiscusData1D> m_data;
58 // optional spectrum axis
59 std::shared_ptr<std::vector<double>> m_specAxis;
60};
61
64 std::string_view materialName;
65 std::shared_ptr<DiscusData2D> SQ;
66 std::shared_ptr<DiscusData2D> logSQ{};
67 std::shared_ptr<DiscusData1D> QSQScaleFactor{};
68 std::shared_ptr<DiscusData2D> QSQ{};
69 std::shared_ptr<DiscusData2D> InvPOfQ{};
70 std::shared_ptr<int> scatterCount = std::make_shared<int>(0);
71};
72
81
88class MANTID_ALGORITHMS_DLL DiscusMultipleScatteringCorrection : public API::Algorithm {
89public:
90 // use small_vector to avoid performance hit from heap allocation of std::vector. Use size 5 in line with Track.h
91 using ComponentWorkspaceMappings = boost::container::small_vector<ComponentWorkspaceMapping, 5>;
93 const std::string name() const override { return "DiscusMultipleScatteringCorrection"; }
95 int version() const override { return 1; }
96 const std::vector<std::string> seeAlso() const override {
97 return {"MayersSampleCorrection", "CarpenterSampleCorrection", "VesuvioCalculateMS"};
98 }
100 const std::string category() const override { return "CorrectionFunctions"; }
102 const std::string summary() const override {
103 return "Calculates a multiple scattering correction using a Monte Carlo method";
104 }
105 const std::string alias() const override { return "Muscat"; }
106 bool checkGroups() override { return false; }
107
108protected:
109 virtual std::shared_ptr<SparseWorkspace> createSparseWorkspace(const API::MatrixWorkspace &modelWS,
110 const size_t nXPoints, const size_t rows,
111 const size_t columns);
112 virtual std::unique_ptr<InterpolationOption> createInterpolateOption();
113 double interpolateFlat(const DiscusData1D &histToInterpolate, double x);
114 std::tuple<double, int> sampleQW(const std::shared_ptr<DiscusData2D> &CumulativeProb, double x);
115 double interpolateSquareRoot(const DiscusData1D &histToInterpolate, double x);
116 double interpolateGaussian(const DiscusData1D &histToInterpolate, double x);
117 double Interpolate2D(const ComponentWorkspaceMapping &SQWSMapping, double q, double w);
118 void updateTrackDirection(Geometry::Track &track, const double cosT, const double phi);
119 void integrateCumulative(const DiscusData1D &h, const double xmin, const double xmax, std::vector<double> &resultX,
120 std::vector<double> &resultY, const bool returnCumulative);
122 void getXMinMax(const Mantid::API::MatrixWorkspace &ws, double &xmin, double &xmax) const;
123 void prepareSampleBeamGeometry(const API::MatrixWorkspace_sptr &inputWS);
124 const std::shared_ptr<Geometry::CSGObject>
125 createCollimatorHexahedronShape(const Kernel::V3D &samplePos, const Mantid::Geometry::DetectorInfo &detectorInfo,
126 const size_t &histogramIndex);
127
128private:
129 void init() override;
130 void exec() override;
131 std::map<std::string, std::string> validateInputs() override;
132 API::MatrixWorkspace_sptr createOutputWorkspace(const API::MatrixWorkspace &inputWS) const;
133 std::tuple<double, double> new_vector(const Kernel::Material &material, double k, bool specialSingleScatterCalc);
134 std::tuple<std::vector<double>, std::vector<double>>
135 simulatePaths(const int nEvents, const int nScatters, Kernel::PseudoRandomNumberGenerator &rng,
136 const ComponentWorkspaceMappings &componentWorkspaces, const double kinc,
137 const std::vector<double> &wValues, bool specialSingleScatterCalc,
138 const Mantid::Geometry::DetectorInfo &detectorInfo, const size_t &histogramIndex);
139 std::tuple<bool, std::vector<double>> scatter(const int nScatters, Kernel::PseudoRandomNumberGenerator &rng,
140 const ComponentWorkspaceMappings &componentWorkspaces,
141 const double kinc, const std::vector<double> &wValues,
142 bool specialSingleScatterCalc,
143 const Mantid::Geometry::DetectorInfo &detectorInfo,
144 const size_t &histogramIndex);
145
147 Geometry::Track generateInitialTrack(Kernel::PseudoRandomNumberGenerator &rng);
148 void inc_xyz(Geometry::Track &track, double vl);
149 const Geometry::IObject *updateWeightAndPosition(Geometry::Track &track, double &weight, const double k,
151 bool specialSingleScatterCalc,
152 const ComponentWorkspaceMappings &componentWorkspaces);
153 bool q_dir(Geometry::Track &track, const Geometry::IObject *shapePtr, const ComponentWorkspaceMappings &invPOfQs,
154 double &k, const double scatteringXSection, Kernel::PseudoRandomNumberGenerator &rng, double &weight);
155 void interpolateFromSparse(API::MatrixWorkspace &targetWS, const SparseWorkspace &sparseWS,
157 void correctForWorkspaceNameClash(std::string &wsName);
158 void setWorkspaceName(const API::MatrixWorkspace_sptr &ws, std::string wsName);
159 void createInvPOfQWorkspaces(ComponentWorkspaceMappings &matWSs, size_t nhists);
160 void convertToLogWorkspace(const std::shared_ptr<DiscusData2D> &SOfQ);
161 void calculateQSQIntegralAsFunctionOfK(ComponentWorkspaceMappings &matWSs, const std::vector<double> &specialKs);
162 void prepareCumulativeProbForQ(double kinc, const ComponentWorkspaceMappings &PInvOfQs);
163 void prepareQSQ(double kinc);
164 double getKf(const double deltaE, const double kinc);
165 std::tuple<double, double, int, double> sampleQWUniform(const std::vector<double> &wValues,
166 Kernel::PseudoRandomNumberGenerator &rng, const double kinc);
167 void prepareStructureFactors();
168 void convertWsBothAxesToPoints(API::MatrixWorkspace_sptr &ws);
169 std::tuple<double, double> getKinematicRange(double kf, double ki);
170 std::vector<std::tuple<double, int, double>> generateInputKOutputWList(const double efixed,
171 const std::vector<double> &xPoints);
172 std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
173 integrateQSQ(const std::shared_ptr<DiscusData2D> &QSQ, double kinc, const bool returnCumulative);
174 double getQSQIntegral(const DiscusData1D &QSQScaleFactor, double k);
175 const ComponentWorkspaceMapping *findMatchingComponent(const ComponentWorkspaceMappings &componentWorkspaces,
176 const Geometry::IObject *shapeObjectWithScatter);
177 void addWorkspaceToDiscus2DData(const Geometry::IObject_const_sptr &shape, const std::string_view &matName,
179 void loadCollimatorInfo();
180 double getDoubleParamFromIDF(std::string paramName);
181 Kernel::V3D getV3DParamFromIDF(std::string paramName);
182 const std::shared_ptr<Geometry::CSGObject> readFromCollimatorCorridorCache(const std::size_t &histogramIndex);
183 void writeToCollimatorCorridorCache(const std::size_t &histogramIndex,
184 const std::shared_ptr<Geometry::CSGObject> &collimatorCorridorCsgObj);
185 long long m_callsToInterceptSurface{0};
186 long long m_IkCalculations{0};
188 int m_maxScatterPtAttempts{};
189 std::shared_ptr<const DiscusData1D> m_sigmaSS; // scattering cross section as a function of k
190 // vectors of S(Q,w) and derived quantities. One entry for sample and each environment component
193 bool m_importanceSampling{};
194 Kernel::DeltaEMode::Type m_EMode{Kernel::DeltaEMode::Undefined};
195 bool m_simulateEnergiesIndependently{};
197 std::shared_ptr<const Geometry::ReferenceFrame> m_refframe;
198 const Geometry::SampleEnvironment *m_env{nullptr};
199 bool m_NormalizeSQ{};
201 std::unique_ptr<IBeamProfile> m_beamProfile;
203 std::unique_ptr<CollimatorInfo> m_collimatorInfo;
204 std::map<std::size_t, std::shared_ptr<Geometry::CSGObject>> m_collimatorCorridorCache;
205 mutable std::shared_mutex m_mutexCorridorCache;
206};
207} // namespace Algorithms
208} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
Base MatrixWorkspace Abstract Class.
DiscusData2D(const std::vector< DiscusData1D > &data, const std::shared_ptr< std::vector< double > > &specAxis)
std::shared_ptr< std::vector< double > > m_specAxis
std::unique_ptr< DiscusData2D > createCopy(bool clearY=false)
Calculates a multiple scattering correction Based on Muscat Fortran code provided by Spencer Howells.
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso algorithms related to this algorithm.
const std::string alias() const override
function to return any aliases of the algorithm.
const std::string category() const override
Algorithm's category for identification.
std::map< std::size_t, std::shared_ptr< Geometry::CSGObject > > m_collimatorCorridorCache
const std::string summary() const override
Summary of algorithms purpose.
const std::string name() const override
Algorithm's name.
bool checkGroups() override
Check the input workspace properties for groups.
boost::container::small_vector< ComponentWorkspaceMapping, 5 > ComponentWorkspaceMappings
std::shared_ptr< const Geometry::ReferenceFrame > m_refframe
Class to provide a consistent interface to an interpolation option on algorithms.
Defines functions and utilities to create and deal with sparse instruments.
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition BoundingBox.h:33
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
IObject : Interface for geometry objects.
Definition IObject.h:42
Defines a single instance of a SampleEnvironment.
Defines a track as a start point and a direction.
Definition Track.h:165
A material is defined as being composed of a given element, defined as a PhysicalConstants::NeutronAt...
Definition Material.h:50
Defines a 1D pseudo-random number generator, i.e.
Class for 3D vectors.
Definition V3D.h:34
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::shared_ptr< const IObject > IObject_const_sptr
Typdef for a shared pointer to a const object.
Definition IObject.h:95
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
Object for holding collimator parameteres loaded from instrument parameters file.
DiscusData1D(std::vector< double > X, std::vector< double > Y)
Type
Define the available energy transfer modes It is important to assign enums proper numbers,...
Definition DeltaEMode.h:29