Skip to content

Commit

Permalink
Started exporting VMD class to python.
Browse files Browse the repository at this point in the history
Required by basis vector methods in MDGeometry
Refs #6027
  • Loading branch information
martyngigg committed Aug 3, 2013
1 parent 1dcfc55 commit 608c45f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set ( EXPORT_FILES
src/Exports/ArrayProperty.cpp
src/Exports/Quat.cpp
src/Exports/V3D.cpp
src/Exports/VMD.cpp
src/Exports/StlContainers.cpp
src/Exports/Logger.cpp
src/Exports/Unit.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "MantidKernel/VMD.h"
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/return_value_policy.hpp>

using Mantid::Kernel::VMD;
using Mantid::Kernel::VMD_t;
using namespace boost::python;

void export_VMD()
{
class_<VMD>("VMD", init<>("Default constructor gives an object with 1 dimension"))
.def(init<VMD_t,VMD_t>("Constructs a 2 dimensional vector at the point given", args(("val0"),("val1"))))
.def(init<VMD_t,VMD_t,VMD_t>("Constructs a 3 dimensional vector at the point given",
args(("val0"),("val1"),("val2"))))
.def(init<VMD_t,VMD_t,VMD_t,VMD_t>("Constructs a 4 dimensional vector at the point given",
args(("val0"),("val1"),("val2"),("val3"))))
.def(init<VMD_t,VMD_t,VMD_t,VMD_t,VMD_t>("Constructs a 5 dimensional vector at the point given",
args(("val0"),("val1"),("val2"),("val3"),("val4"))))
.def(init<VMD_t,VMD_t,VMD_t,VMD_t,VMD_t,VMD_t>("Constructs a 6 dimensional vector at the point given",
args(("val0"),("val1"),("val2"),("val3"),("val5"))))

.def("getNumDims", &VMD::getNumDims, "Returns the number of dimensions the contained in the vector")

//----------------------------- special methods --------------------------------
.def("__getitem__", (const VMD_t &(VMD::*)(size_t)const)&VMD::operator[], return_value_policy<copy_const_reference>())
.def(self + self)
.def(self += self)
// cppcheck-suppress duplicateExpression
.def(self - self)
.def(self -= self)
.def(self * self)
.def(self *= self)
.def(self / self)
.def(self /= self)
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set ( TEST_PY_FILES
UnitFactoryTest.py
UnitsTest.py
V3DTest.py
VMDTest.py
)

check_tests_valid ( ${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES} )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest
from mantid.kernel import VMD

class VMDTest(unittest.TestCase):

def test_default_construction_gives_object_with_single_dimension(self):
one = VMD()
self.assertEquals(1, one.getNumDims())

def test_constructors_with_dimension_pts(self):
pts = [1]
for i in range(2,7):
pts.append(i)
vector = VMD(*pts) #unpack list
self.assertEquals(i, vector.getNumDims())

def test_value_access_is_read_only(self):
vector = VMD(1.0,2.0)
self.assertAlmostEqual(1.0, vector[0])
self.assertAlmostEqual(2.0, vector[1])

try:
vector[1] = 5.0
except TypeError:
pass
except:
self.fail("Operator setters have thrown an unexpected exception type. Expected TypeError.")
else:
self.fail("Operator setters have not thrown an exception. Expected to be read only.")


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

0 comments on commit 608c45f

Please sign in to comment.