-
Notifications
You must be signed in to change notification settings - Fork 122
/
ConvolveWorkspaces.cpp
102 lines (85 loc) · 3.49 KB
/
ConvolveWorkspaces.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
// 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 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/Algorithms/ConvolveWorkspaces.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidCurveFitting/Functions/Convolution.h"
#include "MantidCurveFitting/Functions/TabulatedFunction.h"
#include <gsl/gsl_errno.h>
#include <gsl/gsl_fft_halfcomplex.h>
#include <gsl/gsl_fft_real.h>
#include <sstream>
namespace Mantid {
namespace CurveFitting {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ConvolveWorkspaces)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
using namespace Geometry;
using namespace Functions;
void ConvolveWorkspaces::init() {
declareProperty(std::make_unique<WorkspaceProperty<Workspace2D>>(
"Workspace1", "", Direction::Input),
"The name of the first input workspace.");
declareProperty(std::make_unique<WorkspaceProperty<Workspace2D>>(
"Workspace2", "", Direction::Input),
"The name of the second input workspace.");
declareProperty(std::make_unique<WorkspaceProperty<Workspace2D>>(
"OutputWorkspace", "", Direction::Output),
"The name of the output workspace.");
}
void ConvolveWorkspaces::exec() {
std::string ws1name = getProperty("Workspace1");
std::string ws2name = getProperty("Workspace2");
Workspace2D_sptr ws1 = getProperty("Workspace1");
Workspace2D_sptr ws2 = getProperty("Workspace2");
// Cache a few things for later use
const size_t numHists = ws1->getNumberHistograms();
Workspace2D_sptr outputWS = std::dynamic_pointer_cast<Workspace2D>(
WorkspaceFactory::Instance().create(ws1));
// First check that the workspace are the same size
if (numHists != ws2->getNumberHistograms()) {
throw std::runtime_error("Size mismatch");
}
m_progress = std::make_unique<Progress>(this, 0.0, 1.0, numHists);
// Now convolve the histograms
PARALLEL_FOR_IF(Kernel::threadSafe(*ws1, *ws2, *outputWS))
for (int i = 0; i < static_cast<int>(numHists); ++i) {
PARALLEL_START_INTERUPT_REGION
m_progress->report();
outputWS->setSharedX(i, ws1->sharedX(i));
auto &Yout = outputWS->mutableY(i);
Convolution conv;
auto res = std::make_shared<TabulatedFunction>();
res->setAttributeValue("Workspace", ws1name);
res->setAttributeValue("WorkspaceIndex", i);
conv.addFunction(res);
auto fun = std::make_shared<TabulatedFunction>();
fun->setAttributeValue("Workspace", ws2name);
fun->setAttributeValue("WorkspaceIndex", i);
conv.addFunction(fun);
size_t N = Yout.size();
const double *firstX = &outputWS->mutableX(i)[0];
FunctionDomain1DView xView(firstX, N);
FunctionValues out(xView);
conv.function(xView, out);
for (size_t j = 0; j < N; j++) {
Yout[j] = out.getCalculated(j);
}
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
// Assign it to the output workspace property
setProperty("OutputWorkspace", outputWS);
}
} // namespace Algorithms
} // namespace CurveFitting
} // namespace Mantid