Converting to SciPy sparse matrix without Eigen backend

Hi, if your default backend is PETSc then consider the following

from dolfin import *
from scipy.sparse import csr_matrix

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, 'CG', 1)
u, v = TrialFunction(V), TestFunction(V)

A = assemble(inner(u, v)*dx)
mat = as_backend_type(A).mat()

csr = csr_matrix(mat.getValuesCSR()[::-1], shape=mat.size)
3 Likes