# Multiple Boundary Conditions for NDSolve in mesh with multiple interfaces

Here is my problem: I want to solve a Laplacian equation in a 2D geometry with multiple interfaces, each interface presenting a different boundary condition.

As for an example, I am working on a ring with BC1 on the outside and BC2 on the inside. I would like Mathematica to identify these two interfaces automatically (which I do manually for now, thanks to easy geometry).

Here is my code:

Needs["NDSolveFEM"]
rego = RegionDifference[Disk[{0, 0}, 10], Disk[{0, 0}, 4]];
mesh = ToElementMesh[rego];
mesh["Wireframe"]


sol = NDSolve[{-Laplacian[u[x, y], {x, y}] == 0,
DirichletCondition[u[x, y] == 0, x^2 + y^2 == 16],
DirichletCondition[u[x, y] == 1, x^2 + y^2 == 100]},
u, {x, y} \[Element] mesh];
Plot3D[u[x, y] /. sol, {x, y} \[Element] mesh, PlotRange -> All,
AxesLabel -> {x, y, u}]


which gives this nice plot:

The trick is that I want to automize the selection of the boundaries in the Dirichlet conditions, because analytical expressions are easy to derive only in simple geometries.

I have had a look on other answers without being truly statisfied... must miss something.

Idea: use ToBoundaryMesh[] ?

Thanks.

If I understand the question right, you could use boundary markers like so:

Needs["NDSolveFEM"]
rego = RegionDifference[Disk[{0, 0}, 10], Disk[{0, 0}, 4]];
mesh = ToElementMesh[rego];
(*mesh["Wireframe"]*)


The ToElementMesh auto generated markers. You can inspect them:

groups = mesh["BoundaryElementMarkerUnion"]
{1, 2}


Visualize the boundary elements that are grouped:

temp = Most[Range[0, 1, 1/(Length[groups])]];
colors = ColorData["BrightBands"][#] & /@ temp


Alternatively to colors, you can use Dashing:

dashes = Dashing /@ temp
{Dashing[0], Dashing[1/2]}


Show the boundary mesh:

mesh["Wireframe"["MeshElement" -> "BoundaryElements",
"MeshElementStyle" -> (Directive[#] & /@ colors)]]


Or with dashes:

mesh["Wireframe"["MeshElement" -> "BoundaryElements",
"MeshElementStyle" -> (Directive[#] & /@ dashes)]]


Now, generate boundary conditions that do not use a predicate but the boundary markers in the mesh.

bcs = DirichletCondition[u[x, y] == RandomReal[],
ElementMarker == #] & /@ groups
{DirichletCondition[u[x, y] == 0.23219967730964175,
ElementMarker == 1],
DirichletCondition[u[x, y] == 0.1493304332447205,
ElementMarker == 2]}


Solve the equation as before.

sol = NDSolve[{-Laplacian[u[x, y], {x, y}] == 0, bcs},
u, {x, y} \[Element] mesh];
(*Plot3D[u[x,y]/.sol,{x,y}\[Element]mesh,PlotRange\[Rule]All,\
AxesLabel\[Rule]{x,y,u}]*)

• This is great thanks, exactly what I needed!! Although I am colorblind so I will have to modify the ColorData line ;) Jan 9, 2019 at 16:07
• @Valacar, what would be a better scheme for color blind people? I'd like to change to that. Jan 9, 2019 at 16:29
• like numbers above the color segments (ranging from 1 to number of groups) for example. Also, I am thinking of B&W dots, hyphens, etc. ? Jan 9, 2019 at 16:37
• @Valacar, I hope this is better? Jan 10, 2019 at 6:34
• yes it is! Thanks Jan 10, 2019 at 8:48