Skip to content

Commit

Permalink
refs #11056. Setup for new AddPeaksHKL algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Feb 15, 2015
1 parent 1f5c833 commit 0d10ba2
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/test/MockObjects.h
Expand Up @@ -103,9 +103,9 @@ namespace
MOCK_METHOD0(findDetector,
bool());
MOCK_METHOD2(setQSampleFrame,
void(Mantid::Kernel::V3D QSampleFrame, double detectorDistance));
void(Mantid::Kernel::V3D QSampleFrame, boost::optional<double> detectorDistance));
MOCK_METHOD2(setQLabFrame,
void(Mantid::Kernel::V3D QLabFrame, double detectorDistance));
void(Mantid::Kernel::V3D QLabFrame, boost::optional<double> detectorDistance));
MOCK_METHOD1(setWavelength,
void(double wavelength));
MOCK_CONST_METHOD0(getWavelength,
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Crystal/CMakeLists.txt
@@ -1,5 +1,6 @@

set ( SRC_FILES
src/AddPeakHKL.cpp
src/AnvredCorrection.cpp
src/CalculatePeaksHKL.cpp
src/CalculateUMatrix.cpp
Expand Down Expand Up @@ -69,6 +70,7 @@ set ( SRC_FILES

set ( SRC_UNITY_IGNORE_FILES )
set ( INC_FILES
inc/MantidCrystal/AddPeakHKL.h
inc/MantidCrystal/AnvredCorrection.h
inc/MantidCrystal/BackgroundStrategy.h
inc/MantidCrystal/CalculatePeaksHKL.h
Expand Down Expand Up @@ -140,6 +142,7 @@ set ( INC_FILES
)

set ( TEST_FILES
AddPeakHKLTest.h
AnvredCorrectionTest.h
CalculatePeaksHKLTest.h
CalculateUMatrixTest.h
Expand Down
56 changes: 56 additions & 0 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/AddPeakHKL.h
@@ -0,0 +1,56 @@
#ifndef MANTID_CRYSTAL_ADDPEAKHKL_H_
#define MANTID_CRYSTAL_ADDPEAKHKL_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace Crystal
{

/** AddPeakHKL : TODO: DESCRIPTION
Copyright &copy; 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge National Laboratory & European Spallation Source
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 AddPeakHKL : public API::Algorithm
{
public:
AddPeakHKL();
virtual ~AddPeakHKL();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;
virtual const std::string summary() const;

private:
void init();
void exec();


};


} // namespace Crystal
} // namespace Mantid

#endif /* MANTID_CRYSTAL_ADDPEAKHKL_H_ */
65 changes: 65 additions & 0 deletions Code/Mantid/Framework/Crystal/src/AddPeakHKL.cpp
@@ -0,0 +1,65 @@
#include "MantidCrystal/AddPeakHKL.h"

namespace Mantid
{
namespace Crystal
{

using Mantid::Kernel::Direction;
using Mantid::API::WorkspaceProperty;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddPeakHKL)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
AddPeakHKL::AddPeakHKL()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
AddPeakHKL::~AddPeakHKL()
{
}


//----------------------------------------------------------------------------------------------

/// Algorithms name for identification. @see Algorithm::name
const std::string AddPeakHKL::name() const { return "AddPeakHKL"; }

/// Algorithm's version for identification. @see Algorithm::version
int AddPeakHKL::version() const { return 1;};

/// Algorithm's category for identification. @see Algorithm::category
const std::string AddPeakHKL::category() const { return TODO: FILL IN A CATEGORY;}

/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string AddPeakHKL::summary() const { return TODO: FILL IN A SUMMARY;};

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void AddPeakHKL::init()
{
declareProperty(new WorkspaceProperty<>("InputWorkspace","",Direction::Input), "An input workspace.");
declareProperty(new WorkspaceProperty<>("OutputWorkspace","",Direction::Output), "An output workspace.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void AddPeakHKL::exec()
{
// TODO Auto-generated execute stub
}



} // namespace Crystal
} // namespace Mantid
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/Crystal/src/PredictPeaks.cpp
Expand Up @@ -145,7 +145,7 @@ void PredictPeaks::doHKL(const double h, const double k, const double l,
PARALLEL_CRITICAL(PredictPeaks_numInRange) { numInRange++; }

// Create the peak using the Q in the lab framewith all its info:
Peak p(inst, q);
Peak p(inst, q, boost::optional<double>());
if (p.findDetector()) {
// Only add peaks that hit the detector
p.setGoniometerMatrix(gonio);
Expand Down
61 changes: 61 additions & 0 deletions Code/Mantid/Framework/Crystal/test/AddPeakHKLTest.h
@@ -0,0 +1,61 @@
#ifndef MANTID_CRYSTAL_ADDPEAKHKLTEST_H_
#define MANTID_CRYSTAL_ADDPEAKHKLTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidCrystal/AddPeakHKL.h"

using Mantid::Crystal::AddPeakHKL;
using namespace Mantid::API;

class AddPeakHKLTest : 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 AddPeakHKLTest *createSuite() { return new AddPeakHKLTest(); }
static void destroySuite( AddPeakHKLTest *suite ) { delete suite; }


void test_Init()
{
AddPeakHKL alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_exec()
{
// Name of the output workspace.
std::string outWSName("AddPeakHKLTest_OutputWS");

AddPeakHKL 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_CRYSTAL_ADDPEAKHKLTEST_H_ */
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/PeaksWorkspace.cpp
Expand Up @@ -397,6 +397,10 @@ PeaksWorkspace::peakInfo(Kernel::V3D qFrame, bool labCoords) const {
*/
Peak *PeaksWorkspace::createPeakHKL(V3D HKL) const
{
/*
The following allows us to add peaks where we have a single UB to work from.
*/

Geometry::OrientedLattice lattice = this->sample().getOrientedLattice();
Geometry::Goniometer goniometer = this->run().getGoniometer();

Expand All @@ -412,6 +416,9 @@ Peak *PeaksWorkspace::createPeakHKL(V3D HKL) const
// Set the goniometer
peak->setGoniometerMatrix(goniometer.getR());

// Take the run number from this
peak->setRunNumber(this->getRunNumber());

return peak;
}

Expand Down
Expand Up @@ -50,7 +50,7 @@ void export_IPeak()
.def("setQLabFrame", (void (IPeak::*)(Mantid::Kernel::V3D))&IPeak::setQLabFrame, "Set the peak using the peak's position in reciprocal space, in the lab frame.")
.def("setQLabFrame", setQLabFrame, "Set the peak using the peak's position in reciprocal space, in the lab frame. Detector distance explicitly supplied.") // two argument overload
.def("setQSampleFrame", (void (IPeak::*)(Mantid::Kernel::V3D))&IPeak::setQSampleFrame, "Set the peak using the peak's position in reciprocal space, in the sample frame.")
.def("setQSampleFrame", &IPeak::setQSampleFrame, "Set the peak using the peak's position in reciprocal space, in the sample frame.")
.def("setQSampleFrame", setQSampleFrame, "Set the peak using the peak's position in reciprocal space, in the sample frame.")
.def("setWavelength", &IPeak::setWavelength, "Set the incident wavelength of the neutron. Calculates the energy from this assuming elastic scattering.")
.def("getWavelength", &IPeak::getWavelength, "Return the incident wavelength")
.def("getScattering", &IPeak::getScattering, "Calculate the scattering angle of the peak")
Expand Down
Expand Up @@ -4,6 +4,8 @@
#include "MantidPythonInterface/kernel/Converters/PyObjectToV3D.h"
#include <boost/python/class.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/optional.hpp>
#include <boost/python/manage_new_object.hpp>

using namespace Mantid::API;
using Mantid::PythonInterface::Registry::DataItemInterface;
Expand All @@ -12,21 +14,21 @@ using namespace boost::python;
namespace {

/// Create a peak via it's HKL value from a list or numpy array
void createPeakHKL(IPeaksWorkspace & self, const object& data)
IPeak* createPeakHKL(IPeaksWorkspace & self, const object& data)
{
self.createPeakHKL(Mantid::PythonInterface::Converters(data));
return self.createPeakHKL(Mantid::PythonInterface::Converters::PyObjectToV3D(data)());
}

/// Create a peak via it's QLab value from a list or numpy array
void createPeakQLab(IPeaksWorkspace & self, const object& data)
IPeak* createPeakQLab(IPeaksWorkspace & self, const object& data)
{
self.createPeak(Mantid::PythonInterface::Converters(data));
return self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), boost::optional<double>());
}

/// Create a peak via it's QLab value from a list or numpy array
void createPeakQLabWithDistance(IPeaksWorkspace & self, const object& data, double detectorDistance)
IPeak* createPeakQLabWithDistance(IPeaksWorkspace & self, const object& data, double detectorDistance)
{
self.createPeak(Mantid::PythonInterface::Converters(data), distance);
return self.createPeak(Mantid::PythonInterface::Converters::PyObjectToV3D(data)(), detectorDistance);
}

}
Expand All @@ -39,9 +41,9 @@ void export_IPeaksWorkspace()
.def("addPeak", &IPeaksWorkspace::addPeak, "Add a peak to the workspace")
.def("removePeak", &IPeaksWorkspace::removePeak, "Remove a peak from the workspace")
.def("getPeak", &IPeaksWorkspace::getPeakPtr, return_internal_reference<>(), "Returns a peak at the given index" )
.def("createPeak", createPeakQLab, return_internal_reference<>(), "Create a Peak and return it from its coordinates in the QLab frame")
.def("createPeak", createPeakQLabWithDistance, return_internal_reference<>(), "Create a Peak and return it from its coordinates in the QLab frame, detector-sample distance explicitly provided")
.def("createPeakHKL", createPeakHKL, return_internal_reference<>(), "Create a Peak and return it from its coordinates in the HKL frame")
.def("createPeak", createPeakQLab, return_value_policy<manage_new_object>(), "Create a Peak and return it from its coordinates in the QLab frame")
.def("createPeak", createPeakQLabWithDistance, return_value_policy<manage_new_object>(), "Create a Peak and return it from its coordinates in the QLab frame, detector-sample distance explicitly provided")
.def("createPeakHKL", createPeakHKL, return_value_policy<manage_new_object>(), "Create a Peak and return it from its coordinates in the HKL frame")
.def("hasIntegratedPeaks", &IPeaksWorkspace::hasIntegratedPeaks, "Determine if the peaks have been integrated")
.def("getRun", &IPeaksWorkspace::mutableRun, return_internal_reference<>(),
"Return the Run object for this workspace")
Expand Down
@@ -1,7 +1,8 @@
import unittest
from testhelpers import run_algorithm, WorkspaceCreationHelper
from mantid.kernel import V3D
from mantid.api import IPeaksWorkspace
from mantid.geometry import OrientedLattice
from mantid.api import IPeaksWorkspace, IPeak

class IPeaksWorkspaceTest(unittest.TestCase):
"""
Expand Down Expand Up @@ -38,6 +39,7 @@ def test_interface(self):
# Create a new peak at some Q in the lab frame
qlab = V3D(1,2,3)
p = pws.createPeak(qlab, 1.54)
p.getQLabFrame()
self.assertAlmostEquals( p.getQLabFrame().X(), 1.0, 3)

# Now try to add the peak back
Expand All @@ -50,7 +52,15 @@ def test_interface(self):

# Peaks workspace will not be integrated by default.
self.assertTrue(not pws.hasIntegratedPeaks())


def test_createPeakHKL(self):
pws = WorkspaceCreationHelper.createPeaksWorkspace(0)
lattice = pws.mutableSample().getOrientedLattice()

# Simple test that the creational method is exposed
p = pws.createPeakHKL([1,1,1])
self.assertTrue(IPeak != None)

def test_peak_setQLabFrame(self):
pws = WorkspaceCreationHelper.createPeaksWorkspace(1)
p = pws.getPeak(0)
Expand All @@ -64,7 +74,7 @@ def test_peak_setQLabFrame(self):
except Exception:
self.fail("Tried setQLabFrame with one V3D argument and a double distance")

def test_peak_setQSampleFrame(self):
def test_peak_setQSampleFrame(self):
pws = WorkspaceCreationHelper.createPeaksWorkspace(1)
p = pws.getPeak(0)
try:
Expand Down
Expand Up @@ -1193,6 +1193,8 @@ createPeaksWorkspace(const int numPeaks) {
peaksWS->addPeak(peak);
}

Mantid::Geometry::OrientedLattice lattice;
peaksWS->mutableSample().setOrientedLattice(&lattice);
return peaksWS;
}

Expand Down

0 comments on commit 0d10ba2

Please sign in to comment.