Commit 22e9c16f authored by Michaël Sghaïer's avatar Michaël Sghaïer
Browse files

Wrapper of the FieldMatrix class

parent f675c5a1
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@ IF(Boost_FOUND)
    ADD_LIBRARY(fvector SHARED fvector.cc)
    TARGET_LINK_LIBRARIES(fvector ${Boost_LIBRARIES})

    ADD_LIBRARY(fmatrix SHARED fmatrix.cc)
    TARGET_LINK_LIBRARIES(fmatrix ${Boost_LIBRARIES})

ELSEIF(NOT Boost_FOUND)
    MESSAGE(FATAL_ERROR "Unable to find correct Boost version. Did you set BOOST_ROOT?")
ENDIF()

src/fmatrix.cc

0 → 100644
+10 −0
Original line number Diff line number Diff line
#include "./fmatrix.hh"
#include <boost/python.hpp>

using namespace boost::python;

BOOST_PYTHON_MODULE(libfmatrix)
{
    WrapFieldMatrix<double, 1, 1>();
    WrapFieldMatrix<double, 1, 2>();
}

src/fmatrix.hh

0 → 100644
+67 −0
Original line number Diff line number Diff line
#include <../dune/common/fmatrix.hh>
#include <../dune/common/fvector.hh>
#include <boost/python.hpp>

using namespace boost::python;

template<class K, int ROWS, int COLS>
class WrapFieldMatrix
{
public:
    typedef typename Dune::FieldMatrix<K, ROWS, COLS> FMatrix;

    static FMatrix leftmultiplyany(FMatrix* fmatrix, object const& M)
    {
        Dune::FieldMatrix<K, ROWS, ROWS>& fmatrix2 =
            extract<Dune::FieldMatrix<K, ROWS, ROWS>&>(M);

        return fmatrix->leftmultiplyany(fmatrix2);
    }

    static FMatrix rightmultiplyany(FMatrix* fmatrix, object const& M)
    {
        Dune::FieldMatrix<K, COLS, COLS>& fmatrix2 =
            extract<Dune::FieldMatrix<K, COLS, COLS>&>(M);

        return fmatrix->rightmultiplyany(fmatrix2);
    }

    static FMatrix& rightmultiply(FMatrix* fmatrix, object const& M)
    {
        Dune::FieldMatrix<K, COLS, COLS>& fmatrix2 =
            extract<Dune::FieldMatrix<K, COLS, COLS>&>(M);

        return fmatrix->rightmultiply(fmatrix2);
    }

    static int mat_rows(FMatrix* _)
    {
        return ROWS;
    }

    static int mat_cols(FMatrix* _)
    {
        return COLS;
    }

    static Dune::FieldVector<K, COLS>& mat_access(FMatrix* fmatrix, unsigned int i)
    {
        return fmatrix->mat_access(i);
    }

    WrapFieldMatrix()
    {
        class_<FMatrix>(("FieldMatrix" +
                         std::to_string(ROWS) + std::to_string(COLS)).c_str())

            .def(init<>("Default constructor"))
            .def(init<K>(args("t"),
                        "Constructor initializing the whole matrix with a scalar"))
            .def("leftmultiplyany", leftmultiplyany, return_value_policy<return_by_value>())
            .def("rightmultiply", rightmultiply, return_value_policy<reference_existing_object>())
            .def("rightmultiplyany", rightmultiplyany, return_value_policy<return_by_value>())
            .def("mat_rows", mat_rows)
            .def("mat_cols", mat_cols)
            .def("mat_access", mat_access, return_value_policy<reference_existing_object>());
    }
};