# Mesh Generation help

Mathematica Asked by kmulc on January 13, 2021

I am having trouble generating a mesh of a rectangle inside another rectangle. I have been following the steps on this article: https://reference.wolfram.com/language/PDEModels/tutorial/HeatTransfer/ModelCollection/ShrinkFitting.html

However, I have been getting an error that I cannot combine the inner rectangle to the outer rectangle with a hole in it. It says "Coordinate Skeleton[2] should be a pair of numbers, or a Scaled or Offset form." in the debugger. Here is my code, it is more or less what the tutorial does.

bathx = 30;
bathy = 30;
plastic = Rectangle[{10, 10}, {20, 20}];
waterbath =
RegionDifference[Rectangle[{0, 0}, {bathx, bathy}], plastic];
[CapitalOmega] = RegionUnion[plastic, waterbath];
bounds = 1.1*{{0, bathx}, {0, bathy}};
mesh = ToElementMesh[[CapitalOmega], bounds];

Show[mesh["Wireframe"]]

Subscript[bm, w] = ToBoundaryMesh[waterbath];
Subscript[bm, p] = ToBoundaryMesh[plastic];
Needs["FEMAddOns"]
bmesh = FEMUtilsBoundaryElementMeshJoin[Subscript[bm, w],
Subscript[bm, p]]

waterbathCoordinate = {1, 1};
plasticCoordinate = {15, 15};
markerColors = {Blue, Orange};
markerCoordinates = {{waterbathCoordinate}, {plasticCoordinate}};

Show[{bmesh["Wireframe"],
Point /@ #2} &, {markerColors, markerCoordinates}]]}]

markerSpecification = {{plasticCoordinate, 1}, {waterbathCoordinate,
2}};

mesh2 = ToElementMesh[bmesh, "RegionMarker" -> markerSpecification,
"MaxCellMeasure" -> 5*10^-6];
GraphicsRow[{mesh2[
"Wireframe"[
"MeshElementStyle" ->
Map[Directive[FaceForm[#], EdgeForm[]] &, markerColors]]],
mesh2["Wireframe"[
Sequence[PlotRange -> {bounds},
"MeshElementStyle" -> Map[FaceForm[#] &, markerColors]]]]}]

Show[{mesh2["Wireframe"]}]
$$$$


Tim's answer is spot on. Here is a slightly different approach that uses a trick. The idea is to use the waterbath as the region and tell ToElementMesh to also mesh the interior region (the plastic). If this approach is applicable in your case then you can reduce the amount of code somewhat.

Needs["NDSolveFEM"]
bathx = 30;
bathy = 30;
plastic = Rectangle[{10, 10}, {20, 20}];
waterbath =
RegionDifference[Rectangle[{0, 0}, {bathx, bathy}], plastic];
waterbathCoordinate = {1, 1};
plasticCoordinate = {15, 15};
markerColors = {Blue, Orange};
markerSpecification = {{plasticCoordinate, 1,
0.25}, {waterbathCoordinate, 2, 1.}};
mesh = ToElementMesh[waterbath, "RegionHoles" -> None,
"RegionMarker" -> markerSpecification]
GraphicsRow[{mesh[
"Wireframe"[
"MeshElementStyle" ->
Map[Directive[FaceForm[#], EdgeForm[]] &, markerColors]]],
mesh["Wireframe"[
"MeshElementStyle" -> Map[FaceForm[#] &, markerColors]]]}]


Also, note that I used a different granularity in the subsections of the mesh.

Answered by user21 on January 13, 2021

The linked example was for a much smaller domain and hence too small of a mesh size specification. The following code should accomplish what you need.

(* Uncomment if not installed *)
bathx = 30;
bathy = 30;
bounds = 1.1*{{0, bathx}, {0, bathy}};
plastic = Rectangle[{10, 10}, {20, 20}];
waterbath = Rectangle[{0, 0}, {bathx, bathy}];

bmw = ToBoundaryMesh[waterbath];
bmp = ToBoundaryMesh[plastic];
bmesh = BoundaryElementMeshJoin[bmw, bmp];

waterbathCoordinate = {1, 1};
plasticCoordinate = {15, 15};
markerColors = {Blue, Orange};
markerCoordinates = {{waterbathCoordinate}, {plasticCoordinate}};

Show[{bmesh["Wireframe"],
Point /@ #2} &, {markerColors, markerCoordinates}]]}]

markerSpecification = {{plasticCoordinate, 1}, {waterbathCoordinate,
2}};

mesh2 = ToElementMesh[bmesh, "RegionMarker" -> markerSpecification,
"MaxCellMeasure" -> {"Length" -> 1}];
GraphicsRow[{mesh2[
"Wireframe"[
"MeshElementStyle" ->
Map[Directive[FaceForm[#], EdgeForm[]] &, markerColors]]],
mesh2["Wireframe"[
Sequence[PlotRange -> {bounds},
"MeshElementStyle" -> Map[FaceForm[#] &, markerColors]]]]}]
mesh2["Wireframe"]


## Related Questions

### Fix two data lists intertwined

2  Asked on August 22, 2021

### Help to create a 2D mesh generator (FEM)

1  Asked on August 22, 2021

### Two-dimensional Laplacian coupled with another equation leading to a BVP with integral bc(s)

2  Asked on August 22, 2021

### Enter without execute

2  Asked on August 22, 2021

### Plotting two functions into one plot with two y-axes. Is there a built option for this in newer versions of Mathematica?

1  Asked on August 22, 2021

### How do I replace specific sequences of a sublist without changing the level of the list?

2  Asked on August 22, 2021 by kycygni

### How to use MMA to simulate the German tank problem

2  Asked on August 22, 2021

### Giving a range to Plot to plot multiple lines at the same time

3  Asked on August 22, 2021

### How to decompose a list of n-tuples into a union of Cartesian products

2  Asked on August 22, 2021 by martin-leung

### Possible error with Series expansion

2  Asked on August 22, 2021 by miguel-oliveira

### Mathematica emits a beep after any evaluation – how to remove it?

2  Asked on August 22, 2021

### Export NetCDF Dimension Name

1  Asked on August 22, 2021 by user1139069

### Convert huge (10-100MB of plain text) Mathematica function for external use

0  Asked on August 22, 2021 by phys

### Why Mathematica doesn’t give output for Integration of Special Function

0  Asked on August 22, 2021 by nilabh

### Prony series with a large number of terms

2  Asked on August 22, 2021

### Breaking “Functional Loops” and Doing Lazy Evaluation In Mathematica

2  Asked on August 22, 2021 by dskeletov

### How to calculate the total area of $frac{cos (2 y) cos (2 x-y)+cos (3 y)}{cos (2 x-y)+cos (y)}$ enclosed in the range $[0,1]$?

5  Asked on August 19, 2021

### interact with web pages with many hyperlinks and search for a specific name or phrase How?

1  Asked on August 19, 2021

### NestList in 12.1/12.2 is 10x slower than 11.3/12.0

0  Asked on August 18, 2021