Area of a mesh triangle

Hello,
I’m new to Fenics and there’s something that seems elementary to do to me but I didn’t manage to find anything on google or this forum:
Being given a mesh, how do I get the volume of the ith triangle of the mesh ? I’ve tried this hack by defining P0 characteristic functions of each triangle of the mesh and integrating it:

VD = FunctionSpace(mesh, 'DG', 0)

characteristic = [ Function(VD) for i in mesh.cells() ]
for i,chi in enumerate(characteristic):
    tmp = np.zeros((nCells))
    tmp[i] = 1
    chi.vector().set_local(tmp)

tArea = []
for i,chi in enumerate(characteristic):
    tAreai = chi*dx
    tAreai = assemble(tAreai).array()
    tArea.append(tArea)

however i get the error " AttributeError: ‘float’ object has no attribute ‘array’ "
But when I replace the wrong line by
tAreai = assemble(tAreai)
the output isn’t float but someting strange (something like [[…],[…],…] )

Also I find it weird for fenics to don’t have direct access to triangle area. Could you help me ?

1 Like

You can take a look at the Cell object : https://fenicsproject.org/docs/dolfin/2017.1.0/python/programmers-reference/cpp/mesh/Cell.html
It has a method to compute the generalized volume of cell (triangle in 2D) from a mesh and an index, see the snippet below.

from fenics import *

mesh = UnitSquareMesh(2,2)

cell = Cell(mesh, 0)
print('Area:'cell.volume())
1 Like

Thank you very much !