Problem with assemble_matrix and mult operation

Hi,

I have an issue when I use the mult operation between a matrix and a vector. I’m not sure what that error means, I think that the matrix A is not well defined.

import dolfinx
import ufl
from petsc4py import PETSc
from mpi4py import MPI
import numpy as np
from dolfinx import fem

xc = 25
yc = 25
R = 25

mesh = dolfinx.mesh.create_box(MPI.COMM_WORLD, [np.array([0,0,0]), np.array([50, 50, 50])], [5,5,5])
V =  fem.VectorFunctionSpace(mesh, ("CG", 1))
def w_init(x):
    
    values = np.zeros((3, x.shape[1]))
    condition = ((x[0, :]-xc)**2 + (x[1, :]-yc)**2) < R*R
    values[2, condition] = 1.0
    values[0, ~condition] = 0
    values[1, ~condition] =  1.0
    values[2, ~condition] = 0
    return values

w = dolfinx.fem.Function(V)
w.interpolate(w_init)

FL  =  ufl.inner(ufl.grad(w), ufl.grad(w))
dFdw =ufl.derivative(FL * ufl.dx, w)
dFdwdw = ufl.derivative(dFdw, w)

A = dolfinx.fem.petsc.assemble_matrix(dolfinx.fem.form(dFdwdw))
A.assemble()
vectors= PETSc.Vec().createSeq(V.dofmap.index_map.size_local)
# vectors = A.createVecs()
A.mult(w.vector, vectors)
File PETSc/Mat.pyx:1311, in petsc4py.PETSc.Mat.mult()

Error: error code 73
[0] MatMult() at ...
[0] Object is in wrong state
[0] Not for unassembled matrix

thanks

Call A.assemble() after assembling A.

Hello Jorgen. I forgot to add that line when I was copying the code to the forum. Calling A.assemble() after assembly A does not solve the problem. I have edited the original question and added the A.assemble() .

it partially solves your problem, but, then you get another error:

Traceback (most recent call last):
  File "/root/shared/mwe123.py", line 35, in <module>
    A.mult(w.vector, vectors)
  File "PETSc/Mat.pyx", line 1311, in petsc4py.PETSc.Mat.mult
petsc4py.PETSc.Error: error code 60
[0] MatMult() at /usr/local/petsc/src/mat/interface/matrix.c:2365
[0] Nonconforming object sizes
[0] Mat mat,Vec y: global dim 648 216

which is due to how you create your vector:

Using:

vector =  A.createVecRight()

A.mult(w.vector, vector)

resolves the issue.