15#include <boost/algorithm/string.hpp>
32 return std::dynamic_pointer_cast<CompositeFunction>(function);
35CompositeFunction_sptr createEmptyComposite() {
return toComposite(createIFunction(
"name=CompositeFunction")); }
37std::vector<std::string> getFunctionNamesInString(std::string
const &functionString) {
38 std::vector<std::string> functionNames;
40 if (str.substr(0, 5) ==
"name=")
41 functionNames.emplace_back(str.substr(5));
45bool isValueWithinConstraint(std::string
const &constraint,
double value) {
47 return limits.first.toDouble() <=
value &&
value <= limits.second.toDouble();
55 : m_workspaceName(
std::move(workspaceName)), m_workspaceIndex(workspaceIndex), m_startX(startX), m_endX(endX),
56 m_function(nullptr) {}
96 auto const functionNames = getFunctionNamesInString(function);
97 const std::string iFunctionName = iFunction->name();
98 const auto it = std::find_if(functionNames.cbegin(), functionNames.cend(),
99 [&iFunctionName](
const auto &name) { return iFunctionName == name; });
100 if (it != functionNames.cend())
105 for (
auto const &functionName : getFunctionNamesInString(function)) {
106 if (composite->hasFunction(functionName))
107 composite->removeFunction(composite->functionIndex(functionName));
110 if (composite->nFunctions() == 0)
112 else if (composite->nFunctions() == 1)
125 if (toComposite(function)) {
126 g_log.
error(
"Add function failed: Nested composite functions are not supported.");
130 if (
auto composite = toComposite(
m_function)) {
131 composite->addFunction(function);
133 auto newComposite = createEmptyComposite();
134 newComposite->addFunction(
m_function->clone());
135 newComposite->addFunction(function);
142 m_function->setParameter(parameter, newValue);
148 for (
auto paramIndex = 0u; paramIndex <
m_function->nParams(); ++paramIndex) {
149 if (
auto const tie =
m_function->getTie(paramIndex)) {
150 auto const parameterName =
m_function->parameterName(paramIndex);
160 throw std::runtime_error(
"The function does not contain the parameter " + parameter +
".");
165 throw std::runtime_error(
"This tie does not have a value.");
167 return std::stod(tie);
173 m_function->setAttribute(attribute, newValue);
179 throw std::runtime_error(
"The function does not contain this attribute.");
190 return m_function->getParameterStatus(
m_function->parameterIndex(parameter)) == IFunction::ParameterStatus::Active;
196 throw std::runtime_error(
"The parameter does not exist in this domain.");
207 return m_function->getParameterStatus(
m_function->parameterIndex(parameter)) == IFunction::ParameterStatus::Fixed;
209 throw std::runtime_error(
"The parameter does not exist in this domain.");
214 auto const parameterIndex =
m_function->parameterIndex(parameter);
215 if (
auto const tie =
m_function->getTie(parameterIndex)) {
216 auto const tieStr = tie->asString();
217 auto const equalsIndex = tieStr.find(
"=");
218 if (equalsIndex != std::string::npos)
219 return tieStr.substr(equalsIndex + 1);
223 throw std::runtime_error(
"The parameter does not exist in this domain.");
228 auto const parameterIndex =
m_function->parameterIndex(parameter);
229 auto const constraint =
m_function->getConstraint(parameterIndex);
230 return constraint ? constraint->asString() :
"";
232 throw std::runtime_error(
"The parameter does not exist in this domain.");
257 }
catch (std::invalid_argument
const &ex) {
260 }
catch (std::runtime_error
const &ex) {
273 std::string
const &constraint) {
275 if (functionIndex.empty())
277 else if (
auto composite = toComposite(
m_function))
283 std::string
const ¶meter, std::string
const &constraint) {
285 if (index < composite->nFunctions()) {
286 auto function = composite->getFunction(
index);
287 if (function->hasParameter(parameter))
288 function->addConstraints(constraint);
293 std::vector<std::string> tiedParameters;
295 for (
auto paramIndex = 0u; paramIndex <
m_function->nParams(); ++paramIndex)
298 return tiedParameters;
302 std::size_t
const ¶meterIndex)
const {
303 if (
auto const tie =
m_function->getTie(parameterIndex)) {
304 for (
auto rhsParameter : tie->getRHSParameters()) {
305 if (parameter == rhsParameter.parameterName())
306 tiedParameters.emplace_back(
m_function->parameterName(parameterIndex));
317 auto const parameterIndex =
m_function->parameterIndex(parameter);
318 if (
auto const constraint =
m_function->getConstraint(parameterIndex))
319 isValid = isValueWithinConstraint(constraint->asString(),
value);
322 g_log.
warning(
"The provided value for '" + parameter +
"' is not within its constraints.");
335 auto isValid =
false;
337 auto const parameterValue =
m_function->getParameter(parameter);
338 isValid = isValueWithinConstraint(constraint, parameterValue);
340 g_log.
warning(
"The provided constraint for '" + parameter +
"' does not encompass its current value.");
361 ")' could not be found.");
368 return std::pair<double, double>(xData.front(), xData.back());
371 throw std::invalid_argument(
"The workspace '" +
m_workspaceName +
"' is not a matrix workspace.");
double value
The value of the point.
IPeaksWorkspace_sptr workspace
std::map< DeltaEMode::Type, std::string > index
void setParameterValue(std::string const ¶meter, double newValue)
std::string domainName() const
void updateParameterConstraint(std::string const &functionIndex, std::string const ¶meter, std::string const &constraint)
void removeFunctionFromIFunction(std::string const &function, Mantid::API::IFunction_sptr &iFunction)
std::string getParameterTie(std::string const ¶meter) const
FitDomain(std::string workspaceName, WorkspaceIndex workspaceIndex, double startX, double endX)
double getParameterValue(std::string const ¶meter) const
bool isParameterValueWithinConstraints(std::string const ¶meter, double value) const
std::string workspaceName() const noexcept
void setParameterFixed(std::string const ¶meter, bool fix) const
WorkspaceIndex m_workspaceIndex
void addFunctionToExisting(Mantid::API::IFunction_sptr const &function)
void removeInvalidatedTies()
bool isValidParameterConstraint(std::string const ¶meter, std::string const &constraint) const
bool isParameterActive(std::string const ¶meter) const
bool hasParameter(std::string const ¶meter) const
void removeFunctionFromComposite(std::string const &function, Mantid::API::CompositeFunction_sptr &composite)
void setAttributeValue(std::string const &attribute, const Mantid::API::IFunction::Attribute &newValue)
Mantid::API::IFunction_sptr getFunctionCopy() const
void setFunction(Mantid::API::IFunction_sptr const &function)
void removeFunction(std::string const &function)
bool updateParameterTie(std::string const ¶meter, std::string const &tie)
Mantid::API::IFunction::Attribute getAttributeValue(std::string const &attribute) const
bool isValidStartX(double startX) const
bool setParameterTie(std::string const ¶meter, std::string const &tie)
std::string getParameterConstraint(std::string const ¶meter) const
double endX() const noexcept
double getTieValue(std::string const &tie) const
bool setEndX(double startX)
void removeParameterConstraint(std::string const ¶meter)
bool isParameterFixed(std::string const ¶meter) const
void clearParameterTie(std::string const ¶meter)
void appendParametersTiedTo(std::vector< std::string > &tiedParameters, std::string const ¶meter, std::size_t const ¶meterIndex) const
Mantid::API::IFunction_sptr m_function
void setWorkspaceName(std::string const &workspaceName)
void addFunction(Mantid::API::IFunction_sptr const &function)
bool isValidEndX(double endX) const
bool isValidParameterTie(std::string const ¶meter, std::string const &tie) const
bool setStartX(double startX)
std::vector< std::string > getParametersTiedTo(std::string const ¶meter) const
WorkspaceIndex workspaceIndex() const noexcept
double startX() const noexcept
std::string m_workspaceName
std::pair< double, double > xLimits() const
Attribute is a non-fitting parameter.
Base MatrixWorkspace Abstract Class.
The Logger class is in charge of the publishing messages from the framework through various channels.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
std::shared_ptr< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
std::string to_string(const wide_integer< Bits, Signed > &n)