Hi everyone,
I was wondering if in dolfinx exists the possibility of computing the size of the elements of a mesh.
I have found that a characteristic size can be found as in Evaluation of cell sizes - #2 by dokken, however I was looking for something a bit different. Assuming that the mesh is composed by triangles, is it possible to find the area of each triangle?
Yes, you’re just computing \int_{\kappa} 1 \; \mathrm{d}\vec{x}, for each element \kappa in the mesh. This is particularly useful for high order elements, for example.
import gmsh
from mpi4py import MPI
import meshio
from dolfinx import mesh, io, fem
from ufl import TrialFunction, TestFunction, dx
import meshio
import numpy as np
gmsh.initialize()
current_mesh = "mesh_test_1"
gmsh.open(current_mesh + ".msh")
model = gmsh.model
model.setCurrent(current_mesh)
print('Model ' + model.getCurrent() + ' (' +str(model.getDimension()) + 'D)')
gmshiomsh, cell_markers, facet_markers = io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0, gdim=2) # Extract the data
def create_mesh(mesh, cell_type, prune_z=False): # These are all functions for a meshio mesh
cells = mesh.get_cells_type(cell_type)
cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
print(f'There are {len(cell_data)} elements of type {cell_type}')
points = mesh.points[:,:2] if prune_z else mesh.points
out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={"name_to_read":[cell_data]})
return out_mesh
# Read in mesh
msh = meshio.read("mesh_test_1.msh")
# Create and save one file for the mesh, and one file for the facets
triangle_mesh = create_mesh(msh, "triangle", prune_z=True)
line_mesh = create_mesh(msh, "line", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)
meshio.write("mt.xdmf", line_mesh)
with io.XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "r") as xdmf:
mymesh = xdmf.read_mesh(name="Grid")
ct = xdmf.read_meshtags(mymesh, name="Grid")
mymesh.topology.create_connectivity(mymesh.topology.dim, mymesh.topology.dim-1)
with io.XDMFFile(MPI.COMM_WORLD, "mt.xdmf", "r") as xdmf:
ft = xdmf.read_meshtags(mymesh, name="Grid")
V = fem.FunctionSpace(mymesh, ("CG", 1))
v = TestFunction(V)
cell_area_form = fem.form(v*dx)
cell_area = fem.assemble_vector(cell_area_form)
print(f'Cell area array has length {len(cell_area.array)}')
You are using a CG 1 (or better known as a first order lagrange space). This corresponds to each vertex of your mesh, not each cell. For having a value per cell, you need to change the function space to ("DG", 0).