A shape of your own¶
A box has straight walls, and a disk is round. This part solves on a shape that you draw yourself: a triangular plate, then the same plate with a round hole. You say on each curve of the boundary what holds there, and read the result at any point.
Step 1: a region from a polygon¶
import math
import numpy as np
import sympy as sp
import pdemagic as pm
from pdemagic import curves
x, y = sp.symbols("x y", real=True)
u = sp.Function("u")(x, y)
A Region is the part of the plane that one or more closed curves
bound. The curves come from pdemagic.curves. A Polygon runs through
its corners, here those of a triangle with three equal sides:
top = math.sqrt(3)
outline = curves.Polygon((0, 0), (2, 0), (1, top), name="outline")
plate = pm.Region(x, y, outline)
Step 2: solve on it¶
A region is a domain like any other. This plate is heated evenly and
kept cold along its edge, as the square one in
part 2 was. One equation as boundary holds
on the whole boundary:
warmed = pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=plate, boundary=sp.Eq(u, 0))
solid = pm.solve(warmed)
solid.plot()
Step 3: the grid, and the points outside¶
The values of the solution come on a grid over the rectangle that holds
the region. A point of the grid that lies outside the region holds no
number: nan.
values = solid[u]
print(values.shape)
print(values[0, -1])
print(round(float(np.nanmax(values)), 4))
(129, 112)
nan
0.1111
values[0, -1] is the upper left corner of the rectangle, beside the
triangle. Use the functions of numpy that skip nan, such as
np.nanmax, or the mask np.isfinite(values).
On a region this grid is only where the solution is shown. The method works on a mesh of its own, which follows the boundary, so the grid does not affect the accuracy. For this plate the exact temperature is known, and the result agrees with it to twelve digits wherever it has a value:
X, Y = solid.mesh()
exact = Y * (top * X - Y) * (top * (2 - X) - Y) / (4 * top)
inside = np.isfinite(values)
print(abs(values - exact)[inside].max() < 1e-12)
True
Step 4: a hole¶
A point belongs to the region if it lies inside an odd number of its
curves. A curve inside another one is therefore a hole. Add a Circle
to the triangle:
hole = curves.Circle((1, 0.6), 0.25, name="hole")
pierced = pm.Region(x, y, outline, hole)
print(pierced.pieces)
('outline side 1', 'outline side 2', 'outline side 3', 'hole')
The boundary now has four pieces: the three sides of the polygon, and
the circle. Plans and messages call them by these names, which come from
the name that each curve was given.
Step 5: a condition for each curve¶
With more than one curve, boundary is a mapping from each curve to its
equation. A warm pipe runs through the hole, and the edge of the plate
stays cold. Nothing heats the plate itself any more, so the equation is
Laplace’s:
around_a_pipe = pm.Problem(
sp.Eq(pm.laplacian(u), 0),
domain=pierced,
boundary={outline: sp.Eq(u, 0), hole: sp.Eq(u, 1)},
)
heated = pm.solve(around_a_pipe)
heated.plot()
Step 6: values at any point¶
Call the solution with coordinates for its value anywhere in the region: between the points of the grid, and on the boundary, where the grid has no points. The value comes from what the method solved with, and is as accurate as the solution.
print(round(float(heated(1.0, 0.2)), 6)) # between pipe and lower side
print(round(float(heated(1.25, 0.6)), 6)) # on the pipe
print(round(float(heated(1.0, 0.0)), 6) == 0) # on the lower side
print(heated(1.0, 0.6)) # in the hole
0.495801
1.0
True
nan
Arrays work as numbers do. The temperature at six points on the line between the lower side and the pipe:
heights = np.linspace(0.05, 0.3, 6)
print(np.round(heated(1.0, heights), 3))
[0.117 0.237 0.362 0.496 0.642 0.807]
Step 7: a condition for each side¶
The sides of a polygon are pieces of their own: outline.sides[0] runs
from the first corner to the second. Each can take its own condition.
Here the lower side is warm, the two others are cold, and the hole is
insulated: nothing flows through its edge.
On a shape the boundary follows no coordinate, so the slope across it is
written with pm.normal_derivative(u), the derivative along the normal
that points out of the region.
lower, right, left = outline.sides
around_the_hole = pm.Problem(
sp.Eq(pm.laplacian(u), 0),
domain=pierced,
boundary={
lower: sp.Eq(u, sp.sin(sp.pi * x / 2)),
right: sp.Eq(u, 0),
left: sp.Eq(u, 0),
hole: sp.Eq(pm.normal_derivative(u), 0),
},
)
flowing = pm.solve(around_the_hole)
flowing.plot()
The heat flows around the hole. The plan says in its notes what holds on which piece:
print(flowing.plan.notes[1])
Boundary conditions: the value on `outline side 1`, the value on `outline side 2`, the value on `outline side 3`, the derivative along the normal on `hole`.
What you have learned¶
A
pm.Regionis what closed curves bound: acurves.Polygon, acurves.Circle, and others. A curve inside another one is a hole.boundary=takes one equation for the whole boundary, or a mapping from each curve, or each side of a polygon, to its equation.The solution comes on a grid with
nanoutside the region, andsolution(x, y)gives the value at any point of the region.pm.normal_derivative(u)is the slope across the boundary.
Shapes in the plane has the rest: curves from lines and arcs, corners that point inwards, and what the planner chooses on a shape. The gallery has a plate with a hole. Next: tones and states.