This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

The following code does not work, why?

–2 votes
    u = TrialFunction(self.V)
    v = TestFunction(self.V)

    m = u*v*dx
    M = uBLASSparseMatrix()
    assemble(m, tensor = M)

    #Convert into 
    (row, col, data) = M.data()   # get sparse data
        col = np.intc(col)
        row = np.intc(row)
        n   = M.size(0)

        self.M = csc_matrix( (data,col,row), shape=(n,n), dtype='d')
asked Dec 16, 2015 by Liu_Wen FEniCS Novice (100 points)

What error do you get? Can you post a complete example? Is csc_matrix from scipy or somewhere else?

If you're using FEniCS 1.6.0, maybe it doesn't work because it seems uBLASSparseMatrix() has been removed...
An error message and a working example (minimal python code that actually runs) would help a lot.

I tried this:

#!/usr/bin/python2
import numpy as np
from dolfin import *
from scipy.sparse import csc_matrix

mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, "DG", 1)

u = TrialFunction(V)
v = TestFunction(V)

m = u*v*dx
M = uBLASSparseMatrix()
assemble(m, tensor = M)

#Convert into 
(row, col, data) = M.data()   # get sparse data
col = np.intc(col)
row = np.intc(row)
n   = M.size(0)

M = csc_matrix( (data,col,row), shape=(n,n), dtype='d')

and it returned:

NameError: name 'uBLASSparseMatrix' is not defined

1 Answer

+1 vote

It seems that you are trying to convert your Matrix assembled in Fenics to a sparse python format. I suggest you do it with PETScMatrix like this:

from dolfin import*
import numpy as np
from petsc4py import PETSc
import scipy.sparse as sp_sparse

Assembling your matrix goes here

test = M.mat()
row, col, data = test.getValuesCSR()
M_scipy = sp_sparse.csc_matrix((data, col, row), shape=(N_row, N_row),dtype=np.float) 
answered Apr 7, 2016 by jh600 FEniCS Novice (900 points)
...