5
$\begingroup$

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}]

enter image description here

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

Is there a simple solution?

Thanks!

addendum

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!

$\endgroup$

2 Answers 2

5
$\begingroup$

Note you must first load << NDSolve`FEM`. 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}]

enter image description here

addendum

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}]
$\endgroup$
4
  • $\begingroup$ Thanks for your helpful answer. I forgot to copy Needs["NDSolveFEM"]. Is it possible to keep the "meshnumbering" of mesh and mesh1 the same? $\endgroup$ Aug 18, 2022 at 15:40
  • $\begingroup$ 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. $\endgroup$ Aug 18, 2022 at 16:12
  • $\begingroup$ Please have a look on my modified question $\endgroup$ Aug 19, 2022 at 6:50
  • $\begingroup$ I added your example to my answer. $\endgroup$ Aug 19, 2022 at 9:09
4
$\begingroup$

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["NDSolve`FEM`"]
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"]

enter image description here

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]]

enter image description here

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}]

enter image description here

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.