5
$\begingroup$

I would like to solve the Laplace's equation in between the square domain and a disk. However, using the code below, I was able to generate mesh but not able to obtain results with correct boundary conditions.

The desired B.C.s are 1 at the outer boundary and 0 at inner circular boundary.

(*Import required FEM package*)
Remove["Global`*"]
Needs["NDSolve`FEM`"];
With[{inner = Disk[{0, 0}, {0.25, 0.25}], 
  outer = 
   Rectangle[{-1, -1}, {1, 1}]}, (mesh = 
    ToElementMesh[RegionDifference[outer, inner], 
     MaxCellMeasure -> 0.001])["Wireframe"]]
sol = NDSolveValue[{Laplacian[u[x, y], {x, y}] == 0, 
   DirichletCondition[
    u[x, y] == 1, {x, y} \[Element] RegionBoundary[outer]], 
   DirichletCondition[
    u[x, y] == 0, {x, y} \[Element] RegionBoundary[inner]]}, 
  u, {x, y} \[Element] mesh]
DensityPlot[sol[x, y], {x, y} \[Element] mesh, 
 ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]

enter image description here

enter image description here

$\endgroup$

1 Answer 1

7
$\begingroup$

Using 'RegionBoundary' for the boundary conditions does not work well. Either specify an implicit region like in the example below or use ElementMarker.

(*Import required FEM package*)Remove["Global`*"]
Needs["NDSolve`FEM`"];
With[{inner = Disk[{0, 0}, {0.25, 0.25}], 
   outer = Rectangle[{-1, -1}, {1, 1}]}, (mesh = 
     ToElementMesh[RegionDifference[outer, inner], 
      MaxCellMeasure -> 0.001])["Wireframe"]];
sol = NDSolveValue[{Laplacian[u[x, y], {x, y}] == 0, 
    DirichletCondition[u[x, y] == 1, x^2 + y^2 >= 1/2], 
    DirichletCondition[u[x, y] == 0, x^2 + y^2 < 1/2]}, 
   u, {x, y} \[Element] mesh];
DensityPlot[sol[x, y], {x, y} \[Element] mesh, 
 ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]

enter image description here

Or with markers

(*Import required FEM package*)Remove["Global`*"]
Needs["NDSolve`FEM`"];
With[{inner = Disk[{0, 0}, {0.25, 0.25}], 
   outer = Rectangle[{-1, -1}, {1, 1}]}, (mesh = 
     ToElementMesh[RegionDifference[outer, inner](*,MaxCellMeasure->
      0.001*)])["Wireframe"]];

mesh["PointElementMarkerUnion"]
(* {1, 2, 3, 4, 5, 6, 7, 8, 9} *)

mesh["Wireframe"["MeshElement" -> "PointElements", 
  "MeshElementMarkerStyle" -> Red]]

enter image description here

sol = NDSolveValue[{Laplacian[u[x, y], {x, y}] == 0, 
    DirichletCondition[u[x, y] == 1, ElementMarker != 5], 
    DirichletCondition[u[x, y] == 0, ElementMarker == 5]}, 
   u, {x, y} \[Element] mesh];
DensityPlot[sol[x, y], {x, y} \[Element] mesh, 
 ColorFunction -> "TemperatureMap", PlotLegends -> Automatic]

enter image description here

See the Markers section in he ElementMesh Generation Tutorial for more details.

$\endgroup$
1
  • 1
    $\begingroup$ Ah! so when writing DirichletCondition[u[x, y] == 1, x^2 + y^2 >= 1/2] then NDSolve will internally figure where the boundaries actually are, and it just needs to check that the condition x^2 + y^2 >= 1/2 is true there or not. If the condition is true, then it will set u to 1 there. I thought that one had to specify the actual boundary itself in DirichletCondition. like you did with the markers. For example for unit circle, one has to say x^2+y^2==1 to indicate the boundary is at edge of circle. i.e. predicate has to be == and not > like you had. learned something new. $\endgroup$
    – Nasser
    Commented Nov 9, 2022 at 3:43

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.