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->createDomain(dom, values, i0);
54 creator->separateCompositeMembersInOutput(m_outputCompositeMembers, m_convolutionCompositeMembers);
55 jointDomain->addDomain(dom);
56 i0 += dom->size();
57 }
58 domain.reset(jointDomain.release());
59 ivalues = values;
60}
61
67 auto mdFunction = std::dynamic_pointer_cast<API::MultiDomainFunction>(function);
68 if (mdFunction) {
69 // loop over member functions and init them
70 for (size_t iFun = 0; iFun < mdFunction->nFunctions(); ++iFun) {
71 std::vector<size_t> domainIndices;
72 // get domain indices for this function
73 mdFunction->getDomainIndices(iFun, m_creators.size(), domainIndices);
74 if (!domainIndices.empty()) {
75 size_t index = domainIndices[0];
76 if (index >= m_creators.size()) {
77 std::stringstream msg;
78 msg << "Domain index is out of range. (Function #" << iFun << ")";
79 throw std::runtime_error(msg.str());
80 }
81 m_creators[index]->initFunction(mdFunction->getFunction(iFun));
82 } else {
83 g_log.warning() << "Function #" << iFun << " doesn't apply to any domain\n";
84 }
85 }
86 } else {
88 }
89}
90
104std::shared_ptr<API::Workspace> MultiDomainCreator::createOutputWorkspace(
105 const std::string &baseName, API::IFunction_sptr function, std::shared_ptr<API::FunctionDomain> domain,
106 std::shared_ptr<API::FunctionValues> values, const std::string &outputWorkspacePropertyName) {
107 UNUSED_ARG(domain);
108 UNUSED_ARG(values);
109
110 // split the function into independent parts
111 std::vector<API::IFunction_sptr> functions = function->createEquivalentFunctions();
112 // there must be as many parts as there are domains
113 if (functions.size() != m_creators.size()) {
114 throw std::runtime_error("Number of functions and domains don't match");
115 }
116
118
119 for (size_t i = 0; i < functions.size(); ++i) {
120 std::string localName = baseName + "Workspace_" + std::to_string(i);
121 auto fun = functions[i];
122 auto creator = m_creators[i];
123 std::shared_ptr<API::FunctionDomain> localDomain;
124 std::shared_ptr<API::FunctionValues> localValues;
125 fun->setUpForFit();
126 creator->createDomain(localDomain, localValues);
127 creator->initFunction(fun);
128 auto ws = creator->createOutputWorkspace(localName, fun, localDomain, localValues, "");
129 API::AnalysisDataService::Instance().addOrReplace(localName, ws);
130 outWS->addWorkspace(ws);
131 }
132
133 if (!outputWorkspacePropertyName.empty()) {
136 "Name of the output Workspace holding resulting simulated spectrum");
137 m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspaces");
138 m_manager->setProperty(outputWorkspacePropertyName, outWS);
139 }
140
141 return outWS;
142}
143
144} // namespace Mantid::CurveFitting
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
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.
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:86
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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:732
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