Trying to resolve an error with expressions of different shapes

Hi,
I’m pretty new to Fenics and have run into this error for the first time.

Exception has occurred: UFLException
Can’t add expressions with different shapes.
File “/home/ucarsten/Desktop/PG_0_1_5_G.py”, line 126, in
rhoJ= ((2*rho_2 )/ epsi)- dot(E,grad(grad(rho_2)))

As this seems to be an error that should be quiet common I seem to have been lucky till now.

As this is for an university assignment I wont post the full code (besides it being quiet long at the moment). But what I have been trying to do is calculate some related Differential equations over a 2D mesh. The formula I got problems with is the Jacobian of this
aC = (dot((rho_2**2) / epsi, u) - dot(dot(E, grad(rho_2)), u))*dx
which I’m trying to feed into a non-linear solver

epsi=Constant(1)

V = VectorFunctionSpace(mesh, ‘P’, 1)
Q = FunctionSpace(mesh, ‘Lagrange’, 1)

E = Function(V)
E.assign(project(grad(phi), V))
phi being the result of a previous Differential equation over the same mesh

rho_2=TrialFunction(Q)
u=TestFunction(Q)
rho_2=Function(Q)

aC = (dot((rho_2**2) / epsi, u) - dot(dot(E, grad(rho_2)), u))*dx

rhoJ=((2*rho_2 )/ epsi)- dot(E,grad(grad(rho_2)))

rhoproblem = NonlinearVariationalProblem(aC, rho_2, bcd,rhoJ)

rhosolver = NonlinearVariationalSolver(rhoproblem)

So since the normal solve function can work with aC just not calculate it the problem itself seems to be a dimension mismatch in the jacobian rhoJ and the error underlines the / or the epsi depending if there is a space or between them or not. If this were Matlab I would take a look of the dimensions of the variables and have a play around with that but in Fenics if I print them I get these

(2*rho_2 )/epsi =
2 * f_62 / f_0
dot(E,grad(grad(rho_2))) =
(f_43) . (grad(grad(f_62)))
and to be honest that doesn’t tell me much

I think the problem is either that it isn’t sure how to divide the Trialfunction rho_2 through a Constant or it cant subtract a function x function from a function
but after searching the documentation and googling a lot I don’t think I have gotten any closer to the solution…

any ideas?

It’s difficult to decipher how you derived the Jacobian but rhoJ doesn’t look dimensionally correct. Consider this post.

With the following code:

from fenics import *
mesh  = UnitSquareMesh(20, 20)
epsi = Constant(20)

V = VectorFunctionSpace(mesh, 'P', 1)
Q = FunctionSpace(mesh, 'Lagrange', 1)

E = Function(V)

rho_2 = TrialFunction(Q)
u = TestFunction(Q)
rho_2 = Function(Q)

b = ((2*rho_2 )/ epsi)*dx
b_compiled = fem.form.Form(b)
print(b_compiled.rank())

a = dot(E,grad(grad(rho_2)))*dx
a_compiled = fem.form.Form(a)
print(a_compiled.rank())

The first integrand returns a rank of 0 while the second:

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

The grad(grad(rho_2)) generates a tensor of rank 2 which can’t be summed with your other term of rank 0.

1 Like

Ok so I need to find a way adjust the rank of the first part to fit with the second.
Thanks for pointing out where the actual error is.

As for how I got to that Jacobian I simply derived aC for rho_2 by hand since that is the only variable in that formula.