# Unexpected behavior of ToBoundaryMesh and ToElementMesh

This question is about functions ToBoundaryMesh and ToElementMesh. I use both functions successively because I need information about "Coordinates" and "MeshElements" as an input for function SMTAddMesh in AceFEM.

  << NDSolveFEM

XIO = {{0., 0., 0.}, {2.5, 0., 0.}, {2.5, 2.5, 0.}, {0.,
2.5, 0.}, {0., 0., 2.5}, {2.5, 0., 2.5}, {2.5, 2.5,
2.5}, {0., 2.5, 2.5}};
disksdata = {{{1.25, 1.25, 1.25}, 0.9069579196494525}};

{xmin, xmax, ymin, ymax, zmin,
zmax} = {Min[XIO[[All, 1]]], Max[XIO[[All, 1]]],
Min[XIO[[All, 2]]], Max[XIO[[All, 2]]], Min[XIO[[All, 3]]],
Max[XIO[[All, 3]]]} // N;

disks = And @@
Map[(x - #[[1, 1]])^2 + (y - #[[1, 2]])^2 + (z - #[[1,
3]])^2 >= #[[2]]^2 &, disksdata];

n1 = Cross[XIO[[3]] - XIO[[4]], XIO[[1]] - XIO[[4]]];
n2 = Cross[XIO[[2]] - XIO[[1]], XIO[[5]] - XIO[[1]]];
n3 = Cross[XIO[[3]] - XIO[[2]], XIO[[6]] - XIO[[2]]];
n4 = Cross[XIO[[4]] - XIO[[3]], XIO[[7]] - XIO[[3]]];
n5 = Cross[XIO[[1]] - XIO[[4]], XIO[[8]] - XIO[[4]]];
n6 = Cross[XIO[[6]] - XIO[[5]], XIO[[8]] - XIO[[5]]];

regcond =
disks && n1.({x, y, z} - XIO[[4]]) <=  0 &&
n2.({x, y, z} - XIO[[1]]) <=  0 &&
n3.({x, y, z} - XIO[[2]]) <=  0 &&
n4.({x, y, z} - XIO[[3]]) <=  0 &&
n5.({x, y, z} - XIO[[4]]) <=  0 && n6.({x, y, z} - XIO[[5]]) <=  0;

reg = ImplicitRegion[regcond, {x, y, z}];

bmesh = ToBoundaryMesh[reg, "MeshElementType" -> TetrahedronElement,
"MeshOrder" -> 1,
MaxCellMeasure -> {"Length" ->
Min[xmax - xmin, ymax - ymin, zmax - zmin]/2}]
bmesh["MeshElements"]
bmesh["Coordinates"] // Length


ToBoundaryMesh gives result for number of coordiantes 680. If I use ToElementMesh where input is result of ToBoundaryMeshused before and the same options as before I get different number of coordiantes 817. I would like to know why is that?

elementmesh =
ToElementMesh[bmesh, "MeshElementType" -> TetrahedronElement,
"MeshOrder" -> 1,
MaxCellMeasure -> {"Length" ->
Min[xmax - xmin, ymax - ymin, zmax - zmin]/2}]
elementmesh["MeshElements"]
elementmesh["Coordinates"] // Length


I would also like to know if there is a way how to make the same mesh if I run the code again, because now mesh is generated randomly and I get different mesh every time I run it. I also wonder if there is possibility to get actual "MeshElements" directly from ToBoundaryMesh, now I get Automatic as an answer.

• I would say that ToBoundaryMesh yields a triangle mesh, consisting solely of boundary vertices and boundary triangles while ToElementMesh yields a fulldimensional tetrahedron mesh, containing both the boundary and the interior vertices. – Henrik Schumacher Dec 22 '17 at 15:31
• You get the actual triangles of bmesh with bmesh["BoundaryElements"] and you can see the mesh with bmesh["Wireframe"] – Henrik Schumacher Dec 22 '17 at 15:41
• Any idea how to persuade ToElementMesh to generte each time the same mesh? – NZupan Dec 27 '17 at 9:03
• Why do need ToElementMesh` to generate mesh with the same number of elements each time? As long as the approximation to the boundary is good, there should be no problem? What is it that you are trying to do? – user21 Dec 28 '17 at 10:44
• Thanks for your answers everyone, it is all clear now. I needed the same mesh to rule out some randomly appearing bug. I found a way around it, with simply saving already generated mesh and using it again. – NZupan Dec 28 '17 at 11:42