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#include <span>
24
25namespace Mantid {
26namespace API {
27class Sample;
28}
29namespace Geometry {
30class Instrument;
31}
32
33namespace Algorithms {
34
35// define some simple classes to store 2D datasets instead of using MatrixWorkspace internally
36// This couples the algorithm more loosely to Mantid and avoids some complexity in choosing whether
37// to call readX, dataX etc
39 // separate vectors of X and Y rather than vector of pairs to mirror Histogram class and support edges\points
40 std::vector<double> X;
41 std::vector<double> Y;
43 DiscusData1D(std::vector<double> X, std::vector<double> Y) : X(std::move(X)), Y(std::move(Y)) {}
44};
45
47public:
48 DiscusData2D() : m_data(std::vector<DiscusData1D>{}), m_specAxis(nullptr) {};
49 DiscusData2D(const std::vector<DiscusData1D> &data, const std::shared_ptr<std::vector<double>> &specAxis)
50 : m_data(data), m_specAxis(specAxis) {};
51 std::unique_ptr<DiscusData2D> createCopy(bool clearY = false);
52 size_t getNumberHistograms() { return m_data.size(); }
53 DiscusData1D &histogram(const size_t i) { return m_data[i]; }
54 std::vector<DiscusData1D> &histograms() { return m_data; }
55 const std::vector<double> &getSpecAxisValues();
56
57private:
58 std::vector<DiscusData1D> m_data;
59 // optional spectrum axis
60 std::shared_ptr<std::vector<double>> m_specAxis;
61};
62
65 std::string_view materialName;
66 std::shared_ptr<DiscusData2D> SQ;
67 std::shared_ptr<DiscusData2D> logSQ{};
68 std::shared_ptr<DiscusData1D> QSQScaleFactor{};
69 std::shared_ptr<DiscusData2D> QSQ{};
70 std::shared_ptr<DiscusData2D> InvPOfQ{};
71 std::shared_ptr<int> scatterCount = std::make_shared<int>(0);
72};
73
82
89class MANTID_ALGORITHMS_DLL DiscusMultipleScatteringCorrection : public API::Algorithm {
90public:
91 // use small_vector to avoid performance hit from heap allocation of std::vector. Use size 5 in line with Track.h
92 using ComponentWorkspaceMappings = boost::container::small_vector<ComponentWorkspaceMapping, 5>;
94 const std::string name() const override { return "DiscusMultipleScatteringCorrection"; }
96 int version() const override { return 1; }
97 const std::vector<std::string> seeAlso() const override {
98 return {"MayersSampleCorrection", "CarpenterSampleCorrection", "VesuvioCalculateMS"};
99 }
101 const std::string category() const override { return "CorrectionFunctions"; }
103 const std::string summary() const override {
104 return "Calculates a multiple scattering correction using a Monte Carlo method";
105 }
106 const std::string alias() const override { return "Muscat"; }
107 bool checkGroups() override { return false; }
108
109protected:
110 virtual std::shared_ptr<SparseWorkspace> createSparseWorkspace(const API::MatrixWorkspace &modelWS,
111 const size_t nXPoints, const size_t rows,
112 const size_t columns);
113 virtual std::unique_ptr<InterpolationOption> createInterpolateOption();
114 double interpolateFlat(const DiscusData1D &histToInterpolate, double x);
115 std::tuple<double, int> sampleQW(const std::shared_ptr<DiscusData2D> &CumulativeProb, double x);
116 double interpolateSquareRoot(const DiscusData1D &histToInterpolate, double x);
117 double interpolateGaussian(const DiscusData1D &histToInterpolate, double x);
118 double Interpolate2D(const ComponentWorkspaceMapping &SQWSMapping, double q, double w);
119 void updateTrackDirection(Geometry::Track &track, const double cosT, const double phi);
120 void integrateCumulative(const DiscusData1D &h, const double xmin, const double xmax, std::vector<double> &resultX,
121 std::vector<double> &resultY, const bool returnCumulative);
123 void getXMinMax(const Mantid::API::MatrixWorkspace &ws, double &xmin, double &xmax) const;
124 void prepareSampleBeamGeometry(const API::MatrixWorkspace_sptr &inputWS);
125 const std::shared_ptr<Geometry::CSGObject>
126 createCollimatorHexahedronShape(const Kernel::V3D &samplePos, const Mantid::Geometry::DetectorInfo &detectorInfo,
127 const size_t &histogramIndex);
128
129private:
130 void init() override;
131 void exec() override;
132 std::map<std::string, std::string> validateInputs() override;
133 API::MatrixWorkspace_sptr createOutputWorkspace(const API::MatrixWorkspace &inputWS) const;
134 std::tuple<double, double> new_vector(const Kernel::Material &material, double k, bool specialSingleScatterCalc);
135 std::tuple<std::vector<double>, std::vector<double>>
136 simulatePaths(const int nEvents, const int nScatters, Kernel::PseudoRandomNumberGenerator &rng,
137 const ComponentWorkspaceMappings &componentWorkspaces, const double kinc,
138 const std::vector<double> &wValues, bool specialSingleScatterCalc,
139 const Mantid::Geometry::DetectorInfo &detectorInfo, const size_t &histogramIndex);
140 std::tuple<bool, std::vector<double>> scatter(const int nScatters, Kernel::PseudoRandomNumberGenerator &rng,
141 const ComponentWorkspaceMappings &componentWorkspaces,
142 const double kinc, const std::vector<double> &wValues,
143 bool specialSingleScatterCalc,
144 const Mantid::Geometry::DetectorInfo &detectorInfo,
145 const size_t &histogramIndex);
146
148 Geometry::Track generateInitialTrack(Kernel::PseudoRandomNumberGenerator &rng);
149 void inc_xyz(Geometry::Track &track, double vl);
150 const Geometry::IObject *updateWeightAndPosition(Geometry::Track &track, double &weight, const double k,
152 bool specialSingleScatterCalc,
153 const ComponentWorkspaceMappings &componentWorkspaces);
154 bool q_dir(Geometry::Track &track, const Geometry::IObject *shapePtr, const ComponentWorkspaceMappings &invPOfQs,
155 double &k, const double scatteringXSection, Kernel::PseudoRandomNumberGenerator &rng, double &weight);
156 void interpolateFromSparse(API::MatrixWorkspace &targetWS, const SparseWorkspace &sparseWS,
158 void correctForWorkspaceNameClash(std::string &wsName);
159 void setWorkspaceName(const API::MatrixWorkspace_sptr &ws, std::string wsName);
160 void createInvPOfQWorkspaces(ComponentWorkspaceMappings &matWSs, size_t nhists);
161 void convertToLogWorkspace(const std::shared_ptr<DiscusData2D> &SOfQ);
162 void calculateQSQIntegralAsFunctionOfK(ComponentWorkspaceMappings &matWSs, const std::vector<double> &specialKs);
163 void prepareCumulativeProbForQ(double kinc, const ComponentWorkspaceMappings &PInvOfQs);
164 void prepareQSQ(double kinc);
165 double getKf(const double deltaE, const double kinc);
166 std::tuple<double, double, int, double> sampleQWUniform(const std::vector<double> &wValues,
167 Kernel::PseudoRandomNumberGenerator &rng, const double kinc);
168 void prepareStructureFactors();
169 void convertWsBothAxesToPoints(API::MatrixWorkspace_sptr &ws);
170 std::tuple<double, double> getKinematicRange(double kf, double ki);
171 std::vector<std::tuple<double, int, double>> generateInputKOutputWList(const double efixed,
172 std::span<double const> xPoints);
173 std::tuple<std::vector<double>, std::vector<double>, std::vector<double>>
174 integrateQSQ(const std::shared_ptr<DiscusData2D> &QSQ, double kinc, const bool returnCumulative);
175 double getQSQIntegral(const DiscusData1D &QSQScaleFactor, double k);
176 const ComponentWorkspaceMapping *findMatchingComponent(const ComponentWorkspaceMappings &componentWorkspaces,
177 const Geometry::IObject *shapeObjectWithScatter);
178 void addWorkspaceToDiscus2DData(const Geometry::IObject_const_sptr &shape, const std::string_view &matName,
180 void loadCollimatorInfo();
181 double getDoubleParamFromIDF(std::string paramName);
182 Kernel::V3D getV3DParamFromIDF(std::string paramName);
183 const std::shared_ptr<Geometry::CSGObject> readFromCollimatorCorridorCache(const std::size_t &histogramIndex);
184 void writeToCollimatorCorridorCache(const std::size_t &histogramIndex,
185 const std::shared_ptr<Geometry::CSGObject> &collimatorCorridorCsgObj);
186 long long m_callsToInterceptSurface{0};
187 long long m_IkCalculations{0};
189 int m_maxScatterPtAttempts{};
190 std::shared_ptr<const DiscusData1D> m_sigmaSS; // scattering cross section as a function of k
191 // vectors of S(Q,w) and derived quantities. One entry for sample and each environment component
194 bool m_importanceSampling{};
195 Kernel::DeltaEMode::Type m_EMode{Kernel::DeltaEMode::Undefined};
196 bool m_simulateEnergiesIndependently{};
198 std::shared_ptr<const Geometry::ReferenceFrame> m_refframe;
199 const Geometry::SampleEnvironment *m_env{nullptr};
200 bool m_NormalizeSQ{};
202 std::unique_ptr<IBeamProfile> m_beamProfile;
204 std::unique_ptr<CollimatorInfo> m_collimatorInfo;
205 std::map<std::size_t, std::shared_ptr<Geometry::CSGObject>> m_collimatorCorridorCache;
206 mutable std::shared_mutex m_mutexCorridorCache;
207};
208} // namespace Algorithms
209} // 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