From c9974cd555c10858021c0480b1741e24c1317e90 Mon Sep 17 00:00:00 2001
From: Matthew Andrew <matthew.andrew@tessella.com>
Date: Tue, 10 Mar 2020 15:41:36 +0000
Subject: [PATCH] Add createInitializedMultiDomainFunction to functionFactory
 Re #28057

By far the most common type of multidomain function you want to create is one where each domain has the same functional form and has a domain index equal to it's place in the function. This adds a method to cover this case to function factory.
---
 Framework/API/inc/MantidAPI/FunctionFactory.h |  7 ++++
 Framework/API/src/FunctionFactory.cpp         | 32 +++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/Framework/API/inc/MantidAPI/FunctionFactory.h b/Framework/API/inc/MantidAPI/FunctionFactory.h
index 04482c86f7e..ec82e697a71 100644
--- a/Framework/API/inc/MantidAPI/FunctionFactory.h
+++ b/Framework/API/inc/MantidAPI/FunctionFactory.h
@@ -26,6 +26,7 @@ namespace API {
 class IFunction;
 class CompositeFunction;
 class Expression;
+class MultiDomainFunction;
 
 /** @class FunctionFactoryImpl
 
@@ -52,6 +53,12 @@ public:
   /// Creates an instance of a function
   std::shared_ptr<IFunction> createInitialized(const std::string &input) const;
 
+  /// Creates an instnce of an inizialised multidomain function where each
+  /// domain has the same function.
+  boost::shared_ptr<MultiDomainFunction>
+  createInitializedMultiDomainFunction(const std::string &input,
+                                       size_t domainNumber);
+
   /// Query available functions based on the template type
   template <typename FunctionType>
   std::vector<std::string> getFunctionNames() const;
diff --git a/Framework/API/src/FunctionFactory.cpp b/Framework/API/src/FunctionFactory.cpp
index e9a1ea4e41b..26353c71e8c 100644
--- a/Framework/API/src/FunctionFactory.cpp
+++ b/Framework/API/src/FunctionFactory.cpp
@@ -18,6 +18,7 @@
 #include "MantidKernel/LibraryManager.h"
 #include "MantidKernel/StringTokenizer.h"
 #include <boost/lexical_cast.hpp>
+#include <boost/make_shared.hpp>
 #include <sstream>
 
 namespace Mantid {
@@ -73,6 +74,37 @@ FunctionFactoryImpl::createInitialized(const std::string &input) const {
   return createSimple(e, parentAttributes);
 }
 
+/**
+ * @param input :: An input string which defines the function and initial values
+ * for the parameters.
+ * Parameters of different functions are separated by ';'. Parameters of the
+ * same function
+ * are separated by ','. parameterName=value pairs are used to set a parameter
+ * value. For each function
+ * "name" parameter must be set to a function name. E.g.
+ * input = "name=LinearBackground,A0=0,A1=1; name = Gaussian,
+ * PeakCentre=10.,Sigma=1"
+ * @param domainNumber :: The number of domains to add to the function.
+ * @return A pointer to the created function.
+ */
+boost::shared_ptr<MultiDomainFunction>
+FunctionFactoryImpl::createInitializedMultiDomainFunction(
+    const std::string &input, size_t domainNumber) {
+  auto singleFunction = createInitialized(input);
+  auto multiDomainFunction = boost::make_shared<MultiDomainFunction>();
+
+  if (!singleFunction) {
+    return multiDomainFunction;
+  }
+
+  for (size_t i = 0; i < domainNumber; ++i) {
+    multiDomainFunction->addFunction(singleFunction->clone());
+    multiDomainFunction->setDomainIndex(i, i);
+  }
+
+  return multiDomainFunction;
+}
+
 /**
  * Create a function from an expression.
  * @param expr :: The input expression
-- 
GitLab