[cig-commits] r13082 - in cs/cigma/trunk: . src tests/pytests

luis at geodynamics.org luis at geodynamics.org
Wed Oct 15 10:13:08 PDT 2008


Author: luis
Date: 2008-10-15 10:13:08 -0700 (Wed, 15 Oct 2008)
New Revision: 13082

Added:
   cs/cigma/trunk/src/py_Point.cpp
   cs/cigma/trunk/src/py_Point.h
   cs/cigma/trunk/src/py_Points.h
   cs/cigma/trunk/tests/pytests/test_point.py
   cs/cigma/trunk/tests/pytests/test_points.py
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/src/Points.cpp
   cs/cigma/trunk/src/py_NodeCoordinates.cpp
   cs/cigma/trunk/src/py_Points.cpp
   cs/cigma/trunk/src/py_cigma_module.cpp
   cs/cigma/trunk/src/py_cigma_setup.py
   cs/cigma/trunk/tests/pytests/runner.py
Log:
Split python bindings for Point<dim> & Points<T,dim>

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/Makefile.am	2008-10-15 17:13:08 UTC (rev 13082)
@@ -124,6 +124,8 @@
 	src/NodeCoordinates.cpp \
 	src/nc_array.h \
 	src/nc_array.cpp \
+	src/Function.h \
+	src/Function.cpp \
 	src/io_file_reader.h \
 	src/io_file_reader.cpp \
 	src/io_null_reader.h \
@@ -219,6 +221,7 @@
 	src/numpy_util.h \
 	src/numpy_util.cpp \
 	src/py_DataPath.cpp \
+	src/py_Point.cpp \
 	src/py_Points.cpp \
 	src/py_Quadrature.cpp \
 	src/py_Cell.cpp \
@@ -226,6 +229,7 @@
 	src/py_MeshPart.cpp \
 	src/py_ElementBlock.cpp \
 	src/py_NodeCoordinates.cpp \
+	src/py_Function.cpp \
 	src/py_Misc.cpp \
 	src/py_cigma_module.cpp
 
@@ -270,6 +274,8 @@
 	tests/libcigma/NodeCoordinatesTest.cpp \
 	tests/libcigma/ElementBlockTest.h \
 	tests/libcigma/ElementBlockTest.cpp \
+	tests/libcigma/FunctionTest.h \
+	tests/libcigma/FunctionTest.cpp \
 	tests/libcigma/ReaderTest.h \
 	tests/libcigma/ReaderTest.cpp \
 	tests/libcigma/MiscTest.h \

Modified: cs/cigma/trunk/src/Points.cpp
===================================================================
--- cs/cigma/trunk/src/Points.cpp	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/src/Points.cpp	2008-10-15 17:13:08 UTC (rev 13082)
@@ -21,7 +21,7 @@
 template <typename T, int dim>
 Points<T,dim>::~Points()
 {
-    if (data)
+    if (data != 0)
     {
         delete [] data;
         data = 0;

Modified: cs/cigma/trunk/src/py_NodeCoordinates.cpp
===================================================================
--- cs/cigma/trunk/src/py_NodeCoordinates.cpp	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/src/py_NodeCoordinates.cpp	2008-10-15 17:13:08 UTC (rev 13082)
@@ -106,6 +106,7 @@
     numpy_util::check_rank(x, 2);
     numpy_util::check_size(x, n_points() * n_dim());
     this->data = numpy_util::makeArray(x);
+    this->coords = (double *)numpy_util::data(this->data);
 }
 
 

Added: cs/cigma/trunk/src/py_Point.cpp
===================================================================
--- cs/cigma/trunk/src/py_Point.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/py_Point.cpp	2008-10-15 17:13:08 UTC (rev 13082)
@@ -0,0 +1,56 @@
+#include "py_Point.h"
+#include "numpy_util.h"
+
+using namespace cigma;
+using namespace boost::python;
+
+// ----------------------------------------------------------------------------
+
+template <int dim>
+double pyPoint<dim>::get(int i) const
+{
+    if ((i >= dim) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Index is out of range");
+        throw_error_already_set();
+    }
+    return Point<dim>::get(i);
+}
+
+template <int dim>
+void pyPoint<dim>::set(int i, double val)
+{
+    if ((i >= dim) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Index is out of range");
+        throw_error_already_set();
+    }
+    Point<dim>::set(i, val);
+}
+
+
+// ----------------------------------------------------------------------------
+
+void export_Point()
+{
+    using namespace cigma;
+    using namespace boost::python;
+
+    typedef pyPoint<2> Point2d;
+    class_<Point2d>("Point2d")
+        .def("n_dim", &Point2d::n_dim)
+        .def("get", &Point2d::get)
+        .def("set", &Point2d::set)
+        .def("__repr__", &Point2d::repr)
+        .def(self_ns::str(self))    // __str__
+        ;
+
+    typedef pyPoint<3> Point3d;
+    class_<Point3d>("Point3d")
+        .def("n_dim", &Point3d::n_dim)
+        .def("get", &Point3d::get)
+        .def("set", &Point3d::set)
+        .def("__repr__", &Point3d::repr)
+        .def(self_ns::str(self))    // __str__
+        ;
+}

Added: cs/cigma/trunk/src/py_Point.h
===================================================================
--- cs/cigma/trunk/src/py_Point.h	                        (rev 0)
+++ cs/cigma/trunk/src/py_Point.h	2008-10-15 17:13:08 UTC (rev 13082)
@@ -0,0 +1,33 @@
+#ifndef __PY_POINT_H__
+#define __PY_POINT_H__
+
+#include <boost/python.hpp>
+#include "Point.h"
+
+
+template <int dim>
+struct pyPoint : cigma::Point<dim>
+{
+    double get(int i) const;
+    void set(int i, double val);
+
+    boost::python::str repr()
+    {
+        return boost::python::str(*this);
+    }
+};
+
+template <int dim>
+std::ostream& operator<<(std::ostream& os, const pyPoint<dim>& pt)
+{
+    os << "(";
+    for (int i = 0; i < dim; i++)
+    {
+        os << pt[i];
+        if (i != dim-1) { os << ", "; }
+    }
+    os << ")";
+    return os;
+}
+
+#endif

Modified: cs/cigma/trunk/src/py_Points.cpp
===================================================================
--- cs/cigma/trunk/src/py_Points.cpp	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/src/py_Points.cpp	2008-10-15 17:13:08 UTC (rev 13082)
@@ -1,32 +1,155 @@
-#include "Points.h"
-#include <boost/python.hpp>
+#include "numpy_util.h"
+#include "py_Points.h"
 
+using namespace cigma;
+using namespace boost::python;
+
+
+// ----------------------------------------------------------------------------
+
+template <typename T, int dim>
+pyPoints<T,dim>::pyPoints() : na_data(0)
+{
+    PyErr_SetString(PyExc_RuntimeError, "Constructor called without arguments");
+    throw_error_already_set();
+}
+
+template <typename T, int dim>
+pyPoints<T,dim>::pyPoints(int npts) : Points<T,dim>(npts), na_data(0)
+{
+    std::vector<int> dims;
+    dims.push_back(npts);
+    dims.push_back(dim);
+    this->na_data = numpy_util::makeArray(dims, numpy_util::getEnum<T>());
+    this->npts = npts;
+    this->data = (T *)numpy_util::data(this->na_data);
+    for (int i = 0; i < npts; i++)
+    {
+        for (int j = 0; j < dim; j++)
+        {
+            this->data[dim*i + j] = 0;
+        }
+    }
+}
+
+template <typename T, int dim>
+pyPoints<T,dim>::~pyPoints()
+{
+    //std::cout << "Calling ~pyPoints()" << std::endl;
+    this->npts = 0;
+    this->data = 0;
+}
+
+/*
+template <typename T, int dim>
+Point<dim> pyPoints<T,dim>::point(int i) const
+{
+    if ((i >= this->n_points()) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Index is out of bounds");
+        throw_error_already_set();
+    }
+    return Points<T,dim>::get(i);
+}
+// */
+
+template <typename T, int dim>
+T pyPoints<T,dim>::get(int i, int j) const
+{
+    if ((i >= this->n_points()) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "First index is out of bounds");
+        throw_error_already_set();
+    }
+    if ((j >= dim) || (j < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Second index is out of bounds");
+        throw_error_already_set();
+    }
+    return Points<T,dim>::get(i,j);
+}
+
+template <typename T, int dim>
+void pyPoints<T,dim>::set(int i, int j, double val)
+{
+    if ((i >= this->n_points()) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "First index is out of bounds");
+        throw_error_already_set();
+    }
+    if ((j >= dim) || (j < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Second index is out of bounds");
+        throw_error_already_set();
+    }
+    Points<T,dim>::set(i, j, val);
+}
+
+template <typename T, int dim>
+numeric::array pyPoints<T,dim>::getData()
+{
+    return na_data;
+}
+
+template <typename T, int dim>
+void pyPoints<T,dim>::setData(boost::python::numeric::array x)
+{
+    numpy_util::check_type(x, numpy_util::getEnum<T>());
+    numpy_util::check_rank(x, 2);
+    numpy_util::check_size(x, this->n_points() * dim);
+    this->na_data = numpy_util::makeArray(x);
+    this->data = (T *)numpy_util::data(this->na_data);
+}
+
+// ----------------------------------------------------------------------------
+
 void export_Points()
 {
     using namespace cigma;
     using namespace boost::python;
 
-    typedef Points<float,2> Points2f;
-    class_<Points2f,boost::noncopyable>("Points2f", no_init)
+    typedef pyPoints<float,2> Points2f;
+    class_<Points2f,boost::noncopyable>("Points2f", init<int>())
         .def("n_points", &Points2f::n_points)
         .def("n_dim", &Points2f::n_dim)
+        .def("get", &Points2f::get)
+        .def("set", &Points2f::set)
+        .add_property("data", &Points2f::getData, &Points2f::setData)
+        //.def("__repr__", &py_repr_Points<Points2f>)
+        .def("__repr__", &Points2f::repr)
+        .def(self_ns::str(self))    // __str__
         ;
 
-    typedef Points<float,3> Points3f;
-    class_<Points3f,boost::noncopyable>("Points3f", no_init)
+    typedef pyPoints<float,3> Points3f;
+    class_<Points3f, boost::noncopyable>("Points3f", init<int>())
         .def("n_points", &Points3f::n_points)
         .def("n_dim", &Points3f::n_dim)
+        .def("get", &Points3f::get)
+        .def("set", &Points3f::set)
+        .add_property("data", &Points3f::getData, &Points3f::setData)
+        //.def("__repr__", &py_repr_Points<Points3f>)
+        .def(self_ns::str(self))    // __str__
         ;
 
-    typedef Points<double,2> Points2d;
-    class_<Points2d,boost::noncopyable>("Points2d", no_init)
+    typedef pyPoints<double,2> Points2d;
+    class_<Points2d, boost::noncopyable>("Points2d", init<int>())
         .def("n_points", &Points2d::n_points)
         .def("n_dim", &Points2d::n_dim)
+        .def("get", &Points2d::get)
+        .def("set", &Points2d::set)
+        .add_property("data", &Points2d::getData, &Points2d::setData)
+        //.def("__repr__", &py_repr_Points<Points2d>)
+        .def(self_ns::str(self))    // __str__
         ;
 
-    typedef Points<double,3> Points3d;
-    class_<Points3d,boost::noncopyable>("Points3d", no_init)
+    typedef pyPoints<double,3> Points3d;
+    class_<Points3d, boost::noncopyable>("Points3d", init<int>())
         .def("n_points", &Points3d::n_points)
         .def("n_dim", &Points3d::n_dim)
+        .def("get", &Points3d::get)
+        .def("set", &Points3d::set)
+        .add_property("data", &Points3d::getData, &Points3d::setData)
+        //.def("__repr__", &py_repr_Points<Points3d>)
+        .def(self_ns::str(self))    // __str__
         ;
 }

Added: cs/cigma/trunk/src/py_Points.h
===================================================================
--- cs/cigma/trunk/src/py_Points.h	                        (rev 0)
+++ cs/cigma/trunk/src/py_Points.h	2008-10-15 17:13:08 UTC (rev 13082)
@@ -0,0 +1,59 @@
+#ifndef __PY_POINTS_H__
+#define __PY_POINTS_H__
+
+#include <string>
+#include <iostream>
+#include <boost/python.hpp>
+#include "Points.h"
+#include "py_Point.h"
+
+template <typename T, int dim>
+struct pyPoints : cigma::Points<T,dim>
+{
+    pyPoints();
+    pyPoints(int npts);
+    ~pyPoints();
+
+    //cigma::Point<dim> point(int i) const;
+
+    T get(int i, int j) const;
+    void set(int i, int j, double val);
+    
+    boost::python::numeric::array getData();
+    void setData(boost::python::numeric::array x);
+
+    /* XXX: why isn't this function called by __repr__? */
+    boost::python::str repr()
+    {
+        return boost::python::str(*this);
+    }
+
+private:
+    boost::python::numeric::array na_data;
+};
+
+// ----------------------------------------------------------------------------
+
+template <typename T> std::string point_type_to_str() { return std::string("unknown-type"); }
+template <> std::string point_type_to_str<double>() { return "double"; }
+template <> std::string point_type_to_str<float>()  { return "float"; }
+
+/*
+template <class Pts>
+boost::python::str py_repr_Points(const Pts& pts)
+{
+    return boost::python::str(pts);
+}
+*/
+
+template <typename T, int dim>
+std::ostream& operator<<(std::ostream& os, const pyPoints<T,dim>& pts)
+{
+    os << "<cigma::Points " << point_type_to_str<T>()
+       << " array of shape "
+       << pts.n_points() << " x " << pts.n_dim()
+       << ">";
+    return os;
+}
+
+#endif

Modified: cs/cigma/trunk/src/py_cigma_module.cpp
===================================================================
--- cs/cigma/trunk/src/py_cigma_module.cpp	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/src/py_cigma_module.cpp	2008-10-15 17:13:08 UTC (rev 13082)
@@ -6,6 +6,7 @@
 extern void export_Exception();
 extern void export_Hello();
 extern void export_DataPath();
+extern void export_Point();
 extern void export_Points();
 extern void export_Quadrature();
 extern void export_Cell();
@@ -13,6 +14,7 @@
 extern void export_MeshPart();
 extern void export_ElementBlock();
 extern void export_NodeCoordinates();
+extern void export_Function();
 extern void export_Misc();
 
 using namespace boost::python;
@@ -25,6 +27,7 @@
     export_Exception();
     export_Hello();
     export_DataPath();
+    export_Point();
     export_Points();
     export_Quadrature();
     export_Cell();
@@ -32,6 +35,7 @@
     export_MeshPart();
     export_ElementBlock();
     export_NodeCoordinates();
+    export_Function();
     export_Misc();
 }
 

Modified: cs/cigma/trunk/src/py_cigma_setup.py
===================================================================
--- cs/cigma/trunk/src/py_cigma_setup.py	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/src/py_cigma_setup.py	2008-10-15 17:13:08 UTC (rev 13082)
@@ -20,6 +20,7 @@
     'py_Exception.cpp',
     'py_Hello.cpp',
     'py_DataPath.cpp',
+    'py_Point.cpp',
     'py_Points.cpp',
     'py_Quadrature.cpp',
     'py_Cell.cpp',
@@ -27,6 +28,7 @@
     'py_MeshPart.cpp',
     'py_ElementBlock.cpp',
     'py_NodeCoordinates.cpp',
+    'py_Function.cpp',
     'py_Misc.cpp',
 ]
 

Modified: cs/cigma/trunk/tests/pytests/runner.py
===================================================================
--- cs/cigma/trunk/tests/pytests/runner.py	2008-10-15 17:13:06 UTC (rev 13081)
+++ cs/cigma/trunk/tests/pytests/runner.py	2008-10-15 17:13:08 UTC (rev 13082)
@@ -13,6 +13,13 @@
     suite = unittest.TestSuite()
     for mod in modules:
         suite.addTest(mod.create_suite())
+    
+    def separator():
+        print
+        print '-' * 70
+        print 
+
     runner = unittest.TextTestRunner(verbosity=2)
+    separator()
     runner.run(suite)
 

Added: cs/cigma/trunk/tests/pytests/test_point.py
===================================================================
--- cs/cigma/trunk/tests/pytests/test_point.py	                        (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_point.py	2008-10-15 17:13:08 UTC (rev 13082)
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+
+from _cigma import Point2d, Point3d
+
+import unittest
+class PointTest(unittest.TestCase):
+    def __init__(self, *args):
+        unittest.TestCase.__init__(self, *args)
+    def test_point2d(self):
+        pt = Point2d()
+    def test_point3d(self):
+        pt = Point3d()
+
+def create_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(PointTest))
+    return suite
+

Added: cs/cigma/trunk/tests/pytests/test_points.py
===================================================================
--- cs/cigma/trunk/tests/pytests/test_points.py	                        (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_points.py	2008-10-15 17:13:08 UTC (rev 13082)
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+from _cigma import Points2d, Points3d
+
+import unittest
+class PointsTest(unittest.TestCase):
+    def __init__(self, *args):
+        unittest.TestCase.__init__(self, *args)
+    def test_points2d(self):
+        x = Points2d(10)
+    def test_points3d(self):
+        x = Points3d(10)
+
+def create_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(PointsTest))
+    return suite



More information about the cig-commits mailing list