Mantid
Loading...
Searching...
No Matches
IFunctionMD.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 "MantidAPI/Jacobian.h"
18
20#include <boost/lambda/lambda.hpp>
21
22#include <algorithm>
23#include <cfloat>
24#include <functional>
25#include <iterator>
26#include <sstream>
27
28namespace Mantid::API {
29using namespace Geometry;
30
32std::shared_ptr<IFunction> IFunctionMD::clone() const {
33 auto fun = IFunction::clone();
34 return fun;
35}
36
40void IFunctionMD::setWorkspace(std::shared_ptr<const Workspace> ws) {
41 try {
42 IMDWorkspace_const_sptr workspace = std::dynamic_pointer_cast<const IMDWorkspace>(ws);
43 if (!workspace) {
44 throw std::invalid_argument("Workspace has a wrong type (not a IMDWorkspace)");
45 }
46
47 if (m_dimensionIndexMap.empty()) {
49 }
50
51 m_dimensions.resize(m_dimensionIndexMap.size());
52 std::map<std::string, size_t>::const_iterator it = m_dimensionIndexMap.begin();
53 std::map<std::string, size_t>::const_iterator end = m_dimensionIndexMap.end();
54 for (; it != end; ++it) {
55 std::shared_ptr<const Mantid::Geometry::IMDDimension> dim = workspace->getDimensionWithId(it->first);
56 if (!dim) {
57 throw std::invalid_argument("Dimension " + it->first + " dos not exist in workspace " + ws->getName());
58 }
59 m_dimensions[it->second] = dim;
60 }
61
62 } catch (std::exception &) {
63 throw;
64 }
65}
66
71void IFunctionMD::function(const FunctionDomain &domain, FunctionValues &values) const {
72 const auto *dmd = dynamic_cast<const FunctionDomainMD *>(&domain);
73 if (!dmd) {
74 throw std::invalid_argument("Unexpected domain in IFunctionMD");
75 }
76 evaluateFunction(*dmd, values);
77}
78
86 domain.reset();
87 size_t i = 0;
88 for (const IMDIterator *r = domain.getNextIterator(); r != nullptr; r = domain.getNextIterator()) {
89 this->reportProgress("Evaluating function for box " + std::to_string(i + 1));
90 values.setCalculated(i, functionMD(*r));
91 i++;
92 };
93}
94
102void IFunctionMD::useDimension(const std::string &id) {
103 size_t n = m_dimensionIndexMap.size();
104 if (m_dimensionIndexMap.find(id) != m_dimensionIndexMap.end()) {
105 throw std::invalid_argument("Dimension " + id + " has already been used.");
106 }
108}
109
117 if (!workspace) {
118 throw std::runtime_error("Method IFunctionMD::useAllDimensions() can only "
119 "be called after setting the workspace");
120 }
121 for (size_t i = 0; i < workspace->getNumDims(); ++i) {
122 useDimension(workspace->getDimension(i)->getDimensionId());
123 }
124 this->initDimensions();
125}
126
127} // namespace Mantid::API
128
129//#include "MantidAPI/ParamFunction.h"
130//#include "MantidKernel/VMD.h"
131//
132// using Mantid::Kernel::VMD;
133
134// namespace Mantid
135//{
136// namespace API
137// {
138//
139// /**
140// * An example MD function. Gaussian in N dimensions
141// */
142// class GaussianMD: public IFunctionMD, public ParamFunction
143// {
144// public:
145// /**
146// * Defining gaussian's parameters here, ie after the workspace is set
147// and
148// * the dimensions are known.
149// */
150// void initDimensions()
151// {
152// if (!getWorkspace()) return;
153// std::map<std::string,size_t>::const_iterator it =
154// m_dimensionIndexMap.begin();
155// std::map<std::string,size_t>::const_iterator end =
156// m_dimensionIndexMap.end();
157// for(; it != end; ++it)
158// {
159// declareParameter(it->first+"_centre",0.0);
160// declareParameter(it->first+"_alpha",1.0);
161// }
162// declareParameter("Height",1.0);
163// }
164// std::string name() const {return "GaussianMD";}
165// protected:
166//
167// /**
168// * Calculate the function value at a point r in the MD workspace
169// * @param r :: MD workspace iterator with a reference to the current
170// point
171// */
172// double functionMD(IMDIterator& r) const
173// {
174// double arg = 0.0;
175// size_t n = m_dimensions.size();
176// VMD center = r.getCenter();
177// for(size_t i = 0; i < n; ++i)
178// {
179// double c = getParameter(2*i);
180// double a = getParameter(2*i + 1);
181// double t = center[i] - c;
182// arg += a*t*t;
183// }
184// return getParameter("Height") * exp(-arg);
185// }
186// };
187//
188// // Subscribe the function into the factory.
189// DECLARE_FUNCTION(GaussianMD);
190// } // API
191//} // Mantid
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
Implements a domain for MD functions (IFunctionMD).
const IMDIterator * getNextIterator() const
Next iterator.
void reset() const override
Reset the iterator to point to the start of the domain.
Base class that represents the domain of a function.
A class to store values calculated by a function.
void setCalculated(double value)
set all calculated values to same number
void function(const FunctionDomain &domain, FunctionValues &values) const override
Definition: IFunctionMD.cpp:71
void evaluateFunction(const FunctionDomainMD &domain, FunctionValues &values) const
Performs the function evaluations on the MD domain.
Definition: IFunctionMD.cpp:85
std::shared_ptr< IFunction > clone() const override
Virtual copy constructor.
Definition: IFunctionMD.cpp:32
std::vector< std::shared_ptr< const Mantid::Geometry::IMDDimension > > m_dimensions
dimensions used in this function in the expected order
Definition: IFunctionMD.h:76
virtual double functionMD(const IMDIterator &r) const =0
Does the function evaluation. Must be implemented in derived classes.
virtual void useDimension(const std::string &id)
User functions call this method in their constructors to set up the order of the dimensions.
std::map< std::string, size_t > m_dimensionIndexMap
maps dimension id to its index in m_dimensions
Definition: IFunctionMD.h:74
virtual void initDimensions()
Do finction initialization after useAllDimensions() was called.
Definition: IFunctionMD.h:69
virtual void useAllDimensions(std::shared_ptr< const IMDWorkspace > workspace)
Use all the dimensions in the workspace.
void setWorkspace(std::shared_ptr< const Workspace > ws) override
Set the workspace.
Definition: IFunctionMD.cpp:40
virtual std::shared_ptr< IFunction > clone() const
Virtual copy constructor.
Definition: IFunction.cpp:109
void reportProgress(const std::string &msg="") const
Reports progress with an optional message.
Definition: IFunction.cpp:132
This is an interface to an iterator of an IMDWorkspace.
Definition: IMDIterator.h:39
std::shared_ptr< const IMDWorkspace > IMDWorkspace_const_sptr
Shared pointer to the IMDWorkspace base class (const version)
Definition: IMDWorkspace.h:148
std::string to_string(const wide_integer< Bits, Signed > &n)