Problems and operators

A problem is stated with pm.Problem, and the operators pm.laplacian, pm.grad, pm.div and pm.normal_derivative write its equations and conditions in the coordinates of its domain.

The problem

class pdemagic.Problem(*equations, domain, data=None, bodies=(), time=None, initial=None, boundary=None, guess=None, eigenvalue=None)

A set of equations on a domain.

A problem states the mathematics and nothing else. How it is solved is decided by pdemagic.plan(), or by you.

Parameters:
  • *equations (sp.Eq) – The equations, as sympy.Eq objects. The unknowns are the undefined functions in them, such as phi(x, y, z).

  • domain (Space | Box | Disk | Ball | Cylinder | Region) – Where the unknowns live.

  • data (Mapping[AppliedUndef, GridData] | None) – Values for undefined functions that are not unknowns but given data, as a mapping from the function to its GridData.

  • bodies (Sequence[Conductor | Dielectric]) – Conductors and dielectrics in the domain, as Conductor and Dielectric objects. Outside them the equations hold as they are written.

  • time (tuple[sp.Symbol, float, float] | None) – For a problem that depends on time: the symbol that stands for the time, and the start and the end of the time span, as in (t, 0.0, 2.0). The unknowns are then functions of the coordinates of the domain and of the time, in that order.

  • initial (Mapping[AppliedUndef, Any] | None) – The unknowns at the start of the time span, as a mapping from each unknown to its initial value: an expression in the coordinates, a number, or a GridData. An equation of second order in time also needs the initial rate of change, under the key sympy.Derivative(u, t).

  • guess (Mapping[AppliedUndef, Any] | None) – For a nonlinear stationary equation: where Newton’s method starts, as a mapping from the unknown to its starting value: an expression in the coordinates, a number, or a GridData. Without one the method starts from zero. Such an equation can have several solutions, and the starting value decides which one is found. On a box with walls and on a round domain it is an expression or a number.

  • boundary (sp.Eq | Mapping[Any, sp.Eq] | None) –

    On a Box, a Disk, a Ball or a Cylinder: the conditions on its faces. Each is an equation that holds on a face, such as sympy.Eq(u, 0) for a given value, sympy.Eq(u.diff(x), 1) for a given derivative, or a mix of both. Give one equation for all faces, or a mapping from each face to its equation, as in {(x, 0): sympy.Eq(u, 0), (x, 1): sympy.Eq(u, 1)}. A face is the coordinate that is constant on it and the end it lies at: the rim of a disk of radius 2 is (r, 2). A disk, a ball or a cylinder without a condition has vacuum outside. normal_derivative() is the derivative along the coordinate of the face, in the direction that points out of the domain.

    An equation of fourth order on a Box, such as the plate equation, takes two conditions on each face: give a list of two equations in the place of one, as in [sympy.Eq(u, 0), sympy.Eq(pm.normal_derivative(u), 0)] for a plate that is clamped all around.

    On a Region: the conditions on the curves that bound it. Give one equation for the whole boundary, or a mapping from each curve, or each piece of one, to its equation, as in {rim: sympy.Eq(u, 0), hole: sympy.Eq(pm.normal_derivative(u), 1)}. normal_derivative() is the derivative along the normal that points out of the region. A system has one condition for each unknown: give a list of equations in the place of one, as in [sympy.Eq(u, 0), sympy.Eq(pm.normal_derivative(v), 0)].

  • eigenvalue (tuple[sp.Symbol, int] | None) – For an eigenproblem: the symbol that stands for the eigenvalue in the equation, and how many of the lowest eigenvalues are wanted, as in (E, 4). The equation is then one for the unknown and the eigenvalue together, such as sympy.Eq(-psi.diff(x, 2) / 2 + V * psi, E * psi), and the solution holds the eigenvalues with their eigenfunctions.

Examples

>>> import sympy as sp
>>> import pdemagic as pm
>>> x, y, z = sp.symbols("x y z", real=True)
>>> phi = sp.Function("phi")(x, y, z)
>>> rho = sp.exp(-(x**2 + y**2 + z**2))
>>> problem = pm.Problem(
...     sp.Eq(pm.laplacian(phi), -4 * sp.pi * rho),
...     domain=pm.FreeSpace(x, y, z),
... )
>>> problem.unknowns
(phi(x, y, z),)
to_dict()

Return the problem in the form the core reads.

The values of data fields are not part of it. They are passed to the core separately, as arrays.

Return type:

dict[str, Any]

to_json()

Return the problem as JSON, the form every frontend produces.

Return type:

str

Operators

Without coordinates the operators stay unevaluated, and the problem gives them their meaning in the coordinates of its domain: Cartesian, polar, spherical or cylindrical.

Operator

Where it stands

pm.laplacian(u)

In an equation, on every domain. On a round domain it is the only way to write the derivatives in space.

pm.grad(u)

Inside pm.div, times a coefficient.

pm.div(a * pm.grad(u))

In an equation: a medium, a coefficient that varies inside the derivatives.

pm.normal_derivative(u)

In a boundary condition: the slope along the normal that points out of the domain, on a piece of a region and on a face of a box or of a round domain.

pdemagic.laplacian(function, *coordinates)

Return the Laplacian of a function.

Without coordinates the result is the Laplacian as an operator, which takes its meaning from the domain of the problem it is used in. This is the form to use in equations, and the only one that works in polar and spherical coordinates.

With coordinates the result is the sum of the second derivatives with respect to them, which is the Laplacian in Cartesian coordinates. For a formula in other coordinates use the domain: Ball.laplacian.

Parameters:
  • function (Expr) – The function to apply the Laplacian to.

  • *coordinates (Symbol) – Cartesian coordinates to differentiate with respect to.

Return type:

Expr

Examples

>>> import sympy as sp
>>> x, y = sp.symbols("x y")
>>> laplacian(sp.Function("u")(x, y))
Laplacian(u(x, y))
>>> laplacian(x**2 * y, x, y)
2*y
pdemagic.grad(function)

Return the gradient of a function, as an operator.

The gradient is a vector. Use it inside div(), multiplied by a coefficient, to state an equation with a coefficient that varies in space.

Examples

>>> import sympy as sp
>>> x, y, z = sp.symbols("x y z", real=True)
>>> phi = sp.Function("phi")(x, y, z)
>>> epsilon = 1 + sp.exp(-(x**2 + y**2 + z**2))
>>> div(epsilon * grad(phi))
Divergence((exp(-x**2 - y**2 - z**2) + 1)*Gradient(phi(x, y, z)))
Parameters:

function (Expr)

Return type:

Expr

pdemagic.div(vector)

Return the divergence of a vector, as an operator.

The vector must be a gradient times a coefficient: div(epsilon * grad(phi)). Without a coefficient this is the Laplacian.

Examples

>>> import sympy as sp
>>> x, y = sp.symbols("x y", real=True)
>>> u = sp.Function("u")(x, y)
>>> div(grad(u))
Divergence(Gradient(u(x, y)))
Parameters:

vector (Expr)

Return type:

Expr

pdemagic.normal_derivative(function)

Return the derivative of a function along the outward normal.

Use it in a boundary condition on a curve of a Region: there it stands for the derivative of the unknown along the normal of the curve that points out of the region. On a face of a Box or of a round domain it is the derivative along the coordinate of the face, with the sign that points out of the domain: one condition can then stand for all faces.

Examples

No flux through the boundary:

>>> import sympy as sp
>>> x, y = sp.symbols("x y", real=True)
>>> u = sp.Function("u")(x, y)
>>> sp.Eq(normal_derivative(u), 0)
Eq(NormalDerivative(u(x, y)), 0)
Parameters:

function (Expr)

Return type:

Expr

See also: Problems, unknowns and equations, Boundary conditions, and What is solved where for the equations that a problem may hold on each domain.