How to view the PETScMatrix

Hello everyone!
I’m working with ubuntu 18.04 and Fenics to solve the 2D Possion equation, and the code is as follows:
import dolfin as df
import matplotlib.pyplot as plt
import numpy as np

Create mesh and define function space

mesh = df.UnitSquareMesh(6, 4)
V = df.FunctionSpace(mesh, “Lagrange”, 1)

Define boundary conditions

u0 = df.Expression(“1 + x[0]x[0] + 2x[1]*x[1]”, degree =2)
def u0_boundary(x, on_boundary):
return on_boundary
bc = df.DirichletBC(V, u0, u0_boundary)

Define variational problem

u = df.TrialFunction(V)
v = df.TestFunction(V)
f = df.Constant(-6.0)
a = df.inner(df.nabla_grad(u), df.nabla_grad(v))* df.dx
L = fvdf.dx

A = df.PETScMatrix()
b = df.PETScVector()
A = df.assemble(a)
b = df.assemble(L)
bc.apply(A, b)

u =df. Function(V)
U = u.vector()
#df.solve(A, U, b, ‘cg’, ‘ilu’)

solver = df.KrylovSolver(“cg”, “ilu”)
solver.parameters[“absolute_tolerance”] = 1e-17
solver.parameters[“relative_tolerance”] = 1e-4
solver.parameters[“maximum_iterations”] = 1000
solver.parameters[“error_on_nonconvergence”] = True
solver.parameters[“report”] = True

solver.solve(A, U, b)

Compute solution

file = df.File(“poisson-1.pvd”)
file << u

and now I want to output the PETScMatrix A in the form like the figure, but when I tried the function MatView in PETSc library, but it seems the function isn’t existing. Can anyone tell me how could I do to achieve the goal?
image

You need to call A.mat() to get the petsc4py matrix object with its functionality.

Please note that for further questions, please use markdown format to encapsulate your code i.e.

```python
import dolfin as df

def some_function(x):
    return x
# Replace this with code
# ...

```

Hello! Based on the given advice, I tried:

from petsc4py import PETSc
A.mat().MatView()

but it reminded me that AttributeError: ‘dolfin.cpp.la.Matrix’ object has no attribute ‘mat’, then what should I do for this condition

Do as_backend(A) first.

When you get error messages please supply a minimal example that reproduces the error. Without a minimal example it becomes educated guessing from other people what’s gone wrong.

The code is changed as:

from petsc4py import PETSc
PetSc.MatView(A.mat())

After running the code above, it reminded me that "AttritbuteError: module ‘petsc4py.PETSc’ has no attribute ‘MatView’ ". The output error is the same when using:

import petsc4py
petsc4py.MatView(A.mat())

or

import dolfin as df
df.MatView(A.mat())

Again, please make a minimal reproducible example.
In the snippets above, you have not defined A, so I cannot help you reproducing your issue.

MatView is a function from PETSc (petsc4py,src/binding/petsc4py/src/PETSc/Mat.pyx · main · PETSc / petsc · GitLab), working in a PETSc matrix. Thus you would need something like:

from dolfin import PETScMatrix, as_backend_type
A = PETScMatrix()
A_petsc = as_backend_type(A).mat()
A_petsc.assemble()
A_petsc.view()

Hello! I retried the process based on your suggestion, and the code is as follows:

import dolfin as df
import matplotlib.pyplot as plt
import numpy as np
from dolfin import PETScMatrix, as_backend_type

mesh = df.UnitSquareMesh(6, 4)
V = df.FunctionSpace(mesh, "Lagrange", 1)

# Define boundary conditions
u0 = df.Expression("1 + x[0]*x[0] + 2*x[1]*x[1]", degree =2)
def u0_boundary(x, on_boundary):
    return on_boundary
bc = df.DirichletBC(V, u0, u0_boundary)

# Define variational problem
u = df.TrialFunction(V)
v = df.TestFunction(V)
f = df.Constant(-6.0)
a = df.inner(df.nabla_grad(u), df.nabla_grad(v))* df.dx
L = f*v*df.dx

A = PETScMatrix()
A_petsc = as_backend_type(A.mat())
A_petsc.assemble(a)
A_petsc.view()

While it only output an error as follows, instead in the form of the figure in the below.

TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_27380/3677198767.py in <cell line: 3>()
      1 A = PETScMatrix()
      2 A_petsc = as_backend_type(A.mat())
----> 3 A_petsc.assemble(a)
      4 A_petsc.view()

PETSc/Mat.pyx in petsc4py.PETSc.Mat.assemble()

PETSc/petscmat.pxi in petsc4py.PETSc.assemblytype()

TypeError: an integer is required

image

I never said you should add a here.
This whole step should be replaced by your normal assembly.

Actually, I haven’t truly understand what you mean since I know too less. But I also tried without a in the function, and it returns a message that:

Mat Object: 1 MPI processes
  type not yet set

instead the matrix content I needed in the form of the figure.