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

integration per cell

+3 votes

I'd like to compute the integral of an Expression per cell, i.e.,
$$
E_i = \int_{T_i} e.
$$

I see that in the basic AMR example demo/undocumented/adaptive-poisson/python/demo_adaptive-poisson.py, manual integration is done by evaluating the right-hand sides f at all cell.midpoint()s.

Is there a "proper" way to handle things (i.e., accurate for higher-than-linear elements)?

asked Aug 1, 2013 by nschloe FEniCS User (7,120 points)

1 Answer

+5 votes
 
Best answer

Yes, just multiply the Expression with a piecewise constant TestFunction and assemble it into a Vector. Something like this:

e = <some Expression>
DG = FunctionSpace(mesh, "DG", 0)
v = TestFunction(DG)
L = v*e*dx
b = assemble(L)
E = b.array()

or in one line just this:

E = assemble(TestFunction(FunctionSpace(mesh, "DG", 0))*e*dx).array()
answered Aug 1, 2013 by logg FEniCS Expert (11,790 points)
selected Aug 2, 2013 by nschloe
...