Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #10281. Exporting SpaceGroup and Factory to python
- Loading branch information
Michael Wedel
committed
Oct 10, 2014
1 parent
14ce7da
commit fd637c9
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Code/Mantid/Framework/PythonInterface/mantid/geometry/src/Exports/SpaceGroup.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.") | ||
; | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
Code/Mantid/Framework/PythonInterface/mantid/geometry/src/Exports/SpaceGroupFactory.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Code/Mantid/Framework/PythonInterface/test/python/mantid/geometry/SpaceGroupTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |