How do you check that a MeshRefinementFunction
is doing what you want in a 3D mesh where you only see the boundary mesh?
I started by making a block with a hole as follows
Needs["NDSolve`FEM`"]
Lx = 0.1; Ly = 0.05;
h = 0.04;
r = 0.01;
ir = ImplicitRegion[x^2 + y^2 >= r^2, {x, y, z}];
mesh = ToElementMesh[ir, {{-Lx, Lx}, {-Ly, Ly}, {0, h}}];
mesh["Wireframe"]
I am interested in the region around the top of the hole so I decided to refine this using the following MeshRefinmentFunction
mrf = Compile[{{c, _Real, 2}, {a, _Real, 0}},
Block[{d, com, c0 = {0, 0, h}},
com = Total[c]/3;
d = Norm[com - c0];
If[d < 3 r && a > 10^-9, True, False]
]
];
ir = ImplicitRegion[x^2 + y^2 >= r^2, {x, y, z}];
mesh = ToElementMesh[ir, {{-Lx, Lx}, {-Ly, Ly}, {0, h}},
MeshRefinementFunction -> mrf];
mesh["Wireframe"]
The problem here is that you only see the boundary mesh and the top of the hole looks like it does not have a refined mesh. I thus looked a bit closer with this
Show[mesh["Wireframe"], PlotRange -> {{-Lx/4, Lx/4}, {0, Ly/2}, All}]
This does look like the top of the inside of the hole is not refined. However, this could be just some illusion so my attempt to check further was to get the coordinates and plot those in slices going from the bottom of the hole to the top
coords = mesh["Coordinates"];
inc = h/20;
Table[Graphics3D[{Point[coords]},
PlotRange -> {{-Lx/4, Lx/4}, {-Ly/2, Ly/2}, {z, z + inc}},
ViewPoint -> {0, 0, 1}], {z, 0, h - inc, inc}]
I noted that I have lots of black points where the refinement starts but as I work up the density of black points reduces around the edges of the hole. This seems to confirm that the mesh is not refined near the top of the hole. However, this could be an illusion. So the first question is how best to check to see if the mesh has been properly refined?
I am also aware that my MeshRefinementFunction
could be wrong or that for some reason the refinement process can't see the top of the hole. So the second question is: Is my MeshRefinementFunction
wrong?
Thanks for any help.
Edit 1
A suggestion by Henrik Schumacher is to plot the MeshElements directly. Using this we can again plot in slices (fewer this time).
inc = h/20;
Table[Show[
mesh["Wireframe"["MeshElement" -> "MeshElements",
"MeshElementStyle" -> EdgeForm[{Black, Thin, Opacity[0.3]}]]],
PlotRange -> {{-Lx/4, Lx/4}, {-Ly/2, Ly/2}, {z, z + inc}},
ViewPoint -> {0, 0, 1}], {z, 0, h - inc, inc}]
This again suggests that the refinement is not working near the top of the hole.