Mantid
Loading...
Searching...
No Matches
LeBailFit.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 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
11#include "MantidAPI/IFunction.h"
19#include "MantidKernel/System.h"
20
21#include <gsl/gsl_sf_erf.h>
22
23namespace Mantid {
24namespace HistogramData {
25class HistogramX;
26class HistogramY;
27} // namespace HistogramData
28
29namespace CurveFitting {
30namespace Algorithms {
31
37struct Parameter {
38 // Regular
39 std::string name;
40 double curvalue = 0;
41 double prevalue = 0;
42 double minvalue = 0;
43 double maxvalue = 0;
44 bool fit = false;
45 double stepsize = 0;
46 double fiterror = 0;
47 // Monte Carlo
48 bool nonnegative = false;
49 double mcA0 = 0;
50 double mcA1 = 0;
51 // Monte Carlo record
52 double sumstepsize = 0;
53 double maxabsstepsize = 0;
54 double maxrecordvalue = 0;
55 double minrecordvalue = 0;
56 size_t numpositivemove = 0;
57 size_t numnegativemove = 0;
58 size_t numnomove = 0;
60};
61
62class MANTID_CURVEFITTING_DLL LeBailFit final : public API::Algorithm {
63public:
65 enum FunctionMode { CALCULATION, FIT, BACKGROUNDPROCESS, MONTECARLO };
66
67 LeBailFit();
68
70 const std::string name() const override { return "LeBailFit"; }
72 const std::string summary() const override { return "Do LeBail Fit to a spectrum of powder diffraction data. "; }
73
75 int version() const override { return 1; }
76 const std::vector<std::string> seeAlso() const override { return {"CreateLeBailFitInput", "FitPowderDiffPeaks"}; }
77
79 const std::string category() const override { return "Diffraction\\Fitting"; }
80
81private:
82 // Implement abstract Algorithm methods
83 void init() override;
84 // Implement abstract Algorithm methods
85 void exec() override;
86
88 void processInputProperties();
89
90 //-------------- Pattern Calculation & Minimizing -------------------
92 void execPatternCalculation();
93
95 void execRefineBackground();
96
97 //-------------- Functions to set up the Le Bail Fit -----------------
99 void createLeBailFunction();
100
102 API::MatrixWorkspace_sptr cropWorkspace(const API::MatrixWorkspace_sptr &inpws, size_t wsindex);
103
105 void processInputBackground();
106
107 //-------------- Le Bail Formular: Calculate Peak Intensities ------------
109 bool calculatePeaksIntensities(API::MatrixWorkspace_sptr dataws, size_t workspaceindex, bool zerobackground,
110 std::vector<double> &allpeaksvalues);
111
112 //-------------- Import and Export ---------------------------------------
114 void parseInstrumentParametersTable();
115
117 void parseBraggPeaksParametersTable();
118
120 void parseBackgroundTableWorkspace(const DataObjects::TableWorkspace_sptr &bkgdparamws,
121 std::vector<std::string> &bkgdparnames, std::vector<double> &bkgdorderparams);
122
124 void exportBraggPeakParameterToTable();
125
127 void exportInstrumentParameterToTable(std::map<std::string, Parameter> parammap);
128
130 void createOutputDataWorkspace();
131
132 //-------------- Random Walk Suite ----------------------------------------
134 void execRandomWalkMinimizer(size_t maxcycles, std::map<std::string, Parameter> &parammap);
135
137 void doMarkovChain(const std::map<std::string, Parameter> &parammap, const Mantid::HistogramData::HistogramX &vecX,
138 const Mantid::HistogramData::HistogramY &vecPurePeak, const std::vector<double> &vecBkgd,
139 size_t maxcycles, const Kernel::Rfactor &startR, int randomseed);
140
142 void setupBuiltInRandomWalkStrategy();
143
144 void setupRandomWalkStrategyFromTable(const DataObjects::TableWorkspace_sptr &tablews);
145
147 void addParameterToMCMinimize(std::vector<std::string> &parnamesforMC, const std::string &parname);
148
150 bool calculateDiffractionPattern(const Mantid::HistogramData::HistogramX &vecX,
151 const Mantid::HistogramData::HistogramY &vecY, bool inputraw, bool outputwithbkgd,
152 const Mantid::HistogramData::HistogramY &vecBkgd, std::vector<double> &values,
153 Kernel::Rfactor &rfactor);
154
156 bool acceptOrDeny(Kernel::Rfactor currR, Kernel::Rfactor newR);
157
159 bool proposeNewValues(const std::vector<std::string> &mcgroup, Kernel::Rfactor r,
160 std::map<std::string, Parameter> &curparammap, std::map<std::string, Parameter> &newparammap,
161 bool prevBetterRwp);
162
164 double limitProposedValueInBound(const Parameter &param, double newvalue, double direction, int choice);
165
167 void bookKeepBestMCResult(std::map<std::string, Parameter> parammap, const std::vector<double> &bkgddata,
168 Kernel::Rfactor rfactor, size_t istep);
169
171 void applyParameterValues(std::map<std::string, Parameter> &srcparammap,
172 std::map<std::string, Parameter> &tgtparammap);
173
175 void storeBackgroundParameters(std::vector<double> &bkgdparamvec);
176
179 void recoverBackgroundParameters(const std::vector<double> &bkgdparamvec);
180
182 void proposeNewBackgroundValues();
183
184 //--------------------------------------------------------------------------------------------
185
188
194
195 size_t m_wsIndex;
196
197 double m_startX, m_endX;
198
200 std::vector<std::pair<std::vector<int>, double>> m_inputPeakInfoVec;
201
204
206 std::map<std::string, Parameter> m_funcParameters; // char = f: fit... = t: tie to value
208 std::map<std::string, double> m_origFuncParameters;
209
211 std::map<std::string, double> convertToDoubleMap(std::map<std::string, Parameter> &inmap);
212
214
215 std::string m_peakType;
216
218 std::string m_backgroundType;
219
221 std::vector<double> m_backgroundParameters;
222 std::vector<std::string> m_backgroundParameterNames;
223 unsigned int m_bkgdorder;
224
227
231
233 std::string mMinimizer;
236
240
243
245
246 //-------------------------- Monte Carlo Variables--------------------------
247 std::map<int, std::vector<std::string>> m_MCGroups;
249
250 double m_bestRwp;
251 double m_bestRp;
252
253 std::map<std::string, Parameter> m_bestParameters;
254 std::vector<double> m_bestBackgroundData;
256
259
262
266
268 enum { RANDOMWALK, DRUNKENWALK } m_walkStyle;
269
272
275
276 //------------------------ Background Refinement Variables
277 //-----------------------
278 std::vector<std::string> m_bkgdParameterNames;
280 std::vector<double> m_bkgdParameterBuffer;
281 std::vector<double> m_bestBkgdParams;
283 std::vector<double> m_bkgdParameterStepVec;
284
286};
287
289void writeRfactorsToFile(std::vector<double> vecX, std::vector<Kernel::Rfactor> vecR, const std::string &filename);
290
291} // namespace Algorithms
292} // namespace CurveFitting
293} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
std::vector< std::pair< std::vector< int >, double > > m_inputPeakInfoVec
Input Bragg peak information for future processing;.
Definition: LeBailFit.h:200
Functions::BackgroundFunction_sptr m_backgroundFunction
Background function.
Definition: LeBailFit.h:203
bool m_tolerateInputDupHKL2Peaks
Flag to allow peaks with duplicated (HKL)^2 in input .hkl file.
Definition: LeBailFit.h:274
std::map< std::string, double > m_origFuncParameters
Input function parameters that are stored for reference.
Definition: LeBailFit.h:208
bool m_inputParameterPhysical
Flag to show whether the input profile parameters are physical to all peaks.
Definition: LeBailFit.h:239
const std::string category() const override
Algorithm's category for identification overriding a virtual method.
Definition: LeBailFit.h:79
std::map< std::string, Parameter > m_funcParameters
Function parameters updated by fit.
Definition: LeBailFit.h:206
LeBailFunction_sptr m_lebailFunction
Le Bail Function (Composite)
Definition: LeBailFit.h:187
std::vector< double > m_backgroundParameters
Background polynomials.
Definition: LeBailFit.h:221
std::vector< double > m_bkgdParameterBuffer
Definition: LeBailFit.h:280
std::vector< std::string > m_bkgdParameterNames
Definition: LeBailFit.h:278
const std::string summary() const override
Summary of algorithms purpose.
Definition: LeBailFit.h:72
int version() const override
Algorithm's version for identification overriding a virtual method.
Definition: LeBailFit.h:75
double m_minimumPeakHeight
Minimum height of a peak to be counted in smoothing background.
Definition: LeBailFit.h:271
DataObjects::Workspace2D_sptr m_outputWS
Definition: LeBailFit.h:191
std::map< int, std::vector< std::string > > m_MCGroups
Definition: LeBailFit.h:247
bool m_useAnnealing
Flag to use Annealing Simulation (i.e., use automatic adjusted temperature)
Definition: LeBailFit.h:265
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso (these are not validated) algorithms related to this algorithm....
Definition: LeBailFit.h:76
API::MatrixWorkspace_sptr m_dataWS
Instance data.
Definition: LeBailFit.h:190
size_t m_numMinimizeSteps
Number of minimization steps. For both MC and regular.
Definition: LeBailFit.h:258
std::map< std::string, Parameter > m_bestParameters
Definition: LeBailFit.h:253
DataObjects::TableWorkspace_sptr parameterWS
Definition: LeBailFit.h:192
std::vector< double > m_bestBackgroundData
Definition: LeBailFit.h:254
double m_Temperature
Monte Carlo temperature.
Definition: LeBailFit.h:261
std::vector< std::string > m_backgroundParameterNames
Definition: LeBailFit.h:222
std::string m_backgroundType
Background type.
Definition: LeBailFit.h:218
std::string m_peakType
============================= =========================== ///
Definition: LeBailFit.h:215
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Definition: LeBailFit.h:70
std::vector< double > m_bkgdParameterStepVec
Definition: LeBailFit.h:283
bool calculatePeaksIntensities(API::MatrixWorkspace_sptr dataws, size_t workspaceindex, bool zerobackground, std::vector< double > &allpeaksvalues)
Calcualte peak heights from model to data.
DataObjects::TableWorkspace_sptr reflectionWS
Definition: LeBailFit.h:193
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< LeBailFunction > LeBailFunction_sptr
void writeRfactorsToFile(std::vector< double > vecX, std::vector< Kernel::Rfactor > vecR, const std::string &filename)
Write a set of (XY) data to a column file.
Definition: LeBailFit.cpp:2225
std::shared_ptr< BackgroundFunction > BackgroundFunction_sptr
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
Helper class which provides the Collimation Length for SANS instruments.
LeBailFit : Algorithm to do Le Bail Fit.
Definition: LeBailFit.h:37
R factor for powder data analysis.
Definition: Statistics.h:52