# 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: ```python 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: ```python 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](planner.md), and `pm.solve(problem)` returns the [solution](solutions.md). 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: ```python 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](round.md) 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](varying.md). - `pm.normal_derivative(u)` is the slope along the normal that points out of the domain. It stands in [boundary conditions](boundary-conditions.md), not in equations. ```python 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](complex.md). An undefined function that is not an unknown stands for [data from an array](data.md). A symbol that is not a coordinate is not taken. A parameter must be a number by the time the problem is planned: ```python 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) ``` ```text 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: ```python 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](data.md). ## 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: ```python 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](systems.md) 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 | |---|---|---| | `domain` | where the unknowns live | [Domains](domains.md) | | `boundary` | what holds on the faces of a box or a round domain, or on the curves of a region | [Boundary conditions](boundary-conditions.md) | | `time` | the symbol for the time, with the start and the end, as in `(t, 0, 2)` | [Problems in time](time.md) | | `initial` | the unknowns at the start of the time span | [Problems in time](time.md) | | `eigenvalue` | the symbol for the eigenvalue and how many are wanted, as in `(E, 4)` | [Eigenproblems](eigen.md) | | `guess` | where Newton's method starts on a nonlinear stationary equation | [Nonlinear stationary equations](nonlinear.md) | | `data` | the arrays behind undefined functions that are not unknowns | [Data from arrays](data.md) | | `bodies` | conductors and dielectrics in the domain | [Bodies](bodies.md) | 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.Problem` and the operators](../ref/problem.md), with every argument in full. - [What is solved where](../ref/equations.md): which equation on which domain. - [The error classes](../ref/errors.md) and the [known limits](../dev/limits.md). - [A first problem](../intro/tutorial-1-first-problem.md), the tutorial that takes one problem through the whole workflow. - [Recipes](../howto/index.md) for single tasks.