Tangential gradient operator error: invalid ranks 1 and 1 in product

Hello
I am trying to implement a fluid flow problem with boundary forces. Weak form of the boundary force term would be dot( \mathbf P, \bar { \nabla \mathbf v})*ds , where \mathbf P = I -\mathbf n * \mathbf n is the tangential projection operator, \bar \nabla()= \nabla() -\mathbf n*(\mathbf n\cdot \nabla()) is the tangential gradient operator, \mathbf n is the unit normal vector and \mathbf v is the test function. However this term is causing an error:

UFLException: Invalid ranks 1 and 1 in product.

Can anyone kindly help me with this issue?
Thanks.

Following is the minimum working example that generates the error:

from dolfin import *
from mshr import *

define geometry and mesh

b=0.3 # parameter for ellipse
geometry = Ellipse(Point(0.0, 0.0), 1, b)
mesh = generate_mesh(geometry, 16)
n=FacetNormal(mesh)

Define function spaces

P2 = VectorElement(‘CG’, triangle, 5)
P1 = FiniteElement(‘CG’, triangle, 3)
TH = MixedElement([P2, P1])
W = FunctionSpace(mesh, TH)

Specify Boundary Conditions

#boundary = ‘on_boundary’
#bcu = DirichletBC(W.sub(0), Expression( (‘1’,‘1’) , degree=2), boundary) # random bc
#bc = [bcu]

Define trial and test functions

(u, p) = TrialFunctions(W)
(v, q) = TestFunctions(W)
def epsilon(u):
return sym(nabla_grad(u))

tangential gradient operator

def grad_t(v):
return grad(v) - dot(grad(v),n)*n

tangential projection operator

def pt(n):
return Identity(len(n)) -n*n
#weak formulation
a1 = inner( epsilon(u), nabla_grad(v)) * dx + div(u) * q * dx +p * div(v) * dx- dot(pt(n),grad(v))*ds

You need to use the outer projection for the tangential projection:

def tangential_proj(u, n):
    return (ufl.Identity(u.ufl_shape[0]) - ufl.outer(n, n)) * u
def sym_grad(u):
    return ufl.sym(ufl.grad(u))
def T(u, p):
    return 2 * mu * sym_grad(u) - p * ufl.Identity(u.ufl_shape[0])
g_tau = tangential_proj(T(u_ex, p0)*n, n)

Thanks for the help, Dokken.