Skip to content

Commit

Permalink
Re #4158. Added FitMD algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 2, 2012
1 parent f8d1960 commit 12892a0
Show file tree
Hide file tree
Showing 24 changed files with 1,092 additions and 566 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/FunctionDomainMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MANTID_API_DLL FunctionDomainMD: public FunctionDomain
mutable size_t m_currentIndex;
/// The size of the domain
size_t m_size;
mutable bool m_justReset;
};

} // namespace API
Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/Framework/API/src/FunctionDomainMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace API
FunctionDomainMD::FunctionDomainMD(IMDWorkspace_const_sptr ws, size_t start, size_t length):
m_iterator(ws->createIterator()),
m_startIndex(start),
m_currentIndex(0)
m_currentIndex(0),
m_justReset(true)
{
size_t dataSize = m_iterator->getDataSize();
m_size = length == 0 ? dataSize: length;
Expand Down Expand Up @@ -53,8 +54,9 @@ FunctionDomainMD::~FunctionDomainMD()
*/
const IMDIterator* FunctionDomainMD::getNextIterator() const
{
if (m_currentIndex == 0)
if (m_justReset)
{
m_justReset = false;
return m_iterator;
}
if (!m_iterator->next() || m_currentIndex >= m_size)
Expand All @@ -73,6 +75,7 @@ void FunctionDomainMD::reset() const
{
m_iterator->jumpTo(m_startIndex);
m_currentIndex = 0;
m_justReset = true;
}

} // namespace API
Expand Down
171 changes: 2 additions & 169 deletions Code/Mantid/Framework/API/src/IFunctionMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,6 @@ namespace API
m_dimensions[it->second] = dim;
}

//if (copyData)
//{
// IMDIterator* r = workspace->createIterator();
// m_dataSize = r->getDataSize();

// // fill in m_data and m_weights
// m_data.reset(new double[m_dataSize]);
// m_weights.reset(new double[m_dataSize]);

// size_t i = 0;
// do
// {
// double signal = r->getNormalizedSignal(); //point.getSignal();
// double error = r->getNormalizedError(); //point.getError();
// if (error == 0) error = 1.;
// m_data[i] = signal;
// m_weights[i] = 1./error;
// i++;
// } while(r->next());
// delete r;

// if (m_dataSize == 0)
// {
// throw std::runtime_error("Fitting data is empty");
// }
//}

}
catch(std::exception& e)
{
Expand All @@ -106,8 +79,9 @@ namespace API
{
throw std::invalid_argument("Unexpected domain in IFunctionMD");
}
domain.reset();
size_t i=0;
for(const IMDIterator* r = dmd->getNextIterator(); r != NULL;)
for(const IMDIterator* r = dmd->getNextIterator(); r != NULL; r = dmd->getNextIterator())
{
values.setCalculated(i,functionMD(*r));
i++;
Expand Down Expand Up @@ -210,147 +184,6 @@ namespace API
//
// // Subscribe the function into the factory.
// DECLARE_FUNCTION(GaussianMD);
//
// /**
// * Another example MD function. A function defined as a muParser string.
// */
// class UserFunctionMD: public IFunctionMD, public ParamFunction
// {
// private:
// mu::Parser m_parser;
// mutable std::vector<double> m_vars;
// std::vector<std::string> m_varNames;
// std::string m_formula;
// public:
// UserFunctionMD()
// {
// m_vars.resize(4);
// std::string varNames[] = {"x","y","z","t"};
// m_varNames.assign(varNames,varNames+m_vars.size());
// for(size_t i = 0; i < m_vars.size(); ++i)
// {
// m_parser.DefineVar(m_varNames[i],&m_vars[i]);
// }
// }
// bool hasAttribute(const std::string& attName)const
// {
// UNUSED_ARG(attName);
// return attName == "Formula";
// }
// Attribute getAttribute(const std::string& attName)const
// {
// UNUSED_ARG(attName);
// return Attribute(m_formula);
// }
//
// void setAttribute(const std::string& attName,const Attribute& attr)
// {
// UNUSED_ARG(attName);
// m_formula = attr.asString();
// if (!m_vars.empty())
// {
// setFormula();
// }
// }
// /**
// * Defining function's parameters here, ie after the workspace is set and
// * the dimensions are known.
// */
// void initDimensions()
// {
// if (!getWorkspace()) return;
// if (m_vars.size() > 4)
// {
// m_vars.resize(m_dimensionIndexMap.size());
// m_varNames.resize(m_dimensionIndexMap.size());
// for(size_t i = 0; i < m_vars.size(); ++i)
// {
// m_varNames[i] = "x" + boost::lexical_cast<std::string>(i);
// m_parser.DefineVar(m_varNames[i],&m_vars[i]);
// }
// }
// setFormula();
// }
//
// std::string name() const {return "UserFunctionMD";}
// protected:
//
// /**
// * Calculate the function value at a point r in the MD workspace
// * @param r :: MD workspace iterator with a reference to the current point
// */
// double functionMD(IMDIterator& r) const
// {
// size_t n = m_dimensions.size();
// VMD center = r.getCenter();
// for(size_t i = 0; i < n; ++i)
// {
// m_vars[i] = center[i];
// }
// return m_parser.Eval();
// }
// /** Static callback function used by MuParser to initialize variables implicitly
// @param varName :: The name of a new variable
// @param pufun :: Pointer to the function
// */
// static double* AddVariable(const char *varName, void *pufun)
// {
// UserFunctionMD& fun = *reinterpret_cast<UserFunctionMD*>(pufun);
//
// std::vector<std::string>::iterator x = std::find(fun.m_varNames.begin(),fun.m_varNames.end(),varName);
// if (x != fun.m_varNames.end())
// {
// //std::vector<std::string>::difference_type i = std::distance(fun.m_varNames.begin(),x);
// throw std::runtime_error("UserFunctionMD variables are not defined");
// }
// else
// {
// try
// {
// fun.declareParameter(varName,0.0);
// }
// catch(...)
// {}
// }
//
// // The returned pointer will never be used. Just returning a valid double pointer
// return &fun.m_vars[0];
// }
//
// /**
// * Initializes the mu::Parser.
// */
// void setFormula()
// {
// // variables must be already defined
// if (m_vars.empty()) return;
// if (m_formula.empty())
// {
// m_formula = "0";
// }
// m_parser.SetVarFactory(AddVariable,this);
// m_parser.SetExpr(m_formula);
// // declare function parameters using mu::Parser's implicit variable setting
// m_parser.Eval();
// m_parser.ClearVar();
// // set muParser variables
// for(size_t i = 0; i < m_vars.size(); ++i)
// {
// m_parser.DefineVar(m_varNames[i],&m_vars[i]);
// }
// for(size_t i=0;i<nParams();i++)
// {
// m_parser.DefineVar(parameterName(i),getParameterAddress(i));
// }
//
// m_parser.SetExpr(m_formula);
// }
//
// };

// Subscribe the function into the factory.
//DECLARE_FUNCTION(UserFunctionMD);

// } // API
//} // Mantid

9 changes: 8 additions & 1 deletion Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set ( SRC_FILES
src/FRConjugateGradientMinimizer.cpp
# src/Fit.cpp
src/Fit1D.cpp
src/FitMD.cpp
src/FitMW.cpp
src/FuncMinimizerFactory.cpp
src/GausDecay.cpp
Expand All @@ -30,6 +31,7 @@ set ( SRC_FILES
src/GaussianLinearBG1D.cpp
# src/GenericFit.cpp
src/GSLFunctions.cpp
src/IFit.cpp
src/IFuncMinimizer.cpp
src/IkedaCarpenterPV.cpp
src/LevenbergMarquardtMinimizer.cpp
Expand All @@ -56,6 +58,7 @@ set ( SRC_FILES
src/StretchExpMuon.cpp
src/UserFunction.cpp
src/UserFunction1D.cpp
src/UserFunctionMD.cpp
)

set ( SRC_UNITY_IGNORE_FILES src/Fit1D.cpp src/GSLFunctions.cpp )
Expand Down Expand Up @@ -83,6 +86,7 @@ set ( INC_FILES
inc/MantidCurveFitting/FRConjugateGradientMinimizer.h
# inc/MantidCurveFitting/Fit.h
inc/MantidCurveFitting/Fit1D.h
inc/MantidCurveFitting/FitMD.h
inc/MantidCurveFitting/FitMW.h
inc/MantidCurveFitting/FuncMinimizerFactory.h
inc/MantidCurveFitting/Jacobian.h
Expand All @@ -96,6 +100,7 @@ set ( INC_FILES
inc/MantidCurveFitting/Gaussian1D.h
inc/MantidCurveFitting/GaussianLinearBG1D.h
inc/MantidCurveFitting/GenericFit.h
inc/MantidCurveFitting/IFit.h
inc/MantidCurveFitting/IFuncMinimizer.h
inc/MantidCurveFitting/IkedaCarpenterPV.h
inc/MantidCurveFitting/LevenbergMarquardtMinimizer.h
Expand Down Expand Up @@ -123,6 +128,7 @@ set ( INC_FILES
inc/MantidCurveFitting/StretchExpMuon.h
inc/MantidCurveFitting/UserFunction.h
inc/MantidCurveFitting/UserFunction1D.h
inc/MantidCurveFitting/UserFunctionMD.h
)

set ( TEST_FILES
Expand All @@ -144,6 +150,7 @@ set ( TEST_FILES
test/ExpDecayTest.h
test/ExpDecayMuonTest.h
test/ExpDecayOscTest.h
test/FitMDTest.h
test/FitMWTest.h
test/FRConjugateGradientTest.h
test/FuncMinimizerFactoryTest.h
Expand Down Expand Up @@ -187,7 +194,7 @@ ADD_PRECOMPILED_HEADER( inc/MantidCurveFitting/PrecompiledHeader.h MantidCurveFi
# Add the target for this directory
add_library ( CurveFitting ${SRC_FILES} ${INC_FILES})
# Set the name of the generated library
set_target_properties ( CurveFitting PROPERTIES OUTPUT_NAME MantidCurveFitting )
set_target_properties ( CurveFitting PROPERTIES OUTPUT_NAME MantidCurveFitting COMPILE_DEFINITIONS IN_MANTID_CURVEFITTING)
# Add to the 'Framework' group in VS
set_property ( TARGET CurveFitting PROPERTY FOLDER "MantidFramework" )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DLLExport CostFuncLeastSquares : public CostFuncFitting
/// @return :: The value of the function
virtual double valAndDeriv(std::vector<double>& der) const;

virtual double valDerivHessian(bool evalFunction = true) const;
virtual double valDerivHessian(bool evalFunction = true, bool evalDeriv = true, bool evalHessian = true) const;
const GSLVector& getDeriv() const;
const GSLMatrix& getHessian() const;
void push();
Expand Down
77 changes: 77 additions & 0 deletions Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/FitMD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef MANTID_CURVEFITTING_FITMD_H_
#define MANTID_CURVEFITTING_FITMD_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidCurveFitting/IFit.h"

namespace Mantid
{

namespace API
{
class FunctionDomain;
class FunctionDomainMD;
class FunctionValues;
class IMDWorkspace;
}

namespace CurveFitting
{
/**
New algorithm for fitting functions. The name is temporary.
@author Roman Tolchenov, Tessella plc
@date 06/12/2011
Copyright &copy; 2007-8 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport FitMD : public IFit
{
public:
/// Default constructor
FitMD() : IFit() {};
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "FitMD";}
/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return (1);}
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Optimization";}

protected:
/// Sets documentation strings for this algorithm
virtual void initDocs();

/// declare properties that specify the dataset within the workspace to fit to.
virtual void declareDatasetProperties(){}
/// Create a domain from the input workspace
virtual void createDomain(boost::shared_ptr<API::FunctionDomain>&, boost::shared_ptr<API::FunctionValues>&);

/// The input MareixWorkspace
boost::shared_ptr<API::IMDWorkspace> m_IMDWorkspace;
};


} // namespace CurveFitting
} // namespace Mantid

#endif /*MANTID_CURVEFITTING_FITMD_H_*/

0 comments on commit 12892a0

Please sign in to comment.