This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

How to resolve "shape mismatch" error

0 votes

Hello
I have a 2 dimensional problem with this variational form:

a = inner(u,v)*dens*dx + dt*dt*inner(C*grad(u), grad(v))*dx+dt*dt*inner(C*nabla_grad(u), grad(v))*dx

C is a 6*6 matrix like:

C= Constant(((249,93,88,0,0,0),
(93,249,88,0,0,0),
(88,88,284,0,0,0),
(0,0,0,70,0,0),
(0,0,0,0,70,0),
(0,0,0,0,0,78)))

Other required details are:

dens=2700
domain = mshr.Rectangle(Point(0,0), Point(6,3))
mesh = mshr.generate_mesh(domain, 30)
V=VectorFunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)

When I run my code I get the "shape mismatch" error in the line where I have defined "a" (variational form).
The thing is when I change the shape of matrix C from 66 to 22 like:

C= Constant(((249,0),
(0,249)))

then my code works but I have to use the 6*6 matrix. Does anybody know how I can fix this error? Thanks in advance for your help.

asked Feb 10, 2016 by jafar FEniCS Novice (670 points)

1 Answer

0 votes

The major trick to apply in this type of situation is to break your form into multiple lines:

a = inner(u,v)*dens*dx
a+= dt*dt*inner(C*grad(u), grad(v))*dx
a+= dt*dt*inner(C*nabla_grad(u), grad(v))*dx

Now FEniCS will tell you which part of the form is the problem and this will make it easier to solve

answered Feb 10, 2016 by Tormod Landet FEniCS User (5,130 points)

Looking more at your error, how to you think FEniCS should perform the multiplication

C*grad(u)

If the mesh is 2D then grad(u) is a 2x2 matrix. Which rules do you propose to use to multiply your 6x6 matrix with a 2x2 matrix? When you have decided on how this can be done for your problem in a mathematically sound way then I am sure you can get FEniCS to assemble a correct form without any errors

...