In [8]:
from dolfin import *

mesh = BoxMesh(0, 0, 0, 1, 1, 20, 10, 10, 10)
ff   = FacetFunction("size_t", mesh, 0)

# calculate boundaries :
for f in facets(mesh):
  n   = f.normal()
  tol = 1e-3 
  # surface :
  if n.z() >= tol and f.exterior():
    ff[f] = 1
  # base :
  elif n.z() <= -tol and f.exterior():
    ff[f] = 2

# define function space :
Q   = FunctionSpace(mesh, "CG", 2)
u = grad(Function(Q))
def vert_integrate(u, ff, Q):
  phi    = TestFunction(Q)
  v      = TrialFunction(Q)
  bc     = DirichletBC(Q, 0, ff, 2)
  a      = v.dx(2) * phi * dx
  L      = u * phi * dx
  v      = Function(Q)
  solve(a == L, v, bc)
  return v
one = Constant(1)
v = vert_integrate(u, ff, Q)


DEBUG:UFL:No integrals left after transformation, returning empty form.
DEBUG:FFC:Reusing form from cache.
Trying to integrate expression of rank 1 with free indices ().
ERROR:UFL:Trying to integrate expression of rank 1 with free indices ().
---------------------------------------------------------------------------
UFLException                              Traceback (most recent call last)
<ipython-input-8-2aee7c487e15> in <module>()
     28   return v
     29 one = Constant(1)
---> 30 v = vert_integrate(u, ff, Q)

<ipython-input-8-2aee7c487e15> in vert_integrate(u, ff, Q)
     23   bc     = DirichletBC(Q, 0, ff, 2)
     24   a      = v * phi * dx
---> 25   L      = u * phi * dx
     26   v      = Function(Q)
     27   solve(a == L, v, bc)

/home/mwathen/Work/FEniCS/lib/python2.7/site-packages/ufl/integral.pyc in __rmul__(self, integrand)
    267         if not is_true_ufl_scalar(integrand):
    268             error("Trying to integrate expression of rank %d with free indices %r." \
--> 269                   % (integrand.rank(), integrand.free_indices()))
    270 
    271         # Is the measure in a state where multiplication is not allowed?

/home/mwathen/Work/FEniCS/lib/python2.7/site-packages/ufl/log.pyc in error(self, *message)
    152         "Write error message and raise an exception."
    153         self._log.error(*message)
--> 154         raise self._exception_type(self._format_raw(*message))
    155 
    156     def begin(self, *message):

UFLException: Trying to integrate expression of rank 1 with free indices ().

In [6]:
v.vector().array()


Out[6]:
array([ 20.,  18.,  20., ...,   0.,   1.,   0.])