Skip to content

Convert the solution in Function class and assembled operator to numpy array or scipy.csr_matrix vice and versa #3103

Closed Answered by connorjward
JunmingDuan asked this question in Firedrake support
Discussion options

You must be logged in to vote

Matrix

I have been hacking around and found the following solution to get a scipy csr_matrix from a Firedrake Matrix:

petsc_mat = firedrake_matrix.M.handle
ai, aj, av = petsc_mat.getValuesCSR()
scipy_matrix = scipy.sparse.csr_matrix((av, aj, ai))

Note that this should probably not be attempted in parallel.

For the inverse operation see this post.

Vector

Firedrake Functions are natively stored as numpy arrays so this is a simple operation. You just want function.dat.data

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by JunmingDuan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants