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

luis at geodynamics.org luis at geodynamics.org
Wed Oct 29 15:11:24 PDT 2008


Author: luis
Date: 2008-10-29 15:11:24 -0700 (Wed, 29 Oct 2008)
New Revision: 13162

Added:
   cs/cigma/trunk/src/py_ElementBlock.h
   cs/cigma/trunk/tests/pytests/test_mesh_part.py
Modified:
   cs/cigma/trunk/Makefile.am
   cs/cigma/trunk/src/eb_array.cpp
   cs/cigma/trunk/src/eb_array.h
   cs/cigma/trunk/src/py_ElementBlock.cpp
   cs/cigma/trunk/tests/libcigma/ElementBlockTest.cpp
   cs/cigma/trunk/tests/libcigma/ElementBlockTest.h
Log:
Python bindings for eb_array (an array implementation of ElementBlock)

Modified: cs/cigma/trunk/Makefile.am
===================================================================
--- cs/cigma/trunk/Makefile.am	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/Makefile.am	2008-10-29 22:11:24 UTC (rev 13162)
@@ -116,12 +116,14 @@
 	src/fe_tri3.cpp \
 	src/Quadrature.h \
 	src/Quadrature.cpp \
-	src/ElementBlock.h \
-	src/ElementBlock.cpp \
 	src/NodeCoordinates.h \
 	src/NodeCoordinates.cpp \
 	src/nc_array.h \
 	src/nc_array.cpp \
+	src/ElementBlock.h \
+	src/ElementBlock.cpp \
+	src/eb_array.h \
+	src/eb_array.cpp \
 	src/MeshPart.h \
 	src/MeshPart.cpp \
 	src/DofHandler.h \

Modified: cs/cigma/trunk/src/eb_array.cpp
===================================================================
--- cs/cigma/trunk/src/eb_array.cpp	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/src/eb_array.cpp	2008-10-29 22:11:24 UTC (rev 13162)
@@ -1,31 +1,51 @@
+#include <cassert>
 #include "eb_array.h"
-
 using namespace cigma;
 
 eb_array::eb_array()
 {
     nel = 0;
     ndofs = 0;
+    connect = 0;
+}
 
-    cell = 0;
-    locator = 0;
+eb_array::eb_array(int nel, int ndofs)
+{
+    this->nel = nel;
+    this->ndofs = ndofs;
+    this->connect = new int[nel*ndofs]; // XXX: use IdType
 }
 
 eb_array::~eb_array()
 {
-    if (cell != 0)
+    nel = 0;
+    ndofs = 0;
+    if (connect != 0)
     {
-        delete cell;
-        cell = 0;
+        delete [] connect;
+        connect = 0;
     }
-    if (locator != 0)
+}
+
+void eb_array::reinit(int nel, int ndofs)
+{
+    if (this->connect != 0)
     {
-        delete locator;
-        locator = 0;
+        delete [] this->connect;
     }
+    this->nel = nel;
+    this->ndofs = ndofs;
+    this->connect = new int[nel*ndofs]; // XXX: use IdType
 }
 
-int eb_array::getCellNodeId(int e, int i)
+void eb_array::setConnectivity(int *connect)
 {
-    return 0; // XXX
+    assert(nel > 0);
+    assert(ndofs > 0);
+    assert(this->connect != 0);
+    for (int n = 0; n < nel*ndofs; n++)
+    {
+        this->connect[n] = connect[n];
+    }
 }
+

Modified: cs/cigma/trunk/src/eb_array.h
===================================================================
--- cs/cigma/trunk/src/eb_array.h	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/src/eb_array.h	2008-10-29 22:11:24 UTC (rev 13162)
@@ -9,24 +9,32 @@
 public:
 
     eb_array();
+    eb_array(int nel, int ndofs);
     ~eb_array();
 
+    void reinit(int nel, int ndofs);
+    void setConnectivity(int *connect);
+
     int n_cells() const;
     int n_dofs() const;
+    int getId(int e, int i) const;
 
-    int getCellNodeId(int e, int i);
-
 public:
 
     int nel;
     int ndofs;
     int *connect;   // XXX: replace int by IdType
-
-    cigma::Cell *cell;
-    cigma::Locator *locator;
 };
 
 inline int eb_array::n_cells() const { return nel; }
+
 inline int eb_array::n_dofs() const { return ndofs; }
 
+inline int eb_array::getId(int e, int i) const
+{
+    assert((0 <= e) && (e < nel)); // XXX: comment out
+    assert((0 <= i) && (i < ndofs));
+    return connect[ndofs*e + i];
+}
+
 #endif

Modified: cs/cigma/trunk/src/py_ElementBlock.cpp
===================================================================
--- cs/cigma/trunk/src/py_ElementBlock.cpp	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/src/py_ElementBlock.cpp	2008-10-29 22:11:24 UTC (rev 13162)
@@ -1,12 +1,78 @@
-#include "ElementBlock.h"
-#include <boost/python.hpp>
+#include "py_ElementBlock.h"
+using namespace cigma;
+using namespace boost::python;
 
+// ----------------------------------------------------------------------------
+
+py_eb_array::py_eb_array() : data(0)
+{
+    PyErr_SetString(PyExc_RuntimeError, "Constructor called without arguments");
+    throw_error_already_set();
+}
+
+py_eb_array::py_eb_array(int nel, int ndofs) : eb_array(), data(0)
+{
+    std::vector<int> dims;
+    dims.push_back(nel);
+    dims.push_back(ndofs);
+    this->data = numpy_util::makeArray(dims, PyArray_INT);
+
+    this->nel = nel;
+    this->ndofs = ndofs;
+    this->connect = (int *) numpy_util::data(this->data);
+}
+
+py_eb_array::~py_eb_array()
+{
+    //std::cout << "Calling ~py_eb_array()\n";
+    nel = 0;
+    ndofs = 0;
+    connect = 0;
+}
+
+int py_eb_array::getId(int e, int i) const
+{
+    if ((e >= n_cells()) || (e < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "First index is out of bounds");
+        throw_error_already_set();
+    }
+    if ((i >= n_dofs()) || (i < 0))
+    {
+        PyErr_SetString(PyExc_IndexError, "Second index is out of bounds");
+        throw_error_already_set();
+    }
+    return eb_array::getId(e,i);
+}
+
+numeric::array py_eb_array::getData()
+{
+    return data;
+}
+
+void py_eb_array::setData(numeric::array x)
+{
+    numpy_util::check_type(x, PyArray_INT);
+    numpy_util::check_rank(x, 2);
+    numpy_util::check_size(x, n_cells() * n_dofs());
+    this->data = numpy_util::makeArray(x);
+    this->connect = (int *) numpy_util::data(this->data);
+}
+
+
+// ----------------------------------------------------------------------------
+
 void export_ElementBlock()
 {
     using namespace cigma;
     using namespace boost::python;
 
-    class_<ElementBlock, boost::noncopyable>("ElementBlock", no_init)
+    typedef py_eb_array eb;
+    class_<py_eb_array, boost::noncopyable>("eb_array", init<int,int>())
+        .def("n_cells", &eb::n_cells)
+        .def("n_dofs", &eb::n_dofs)
+        .def("getId", &eb::getId)
+        .add_property("data", &eb::getData, &eb::setData)
         ;
 }
 

Added: cs/cigma/trunk/src/py_ElementBlock.h
===================================================================
--- cs/cigma/trunk/src/py_ElementBlock.h	                        (rev 0)
+++ cs/cigma/trunk/src/py_ElementBlock.h	2008-10-29 22:11:24 UTC (rev 13162)
@@ -0,0 +1,24 @@
+#ifndef __PY_ELEMENT_BLOCK_H__
+#define __PY_ELEMENT_BLOCK_H__
+
+#include "numpy_util.h"
+#include "Common.h"
+#include "ElementBlock.h"
+#include "eb_array.h"
+
+struct py_eb_array : eb_array
+{
+    py_eb_array();
+    py_eb_array(int nel, int ndofs);
+    ~py_eb_array();
+
+    int getId(int e, int i) const;
+
+    boost::python::numeric::array getData();
+    void setData(boost::python::numeric::array data);
+
+private:
+    boost::python::numeric::array data;
+};
+
+#endif

Modified: cs/cigma/trunk/tests/libcigma/ElementBlockTest.cpp
===================================================================
--- cs/cigma/trunk/tests/libcigma/ElementBlockTest.cpp	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/tests/libcigma/ElementBlockTest.cpp	2008-10-29 22:11:24 UTC (rev 13162)
@@ -1,9 +1,41 @@
 #include "ElementBlockTest.h"
 using namespace libcigma;
 
-ElementBlockTest::ElementBlockTest() {}
-ElementBlockTest::~ElementBlockTest() {}
+#include <boost/smart_ptr.hpp>
+using boost::shared_ptr;
 
+#include "ElementBlock.h"
+#include "eb_array.h"
+using namespace cigma;
+
+const double delta = 1e-8;
+
 void ElementBlockTest::test_eb_array()
 {
+    int e,j;
+    const int nel = 4;
+    const int ndofs = 8;
+
+    int connect[nel*ndofs] = {
+        11, 12, 13, 14, 15, 16, 17, 18,
+        21, 22, 23, 24, 25, 26, 27, 28,
+        31, 32, 33, 34, 35, 36, 37, 38,
+        41, 42, 43, 44, 45, 46, 47, 48
+    };
+
+    shared_ptr<eb_array> eb(new eb_array(nel, ndofs));
+    eb->setConnectivity(connect);
+
+    // test dimensions
+    CPPUNIT_ASSERT_EQUAL(eb->n_cells(), nel);
+    CPPUNIT_ASSERT_EQUAL(eb->n_dofs(), ndofs);
+
+    // test getter
+    for (e = 0; e < nel; e++)
+    {
+        for (j = 0; j < ndofs; j++)
+        {
+            CPPUNIT_ASSERT_EQUAL(eb->getId(e,j), connect[ndofs*e + j]);
+        }
+    }
 }

Modified: cs/cigma/trunk/tests/libcigma/ElementBlockTest.h
===================================================================
--- cs/cigma/trunk/tests/libcigma/ElementBlockTest.h	2008-10-29 22:11:22 UTC (rev 13161)
+++ cs/cigma/trunk/tests/libcigma/ElementBlockTest.h	2008-10-29 22:11:24 UTC (rev 13162)
@@ -14,8 +14,8 @@
         CPPUNIT_TEST_SUITE_END();
 
     public:
-        ElementBlockTest();
-        ~ElementBlockTest();
+        ElementBlockTest() {}
+        ~ElementBlockTest() {}
 
         void test_eb_array();
     };

Added: cs/cigma/trunk/tests/pytests/test_mesh_part.py
===================================================================
--- cs/cigma/trunk/tests/pytests/test_mesh_part.py	                        (rev 0)
+++ cs/cigma/trunk/tests/pytests/test_mesh_part.py	2008-10-29 22:11:24 UTC (rev 13162)
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+import numpy
+from _cigma import MeshPart, nc_array, eb_array
+
+import unittest
+class MeshPartTest(unittest.TestCase):
+    def __init__(self, *args):
+        unittest.TestCase.__init__(self, *args)
+    def test_nc_array(self):
+        nc = nc_array(10,3)
+        self.assertEqual(nc.n_points(), 10)
+        self.assertEqual(nc.n_dim(), 3)
+    def test_eb_array(self):
+        eb = eb_array(2,8)
+        self.assertEqual(eb.n_cells(), 2)
+        self.assertEqual(eb.n_dofs(), 8)
+    def test_mesh_part(self):
+        pass
+
+def create_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(MeshPartTest))
+    return suite
+



More information about the CIG-COMMITS mailing list