Problems, unknowns and equations¶
A problem is a set of sympy equations on a domain. This page says how one is stated: what the unknown is, how an equation is written, and what may stand in it. The examples start from these lines:
import numpy as np
import sympy as sp
import pdemagic as pm
x, y = sp.symbols("x y", real=True)
The unknown and the equation¶
The coordinates are sympy symbols. The unknown is an undefined function of
them, and the equation is a sp.Eq. The domain says where the equation
holds:
u = sp.Function("u")(x, y)
charge = sp.exp(-(x**2) - y**2)
problem = pm.Problem(
sp.Eq(pm.laplacian(u), -charge),
domain=pm.FreeSpace(x, y),
)
assert problem.unknowns == (u,)
A problem states the mathematics and nothing else. It names no method, no
grid and no tolerance: those are found by the planner, and
pm.solve(problem) returns the solution.
The equation can be written in any equivalent form. pdemagic reads it as a whole, so it does not matter on which side a term stands, nor whether the Laplacian is written out:
written_out = pm.Problem(
sp.Eq(-u.diff(x, 2) - u.diff(y, 2) - charge, 0),
domain=pm.FreeSpace(x, y),
)
assert np.allclose(pm.solve(written_out)[u], pm.solve(problem)[u])
Derivatives and operators¶
A derivative is a sympy derivative: u.diff(x, 2) or
sp.Derivative(u, x, 2). Three operators come with pdemagic:
pm.laplacian(u)is the Laplacian as an operator. It takes its meaning from the domain, so the same equation works in Cartesian, polar and spherical coordinates. On round domains it is the only way to write the equation.pm.div(a * pm.grad(u))is a flux with a coefficient inside the derivatives, as a permittivity or a conductivity is. See Media and potentials.pm.normal_derivative(u)is the slope along the normal that points out of the domain. It stands in boundary conditions, not in equations.
conductivity = 1 + x**2
in_a_medium = sp.Eq(-pm.div(conductivity * pm.grad(u)), charge)
What may stand in an equation¶
Beside the unknown and its derivatives, an equation holds numbers and
formulas in the coordinates: the source, and the coefficients. They are
built from what sympy offers, such as sp.exp, sp.sin and sp.sqrt.
A formula that contains sp.I makes the unknown
complex. An undefined function that is not an unknown
stands for data from an array.
A symbol that is not a coordinate is not taken. A parameter must be a number by the time the problem is planned:
kappa = sp.Symbol("kappa", positive=True)
screened = sp.Eq(pm.laplacian(u) - kappa**2 * u, -charge)
try:
pm.plan(pm.Problem(screened, domain=pm.FreeSpace(x, y)))
except pm.InvalidProblemError as error:
message = str(error)
print(message)
the symbol `kappa` is not a coordinate of the domain; substitute a number for it
Keep the parameter in the formula while you work on it, and substitute when you build the problem:
with_a_number = pm.Problem(screened.subs(kappa, 2), domain=pm.FreeSpace(x, y))
assert pm.plan(with_a_number).notes[0].startswith("The equation is a screened Poisson")
Some sympy expressions have no counterpart in pdemagic yet, among them
Piecewise, Heaviside, Min, Max and the Bessel functions. Give such
a function as an array.
Several equations¶
A problem with several unknowns takes as many equations, one after the other. A plate that rests on its edge, written as two equations of second order for the deflection and the bending moment:
from pdemagic import curves
w = sp.Function("w")(x, y)
m = sp.Function("m")(x, y)
plate = pm.Problem(
sp.Eq(pm.laplacian(w), m),
sp.Eq(pm.laplacian(m), 1),
domain=pm.Region(x, y, curves.Circle((0, 0), 1)),
boundary=[sp.Eq(w, 0), sp.Eq(m, 0)],
)
assert set(plate.unknowns) == {w, m}
Systems of equations says which systems are solved.
The arguments of a problem¶
The equations come first, and everything else has a name:
Argument |
What it states |
Read more |
|---|---|---|
|
where the unknowns live |
|
|
what holds on the faces of a box or a round domain, or on the curves of a region |
|
|
the symbol for the time, with the start and the end, as in |
|
|
the unknowns at the start of the time span |
|
|
the symbol for the eigenvalue and how many are wanted, as in |
|
|
where Newton’s method starts on a nonlinear stationary equation |
|
|
the arrays behind undefined functions that are not unknowns |
|
|
conductors and dielectrics in the domain |
In a problem in time the unknown is a function of the coordinates and of
the time, in that order: sp.Function("T")(x, y, t).
When a problem is refused¶
Two errors tell the two reasons apart. pm.InvalidProblemError says that
the statement itself is not right: a symbol without a value, a face
without a condition, an equation with no unique solution.
pm.UnsupportedProblemError says that the problem is a sound one that
pdemagic does not solve yet. Both name what was found.
See also¶
The reference of
pm.Problemand the operators, with every argument in full.What is solved where: which equation on which domain.
The error classes and the known limits.
A first problem, the tutorial that takes one problem through the whole workflow.
Recipes for single tasks.