Assembling Gram matrix

Hi all,

This probably comes off as a very basic question, however, I have been searching for a couple of hours and I don’t know how to continue. Hopefully, someone here can explain to me what I am doing wrong! I am trying to compute the Gram matrix of a solution. My MWE looks as follows:

from mpi4py import MPI
from dolfinx.fem import functionspace,form
from dolfinx.fem.petsc import assemble_matrix
from dolfinx.mesh import create_unit_interval
from ufl import TestFunction, TrialFunction, inner, grad, dx, dot

mesh = create_unit_interval(MPI.COMM_SELF, 8)
V = functionspace(mesh,('CG',1))
u = TrialFunction(V)
v = TestFunction(V)

G1 = form(inner(u,v) * dx)
G1_assembled = assemble_matrix(G1)

print(G1)

However, I don’t know how to set this into an array that I can save or pickle. Could someone please explain to me how to continue? Thanks a lot in advance!
Kind regards, Daan

Just add

G1_assembled.assemble()

and then

G1_dense = G1_assembled[:,:]

You could also use the built in matrices, and do the following

from mpi4py import MPI
from dolfinx.fem import functionspace,form, assemble_matrix
from dolfinx.mesh import create_unit_interval
from ufl import TestFunction, TrialFunction, inner, dx

mesh = create_unit_interval(MPI.COMM_SELF, 8)
V = functionspace(mesh,('CG',1))
u = TrialFunction(V)
v = TestFunction(V)

G1 = form(inner(u,v) * dx)
G1_assembled = assemble_matrix(G1)
G1_assembled.scatter_reverse()
print(G1_assembled.to_scipy())

to get a scipy csr matrix