-
Notifications
You must be signed in to change notification settings - Fork 122
/
AutoDiffTestAlg.cpp
212 lines (161 loc) · 6.68 KB
/
AutoDiffTestAlg.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
#include "MantidCurveFitting/AutoDiffTestAlg.h"
#include "MantidCurveFitting/GaussianNumDiff.h"
#include "MantidCurveFitting/GaussianAutoDiff.h"
#include "MantidCurveFitting/GaussianHandCoded.h"
#include "MantidCurveFitting/LorentzianFamily.h"
#include "MantidCurveFitting/PearsonVIIFamily.h"
#include "MantidAPI/FunctionDomain1D.h"
#include "MantidAPI/FunctionValues.h"
#include "MantidCurveFitting/Jacobian.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidKernel/Timer.h"
#include "MantidKernel/Logger.h"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
namespace Mantid
{
namespace CurveFitting
{
using Mantid::Kernel::Direction;
using Mantid::API::WorkspaceProperty;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AutoDiffTestAlg)
using namespace API;
//----------------------------------------------------------------------------------------------
/** Constructor
*/
AutoDiffTestAlg::AutoDiffTestAlg()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
AutoDiffTestAlg::~AutoDiffTestAlg()
{
}
//----------------------------------------------------------------------------------------------
const std::string AutoDiffTestAlg::name() const { return "AutoDiffTestAlg"; }
/// Algorithm's version for identification. @see Algorithm::version
int AutoDiffTestAlg::version() const { return 1;}
/// Algorithm's category for identification. @see Algorithm::category
const std::string AutoDiffTestAlg::category() const { return "Test"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string AutoDiffTestAlg::summary() const { return "Test"; }
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void AutoDiffTestAlg::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input), "Data with 20 gaussian peaks.");
declareProperty("GaussianParameters", "", "Function parameters");
declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace","",Direction::Output), "Data with 20 gaussian peaks.");
declareProperty("DerivativeType","adept","How to calculate derivatives.");
}
//----------------------------------------------------------------------------------------------
namespace
{
// Logger
Kernel::Logger g_log("AutoDiffTestAlg");
IFunction_sptr getFunction(const std::string& type)
{
if ( type == "adept" )
{
//return IFunction_sptr(new Lorentzians::LorentzianAutoDiff);
return IFunction_sptr(new Pearsons::PearsonVIIAutoDiff);
return IFunction_sptr(new GaussianAutoDiff);
} else if ( type == "num") {
//return IFunction_sptr(new Lorentzians::LorentzianNumDiff);
return IFunction_sptr(new Pearsons::PearsonVIINumDiff);
return IFunction_sptr(new GaussianNumDiff);
}
//return IFunction_sptr(new Lorentzians::LorentzianHandCoded);
return IFunction_sptr(new Pearsons::PearsonVIIHandCoded);
return IFunction_sptr(new GaussianHandCoded);
}
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void AutoDiffTestAlg::exec()
{
MatrixWorkspace_sptr fitData = getProperty("InputWorkspace");
m_derType = getPropertyValue("DerivativeType");
g_log.warning() << "Using " << m_derType << " derivatives." << std::endl;
std::string parameterString = getProperty("GaussianParameters");
std::vector<double> paramValues = parameterValues(parameterString);
IFunction_sptr f = getFunction(m_derType);
f->initialize();
if(fitData->getNumberHistograms() > (f->nParams() + 1)) {
for(size_t i = 0; i < paramValues.size(); ++i) {
f->setParameter(i, paramValues[i] - 0.001 * paramValues[i]);
}
IAlgorithm_sptr fitAlgorithm = createChildAlgorithm("Fit", -1, -1, true);
fitAlgorithm->setProperty("CreateOutput", true);
fitAlgorithm->setProperty("Output", "FitPeaks1D");
fitAlgorithm->setProperty("CalcErrors", true);
fitAlgorithm->setProperty("Function", f);
fitAlgorithm->setProperty("InputWorkspace", fitData);
fitAlgorithm->setProperty("WorkspaceIndex", static_cast<int>(f->nParams() + 1));
fitAlgorithm->execute();
g_log.notice() << "Fit results:" << std::endl;
for(size_t i = 0; i < paramValues.size(); ++i) {
g_log.notice() << " " << f->parameterName(i) << " = " << f->getParameter(i) << " (" << f->getError(i) << ")" << std::endl;
}
}
IFunction_sptr g = getFunction(m_derType);
g->initialize();
for(size_t i = 0; i < paramValues.size(); ++i) {
g->setParameter(i, paramValues[i]);
}
FunctionDomain1DVector x(fitData->readX(0));
FunctionValues y(x);
CurveFitting::Jacobian J(x.size(), g->nParams());
Kernel::Timer timerF;
for(size_t i = 0; i < 10000; ++i) {
g->function(x, y);
}
g_log.warning() << "Calculating function took " << timerF.elapsed() / 10000 << " seconds to complete." << std::endl;
Kernel::Timer timerDf;
for(size_t i = 0; i < 10000; ++i) {
g->functionDeriv(x, J);
}
g_log.warning() << "Calculating derivatives took " << timerDf.elapsed() / 10000 << " seconds to complete." << std::endl;
MatrixWorkspace_sptr t = WorkspaceFactory::Instance().create(fitData);
MantidVec &yd = t->dataY(0);
const MantidVec &yr = fitData->readY(0);
for(size_t i = 0; i < x.size(); ++i) {
yd[i] = 1.0 - y[i] / yr[i];
if(std::isinf(yd[i])) {
yd[i] = 0.0;
}
}
for(size_t j = 1; (j < f->nParams() + 1); ++j) {
MantidVec &partialDeriv = t->dataY(j);
const MantidVec &reference = fitData->readY(j);
for(size_t i = 0; i < partialDeriv.size(); ++i) {
partialDeriv[i] = 1.0 - J.get(i, j - 1) / reference[i];
if(std::isinf(partialDeriv[i])) {
partialDeriv[i] = 0.0;
}
}
}
for(size_t i = 0; i < fitData->getNumberHistograms(); ++i) {
MantidVec &xd = t->dataX(i);
xd = fitData->readX(i);
}
// test accuracy
setProperty("OutputWorkspace", t);
}
std::vector<double> AutoDiffTestAlg::parameterValues(const std::string ¶meterString) const
{
std::vector<std::string> strings;
boost::split(strings, parameterString, boost::is_any_of(", "));
std::vector<double> values(strings.size());
std::cout << std::setprecision(17);
for(size_t i = 0; i < values.size(); ++i) {
values[i] = boost::lexical_cast<double>(strings[i]);
}
return values;
}
} // namespace CurveFitting
} // namespace Mantid