-
Notifications
You must be signed in to change notification settings - Fork 122
/
MonteCarloAbsorption.cpp
433 lines (388 loc) · 19.3 KB
/
MonteCarloAbsorption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/MonteCarloAbsorption.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidAlgorithms/BeamProfileFactory.h"
#include "MantidAlgorithms/InterpolationOption.h"
#include "MantidAlgorithms/SampleCorrections/DetectorGridDefinition.h"
#include "MantidAlgorithms/SampleCorrections/MCInteractionStatistics.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidGeometry/Instrument/SampleEnvironment.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/DeltaEMode.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MersenneTwister.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/VectorHelper.h"
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
using Mantid::DataObjects::Workspace2D;
namespace PhysicalConstants = Mantid::PhysicalConstants;
/// @cond
namespace {
constexpr int DEFAULT_NEVENTS = 1000;
constexpr int DEFAULT_SEED = 123456789;
constexpr int DEFAULT_LATITUDINAL_DETS = 5;
constexpr int DEFAULT_LONGITUDINAL_DETS = 10;
/// Energy (meV) to wavelength (angstroms)
inline double toWavelength(double energy) {
static const double factor =
1e10 * PhysicalConstants::h / sqrt(2.0 * PhysicalConstants::NeutronMass * PhysicalConstants::meV);
return factor / sqrt(energy);
}
struct EFixedProvider {
explicit EFixedProvider(const ExperimentInfo &expt) : m_expt(expt), m_emode(expt.getEMode()), m_value(0.0) {
if (m_emode == DeltaEMode::Direct) {
m_value = m_expt.getEFixed();
}
}
inline DeltaEMode::Type emode() const { return m_emode; }
inline double value(const Mantid::detid_t detID) const {
if (m_emode != DeltaEMode::Indirect)
return m_value;
else
return m_expt.getEFixed(detID);
}
private:
const ExperimentInfo &m_expt;
const DeltaEMode::Type m_emode;
double m_value;
};
} // namespace
/// @endcond
namespace Mantid::Algorithms {
DECLARE_ALGORITHM(MonteCarloAbsorption)
//------------------------------------------------------------------------------
// Private methods
//------------------------------------------------------------------------------
/**
* Initialize the algorithm
*/
void MonteCarloAbsorption::init() {
// The input workspace must have an instrument and units of wavelength
auto wsValidator = std::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("Wavelength");
wsValidator->add<InstrumentValidator>();
declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator),
"The name of the input workspace. The input workspace must "
"have X units of wavelength.");
declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
"The name to use for the output workspace.");
auto positiveInt = std::make_shared<Kernel::BoundedValidator<int>>();
positiveInt->setLower(1);
declareProperty("NumberOfWavelengthPoints", EMPTY_INT(), positiveInt,
"The number of wavelength points for which a simulation is "
"attempted if ResimulateTracksForDifferentWavelengths=true");
declareProperty("EventsPerPoint", DEFAULT_NEVENTS, positiveInt,
"The number of \"neutron\" events to generate per simulated point");
declareProperty("SeedValue", DEFAULT_SEED, positiveInt, "Seed the random number generator with this value");
auto interpolateOpt = createInterpolateOption();
declareProperty(interpolateOpt->property(), interpolateOpt->propertyDoc());
declareProperty("SparseInstrument", false,
"Enable simulation on special "
"instrument with a sparse grid of "
"detectors interpolating the "
"results to the real instrument.");
auto threeOrMore = std::make_shared<Kernel::BoundedValidator<int>>();
threeOrMore->setLower(3);
declareProperty("NumberOfDetectorRows", DEFAULT_LATITUDINAL_DETS, threeOrMore,
"Number of detector rows in the detector grid of the sparse instrument.");
setPropertySettings("NumberOfDetectorRows",
std::make_unique<EnabledWhenProperty>("SparseInstrument", ePropertyCriterion::IS_NOT_DEFAULT));
auto twoOrMore = std::make_shared<Kernel::BoundedValidator<int>>();
twoOrMore->setLower(2);
declareProperty("NumberOfDetectorColumns", DEFAULT_LONGITUDINAL_DETS, twoOrMore,
"Number of detector columns in the detector grid "
"of the sparse instrument.");
setPropertySettings("NumberOfDetectorColumns",
std::make_unique<EnabledWhenProperty>("SparseInstrument", ePropertyCriterion::IS_NOT_DEFAULT));
// Control the number of attempts made to generate a random point in the
// object
declareProperty("MaxScatterPtAttempts", 5000, positiveInt,
"Maximum number of tries made to generate a scattering point "
"within the sample (+ optional container etc). Objects with "
"holes in them, e.g. a thin annulus can cause problems "
"if this number is too low.\n"
"If a scattering point cannot be generated by increasing "
"this value then there is most likely a problem with "
"the sample geometry.");
declareProperty("ResimulateTracksForDifferentWavelengths", false, "Resimulate tracks for each wavelength point.");
setPropertySettings("NumberOfWavelengthPoints",
std::make_unique<EnabledWhenProperty>("ResimulateTracksForDifferentWavelengths",
ePropertyCriterion::IS_NOT_DEFAULT));
auto scatteringOptionValidator = std::make_shared<StringListValidator>();
scatteringOptionValidator->addAllowedValue("SampleAndEnvironment");
scatteringOptionValidator->addAllowedValue("SampleOnly");
scatteringOptionValidator->addAllowedValue("EnvironmentOnly");
declareProperty("SimulateScatteringPointIn", "SampleAndEnvironment",
"Simulate the scattering point in the vicinity of the sample or its "
"environment or both (default).",
scatteringOptionValidator);
}
/**
* Execution code
*/
void MonteCarloAbsorption::exec() {
const MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
const int nevents = getProperty("EventsPerPoint");
const bool resimulateTracks = getProperty("ResimulateTracksForDifferentWavelengths");
const int seed = getProperty("SeedValue");
InterpolationOption interpolateOpt;
interpolateOpt.set(getPropertyValue("Interpolation"), true, resimulateTracks);
const bool useSparseInstrument = getProperty("SparseInstrument");
const int maxScatterPtAttempts = getProperty("MaxScatterPtAttempts");
auto simulatePointsIn = MCInteractionVolume::ScatteringPointVicinity::SAMPLEANDENVIRONMENT;
const auto pointsInProperty = getPropertyValue("SimulateScatteringPointIn");
if (pointsInProperty == "SampleOnly") {
simulatePointsIn = MCInteractionVolume::ScatteringPointVicinity::SAMPLEONLY;
} else if (pointsInProperty == "EnvironmentOnly") {
simulatePointsIn = MCInteractionVolume::ScatteringPointVicinity::ENVIRONMENTONLY;
}
auto outputWS = doSimulation(*inputWS, static_cast<size_t>(nevents), resimulateTracks, seed, interpolateOpt,
useSparseInstrument, static_cast<size_t>(maxScatterPtAttempts), simulatePointsIn);
setProperty("OutputWorkspace", std::move(outputWS));
}
/**
* Validate the input properties.
* @return a map where keys are property names and values the found issues
*/
std::map<std::string, std::string> MonteCarloAbsorption::validateInputs() {
std::map<std::string, std::string> issues;
const bool resimulateTracksForDiffWavelengths = getProperty("ResimulateTracksForDifferentWavelengths");
// Only interpolate between wavelength points if resimulating tracks
if (resimulateTracksForDiffWavelengths) {
const int nlambda = getProperty("NumberOfWavelengthPoints");
InterpolationOption interpOpt;
const std::string interpValue = getPropertyValue("Interpolation");
interpOpt.set(interpValue, true, resimulateTracksForDiffWavelengths);
const auto nlambdaIssue = interpOpt.validateInputSize(nlambda);
if (!nlambdaIssue.empty()) {
issues["NumberOfWavelengthPoints"] = nlambdaIssue;
}
}
return issues;
}
/**
* Factory method to return an instance of the required interaction volume
* class
* @param sample A reference to the object defining details of the sample
* @param maxScatterPtAttempts The maximum number of tries to generate a random
* point within the object
* @param pointsIn Where to generate the scattering point in
* @return a pointer to an MCAbsorptionStrategy object
*/
std::shared_ptr<IMCInteractionVolume>
MonteCarloAbsorption::createInteractionVolume(const API::Sample &sample, const size_t maxScatterPtAttempts,
const MCInteractionVolume::ScatteringPointVicinity pointsIn) {
auto interactionVol = std::make_shared<MCInteractionVolume>(sample, maxScatterPtAttempts, pointsIn);
return interactionVol;
}
/**
* Factory method to return an instance of the required absorption strategy
* class
* @param interactionVol The interaction volume object to inject into the
* strategy
* @param beamProfile A reference to the object the beam profile
* @param EMode The energy mode of the instrument
* @param nevents The number of Monte Carlo events used in the simulation
* @param maxScatterPtAttempts The maximum number of tries to generate a random
* point within the object
* @param regenerateTracksForEachLambda Whether to resimulate tracks for each
* wavelength point or not
* @return a pointer to an MCAbsorptionStrategy object
*/
std::shared_ptr<IMCAbsorptionStrategy>
MonteCarloAbsorption::createStrategy(IMCInteractionVolume &interactionVol, const IBeamProfile &beamProfile,
Kernel::DeltaEMode::Type EMode, const size_t nevents,
const size_t maxScatterPtAttempts, const bool regenerateTracksForEachLambda) {
auto MCAbs = std::make_shared<MCAbsorptionStrategy>(interactionVol, beamProfile, EMode, nevents, maxScatterPtAttempts,
regenerateTracksForEachLambda);
return MCAbs;
}
/**
* Factory method to return an instance of the required SparseInstrument class
* @param modelWS The full workspace that the sparse one will be based on
* @param wavelengthPoints The number of wavelength points to include in the
* histograms in the sparse workspace
* @param rows The number of rows of detectors to create
* @param columns The number of columns of detectors to create
* @return a pointer to an SparseInstrument object
*/
std::shared_ptr<SparseWorkspace> MonteCarloAbsorption::createSparseWorkspace(const API::MatrixWorkspace &modelWS,
const size_t wavelengthPoints,
const size_t rows, const size_t columns) {
auto sparseWS = std::make_shared<SparseWorkspace>(modelWS, wavelengthPoints, rows, columns);
return sparseWS;
}
/**
* Factory method to return an instance of the required InterpolationOption
* class
* @return a pointer to an InterpolationOption object
*/
std::unique_ptr<InterpolationOption> MonteCarloAbsorption::createInterpolateOption() {
auto interpolationOpt = std::make_unique<InterpolationOption>();
return interpolationOpt;
}
/**
* Run the simulation over the whole input workspace
* @param inputWS A reference to the input workspace
* @param nevents Number of MC events per wavelength point to simulate
* @param resimulateTracksForDiffWavelengths Whether to resimulate the tracks
* for each wavelength point
* @param seed Seed value for the random number generator
* @param interpolateOpt Method of interpolation to compute unsimulated points
* @param useSparseInstrument If true, use sparse instrument in simulation
* @param maxScatterPtAttempts The maximum number of tries to generate a
* scatter point within the object
* @param pointsIn Where to simulate the scattering point in
* @return A new workspace containing the correction factors & errors
*/
MatrixWorkspace_uptr MonteCarloAbsorption::doSimulation(const MatrixWorkspace &inputWS, const size_t nevents,
const bool resimulateTracksForDiffWavelengths, const int seed,
const InterpolationOption &interpolateOpt,
const bool useSparseInstrument,
const size_t maxScatterPtAttempts,
const MCInteractionVolume::ScatteringPointVicinity pointsIn) {
auto outputWS = createOutputWorkspace(inputWS);
const auto inputNbins = static_cast<int>(inputWS.blocksize());
int nlambda;
if (resimulateTracksForDiffWavelengths) {
nlambda = getProperty("NumberOfWavelengthPoints");
if (isEmpty(nlambda) || nlambda > inputNbins) {
if (!isEmpty(nlambda)) {
g_log.warning() << "The requested number of wavelength points is larger "
"than the spectra size. "
"Defaulting to spectra size.\n";
}
nlambda = inputNbins;
}
} else {
nlambda = inputNbins;
}
SparseWorkspace_sptr sparseWS;
if (useSparseInstrument) {
const int latitudinalDets = getProperty("NumberOfDetectorRows");
const int longitudinalDets = getProperty("NumberOfDetectorColumns");
sparseWS = createSparseWorkspace(inputWS, nlambda, latitudinalDets, longitudinalDets);
}
MatrixWorkspace &simulationWS = useSparseInstrument ? *sparseWS : *outputWS;
const MatrixWorkspace &instrumentWS = useSparseInstrument ? simulationWS : inputWS;
// Cache information about the workspace that will be used repeatedly
auto instrument = instrumentWS.getInstrument();
const auto nhists = static_cast<int64_t>(instrumentWS.getNumberHistograms());
EFixedProvider efixed(instrumentWS);
auto beamProfile = BeamProfileFactory::createBeamProfile(*instrument, inputWS.sample());
// Configure progress
Progress prog(this, 0.0, 1.0, nhists);
prog.setNotifyStep(0.01);
const std::string reportMsg = "Computing corrections";
// Configure strategy
auto interactionVolume = createInteractionVolume(inputWS.sample(), maxScatterPtAttempts, pointsIn);
auto strategy = createStrategy(*interactionVolume, *beamProfile, efixed.emode(), nevents, maxScatterPtAttempts,
resimulateTracksForDiffWavelengths);
const auto &spectrumInfo = simulationWS.spectrumInfo();
PARALLEL_FOR_IF(Kernel::threadSafe(simulationWS))
for (int64_t i = 0; i < nhists; ++i) {
PARALLEL_START_INTERRUPT_REGION
auto &outE = simulationWS.mutableE(i);
// The input was cloned so clear the errors out
outE = 0.0;
if (!spectrumInfo.hasDetectors(i) || spectrumInfo.isMasked(i)) {
continue;
}
// Per spectrum values
const auto &detPos = spectrumInfo.position(i);
const double lambdaFixed = toWavelength(efixed.value(spectrumInfo.detector(i).getID()));
MersenneTwister rng(seed + int(i));
const auto lambdas = simulationWS.points(i).rawData();
const auto nbins = lambdas.size();
const size_t lambdaStepSize = nbins / nlambda;
std::vector<double> packedLambdas;
std::vector<double> packedAttFactors;
std::vector<double> packedAttFactorErrors;
for (size_t j = 0; j < nbins; j += lambdaStepSize) {
packedLambdas.push_back(lambdas[j]);
packedAttFactors.push_back(0);
packedAttFactorErrors.push_back(0);
// Ensure we have the last point for the interpolation
if (lambdaStepSize > 1 && j + lambdaStepSize >= nbins && j + 1 != nbins) {
j = nbins - lambdaStepSize - 1;
}
}
MCInteractionStatistics detStatistics(spectrumInfo.detector(i).getID(), inputWS.sample());
strategy->calculate(rng, detPos, packedLambdas, lambdaFixed, packedAttFactors, packedAttFactorErrors,
detStatistics);
if (g_log.is(Kernel::Logger::Priority::PRIO_DEBUG)) {
g_log.debug(detStatistics.generateScatterPointStats());
}
for (size_t j = 0; j < packedLambdas.size(); j++) {
auto idx = simulationWS.yIndexOfX(packedLambdas[j], i);
simulationWS.getSpectrum(i).dataY()[idx] = packedAttFactors[j];
simulationWS.getSpectrum(i).dataE()[idx] = packedAttFactorErrors[j];
}
// Interpolate through points not simulated. Simulation WS only has
// reduced X values if using sparse instrument so no interpolation required
if (!useSparseInstrument && lambdaStepSize > 1) {
auto histnew = simulationWS.histogram(i);
if (lambdaStepSize < nbins) {
interpolateOpt.applyInplace(histnew, lambdaStepSize);
} else {
std::fill(histnew.mutableY().begin() + 1, histnew.mutableY().end(), histnew.y()[0]);
}
outputWS->setHistogram(i, histnew);
}
prog.report(reportMsg);
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
if (useSparseInstrument) {
interpolateFromSparse(*outputWS, *sparseWS, interpolateOpt);
}
return outputWS;
}
MatrixWorkspace_uptr MonteCarloAbsorption::createOutputWorkspace(const MatrixWorkspace &inputWS) const {
MatrixWorkspace_uptr outputWS = DataObjects::create<Workspace2D>(inputWS);
// The algorithm computes the signal values at bin centres so they should
// be treated as a distribution
outputWS->setDistribution(true);
outputWS->setYUnit("");
outputWS->setYUnitLabel("Attenuation factor");
return outputWS;
}
void MonteCarloAbsorption::interpolateFromSparse(MatrixWorkspace &targetWS, const SparseWorkspace &sparseWS,
const Mantid::Algorithms::InterpolationOption &interpOpt) {
const auto &spectrumInfo = targetWS.spectrumInfo();
const auto refFrame = targetWS.getInstrument()->getReferenceFrame();
PARALLEL_FOR_IF(Kernel::threadSafe(targetWS, sparseWS))
for (int64_t i = 0; i < static_cast<decltype(i)>(spectrumInfo.size()); ++i) {
PARALLEL_START_INTERRUPT_REGION
if (spectrumInfo.hasDetectors(i)) {
double lat, lon;
std::tie(lat, lon) = spectrumInfo.geographicalAngles(i);
const auto spatiallyInterpHisto = sparseWS.bilinearInterpolateFromDetectorGrid(lat, lon);
if (spatiallyInterpHisto.size() > 1) {
auto targetHisto = targetWS.histogram(i);
interpOpt.applyInPlace(spatiallyInterpHisto, targetHisto);
targetWS.setHistogram(i, targetHisto);
} else {
targetWS.mutableY(i) = spatiallyInterpHisto.y().front();
}
}
PARALLEL_END_INTERRUPT_REGION
}
PARALLEL_CHECK_INTERRUPT_REGION
}
} // namespace Mantid::Algorithms