# Partial integration

Is there a way in Fenics to compute a partial integral? I need \rho(x)=\int f(x,y)dy, but I get a TypeError: “unsupported operand type(s) for *: ‘Expression’ and ‘Form’”. The problem is that the equation for c only involves the variable x but depends on \rho and not on f. In the following implementation I get the TypeError.

dolfin import *

# Sub domain for Dirichlet boundary condition
class NofluxBoundary(SubDomain):
def inside(self, x, on_boundary):
return bool((x < DOLFIN_EPS or x > (1.0 - DOLFIN_EPS)) \
and on_boundary)

# Sub domain for Periodic boundary conditional
class PeriodicBoundaryC(SubDomain):

# Left boundary is "target domain" G
def inside(self, x, on_boundary):
return bool(x < DOLFIN_EPS and x > -DOLFIN_EPS and on_boundary)

# Map right boundary (H) to left boundary (G)
def map(self, x, y):
y = x - 1.0
y = x

class PeriodicBoundary(SubDomain):
# Left boundary is "target domain" G
def inside(self, x, on_boundary):
return bool(x < DOLFIN_EPS and x > -DOLFIN_EPS and on_boundary)

# Create mesh and finite element
mesh = UnitSquareMesh(32, 32)
pbc = PeriodicBoundaryC()
nofluxb = NofluxBoundary()
pb = PeriodicBoundary()
V = FunctionSpace(mesh, 'CG', 1, constrained_domain=pbc)
Q = FiniteElement("CG", triangle, 1)

domains = MeshFunction("size_t", mesh, mesh.topology().dim())
domains.set_all(0)

boundaries = MeshFunction("size_t", mesh, mesh.topology().dim()-1)
boundaries.set_all(0)
pb.mark(boundaries, 1)
nofluxb.mark(boundaries, 2)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)

W = FunctionSpace(mesh,MixedElement([Q,Q]), constrained_domain=pbc)

#system
u = Function(W)
u.interpolate(Expression(("cos(0.5*pi*x)*cos(0.5*pi*x)", "cos(0.5*pi*x)*cos(0.5*pi*x)"), degree=2))
f, c = split(u)
v, w = TestFunction(W)

r = Expression('x',degree=2)
cospsi = Expression('cos(2*pi*x)',degree=2)
sinpsi = Expression('sin(2*pi*x)',degree=2)

#system

solve(B == 0, f)


Hi, thanks for your answer. I’m new to Fenics so your example was unfortunately not very enlightening. Can you maybe explain why “f*dx(1)” does not work?

Ok I wrote an UserExpression for rho as follows. It works.

class f_x(UserExpression):
def __init__(self, x):
UserExpression.__init__(self)
self.x = x
def eval(self, value, y):
value = f((self.x,y))
def value_shape(self):
return ()

class Rho(UserExpression):
def eval(self, value, x):
fx = f_x(x)
value = assemble(fx*dx)
def value_shape(self):
return ()

rho = Rho()


But now the solve function is stuck on “Solving nonlinear variational problem.” for any time I’ve let the program run. My thinking is that this implementation is very slow. Do you think there’s any possible quicker implementation?