Skip to content
Snippets Groups Projects
Commit efaaa4d2 authored by Roman Tolchenov's avatar Roman Tolchenov
Browse files

Re #9891. Added EvaluateMDFunction algorithm.

parent 0e7ad927
No related merge requests found
......@@ -22,6 +22,7 @@ set ( SRC_FILES
src/CreateMDWorkspace.cpp
src/DivideMD.cpp
src/EqualToMD.cpp
src/EvaluateMDFunction.cpp
src/ExponentialMD.cpp
src/FakeMDEventData.cpp
src/FindPeaksMD.cpp
......@@ -102,6 +103,7 @@ set ( INC_FILES
inc/MantidMDAlgorithms/DllConfig.h
inc/MantidMDAlgorithms/EqualToMD.h
inc/MantidMDAlgorithms/ExponentialMD.h
inc/MantidMDAlgorithms/EvaluateMDFunction.h
inc/MantidMDAlgorithms/FakeMDEventData.h
inc/MantidMDAlgorithms/FindPeaksMD.h
inc/MantidMDAlgorithms/GreaterThanMD.h
......@@ -183,6 +185,7 @@ set ( TEST_FILES
DivideMDTest.h
EqualToMDTest.h
ExponentialMDTest.h
EvaluateMDFunctionTest.h
FakeMDEventDataTest.h
FindPeaksMDTest.h
FitResolutionConvolvedModelTest.h
......
#ifndef MANTID_MDALGORITHMS_EVALUATEMDFUNCTION_H_
#define MANTID_MDALGORITHMS_EVALUATEMDFUNCTION_H_
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
namespace Mantid
{
namespace MDAlgorithms
{
/** EvaluateMDFunction : TODO: DESCRIPTION
Copyright © 2014 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://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport EvaluateMDFunction : public API::Algorithm
{
public:
EvaluateMDFunction();
virtual ~EvaluateMDFunction();
virtual const std::string name() const {return "EvaluateMDFunction";}
virtual int version() const;
virtual const std::string category() const;
virtual const std::string summary() const;
private:
void init();
void exec();
};
} // namespace MDAlgorithms
} // namespace Mantid
#endif /* MANTID_MDALGORITHMS_EVALUATEMDFUNCTION_H_ */
#include "MantidMDAlgorithms/EvaluateMDFunction.h"
#include "MantidAPI/FunctionProperty.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidAPI/FunctionDomainMD.h"
#include "MantidAPI/FunctionValues.h"
namespace Mantid
{
namespace MDAlgorithms
{
using Mantid::Kernel::Direction;
using Mantid::API::WorkspaceProperty;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(EvaluateMDFunction)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
EvaluateMDFunction::EvaluateMDFunction()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
EvaluateMDFunction::~EvaluateMDFunction()
{
}
//----------------------------------------------------------------------------------------------
/// Algorithm's version for identification. @see Algorithm::version
int EvaluateMDFunction::version() const { return 1;};
/// Algorithm's category for identification. @see Algorithm::category
const std::string EvaluateMDFunction::category() const { return "MDAlgorithms";}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string EvaluateMDFunction::summary() const { return "Evaluates an MD function on a MD histo workspace.";};
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void EvaluateMDFunction::init()
{
declareProperty(new WorkspaceProperty<API::IMDHistoWorkspace>("InputWorkspace","",Direction::Input), "An input workspace.");
declareProperty(new API::FunctionProperty("Function"),"Parameters defining the fitting function and its initial values");
declareProperty(new WorkspaceProperty<API::IMDHistoWorkspace>("OutputWorkspace","",Direction::Output), "An output workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void EvaluateMDFunction::exec()
{
API::IMDHistoWorkspace_sptr input = getProperty("InputWorkspace");
auto cloner = API::AlgorithmManager::Instance().create("CloneMDWorkspace");
cloner->initialize();
cloner->setChild(true);
cloner->setProperty("InputWorkspace", input);
cloner->setPropertyValue("OutputWorkspace", "_");
cloner->execute();
API::IMDWorkspace_sptr clone = cloner->getProperty("OutputWorkspace");
API::IMDHistoWorkspace_sptr output = boost::dynamic_pointer_cast<API::IMDHistoWorkspace>(clone);
if ( !output )
throw std::runtime_error("Cannot create output workspace");
API::IFunction_sptr function = getProperty("Function");
function->setWorkspace( output );
API::FunctionDomainMD domain( output );
API::FunctionValues values( domain );
function->function( domain, values );
double *data = values.getPointerToCalculated(0);
size_t length = values.size();
double *outputData = output->getSignalArray();
std::copy( data, data + length, outputData );
setProperty("OutputWorkspace",output);
}
} // namespace MDAlgorithms
} // namespace Mantid
#ifndef MANTID_MDALGORITHMS_EVALUATEMDFUNCTIONTEST_H_
#define MANTID_MDALGORITHMS_EVALUATEMDFUNCTIONTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidMDAlgorithms/EvaluateMDFunction.h"
using Mantid::MDAlgorithms::EvaluateMDFunction;
using namespace Mantid::API;
class EvaluateMDFunctionTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EvaluateMDFunctionTest *createSuite() { return new EvaluateMDFunctionTest(); }
static void destroySuite( EvaluateMDFunctionTest *suite ) { delete suite; }
void test_Init()
{
EvaluateMDFunction alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
// Name of the output workspace.
std::string outWSName("EvaluateMDFunctionTest_OutputWS");
EvaluateMDFunction alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("REPLACE_PROPERTY_NAME_HERE!!!!", "value") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );
// Retrieve the workspace from data service. TODO: Change to your desired type
Workspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace>(outWSName) );
TS_ASSERT(ws);
if (!ws) return;
// TODO: Check the results
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}
void test_Something()
{
TSM_ASSERT( "You forgot to write a test!", 0);
}
};
#endif /* MANTID_MDALGORITHMS_EVALUATEMDFUNCTIONTEST_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment