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

Integral over a certain area

0 votes

I've already had the solution u over a unitsquare domain. Now I want to compute the integral over a certain subdomain, say x[0] and x[1] in range(0,0.25), how do I define the subdomain and then compute the integrals over it?

asked Sep 5, 2016 by zzzyui FEniCS Novice (140 points)

1 Answer

+1 vote

By constructing a custom integration element, you can then integrate over that subdomain. Hopefully this helps.

from dolfin import *

mesh = UnitSquareMesh(256, 256)
V = FunctionSpace(mesh, 'CG', 2)
u = interpolate(Expression('x[0]*x[0] + x[1]*x[1]'), V)

cf = CellFunction('size_t', mesh, 0)
region = AutoSubDomain(lambda x, on: x[0] <= 0.25 and x[1] <= 0.25)
region.mark(cf, 1)
dx_sub = Measure('dx', subdomain_data=cf)

print assemble(u*dx_sub(1))
answered Sep 6, 2016 by nate FEniCS Expert (17,050 points)
...