Skip to content

Add (Vector|Matrix)Backend for numpy array and csr matrix

Carsten Gräser requested to merge feature/add-numpy-backends into master

This adds a Dune::Functions::VectorBackend for flat numpy vectors represented by pybind11::array_t and a Dune::Fufem::MatrixBackend for flat sparse matrices with CSR storage represented by numpy arrays.

Since there's no C/C++ API for scipy.sparse.csr_matrix´, we cannot directly implement a MatrixBackendfor this. However,csr_matrixuses a simple triple of numpy arrays as storage and can be constructed from one. Thus we can almost rely on automatic pybind11 conversion to create acsr_matrix. To this end we let pybind11 auto-convert the result of asTuple()to Python. This will not be acsr_matrixdirectly, but a nested tuple of numpy arrays. The result can be used to construct acsr_matrixin Python usingcsr_matrix(data[0], shape=data[1])`.

To make this work, seamlessly, it also makes the use of VectorBackend in the global functional assembler more flexible and user-friendly by, on the one hand, not relying on support for vector=0 and, on the other hand, auto-generating istlVectorBackend() if needed.

Edited by Carsten Gräser

Merge request reports