# Select part of an ElementMesh

modified

For an, examplary simple, ElementMesh

p = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {1/2, 1/2}};
mesh = ToElementMesh["Coordinates" -> p,"MeshElements" ->
{TriangleElement[{{1, 2, 5  }, {2, 3, 5  }, {5,3, 4 }, { 1, 5,4 }   }]}];
Show[mesh["Wireframe"], Axes -> True, AxesLabel -> {x, y}]


I would like to select part of the mesh {x,y}which fullfills condition y>=x

Is there a simple solution?

Thanks!

Based on @DanielHuber's helpful answer I tried to preserve the meshnumbering

dreiecke = mesh["MeshElements"][[1]][[1]]
dreiecke1 = Select[dreiecke,Apply[And,RegionMember[ImplicitRegion[r >= z, {z, r}], p[[#]]]] &]
ToElementMesh["Coordinates" -> p,"MeshElements" ->{TriangleElement[dreiecke1]}]


but Mathematica gives an error message "ToElementMesh::fememins: The mesh elements are not valid. A set of valid mesh element incidents needs to be positive integers and be able to form a complete sequence starting from 1 to the largest incident present. There are missing incidents; a complete sequence cannot be formed."

Obviously because there are unused meshpoints in "Coordinates".

Any idea how to solve this problem? Thanks!

Note you must first load << NDSolveFEM. Otherwise your example does not evaluate.

You may simple select the coordinates that fulfill the required condition and then create a new Mesh:

mesh1 = ToElementMesh@Select[mesh["Coordinates"], #[[2]] >= #[[1]] &]
Show[mesh1["Wireframe"], Axes -> True, AxesLabel -> {x, y}]


If you want the renumber the points so that all points in the reduced mesh appear before points that are not in the reduced mesh, you must renumber the points as well as the triangles. Here is your example:

dreiecke = mesh["MeshElements"][[1]][[1]]
dreiecke1 =
Select[dreiecke,
Apply[And, RegionMember[ImplicitRegion[r >= z, {z, r}], p[[#]]]] &]

pnew = p[[{1, 5, 3, 4}]];
dreiecke2 = dreiecke1 /. {2 -> tmp, 5 -> 2} /. tmp -> 5;
mesh2 = ToElementMesh["Coordinates" -> pnew,
"MeshElements" -> {TriangleElement[dreiecke2]}]
Show[mesh2["Wireframe"], Axes -> True, AxesLabel -> {x, y}]

• Thanks for your helpful answer. I forgot to copy Needs["NDSolveFEM"]. Is it possible to keep the "meshnumbering" of mesh and mesh1 the same? Aug 18, 2022 at 15:40
• As the number of coordinates in mesh and mesh1 is not the same, the numbering in general is not the same. However, if you arrange the coordinates in such a way, that all coordinates of mesh1 are at the beginning in mesh, then the numbering will not change. Aug 18, 2022 at 16:12
• Please have a look on my modified question Aug 19, 2022 at 6:50
• I added your example to my answer. Aug 19, 2022 at 9:09

As you have noted an ElementMesh can not have an incomplete set of incidents. So that target of your operation can not be an ElementMesh. With that said you can generate a GraphicsComplex.

Consider this mesh:

Needs["NDSolveFEM"]
mesh = ToElementMesh[
"Coordinates" -> {{1.293, 0.228}, {1., 0.}, {0.94, 0.342}, {1.293,
0.}, {1.215, 0.442}, {2., 0.}, {1.879, 0.684}},
"MeshElements" -> {TriangleElement[{{1, 3, 2}, {1, 2, 4}, {1, 4,
6}, {1, 6, 7}, {1, 7, 5}, {1, 5, 3}}, {66, 66, 66, 44, 44,
44}]}];
mesh["Wireframe"]


You can display parts of the mesh according to some criterion; I am using quality but it can be anything else:

pos = Position[mesh["Quality"], _?(# <= 0.9 &)]
(* {{1, 2}, {1, 3}, {1, 5}, {1, 6}} *)


We can visualize our selection:

mesh["Wireframe"[pos]]


But also convert to a GraphicsComplex:

gc = ElementMeshToGraphicsComplex[mesh, pos]


You can do anything you like with this graphics complex:

Graphics[{EdgeForm[Directive[{Thick, Gray}]], FaceForm[Blue], gc}]


This works because the GraphicsComplex has all the coordinates but the list of incidents from the polygons need not be complete.

• Very interesting and helpful answer. I have not yet understood why ToElementMesh needs this restriction, which GraphicsComplex elegant bypasses. Aug 19, 2022 at 9:06
• Some mesh based algorithms work better / can be optimized if I make this assumption. Aug 19, 2022 at 11:24
• Thanks a lot for your support! Aug 19, 2022 at 11:25