老版本采用ufl.CellVolume函数,官方原贴:Form compiler has to compute `CellVolume`
0.7.3版本采用ufl.geometry.ReferenceCellVolume函数,并且需要进行Jacobian坐标变换
官方原贴:Volume_entities and inradius function from cpp.mesh
代码如下
from dolfinx import mesh, fem,
from mpi4py import MPI
import ufl
import numpy as np
# We create a 1*2*3 box
# We then create the mesh, which will consist of hexahedral elements, along with the function space.
domain = mesh.create_box(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([1, 2, 3])],
[20, 20, 30], cell_type=mesh.CellType.hexahedron)
Ve=fem.FunctionSpace(domain,("DG",0))
elmVol_ufl=ufl.geometry.ReferenceCellVolume(domain)
detJ = abs(ufl.JacobianDeterminant(domain) * elmVol_ufl) # 坐标转换
elmVol_expr=fem.Expression(detJ,Ve.element.interpolation_points())
elmVol=fem.Function(Ve)
elmVol.interpolate(elmVol_expr)
print('Total Volume:{}'.format(elmVol.x.array.sum()))