How to view lhs or rhs of LSE

I’m working with ubuntu and dolfinx 0.4.1 and I trying to view the matrix assembled from the weak form. Actually I tried to follow the lines in

however I end up with an error due to the last line.

         ............
  ---> 34     topology = a.mesh.topology
         35     dofmap0 = a.function_spaces[0].dofmap
         36     dofmap1 = a.function_spaces[1].dofmap

        AttributeError: 'Form' object has no attribute 'mesh'

Code :

  from mpi4py import MPI
  import dolfinx as dox
  import numpy as np
  import ufl

  rect = dox.mesh.create_unit_square(MPI.COMM_WORLD, 1, 1,   dox.mesh.CellType.triangle)

  # Defintion function space
  V = dox.fem.FunctionSpace(rect, ("Lagrange", 1))
  Q=dox.fem.FunctionSpace(rect, ("DG", 0))

  # defintion functions
  x=ufl.SpatialCoordinate(rect)
  f=x[0]

  alpha = dox.fem.Function(V)
  alpha.interpolate(lambda x: x[1])

  gamma = dox.fem.Function(Q)
  gamma.interpolate(lambda x: x[0]*x[1])
  
  # boundary
  rect.topology.create_connectivity(1,0)
  edges = rect.topology.connectivity(1,0)
  e1 = dox.mesh.locate_entities_boundary(rect, 1 , lambda X: np.isclose(X[1],0) )
  all_edges=np.arange(len(edges),dtype=np.int32)
  marker = np.zeros(len(edges),dtype=np.int32)
  marker[e1]=1
  edge_tags = dox.mesh.meshtags(rect, 1 , all_edges ,marker)
  ds = ufl.Measure("ds", domain=rect, subdomain_data=edge_tags)
  
  # weak Form
  u = ufl.TrialFunction(V)
  v = ufl.TestFunction(V)
  K=ufl.grad(u)[0]*ufl.grad(v)[0]*ufl.dx + ufl.grad(u)[1]*ufl.grad(v)[1]*ufl.dx + alpha*u*v*ufl.dx +gamma*u*v*ds(1)
  D=f*v*ufl.dx 

  A = dox.fem.assemble_matrix(K)
  #A.assemble()
  #C = A.convert('dense')

You need to wrap K in a dolfinx.fem.form prior to assembly:

A = dox.fem.assemble_matrix(dox.fem.form(K))
1 Like

I don’t have dolfinx 0.4.1 but my guess is that you have to convert the ufl form to a dolfinx form as @conpierce8 suggests. The following modified code works for dolfinx version 0.4.2.0

from mpi4py import MPI
import dolfinx as dox
import numpy as np
import ufl

print(dox.__version__)

rect = dox.mesh.create_unit_square(MPI.COMM_WORLD, 1, 1,   dox.mesh.CellType.triangle)

# Defintion function space
V = dox.fem.FunctionSpace(rect, ("Lagrange", 1))
Q=dox.fem.FunctionSpace(rect, ("DG", 0))

# defintion functions
x=ufl.SpatialCoordinate(rect)
f=x[0]

alpha = dox.fem.Function(V)
alpha.interpolate(lambda x: x[1])

gamma = dox.fem.Function(Q)
gamma.interpolate(lambda x: x[0]*x[1])

# boundary
rect.topology.create_connectivity(1,0)
edges = rect.topology.connectivity(1,0)
e1 = dox.mesh.locate_entities_boundary(rect, 1 , lambda X: np.isclose(X[1],0) )
all_edges=np.arange(len(edges),dtype=np.int32)
marker = np.zeros(len(edges),dtype=np.int32)
marker[e1]=1
edge_tags = dox.mesh.meshtags(rect, 1 , all_edges ,marker)
ds = ufl.Measure("ds", domain=rect, subdomain_data=edge_tags)

# weak Form
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
K=ufl.grad(u)[0]*ufl.grad(v)[0]*ufl.dx + ufl.grad(u)[1]*ufl.grad(v)[1]*ufl.dx + alpha*u*v*ufl.dx +gamma*u*v*ds(1)
D=f*v*ufl.dx 
form = dox.fem.form(K)


# post version 0.4.2.0 ( I think ?)  the corresponding `petsc` wrappers were moved to a separate submodule `dolfinx.fem.petsc`

A = dox.fem.petsc.assemble_matrix(form)
A.assemble()
C = A.convert("dense")
print(C.getDenseArray()) 

thanks @conpierce8 , I then tried

  A = dox.fem.assemble_matrix(dox.fem.form(K))
  A.assemble()

which comes up with

  AttributeError: 'MatrixCSR' object has no attribute 'assemble'

however @bhaveshshrimali suggestion works fine. thanks again.

I still add the code for the RHS, which seems to be

form = dox.fem.form(D)
A = dox.fem.petsc.assemble_vector(form)
print(A.array)