Skip to content

Commit

Permalink
Refs #10281. Exporting SpaceGroup and Factory to python
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Oct 10, 2014
1 parent 14ce7da commit fd637c9
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Expand Up @@ -26,6 +26,8 @@ set ( EXPORT_FILES
src/Exports/Object.cpp
src/Exports/PointGroup.cpp
src/Exports/PointGroupFactory.cpp
src/Exports/SpaceGroup.cpp
src/Exports/SpaceGroupFactory.cpp
src/Exports/SymmetryOperation.cpp
src/Exports/SymmetryOperationFactory.cpp
)
Expand Down
@@ -0,0 +1,58 @@

#include "MantidGeometry/Crystal/SpaceGroup.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToV3D.h"

#include <boost/python/class.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/list.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::SpaceGroup;
using Mantid::Geometry::SymmetryOperation;

using namespace boost::python;

namespace //<unnamed>
{
using namespace Mantid::PythonInterface;

boost::python::list getEquivalentPositions(SpaceGroup & self, const object& point)
{
const std::vector<Mantid::Kernel::V3D> &equivalents = self.getEquivalentPositions(Converters::PyObjectToV3D(point)());

boost::python::list pythonEquivalents;
for(auto it = equivalents.begin(); it != equivalents.end(); ++it) {
pythonEquivalents.append(*it);
}

return pythonEquivalents;
}

std::vector<std::string> getSymmetryOperationStrings(SpaceGroup & self)
{
const std::vector<SymmetryOperation> &symOps = self.getSymmetryOperations();

std::vector<std::string> pythonSymOps;
for(auto it = symOps.begin(); it != symOps.end(); ++it) {
pythonSymOps.push_back((*it).identifier());
}

return pythonSymOps;
}

}

void export_SpaceGroup()
{
register_ptr_to_python<boost::shared_ptr<const SpaceGroup> >();

class_<SpaceGroup, boost::noncopyable>("SpaceGroup", no_init)
.def("order", &SpaceGroup::order)
.def("getSymmetryOperationStrings", &getSymmetryOperationStrings)
.def("number", &SpaceGroup::number)
.def("hmSymbol", &SpaceGroup::hmSymbol)
.def("getEquivalentPositions", &getEquivalentPositions, "Returns an array with all symmetry equivalents of the supplied HKL.")
;
}

@@ -0,0 +1,50 @@
#include "MantidGeometry/Crystal/SpaceGroupFactory.h"
#include "MantidPythonInterface/kernel/PythonObjectInstantiator.h"

#include <boost/python/class.hpp>

using namespace Mantid::Geometry;
using namespace boost::python;

namespace
{
using namespace Mantid::PythonInterface;

std::vector<std::string> allSpaceGroupSymbols(SpaceGroupFactoryImpl &self)
{
return self.subscribedSpaceGroupSymbols();
}

std::vector<std::string> spaceGroupSymbolsForNumber(SpaceGroupFactoryImpl &self, size_t number)
{
return self.subscribedSpaceGroupSymbols(number);
}

bool isSubscribedSymbol(SpaceGroupFactoryImpl &self, const std::string &symbol)
{
return self.isSubscribed(symbol);
}

bool isSubscribedNumber(SpaceGroupFactoryImpl &self, size_t number)
{
return self.isSubscribed(number);
}

}

void export_SpaceGroupFactory()
{

class_<SpaceGroupFactoryImpl,boost::noncopyable>("SpaceGroupFactoryImpl", no_init)
.def("isSubscribedSymbol", &isSubscribedSymbol)
.def("isSubscribedNumber", &isSubscribedNumber)
.def("createSpaceGroup", &SpaceGroupFactoryImpl::createSpaceGroup)
.def("allSubscribedSpaceGroupSymbols", &allSpaceGroupSymbols)
.def("subscribedSpaceGroupSymbols", &spaceGroupSymbolsForNumber)
.def("subscribedSpaceGroupNumbers", &SpaceGroupFactoryImpl::subscribedSpaceGroupNumbers)
.def("Instance", &SpaceGroupFactory::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the SpaceGroupFactory singleton")
.staticmethod("Instance")
;
}

Expand Up @@ -11,6 +11,7 @@ set ( TEST_PY_FILES
ReferenceFrameTest.py
UnitCellTest.py
PointGroupTest.py
SpaceGroupTest.py
SymmetryOperationTest.py
)

Expand Down
@@ -0,0 +1,31 @@
import unittest
from mantid.geometry import SpaceGroup, SpaceGroupFactoryImpl

class SpaceGroupTest(unittest.TestCase):

def test_creation(self):
self.assertRaises(ValueError, SpaceGroupFactoryImpl.Instance().createSpaceGroup, "none")

SpaceGroupFactoryImpl.Instance().createSpaceGroup("I m -3 m")

def test_interface(self):
spaceGroup = SpaceGroupFactoryImpl.Instance().createSpaceGroup("P -1")
self.assertEquals(spaceGroup.hmSymbol(), "P -1")
self.assertEquals(spaceGroup.order(), 2)

symOpStrings = spaceGroup.getSymmetryOperationStrings()

self.assertEqual(len(symOpStrings), 2)
self.assertTrue("x,y,z" in symOpStrings)
self.assertTrue("-x,-y,-z" in symOpStrings)

def test_equivalentPositions(self):
spaceGroup = SpaceGroupFactoryImpl.Instance().createSpaceGroup("P -1")

position = [0.34, 0.3, 0.4]
equivalentPositions = spaceGroup.getEquivalentPositions(position)

self.assertEqual(len(equivalentPositions), 2)

if __name__ == '__main__':
unittest.main()

0 comments on commit fd637c9

Please sign in to comment.