Vectorexpression / shapes do not match

Hello,

I have an expression

f = Expression("cos(pi * x[0]) *cos(pi * x[1]) *cos(pi * x[2])", degree=2)

and i want to write grad_f as a vector

\nabla f = \left(\begin{array}{c} -\pi sin(\pi x)cos(\pi y)cos(\pi z) \\ -\pi cos(\pi x)sin(\pi y)cos(\pi z) \\ -\pi cos(\pi x)cos(\pi y)sin(\pi z) \end{array}\right)

How can I do it? I tried it with

nabla_f = Expression("-1*pi*cos(pi * x[0]) *sin(pi * x[1]) *sin(pi * x[2])", "-1*pi*sin(pi * x[0]) *cos(pi * x[1]) *sin(pi * x[2])", "-1*pi*sin(pi * x[0]) *sin(pi * x[1]) *cos(pi * x[2])", degree =2)

But then I’ve got an shape error on the left side L in my variational formulation.

from dolfin import *
n = 20
mesh = UnitCubeMesh(n, n, n)
V = FiniteElement("N1curl", mesh.ufl_cell(), 1)
S = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
Z = MixedElement([V, S])
W = FunctionSpace(mesh, Z)

(vh, qh) = TestFunctions(W)
(yh, ph) = TrialFunctions(W)

f_expr = Expression("sin(pi * x[0]) *sin(pi * x[1]) *sin(pi * x[2])", degree=2)
nabla_f= Expression("-1*pi*cos(pi * x[0]) *sin(pi * x[1]) *sin(pi * x[2])", "-1*pi*sin(pi * x[0]) *cos(pi * x[1]) *sin(pi * x[2])", "-1*pi*sin(pi * x[0]) *sin(pi * x[1]) *cos(pi * x[2])", degree =2)

g = nabla_f

L = inner(g, vh) * dx

Shapes do not match: <Coefficient id=139771234478592> and <ListTensor id=139771233073984>.

How can I fix it? I have an solution where nabla_f is calculated but this takes much memory and now I want to test it whit a direct given nabla_f.

Thank you and best regards,

noya

Hi @Noya,

You should put the components of \nabla f inside of an array, i.e.

nabla_f= Expression(["-1*pi*cos(pi * x[0]) *sin(pi * x[1]) *sin(pi * x[2])",
                     "-1*pi*sin(pi * x[0]) *cos(pi * x[1]) *sin(pi * x[2])", 
                     "-1*pi*sin(pi * x[0]) *sin(pi * x[1]) *cos(pi * x[2])"], degree =2)
2 Likes