Mantid
Loading...
Searching...
No Matches
MultiDomainCreator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
16#include "MantidKernel/Logger.h"
17
18#include <memory>
19#include <sstream>
20#include <stdexcept>
21
22namespace Mantid::CurveFitting {
23
24namespace {
25Kernel::Logger g_log("MultiDomainCreator");
26}
27
29 m_creators[i] = std::shared_ptr<IDomainCreator>(creator);
30}
31
36bool MultiDomainCreator::hasCreator(size_t i) const { return static_cast<bool>(m_creators[i]); }
37
39void MultiDomainCreator::createDomain(std::shared_ptr<API::FunctionDomain> &domain,
40 std::shared_ptr<API::FunctionValues> &ivalues, size_t i0) {
41 if (m_workspacePropertyNames.size() != m_creators.size()) {
42 throw std::runtime_error("Cannot create JointDomain: number of workspaces does not match "
43 "the number of creators");
44 }
45 auto jointDomain = std::make_unique<API::JointDomain>();
47 i0 = 0;
48 for (auto &creator : m_creators) {
49 if (!creator) {
50 throw std::runtime_error("Missing domain creator");
51 }
53 creator->ignoreInvalidData(m_ignoreInvalidData);
54 creator->createDomain(dom, values, i0);
55 creator->separateCompositeMembersInOutput(m_outputCompositeMembers, m_convolutionCompositeMembers);
56 jointDomain->addDomain(dom);
57 i0 += dom->size();
58 }
59 domain.reset(jointDomain.release());
60 ivalues = values;
61}
62
68 auto mdFunction = std::dynamic_pointer_cast<API::MultiDomainFunction>(function);
69 if (mdFunction) {
70 // loop over member functions and init them
71 for (size_t iFun = 0; iFun < mdFunction->nFunctions(); ++iFun) {
72 std::vector<size_t> domainIndices;
73 // get domain indices for this function
74 mdFunction->getDomainIndices(iFun, m_creators.size(), domainIndices);
75 if (!domainIndices.empty()) {
76 size_t index = domainIndices[0];
77 if (index >= m_creators.size()) {
78 std::stringstream msg;
79 msg << "Domain index is out of range. (Function #" << iFun << ")";
80 throw std::runtime_error(msg.str());
81 }
82 m_creators[index]->initFunction(mdFunction->getFunction(iFun));
83 } else {
84 g_log.warning() << "Function #" << iFun << " doesn't apply to any domain\n";
85 }
86 }
87 } else {
89 }
90}
91
105std::shared_ptr<API::Workspace> MultiDomainCreator::createOutputWorkspace(
106 const std::string &baseName, API::IFunction_sptr function, std::shared_ptr<API::FunctionDomain> domain,
107 std::shared_ptr<API::FunctionValues> values, const std::string &outputWorkspacePropertyName) {
108 UNUSED_ARG(domain);
109 UNUSED_ARG(values);
110
111 // split the function into independent parts
112 std::vector<API::IFunction_sptr> functions = function->createEquivalentFunctions();
113 // there must be as many parts as there are domains
114 if (functions.size() != m_creators.size()) {
115 throw std::runtime_error("Number of functions and domains don't match");
116 }
117
119
120 for (size_t i = 0; i < functions.size(); ++i) {
121 std::string localName = baseName + "Workspace_" + std::to_string(i);
122 auto fun = functions[i];
123 auto creator = m_creators[i];
124 std::shared_ptr<API::FunctionDomain> localDomain;
125 std::shared_ptr<API::FunctionValues> localValues;
126 fun->setUpForFit();
127 creator->createDomain(localDomain, localValues);
128 creator->initFunction(fun);
129 auto ws = creator->createOutputWorkspace(localName, fun, localDomain, localValues, "");
130 API::AnalysisDataService::Instance().addOrReplace(localName, ws);
131 outWS->addWorkspace(ws);
132 }
133
134 if (!outputWorkspacePropertyName.empty()) {
137 "Name of the output Workspace holding resulting simulated spectrum");
138 m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspaces");
139 m_manager->setProperty(outputWorkspacePropertyName, outWS);
140 }
141
142 return outWS;
143}
144
145} // namespace Mantid::CurveFitting
std::map< DeltaEMode::Type, std::string > index
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
An base class for domain creators for use in Fit.
bool m_convolutionCompositeMembers
Perform convolution of output composite components.
virtual void initFunction(API::IFunction_sptr function)
Initialize the function.
bool m_outputCompositeMembers
Output separate composite function values.
std::vector< std::string > m_workspacePropertyNames
Property names for workspaces to get the data from.
Kernel::IPropertyManager * m_manager
Pointer to a property manager.
bool m_ignoreInvalidData
Flag to ignore nans, infinities and zero errors.
void declareProperty(Kernel::Property *prop, const std::string &doc)
Declare a property to the algorithm.
Class to hold a set of workspaces.
A property class for workspaces.
void initFunction(API::IFunction_sptr function) override
Initialize the function.
void setCreator(size_t i, API::IDomainCreator *creator)
Set ith creator.
void createDomain(std::shared_ptr< API::FunctionDomain > &domain, std::shared_ptr< API::FunctionValues > &ivalues, size_t i0=0) override
Create a domain from the input workspace.
std::shared_ptr< API::Workspace > createOutputWorkspace(const std::string &baseName, API::IFunction_sptr function, std::shared_ptr< API::FunctionDomain > domain, std::shared_ptr< API::FunctionValues > values, const std::string &outputWorkspacePropertyName) override
Create the output workspace.
bool hasCreator(size_t i) const
Check if i-th creator has been set with setCreator.
std::vector< std::shared_ptr< API::IDomainCreator > > m_creators
Vector of creators.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition IFunction.h:743
std::shared_ptr< FunctionDomain > FunctionDomain_sptr
typedef for a shared pointer
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Output
An output workspace.
Definition Property.h:54