ERROR:UFL:Shapes do not match, when use inner product

Hi,

I have an issue when I use the inner operator. I have looked the documentation but I am not sure why the error happens. I would appreciate some help.

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)
v = ufl.TestFunction(V)

ufl.inner(ufl.grad(v),w )
ERROR:UFL:Shapes do not match: <Grad id=140437333057152> and <Coefficient id=140437333048000>.

thank

If v is in a VectorFunctionSpace,
grad(v) is a tensor (3,3), which cannot be part of an inner product with a test function from V, which is a vector of shape (3,)

Hi dokken, thank you for the answer.

So having performed an inner product is not correct, given grad(v) is a tensor and v is a vector.

I want to solve the equation for the scalar field \rho,

\int_{\Omega} \nabla v \cdot \nabla \rho dx = \int_{\Omega} \nabla v \cdot w dx

According to your answer, I think that the right-side of the equation must be,

L = ufl.inner(ufl.grad(v[0]), w)*ufl.dx + ufl.inner(ufl.grad(v[1]), w)*ufl.dx + ufl.inner(ufl. grad(v[2]), w)*ufl.dx

and the left side,

a = ufl.inner(ufl.grad(v), ufl.grad(rho))*ufl.dx

This is correct?

Please state the strong form of your problem, as this weak form does not make sense

Hi dokken, the strong form is:

\Delta \rho = - \nabla\cdot \vec{\omega}

with Neuman’s conditions at the boundary (\partial \rho/\partial n = \hat{n}\cdot \vec{\omega} in \Omega) and \vec{\omega} known vector. Then, the weak form is

\int_{\Omega} \nabla \rho \cdot \nabla v dx = \int \vec{\omega}\cdot \nabla v dx

with Dirichlet boundary conditions in the function space V

Here \rho should be in a scalar space, and the same holds for v.

thank so much. I had not noticed that error. Finally, Finally I think that I have fixed the code:

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])


V1 =  fem.FunctionSpace(mesh, ("CG", 1))
V2 =  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(V2)
w.interpolate(w_init)

rho = ufl.TrialFunction(V1)

a = ufl.inner(ufl.grad(v), ufl.grad(rho)) * ufl.dx

L = ufl.inner(ufl.grad(v),w)*ufl.dx

fem.petsc.LinearProblem(a, L,)