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:

rego = RegionDifference[Disk[{0, 0}, 10], Disk[{0, 0}, 4]];
mesh = ToElementMesh[rego];


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



1 Answer 1


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

rego = RegionDifference[Disk[{0, 0}, 10], Disk[{0, 0}, 4]];
mesh = ToElementMesh[rego];

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

enter image description here

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

enter image description here

Or with dashes:

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

enter image description here

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];
  • $\begingroup$ This is great thanks, exactly what I needed!! Although I am colorblind so I will have to modify the ColorData line ;) $\endgroup$
    – Valacar
    Jan 9, 2019 at 16:07
  • $\begingroup$ @Valacar, what would be a better scheme for color blind people? I'd like to change to that. $\endgroup$
    – user21
    Jan 9, 2019 at 16:29
  • $\begingroup$ 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. ? $\endgroup$
    – Valacar
    Jan 9, 2019 at 16:37
  • $\begingroup$ @Valacar, I hope this is better? $\endgroup$
    – user21
    Jan 10, 2019 at 6:34
  • $\begingroup$ yes it is! Thanks $\endgroup$
    – Valacar
    Jan 10, 2019 at 8:48

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.