-
Notifications
You must be signed in to change notification settings - Fork 122
/
CrystalFieldSpectrum.cpp
187 lines (166 loc) · 6.02 KB
/
CrystalFieldSpectrum.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
#include "MantidCurveFitting/Functions/CrystalFieldSpectrum.h"
#include "MantidCurveFitting/Functions/CrystalFieldPeaks.h"
#include "MantidCurveFitting/Functions/CrystalFieldPeakUtils.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/IConstraint.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ParameterTie.h"
#include "MantidCurveFitting/Constraints/BoundaryConstraint.h"
#include "MantidKernel/Strings.h"
#include <algorithm>
#include <iostream>
namespace Mantid {
namespace CurveFitting {
namespace Functions {
using namespace CurveFitting;
using namespace Kernel;
using namespace API;
DECLARE_FUNCTION(CrystalFieldSpectrum)
/// Constructor
CrystalFieldSpectrum::CrystalFieldSpectrum()
: FunctionGenerator(IFunction_sptr(new CrystalFieldPeaks)), m_nPeaks(0) {
declareAttribute("PeakShape", Attribute("Lorentzian"));
declareAttribute("FWHM", Attribute(0.0));
std::vector<double> vec;
declareAttribute("FWHMX", Attribute(vec));
declareAttribute("FWHMY", Attribute(vec));
declareAttribute("FWHMVariation", Attribute(0.1));
declareAttribute("NPeaks", Attribute(0));
declareAttribute("FixAllPeaks", Attribute(false));
}
/// Uses m_crystalField to calculate peak centres and intensities
/// then populates m_spectrum with peaks of type given in PeakShape attribute.
void CrystalFieldSpectrum::buildTargetFunction() const {
m_dirty = false;
auto spectrum = new CompositeFunction;
m_target.reset(spectrum);
m_target->setAttribute("NumDeriv", this->getAttribute("NumDeriv"));
FunctionDomainGeneral domain;
FunctionValues values;
m_source->function(domain, values);
if (values.size() == 0) {
return;
}
if (values.size() % 2 != 0) {
throw std::runtime_error(
"CrystalFieldPeaks returned odd number of values.");
}
auto xVec = getAttribute("FWHMX").asVector();
auto yVec = getAttribute("FWHMY").asVector();
auto fwhmVariation = getAttribute("FWHMVariation").asDouble();
auto peakShape = getAttribute("PeakShape").asString();
auto defaultFWHM = getAttribute("FWHM").asDouble();
size_t nRequiredPeaks = getAttribute("NPeaks").asInt();
bool fixAllPeaks = getAttribute("FixAllPeaks").asBool();
m_nPeaks = CrystalFieldUtils::buildSpectrumFunction(
*spectrum, peakShape, values, xVec, yVec, fwhmVariation, defaultFWHM,
nRequiredPeaks, fixAllPeaks);
storeReadOnlyAttribute("NPeaks", Attribute(static_cast<int>(m_nPeaks)));
}
/// Update m_spectrum function.
void CrystalFieldSpectrum::updateTargetFunction() const {
if (!m_target) {
buildTargetFunction();
return;
}
m_dirty = false;
auto xVec = getAttribute("FWHMX").asVector();
auto yVec = getAttribute("FWHMY").asVector();
auto fwhmVariation = getAttribute("FWHMVariation").asDouble();
FunctionDomainGeneral domain;
FunctionValues values;
m_source->function(domain, values);
m_target->setAttribute("NumDeriv", this->getAttribute("NumDeriv"));
auto &spectrum = dynamic_cast<CompositeFunction &>(*m_target);
m_nPeaks = CrystalFieldUtils::calculateNPeaks(values);
auto maxNPeaks = CrystalFieldUtils::calculateMaxNPeaks(m_nPeaks);
if (maxNPeaks > spectrum.nFunctions()) {
buildTargetFunction();
} else {
CrystalFieldUtils::updateSpectrumFunction(spectrum, values, m_nPeaks, 0,
xVec, yVec, fwhmVariation);
}
storeReadOnlyAttribute("NPeaks", Attribute(static_cast<int>(m_nPeaks)));
}
/// Custom string conversion method
std::string CrystalFieldSpectrum::asString() const {
std::ostringstream ostr;
ostr << "name=" << this->name();
// Print the attributes
std::vector<std::string> attr = this->getAttributeNames();
for (const auto &attName : attr) {
std::string attValue = this->getAttribute(attName).value();
if (!attValue.empty() && attValue != "\"\"" && attValue != "()") {
ostr << ',' << attName << '=' << attValue;
}
}
// Print own parameters
for (size_t i = 0; i < m_nOwnParams; i++) {
const ParameterTie *tie = getTie(i);
if (!tie || !tie->isDefault()) {
ostr << ',' << parameterName(i) << '=' << getParameter(i);
}
}
// collect non-default constraints
std::vector<std::string> constraints;
for (size_t i = 0; i < m_nOwnParams; i++) {
auto constraint = writeConstraint(i);
if (!constraint.empty()) {
constraints.push_back(constraint);
}
}
// collect the non-default ties
std::vector<std::string> ties;
for (size_t i = 0; i < m_nOwnParams; i++) {
auto tie = writeTie(i);
if (!tie.empty()) {
ties.push_back(tie);
}
}
// Print parameters of the important peaks only
const CompositeFunction &spectrum =
dynamic_cast<const CompositeFunction &>(*m_target);
for (size_t ip = 0; ip < m_nPeaks; ++ip) {
const auto &peak = dynamic_cast<IPeakFunction &>(*spectrum.getFunction(ip));
// Print peak's atributes
auto attr = peak.getAttributeNames();
for (const auto &attName : attr) {
std::string attValue = peak.getAttribute(attName).value();
if (!attValue.empty() && attValue != "\"\"") {
ostr << ",f" << ip << "." << attName << '=' << attValue;
}
}
// Print peak's parameters
for (size_t i = 0; i < peak.nParams(); i++) {
const ParameterTie *tie = peak.getTie(i);
if (!tie || !tie->isDefault()) {
ostr << ",f" << ip << "." << peak.parameterName(i) << '='
<< peak.getParameter(i);
}
auto constraint = writeConstraint(i);
if (!constraint.empty()) {
constraints.push_back(constraint);
}
auto tieStr = writeTie(i);
if (!tieStr.empty()) {
ties.push_back(tieStr);
}
}
} // for peaks
// print constraints
if (!constraints.empty()) {
ostr << ",constraints=("
<< Kernel::Strings::join(constraints.begin(), constraints.end(), ",")
<< ")";
}
// print the ties
if (!ties.empty()) {
ostr << ",ties=(" << Kernel::Strings::join(ties.begin(), ties.end(), ",")
<< ")";
}
return ostr.str();
}
} // namespace Functions
} // namespace CurveFitting
} // namespace Mantid