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"
15#include "MantidCurveFitting/DllConfig.h"
20
21#include <gsl/gsl_sf_erf.h>
22
23#include <span>
24
25namespace Mantid {
26namespace HistogramData {
27class HistogramX;
28class HistogramY;
29} // namespace HistogramData
30
31namespace CurveFitting {
32namespace Algorithms {
33
39struct Parameter {
40 // Regular
41 std::string name;
42 double curvalue = 0;
43 double prevalue = 0;
44 double minvalue = 0;
45 double maxvalue = 0;
46 bool fit = false;
47 double stepsize = 0;
48 double fiterror = 0;
49 // Monte Carlo
50 bool nonnegative = false;
51 double mcA0 = 0;
52 double mcA1 = 0;
53 // Monte Carlo record
54 double sumstepsize = 0;
55 double maxabsstepsize = 0;
56 double maxrecordvalue = 0;
57 double minrecordvalue = 0;
58 size_t numpositivemove = 0;
59 size_t numnegativemove = 0;
60 size_t numnomove = 0;
62};
63
64class MANTID_CURVEFITTING_DLL LeBailFit final : public API::Algorithm {
65public:
67 enum FunctionMode { CALCULATION, FIT, BACKGROUNDPROCESS, MONTECARLO };
68
69 LeBailFit();
70
72 const std::string name() const override { return "LeBailFit"; }
74 const std::string summary() const override { return "Do LeBail Fit to a spectrum of powder diffraction data. "; }
75
77 int version() const override { return 1; }
78 const std::vector<std::string> seeAlso() const override { return {"CreateLeBailFitInput", "FitPowderDiffPeaks"}; }
79
81 const std::string category() const override { return "Diffraction\\Fitting"; }
82
83private:
84 // Implement abstract Algorithm methods
85 void init() override;
86 // Implement abstract Algorithm methods
87 void exec() override;
88
90 void processInputProperties();
91
92 //-------------- Pattern Calculation & Minimizing -------------------
94 void execPatternCalculation();
95
97 void execRefineBackground();
98
99 //-------------- Functions to set up the Le Bail Fit -----------------
101 void createLeBailFunction();
102
104 API::MatrixWorkspace_sptr cropWorkspace(const API::MatrixWorkspace_sptr &inpws, size_t wsindex);
105
107 void processInputBackground();
108
109 //-------------- Le Bail Formular: Calculate Peak Intensities ------------
111 bool calculatePeaksIntensities(API::MatrixWorkspace_sptr dataws, size_t workspaceindex, bool zerobackground,
112 std::vector<double> &allpeaksvalues);
113
114 //-------------- Import and Export ---------------------------------------
116 void parseInstrumentParametersTable();
117
119 void parseBraggPeaksParametersTable();
120
122 void parseBackgroundTableWorkspace(const DataObjects::TableWorkspace_sptr &bkgdparamws,
123 std::vector<std::string> &bkgdparnames, std::vector<double> &bkgdorderparams);
124
126 void exportBraggPeakParameterToTable();
127
129 void exportInstrumentParameterToTable(const std::map<std::string, Parameter> &parammap);
130
132 void createOutputDataWorkspace();
133
134 //-------------- Random Walk Suite ----------------------------------------
136 void execRandomWalkMinimizer(size_t maxcycles, std::map<std::string, Parameter> &parammap);
137
139 void doMarkovChain(const std::map<std::string, Parameter> &parammap, const Mantid::HistogramData::HistogramX &vecX,
140 const Mantid::HistogramData::HistogramY &vecPurePeak,
141 const Mantid::HistogramData::HistogramY &vecBkgd, size_t maxcycles, const Kernel::Rfactor &startR,
142 int randomseed);
143
145 void setupBuiltInRandomWalkStrategy();
146
147 void setupRandomWalkStrategyFromTable(const DataObjects::TableWorkspace_sptr &tablews);
148
150 void addParameterToMCMinimize(std::vector<std::string> &parnamesforMC, const std::string &parname);
151
153 bool calculateDiffractionPattern(const Mantid::HistogramData::HistogramX &vecX,
154 const Mantid::HistogramData::HistogramY &vecY, bool inputraw, bool outputwithbkgd,
155 const Mantid::HistogramData::HistogramY &vecBkgd, std::vector<double> &values,
156 Kernel::Rfactor &rfactor);
157
159 bool acceptOrDeny(Kernel::Rfactor currR, Kernel::Rfactor newR);
160
162 bool proposeNewValues(const std::vector<std::string> &mcgroup, Kernel::Rfactor r,
163 std::map<std::string, Parameter> &curparammap, std::map<std::string, Parameter> &newparammap,
164 bool prevBetterRwp);
165
167 double limitProposedValueInBound(const Parameter &param, double newvalue, double direction, int choice);
168
170 void bookKeepBestMCResult(const std::map<std::string, Parameter> &parammap, std::span<double const> bkgddata,
171 Kernel::Rfactor rfactor, size_t istep);
172
174 void applyParameterValues(const std::map<std::string, Parameter> &srcparammap,
175 std::map<std::string, Parameter> &tgtparammap);
176
178 void storeBackgroundParameters(std::vector<double> &bkgdparamvec);
179
182 void recoverBackgroundParameters(const std::vector<double> &bkgdparamvec);
183
185 void proposeNewBackgroundValues();
186
187 //--------------------------------------------------------------------------------------------
188
191
197
198 size_t m_wsIndex;
199
200 double m_startX, m_endX;
201
203 std::vector<std::pair<std::vector<int>, double>> m_inputPeakInfoVec;
204
207
209 std::map<std::string, Parameter> m_funcParameters; // char = f: fit... = t: tie to value
211 std::map<std::string, double> m_origFuncParameters;
212
214 std::map<std::string, double> convertToDoubleMap(std::map<std::string, Parameter> &inmap);
215
217
218 std::string m_peakType;
219
221 std::string m_backgroundType;
222
224 std::vector<double> m_backgroundParameters;
225 std::vector<std::string> m_backgroundParameterNames;
226 unsigned int m_bkgdorder;
227
230
234
236 std::string mMinimizer;
239
243
246
248
249 //-------------------------- Monte Carlo Variables--------------------------
250 std::map<int, std::vector<std::string>> m_MCGroups;
252
253 double m_bestRwp;
254 double m_bestRp;
255
256 std::map<std::string, Parameter> m_bestParameters;
257 std::vector<double> m_bestBackgroundData;
259
262
265
269
271 enum { RANDOMWALK, DRUNKENWALK } m_walkStyle;
272
275
278
279 //------------------------ Background Refinement Variables
280 //-----------------------
281 std::vector<std::string> m_bkgdParameterNames;
283 std::vector<double> m_bkgdParameterBuffer;
284 std::vector<double> m_bestBkgdParams;
286 std::vector<double> m_bkgdParameterStepVec;
287
289};
290
292void writeRfactorsToFile(const std::vector<double> &vecX, const std::vector<Kernel::Rfactor> &vecR,
293 const std::string &filename);
294
295} // namespace Algorithms
296} // namespace CurveFitting
297} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
std::vector< std::pair< std::vector< int >, double > > m_inputPeakInfoVec
Input Bragg peak information for future processing;.
Definition LeBailFit.h:203
Functions::BackgroundFunction_sptr m_backgroundFunction
Background function.
Definition LeBailFit.h:206
bool m_tolerateInputDupHKL2Peaks
Flag to allow peaks with duplicated (HKL)^2 in input .hkl file.
Definition LeBailFit.h:277
std::map< std::string, double > m_origFuncParameters
Input function parameters that are stored for reference.
Definition LeBailFit.h:211
bool m_inputParameterPhysical
Flag to show whether the input profile parameters are physical to all peaks.
Definition LeBailFit.h:242
const std::string category() const override
Algorithm's category for identification overriding a virtual method.
Definition LeBailFit.h:81
std::map< std::string, Parameter > m_funcParameters
Function parameters updated by fit.
Definition LeBailFit.h:209
LeBailFunction_sptr m_lebailFunction
Le Bail Function (Composite)
Definition LeBailFit.h:190
std::vector< double > m_backgroundParameters
Background polynomials.
Definition LeBailFit.h:224
std::vector< double > m_bkgdParameterBuffer
Definition LeBailFit.h:283
std::vector< std::string > m_bkgdParameterNames
Definition LeBailFit.h:281
const std::string summary() const override
Summary of algorithms purpose.
Definition LeBailFit.h:74
int version() const override
Algorithm's version for identification overriding a virtual method.
Definition LeBailFit.h:77
double m_minimumPeakHeight
Minimum height of a peak to be counted in smoothing background.
Definition LeBailFit.h:274
DataObjects::Workspace2D_sptr m_outputWS
Definition LeBailFit.h:194
std::map< int, std::vector< std::string > > m_MCGroups
Definition LeBailFit.h:250
bool m_useAnnealing
Flag to use Annealing Simulation (i.e., use automatic adjusted temperature)
Definition LeBailFit.h:268
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:78
API::MatrixWorkspace_sptr m_dataWS
Instance data.
Definition LeBailFit.h:193
size_t m_numMinimizeSteps
Number of minimization steps. For both MC and regular.
Definition LeBailFit.h:261
std::map< std::string, Parameter > m_bestParameters
Definition LeBailFit.h:256
DataObjects::TableWorkspace_sptr parameterWS
Definition LeBailFit.h:195
double m_Temperature
Monte Carlo temperature.
Definition LeBailFit.h:264
std::vector< std::string > m_backgroundParameterNames
Definition LeBailFit.h:225
std::string m_backgroundType
Background type.
Definition LeBailFit.h:221
std::string m_peakType
============================= =========================== ///
Definition LeBailFit.h:218
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Definition LeBailFit.h:72
std::vector< double > m_bkgdParameterStepVec
Definition LeBailFit.h:286
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:196
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< LeBailFunction > LeBailFunction_sptr
void writeRfactorsToFile(const std::vector< double > &vecX, const std::vector< Kernel::Rfactor > &vecR, const std::string &filename)
Write a set of (XY) data to a column file.
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:39
R factor for powder data analysis.
Definition Statistics.h:66