Skip to content

Commit

Permalink
Refs #9531. Added SeqDomainSpectrumCreator with test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed May 30, 2014
1 parent ea35aea commit 9a354d7
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/CurveFitting/CMakeLists.txt
Expand Up @@ -74,6 +74,7 @@ set ( SRC_FILES
src/ReflectivityMulf.cpp
src/Resolution.cpp
src/SeqDomain.cpp
src/SeqDomainSpectrumCreator.cpp
src/SimplexMinimizer.cpp
src/SpecialFunctionHelper.cpp
src/SplineBackground.cpp
Expand Down Expand Up @@ -178,6 +179,7 @@ set ( INC_FILES
inc/MantidCurveFitting/ReflectivityMulf.h
inc/MantidCurveFitting/Resolution.h
inc/MantidCurveFitting/SeqDomain.h
inc/MantidCurveFitting/SeqDomainSpectrumCreator.h
inc/MantidCurveFitting/SimplexMinimizer.h
inc/MantidCurveFitting/SpecialFunctionSupport.h
inc/MantidCurveFitting/SplineBackground.h
Expand Down Expand Up @@ -269,6 +271,7 @@ set ( TEST_FILES
RefinePowderInstrumentParameters3Test.h
ReflectivityMulfTest.h
ResolutionTest.h
SeqDomainSpectrumCreatorTest.h
SimplexTest.h
SpecialFunctionSupportTest.h
SplineBackgroundTest.h
Expand Down
@@ -0,0 +1,78 @@
#ifndef MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_
#define MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_

#include "MantidKernel/System.h"
#include "MantidAPI/IDomainCreator.h"
#include "MantidAPI/FunctionDomain1D.h"
#include "MantidAPI/MatrixWorkspace.h"

namespace Mantid
{
namespace CurveFitting
{

/** SeqDomainSpectrumCreator :
SeqDomainSpectrumCreator creates a special type of SeqDomain, which contains
one FunctionDomain1DSpectrum for each histogram of the Workspace2D this
domain refers to. It can be used for functions that involve several histograms
at once.
@author Michael Wedel, Paul Scherrer Institut - SINQ
@date 28/05/2014
Copyright © 2014 PSI-MSS
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>
*/

using namespace API;

class DLLExport SeqDomainSpectrumCreator : public IDomainCreator
{
public:
SeqDomainSpectrumCreator(Kernel::IPropertyManager* manager,
const std::string& workspacePropertyName);

virtual ~SeqDomainSpectrumCreator() { }

virtual void createDomain(boost::shared_ptr<FunctionDomain> &domain,
boost::shared_ptr<FunctionValues> &values,
size_t i0 = 0);

virtual Workspace_sptr createOutputWorkspace(const std::string &baseName,
IFunction_sptr function,
boost::shared_ptr<FunctionDomain> domain,
boost::shared_ptr<FunctionValues> values,
const std::string &outputWorkspacePropertyName = "OutputWorkspace");
virtual size_t getDomainSize() const;

protected:
void setParametersFromPropertyManager();
void setMatrixWorkspace(MatrixWorkspace_sptr matrixWorkspace);

std::string m_workspacePropertyName;
MatrixWorkspace_sptr m_matrixWorkspace;
};


} // namespace CurveFitting
} // namespace Mantid

#endif /* MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_ */
128 changes: 128 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/SeqDomainSpectrumCreator.cpp
@@ -0,0 +1,128 @@
#include "MantidCurveFitting/SeqDomainSpectrumCreator.h"
#include "MantidAPI/Workspace.h"
#include "MantidCurveFitting/SeqDomain.h"
#include "MantidCurveFitting/FunctionDomain1DSpectrumCreator.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidCurveFitting/Jacobian.h"
#include "MantidKernel/Matrix.h"
#include "MantidAPI/WorkspaceProperty.h"

namespace Mantid
{
namespace CurveFitting
{

using namespace API;

SeqDomainSpectrumCreator::SeqDomainSpectrumCreator(Kernel::IPropertyManager *manager, const std::string &workspacePropertyName) :
IDomainCreator(manager, std::vector<std::string>(1, workspacePropertyName), SeqDomainSpectrumCreator::Sequential),
m_matrixWorkspace()
{
m_workspacePropertyName = m_workspacePropertyNames.front();
}

void SeqDomainSpectrumCreator::createDomain(boost::shared_ptr<FunctionDomain> &domain, boost::shared_ptr<FunctionValues> &values, size_t i0)
{
setParametersFromPropertyManager();

if(!m_matrixWorkspace) {
throw std::invalid_argument("No matrix workspace assigned - can not create domain.");
}

SeqDomain *seqDomain = SeqDomain::create(m_domainType);

size_t numberOfHistograms = m_matrixWorkspace->getNumberHistograms();
for(size_t i = 0; i < numberOfHistograms; ++i) {
FunctionDomain1DSpectrumCreator *spectrumDomain = new FunctionDomain1DSpectrumCreator;
spectrumDomain->setMatrixWorkspace(m_matrixWorkspace);
spectrumDomain->setWorkspaceIndex(i);

seqDomain->addCreator(IDomainCreator_sptr(spectrumDomain));
}

domain.reset(seqDomain);

if(!values) {
values.reset(new FunctionValues(*domain));
} else {
values->expand(i0 + domain->size());
}

}

Workspace_sptr SeqDomainSpectrumCreator::createOutputWorkspace(const std::string &baseName, IFunction_sptr function, boost::shared_ptr<FunctionDomain> domain, boost::shared_ptr<FunctionValues> values, const std::string &outputWorkspacePropertyName)
{
boost::shared_ptr<SeqDomain> seqDomain = boost::dynamic_pointer_cast<SeqDomain>(domain);

if(!seqDomain) {
throw std::invalid_argument("CreateOutputWorkspace requires SeqDomain.");
}

MatrixWorkspace_sptr outputWs = boost::dynamic_pointer_cast<MatrixWorkspace>(WorkspaceFactory::Instance().create(m_matrixWorkspace));

for(size_t i = 0; i < seqDomain->getNDomains(); ++i) {
FunctionDomain_sptr localDomain;
FunctionValues_sptr localValues;

seqDomain->getDomainAndValues(i, localDomain, localValues);
function->function(*localDomain, *localValues);

const MantidVec& originalXValue = m_matrixWorkspace->readX(i);
MantidVec& xValues = outputWs->dataX(i);
MantidVec& yValues = outputWs->dataY(i);

for(size_t j = 0; j < yValues.size(); ++j) {
xValues[j] = originalXValue[j];
yValues[j] = localValues->getCalculated(j);
}
}

if(m_manager && !outputWorkspacePropertyName.empty()) {
declareProperty(new WorkspaceProperty<MatrixWorkspace>(outputWorkspacePropertyName, "", Kernel::Direction::Output),
"Result workspace");

m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace");
m_manager->setProperty(outputWorkspacePropertyName, outputWs);
}

return outputWs;
}

size_t SeqDomainSpectrumCreator::getDomainSize() const
{
if(!m_matrixWorkspace) {
throw std::invalid_argument("No matrix workspace assigned.");
}

size_t nHist = m_matrixWorkspace->getNumberHistograms();
size_t totalSize = 0;

for(size_t i = 0; i < nHist; ++i) {
totalSize += m_matrixWorkspace->dataY(i).size();
}

return totalSize;
}

void SeqDomainSpectrumCreator::setParametersFromPropertyManager()
{
if(m_manager) {
Workspace_sptr workspace = m_manager->getProperty(m_workspacePropertyName);

setMatrixWorkspace(boost::dynamic_pointer_cast<MatrixWorkspace>(workspace));
}
}

void SeqDomainSpectrumCreator::setMatrixWorkspace(MatrixWorkspace_sptr matrixWorkspace)
{
if(!matrixWorkspace) {
throw std::invalid_argument("InputWorkspace must be a valid MatrixWorkspace.");
}

m_matrixWorkspace = matrixWorkspace;
}



} // namespace CurveFitting
} // namespace Mantid

0 comments on commit 9a354d7

Please sign in to comment.