Shapes in the plane¶
A domain of any shape: a plate with a hole, an L-shaped room, the cross section of a cable. You draw its boundary from lines and arcs, and say on each piece of it what holds there. This page is about the shape and the solution on it.
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 and the curves that bound it¶
A Region is what one or more closed curves bound. The curves come from
pdemagic.curves: a Polygon through its corners, a Circle, an
Ellipse. This is a membrane in the shape of an L, fixed along its edge
and under an even load:
corners = [(0, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (0, -1)]
room = pm.Region(x, y, curves.Polygon(*corners))
membrane = pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=room, boundary=sp.Eq(u, 0))
sagging = pm.solve(membrane)
sagging.plot()
One equation as boundary holds on the whole boundary.
Holes¶
A point belongs to the region if it lies inside an odd number of the curves. A curve inside another one is therefore a hole. In which sense a curve runs does not matter.
The space between two circles is the cross section of a cable. With the potential 0 on the outer conductor and 1 on the inner one, the potential between them is \(\ln r / \ln 0.5\):
shield = curves.Circle((0, 0), 1, name="shield")
core = curves.Circle((0, 0), 0.5, name="core")
cable = pm.Region(x, y, shield, core)
potential = pm.Problem(
sp.Eq(pm.laplacian(u), 0),
domain=cable,
boundary={shield: sp.Eq(u, 0), core: sp.Eq(u, 1)},
)
between = pm.solve(potential)
between.plot()
The boundary is now a mapping from each curve to its equation. What an
equation may state is on the page about
boundary conditions.
Curves from lines and arcs¶
A Curve is put together from pieces that join end to end:
curves.Line and curves.Arc. An arc is given by its centre, its
radius, and the angles at which it starts and ends. With two numbers as
its radius it is an arc of an ellipse with those half axes along x and
y. Each piece takes a condition of its own.
Half a disk, warm along its diameter and cold along its arc:
arc = curves.Arc((0, 0), 1, 0, math.pi)
diameter = curves.Line((-1, 0), (1, 0))
half = pm.Region(x, y, curves.Curve(arc, diameter))
warmed = pm.Problem(
sp.Eq(pm.laplacian(u), 0),
domain=half,
boundary={arc: sp.Eq(u, 0), diameter: sp.Eq(u, 1 - x**2)},
)
pm.solve(warmed).plot()
The sides of a polygon are its pieces: polygon.sides[0] runs from the
first corner to the second.
Names¶
Plans and messages refer to the pieces of the boundary by name. A curve
or a piece takes a name. Without one, a name is made from where it is:
square = curves.Polygon((0, 0), (1, 0), (1, 1), (0, 1))
assert cable.pieces == ("shield", "core")
assert pm.Region(x, y, square).pieces[:2] == ("curve 1 side 1", "curve 1 side 2")
A name can stand for its piece in boundary.
The grid of the solution¶
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, in a hole
or beside the shape, holds no number: nan.
values = between[u]
assert values.shape == (129, 129)
assert np.isnan(values[0, 0]) # a corner of the rectangle, outside the shield
assert np.isnan(values[64, 64]) # the centre, inside the core
assert np.nanmax(values) <= 1.0 + 1e-12
Use the functions of numpy that skip nan, such as np.nanmax, or the
mask np.isfinite(values). The grid does not affect the accuracy. It is
an entry of the plan, plan.grid.shape: ask for more points or fewer.
Values at any point¶
Call the solution with coordinates for its value anywhere in the region, also 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:
assert abs(between(1.0, 0.0)) < 1e-10 # on the shield
assert abs(between(0.0, 0.5) - 1.0) < 1e-10 # on the core
assert np.isnan(between(0.0, 0.0)) # in the hole
angles = np.linspace(0, 2 * np.pi, 200)
half_way = between(0.75 * np.cos(angles), 0.75 * np.sin(angles))
assert abs(half_way - math.log(0.75) / math.log(0.5)).max() < 1e-10
Arrays work as numbers do, and are broadcast against each other.
between.integral() is the integral over the region. It comes the same
way, from the elements and up to the boundary.
What the planner chose¶
The region is cut into quadrilaterals, and on each of them the solution is a polynomial. Three entries of the plan say how:
method = between.plan.method
assert method.kind == "spectral_elements"
assert (method.degree, method.spacing, method.layers) == (12, 0.5, 0)
The accuracy is raised by the degree. The size of the pieces follows from the spacing, the length of the sides of the triangles that the quadrilaterals are cut from. A smaller spacing helps where the boundary has fine detail, and costs more than a higher degree.
Corners that point inwards¶
At a corner of the boundary that points into the region, like the one of the L-shape above, the solution of almost every problem has a singularity: its slope grows without bound towards the corner. No polynomial follows that, so pdemagic lets the elements shrink towards such a corner, layer by layer, each half the size of the one before. The plan says so:
assert sagging.plan.method.layers == 18
note = sagging.plan.notes[3]
assert "the solution has a singularity where the piece `curve 1 side 1`" in note
With the layers the solution is right to ten digits and more, at the
corner too. Without them, with plan.method.layers = 0, about four
digits are left.
assert abs(sagging(-0.5, 0.5) - 0.131052962031) < 1e-10
The same happens where a condition changes from a value to a slope in the middle of a straight side, and pdemagic treats that place in the same way.
What is solved on a region¶
Any linear elliptic equation of second order, with coefficients that may vary, and with a nonlinear term: see Stationary equations, Media and potentials, Waves of one frequency and Nonlinear stationary equations. Beyond that, problems in time, systems and the modes of a shape. The full list is in the reference: what is solved on shapes in the plane.
What does not work yet¶
Shapes in space.
Boundaries that are not made of lines and of arcs of circles and ellipses.
Derivatives above the second order, other than through a system.
In a problem in time the mesh has no layers yet, and a corner that points inwards leaves three or four digits there.
See also¶
A shape of your own, the tutorial.
Gallery: a plate with a hole, a membrane on an L, an L-shaped drum.
Methods: spectral elements, corners, grids and values anywhere.
Recipes: draw a shape with holes, name the pieces of a boundary, evaluate at a point.