Regions, curves and shapes

A region is a shape in the plane that closed curves bound. The curves come from the module pdemagic.curves. The module pdemagic.shapes holds the shapes in space that bodies are made of.

Regions

class pdemagic.Region(x, y, *curves)

A shape in the plane: what one or more closed curves bound.

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.

On each piece of the curves the problem states a boundary condition: see the boundary argument of Problem. The solution is given on a grid over the rectangle that holds the region, and is nan at the points of the grid outside it.

Parameters:
  • x (sp.Symbol) – The two Cartesian coordinates.

  • y (sp.Symbol) – The two Cartesian coordinates.

  • *curves (Curve) – The closed curves that bound the region, from pdemagic.curves.

Examples

The unit disk with a hole off-centre:

>>> import sympy as sp
>>> from pdemagic import curves
>>> x, y = sp.symbols("x y", real=True)
>>> rim = curves.Circle((0, 0), 1, name="rim")
>>> hole = curves.Circle((0.4, 0.2), 0.25, name="hole")
>>> region = Region(x, y, rim, hole)
>>> region.curves[1]
Circle(centre=(0.4, 0.2), radius=0.25)
>>> region.pieces
('rim', 'hole')

An L-shape. Its sides are numbered from the first corner:

>>> corners = [(0, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (0, -1)]
>>> Region(x, y, curves.Polygon(*corners)).pieces[:2]
('curve 1 side 1', 'curve 1 side 2')
property pieces: tuple[str, ...]

The names of the pieces of the boundary, curve by curve.

laplacian(expr)

Return the Laplacian of a formula in the coordinates of the domain.

Parameters:

expr (Expr)

Return type:

Expr

axes(shape)

Return the grid points along each coordinate for a grid of this shape.

The grid is uniform over the rectangle that holds the region, with its first and its last points on the sides of the rectangle. It is the grid of array data that is given without bounds, and, with the shape of the grid of a plan, the grid that the solution is given on.

Parameters:

shape (Sequence[int]) – The number of points along each coordinate.

Return type:

tuple[ndarray, …]

Examples

>>> import sympy as sp
>>> from pdemagic import curves
>>> x, y = sp.symbols("x y", real=True)
>>> region = Region(x, y, curves.Circle((0, 0), 1))
>>> xs, ys = region.axes((5, 3))
>>> xs.tolist(), ys.tolist()
([-1.0, -0.5, 0.0, 0.5, 1.0], [-1.0, 0.0, 1.0])

Curves

Closed curves in the plane, each made of straight lines and of arcs of circles and ellipses. A piece of a curve takes a boundary condition by its name.

Closed curves in the plane: the boundary of a region.

A pdemagic.Region is bounded by one or more closed curves. A curve is made of pieces that join end to end, straight lines and arcs, and a problem states a boundary condition on each piece.

Examples

>>> from pdemagic import curves
>>> curves.Circle((0, 0), 1)
Circle(centre=(0.0, 0.0), radius=1.0)
>>> square = curves.Polygon((0, 0), (1, 0), (1, 1), (0, 1))
>>> len(square.sides)
4
>>> square.sides[0]
Line((0.0, 0.0), (1.0, 0.0))
class pdemagic.curves.Arc(centre, radius, start, end, name=None)

An arc of a circle, or of an ellipse with its axes along the coordinates.

The arc holds the points centre + (rx·cos t, ry·sin t) as t runs from start to end. With end below start it runs clockwise.

Parameters:
  • centre (Sequence[float]) – The centre of the circle or the ellipse.

  • radius (float | Sequence[float]) – The radius of the circle, or the two half axes of the ellipse.

  • start (float) – The angle at which the arc starts and the one at which it ends, in radians.

  • end (float) – The angle at which the arc starts and the one at which it ends, in radians.

  • name (str | None) – A name for the piece, which plans and messages use.

Examples

The upper half of the unit circle, from (1, 0) to (-1, 0):

>>> import math
>>> Arc((0, 0), 1, 0, math.pi)
Arc(centre=(0.0, 0.0), radius=1.0, start=0.0, end=3.141592653589793)
class pdemagic.curves.Circle(centre, radius, name=None)

A whole circle.

Parameters:
  • centre (Sequence[float]) – The centre.

  • radius (float) – The radius.

  • name (str | None) – A name for the circle.

Examples

>>> Circle((0.5, 0), 0.25, name="hole").name
'hole'
class pdemagic.curves.Curve(*pieces, name=None)

A closed curve, made of pieces that join end to end.

The last piece ends where the first one starts. In which sense the curve runs does not matter.

Parameters:
  • *pieces (Line | Arc) – The pieces, Line and Arc objects, in the order in which the curve runs through them.

  • name (str | None) – A name for the curve. Pieces without a name of their own are called after it.

Examples

Half a disk: an arc, closed by its diameter.

>>> import math
>>> half = Curve(Arc((0, 0), 1, 0, math.pi), Line((-1, 0), (1, 0)))
>>> len(half.pieces)
2
class pdemagic.curves.Ellipse(centre, radii, name=None)

A whole ellipse with its axes along the coordinates.

Parameters:
  • centre (Sequence[float]) – The centre.

  • radii (Sequence[float]) – The half axes along the two coordinates.

  • name (str | None) – A name for the ellipse.

Examples

>>> Ellipse((0, 0), (2, 1))
Ellipse(centre=(0.0, 0.0), radii=(2.0, 1.0))
class pdemagic.curves.Line(start, end, name=None)

A straight piece of a curve.

Parameters:
  • start (Sequence[float]) – The two ends.

  • end (Sequence[float]) – The two ends.

  • name (str | None) – A name for the piece, which plans and messages use.

Examples

>>> Line((0, 0), (1, 2))
Line((0.0, 0.0), (1.0, 2.0))
class pdemagic.curves.Polygon(*corners, name=None)

A closed polygon.

Parameters:
  • *corners (Sequence[float]) – The corners, each as two numbers, in the order in which the polygon runs through them. The last one is joined to the first.

  • name (str | None) – A name for the polygon. Its sides are called after it.

Examples

>>> triangle = Polygon((0, 0), (2, 0), (0, 1))
>>> triangle.sides[2]
Line((0.0, 1.0), (0.0, 0.0))
property sides: tuple[Line, ...]

The sides, to state a boundary condition on one of them.

Side i runs from corner i to the next.

Shapes of bodies

Shapes in space, for pm.Conductor and pm.Dielectric. They are not domains: a body lies in three-dimensional free space.

Shapes of bodies: closed surfaces in three dimensions.

A shape says where a body is. What the body is made of is said by pdemagic.Conductor or pdemagic.Dielectric.

Examples

>>> from pdemagic import shapes
>>> shapes.Sphere(radius=0.5)
Sphere(centre=(0.0, 0.0, 0.0), radius=0.5)
>>> shapes.Box(lower=(0, 0, 0), upper=(1, 2, 3)).mesh(panels=100)
Mesh(vertices=64, triangles=124)
class pdemagic.shapes.Box(lower, upper)

A box with its edges along the coordinate axes.

Parameters:
  • lower (Sequence[float]) – The corner with the smallest coordinates.

  • upper (Sequence[float]) – The corner with the largest coordinates.

class pdemagic.shapes.Cylinder(centre=(0.0, 0.0, 0.0), radius=1.0, height=1.0)

A cylinder with its axis along the third coordinate.

Not to be confused with the domain pdemagic.Cylinder.

Parameters:
  • centre (Sequence[float]) – The middle of its axis.

  • radius (float) – Its radius.

  • height (float) – Its length along the axis.

class pdemagic.shapes.Ellipsoid(centre=(0.0, 0.0, 0.0), semi_axes=(1.0, 1.0, 1.0))

An ellipsoid with its axes along the coordinate axes.

Parameters:
  • centre (Sequence[float]) – Its centre.

  • semi_axes (Sequence[float]) – Half its extent along each coordinate axis.

class pdemagic.shapes.Mesh(vertices, triangles)

A closed surface given as triangles.

Any closed surface that does not intersect itself will do, from any mesh generator. Which way round the triangles are listed does not matter, as long as it is the same for all of them.

Parameters:
  • vertices (Any) – The corners of the triangles, as an array of shape (n, 3).

  • triangles (Any) – For each triangle the rows of its three corners in vertices, as an array of shape (m, 3).

Examples

A tetrahedron:

>>> Mesh(
...     vertices=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)],
...     triangles=[(0, 2, 1), (0, 1, 3), (0, 3, 2), (1, 2, 3)],
... )
Mesh(vertices=4, triangles=4)
mesh(panels=1000)

Return the mesh with its normals pointing outwards.

A mesh keeps its triangles, so panels has no effect.

Parameters:

panels (int)

Return type:

Mesh

class pdemagic.shapes.Shape

A closed surface. Use one of the subclasses.

mesh(panels=1000)

Return the surface as triangles, as the solver sees it.

Parameters:

panels (int) – About how many triangles to divide the surface into.

Returns:

The triangles, with their normals pointing out of the shape.

Return type:

Mesh

class pdemagic.shapes.Sphere(centre=(0.0, 0.0, 0.0), radius=1.0)

A sphere.

Parameters:
  • centre (Sequence[float]) – Its centre.

  • radius (float) – Its radius.

See also: Shapes in the plane and Bodies.