Shape mismatch error

Hello,
I am trying to run the code below but receive a shape mismatch error when trying to state the variational problem. I am very confused by this. Any help would be greatly appreciated.

import numpy as np
import matplotlib

import matplotlib.pyplot as plt

from dolfinx import mesh, fem, io, plot, la
import pyvista
from dolfinx import plot
import ufl
import numpy as np
from dolfinx import *
import dolfinx
import numpy, sys

from mpi4py import MPI
from petsc4py import PETSc
from ufl import (VectorElement, FiniteElement,
SpatialCoordinate, TrialFunction, TestFunction,
as_vector, cos, sin, inner, div, grad, dx, pi)

from dolfinx import mesh, fem, io, nls, log
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver

##cretae a rectangular mesh with quadrilateral elements
##note: here theta=x

from mpi4py import MPI
length, height = 10, 3
Nx, Ny = 80, 60
extent = [[0., 0.], [length, height]]
domain = mesh.create_rectangle(
MPI.COMM_WORLD, extent, [Nx, Ny], mesh.CellType.quadrilateral)

##define the non-linear coeff
omega=(np.pi/2)
##define the non-linear coefficient
def vel(x):
return omega + sin(x)

##setting up the variational problem
from ufl import (TestFunction, SpatialCoordinate, TrialFunction,
as_vector, dx, grad, inner, system, equation)
V = fem.FunctionSpace(domain, (“Lagrange”, 1))
u = TrialFunction(V) ##time-dep c at n+1
v = TestFunction(V)
un = fem.Function(V) ##time dep C at n
#f = fem.Constant(domain, 0.0)
D = fem.Constant(domain, 1.0) ##difusion constant
dt = fem.Constant(domain, 0.05)

##define the variational problem
L= vel(u)inner(ufl.grad(u),v)dx + vel(u)vdx + Dinner(grad(u),grad(v)) - Dgrad(u)vdx
a= v*(u-un)dtdx - v*dx
#variational problem stated as a=L
(a, L) = system(ufl.equation(a==L))

Please write the code between ``` and post the full error.

Seems like you cannot take inner between grad(u) and v since they are of different shape, there must be a problem in your variational formulation

Hello, Thanks for your reply! Below is the re-posted code between ‘’'.
Also, the error is: Traceback (most recent call last):

Cell In[5], line 1
L= vel(u)*inner(ufl.grad(u),v)dx + vel(u)vdx + Dinner(grad(u),grad(v))dx - Dgrad(u)vdx

File ~/anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/operators.py:167 in inner
return Inner(a, b)

File ~/anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/tensoralgebra.py:162 in new
raise ValueError(f"Shapes do not match: {ufl_err_str(a)} and {ufl_err_str(b)}")

ValueError: Shapes do not match: and

Indeed the problem is that grad(u) has shape (2,) and v has shape (). They are however defined on the same mesh space. So I do not understand why they have different shapes. I’m new to FEM and FEniCsX, so apologies if my question is naive.
Thanks in advance for replies and kindness!

Code:
‘’'import numpy as np
import matplotlib

import matplotlib.pyplot as plt

from dolfinx import mesh, fem, io, plot, la
import pyvista
from dolfinx import plot
import ufl
import numpy as np
from dolfinx import *
import dolfinx
import numpy, sys

from mpi4py import MPI
from petsc4py import PETSc
from ufl import (VectorElement, FiniteElement,
SpatialCoordinate, TrialFunction, TestFunction,
as_vector, cos, sin, inner, div, grad, dx, pi)

from dolfinx import mesh, fem, io, nls, log
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.nls.petsc import NewtonSolver

##cretae a rectangular mesh with quadrilateral elements
##note: here theta=x

from mpi4py import MPI
length, height = 10, 3
Nx, Ny = 80, 60
extent = [[0., 0.], [length, height]]
domain = mesh.create_rectangle(
MPI.COMM_WORLD, extent, [Nx, Ny], mesh.CellType.quadrilateral)

##define the non-linear coeff
omega=(np.pi/2)
##define the non-linear coefficient
def vel(x):
return omega + sin(x)

##setting up the variational problem
from ufl import (TestFunction, SpatialCoordinate, TrialFunction,
as_vector, dx, grad, inner, system, equation)
V = fem.FunctionSpace(domain, (“Lagrange”, 1))
u = TrialFunction(V) ##time-dep c at n+1
v = TestFunction(V)
un = fem.Function(V) ##time dep C at n
#f = fem.Constant(domain, 0.0)
D = fem.Constant(domain, 1.0) ##difusion constant
dt = fem.Constant(domain, 0.05)

##define the variational problem
#L= vel(u)*inner(ufl.grad(u),v)dx + vel(u)vdx + Dinner(grad(u),grad(v))dx - Dgrad(u)vdx

L= vel(u)ufl.grad(uv)dx + vel(u)vdx + Dinner(grad(u),grad(v))dx - Dgrad(u)vdx
a= v*(u-un)dtdx - v*dx
#variational problem stated as a=L
(a, L) = system(ufl.equation(a==L))‘’’

Hello,
Thanks for your reply! Even if I change L to be:
L= vel(u)vdx + D*inner(grad(u),grad(v))dx - Dgrad(u)vdx
where vel is actually sin(u), I obtain the following error:

Error message:
L= vel(u)vdx + D*inner(grad(u),grad(v))dx - Dgrad(u)vdx
Traceback (most recent call last):

Cell In[6], line 1
L= vel(u)vdx + D*inner(grad(u),grad(v))dx - Dgrad(u)vdx

File ~/anaconda3/envs/fenicsx-env/lib/python3.12/site-packages/ufl/measure.py:361 in rmul
raise ValueError(

ValueError: Can only integrate scalar expressions. The integrand is a tensor expression with value shape (2,) and free indices with labels ().

I don’t understand the error because as far as I am concerned, Finite Element Method is based upon the use of basis functions to solve the PDEs in a small spatial region (finite elements). These basis functions “phi” used to approximate the solution of the PDE are often linear polynomials. But essentially, I believed that there was a basis function defined at each mesh element, and when you combine all the basis functions for all the elements…shouldn’t you end up with a vector? I am very new to FEM and FEniCsX, so my apologies again if the answer to this is obvious. Thanks in advance for any replies and kindness!

Please post your code with triple backquotes ``` not quotes to have proper code formatting. Your error is not related to finite element basis functions but to the mathematical shape of objects incovled in the form. For instance, D*grad(u)*v*dx is not a valid expression since D is a scalar, grad(u) a vector and v a scalar.

1 Like

Hi! Thanks so much for your reply! I will definitely get the quotes correct next time. My apologies for that. This makes more sense to me now. Thanks so much for your reply! :slight_smile:

You can edit your existing post for it to be more readable. Also, if you resolved your issue, we would appreciate if you posted the solution, or tag the relevant post as a solution if it is already posted.

1 Like