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

do numerical integration

+1 vote

Hi all,

I am very new to FEniCS, I have a quick question. How to do numerical integration of a function in FEniCS?

Thanks.

asked Jul 15, 2013 by gxwdaq FEniCS Novice (160 points)

1 Answer

+7 votes
 
Best answer

It depends what is your function. For example

from dolfin import *
mesh = UnitSquareMesh(42, 42)

c = Constant(42.0)
print assemble(c*ds, mesh=mesh)

f = Expression("1.0 + x[0]*x[1]", degree=2)
print assemble(f*dx, mesh=mesh)

V = FunctionSpace(mesh, "Lagrange", 2)
u = project(f, V)
print assemble(u*dx)

Necessary quadrature degree is automatically estimated by algorithms in UFL (altough they are not flawless) and corresponding Gauss rule is used. Note explicit setting of polynomial degree of compiled expression f - it is not recognized in any way.

answered Jul 15, 2013 by Jan Blechta FEniCS Expert (51,420 points)
selected Jul 19, 2013 by Jan Blechta

This doesn't work anymore due to some changes in ufl.

UFLException: An Integral without a Domain is now illegal.

what is the domain here?

from dolfin import *
mesh = UnitSquareMesh(42, 42)

c = Constant(42.0)
print assemble(c*ds(mesh))

f = Expression("1.0 + x[0]*x[1]", degree=2)
print assemble(f*dx(mesh))

V = FunctionSpace(mesh, "Lagrange", 2)
u = project(f, V)
print assemble(u*dx)
...