# Media and potentials: coefficients that vary The coefficients of a linear equation may be functions of the position: a potential in front of the unknown, a medium inside the derivatives, a drift that turns. This page says how such an equation is written, and what each domain asks of its coefficients. ```python 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) ``` ## How it is written Write the equation as it stands. A coefficient in front of a term is a product, as in `(1 + x**2) * u` or `a * sp.Derivative(u, x, 2)`. A medium stands inside the derivatives, $\nabla\cdot(\varepsilon\nabla u)$, and is written with `pm.div(epsilon * pm.grad(u))`: a permittivity, a conductivity. On most domains a coefficient is a formula or an [array](data.md). ## In free space: a coefficient that settles A screened equation with a potential well, in the plane: ```python well = 3 * sp.exp(-((x - 1) ** 2 + y**2)) trapped = pm.Problem( sp.Eq(-pm.laplacian(u) + (1 + well) * u, sp.exp(-((x + 1) ** 2 + y**2))), domain=pm.FreeSpace(x, y), ) dip = pm.solve(trapped) assert dip.plan.method.kind == "fourier_iterative" assert dip.diagnostics["residual"] <= 1e-10 dip.plot() ``` The source sits to the left and the well to the right, where it pushes the solution down. Any coefficient may vary, also the one of the highest derivatives. Two things are asked of them. They must be smooth on the scale of the grid. And along free directions they must settle to constants away from where they vary, so that the equation far away is a Poisson or a screened Poisson equation. The planner finds those constants and puts the box around the region where the coefficients differ from them. A dielectric body with a smooth permittivity is such a medium: $\nabla\cdot(\varepsilon\nabla\varphi) = -4\pi\rho$, with $\varepsilon$ equal to one away from the body. A body with a sharp surface is described differently: see [Bodies](bodies.md). ## On a periodic box: free to vary everywhere On a box that repeats in every direction nothing has to settle, and the equation may be of any order: ```python ring = pm.Space(x, periodic={x: (0, 2 * sp.pi)}) q = sp.Function("q")(x) bent_ring = pm.solve( pm.Problem( sp.Eq( (2 + sp.sin(x)) * sp.Derivative(q, x, 2) + sp.cos(x) * sp.Derivative(q, x) - (1 + sp.cos(x) / 2) * q, sp.cos(3 * x), ), domain=ring, ) ) assert bent_ring.diagnostics["iterations"] < 30 ``` ## The iteration In free space, on periodic boxes and between walls, such equations are solved by an iteration on top of the solver for constant coefficients. `plan.method.tolerance` and `plan.method.max_iterations` set when it stops, and the diagnostics `iterations` and `residual` say how it went. If the iteration does not reach its tolerance, pdemagic raises `pm.ConvergenceError` and returns nothing. ## Between walls In a stationary equation on a [box](boxes.md) every coefficient may vary: a medium inside the derivatives, a coefficient in front of a second or a first derivative, and a potential. Here the solution is known, so that the result can be checked: ```python medium = 1 + x * y known = sp.exp(x) * sp.sin(2 * y) source = sum(sp.diff(medium * sp.diff(known, s), s) for s in (x, y)) plate = pm.Box({x: (0, 1), y: (0, 1)}) in_a_medium = pm.Problem( sp.Eq(pm.div(medium * pm.grad(u)) - (1 + x**2) * u, source - (1 + x**2) * known), domain=plate, boundary=sp.Eq(u, known), ) found = pm.solve(in_a_medium) assert found.plan.method.kind == "collocation_iterative" assert abs(found(0.3, 0.7) - np.exp(0.3) * np.sin(1.4)) < 1e-9 assert found.diagnostics["iterations"] < 20 ``` The iteration takes ten to thirty steps, also for a medium that varies by a factor of a thousand. On a disk, a ring, a ball, a shell or a cylinder the term without derivatives may vary, as in $a\,\nabla^2 u + c(\mathbf{x})\,u = f$. Between walls a coefficient is a formula, and the equation must not leave its solution open up to a constant. ## On a shape On a [shape in the plane](regions.md) any elliptic equation of second order may have coefficients that vary, with nothing asked of them far away. It is solved directly, without an iteration. A plate with a hole that conducts better towards one side, heated evenly: ```python rim = curves.Circle((0, 0), 1, name="rim") hole = curves.Circle((0.4, 0.2), 0.25, name="hole") uneven = pm.Problem( sp.Eq(-pm.div((1 + x**2) * pm.grad(u)), 1), domain=pm.Region(x, y, rim, hole), boundary={rim: sp.Eq(u, 0), hole: sp.Eq(pm.normal_derivative(u), 0)}, ) pm.solve(uneven).plot() ``` A drift, coefficients that differ along `x` and `y`, and a term in the unknown itself may all vary from place to place and be combined: `(1 + x**2) * u.diff(x, 2) + ... - x**2 * u`. ## A mixed derivative A stationary equation may hold a mixed derivative, on a box and on a shape, with a coefficient that is constant or varies. On a shape it describes a material whose axes are a little turned. This one conducts three times as well along `y`, and on the unit disk the solution is $(1 - x^2 - y^2)/8$: ```python disk = pm.Region(x, y, curves.Circle((0, 0), 1)) turned = pm.Problem( sp.Eq(-(u.diff(x, 2) + u.diff(x, y) + 3 * u.diff(y, 2)), 1), domain=disk, boundary=sp.Eq(u, 0), ) grain = pm.solve(turned) assert abs(grain(0.3, 0.4) - (1 - 0.25) / 8) < 1e-10 ``` On a box the same kind of equation is solved by the iteration: ```python def sheared(f): return f.diff(x, 2) + f.diff(y, 2) + sp.Rational(4, 5) * f.diff(x, y) - f across = pm.Problem( sp.Eq(sheared(u), sheared(known)), domain=plate, boundary=sp.Eq(u, known) ) found = pm.solve(across) assert found.plan.method.kind == "collocation_iterative" assert abs(found(0.3, 0.7) - np.exp(0.3) * np.sin(1.4)) < 1e-9 ``` The equation must be elliptic: its second derivatives must act with the same sign in every direction, everywhere in the domain. Beside $a\,u_{xx} + c\,u_{yy}$ a term $b\,u_{xy}$ needs $b^2 < 4ac$, and an equation that is not, like $u_{xx} - u_{yy} = f$, is refused. On a box the iteration takes more steps the closer $b^2$ comes to $4ac$: 13 steps for $b = 0.5$ beside the Laplacian, 30 for $b = 1.5$, about a hundred for $b = 1.95$. A mixed derivative changes what the corners of a box do. The equation is that of a box sheared into a parallelogram, and at a corner that opens wider than a right angle the solution has a singularity, unless what is given on the faces fits. The plan then says that the solution is not resolved. ## What does not work yet - In free space, coefficients that do not settle to constants along free directions. - A medium with a sharp surface, other than through [bodies](bodies.md). - On round domains, a coefficient that varies anywhere but in the term without derivatives. - A mixed derivative in time, on a round domain, and on domains with free directions. ## See also - Reference: [what is solved where](../ref/equations.md). - [Waves in a medium](helmholtz.md#in-a-medium), and potentials in [problems in time](time.md) and in [eigenproblems](eigen.md). - Gallery: [a lens of glass](../gallery/lens-of-glass.md), [a crystal that conducts askew](../gallery/crystal-that-conducts-askew.md). - Methods: [equations whose coefficients vary in space](../methods/varying-coefficients.md), [the iteration between walls](../methods/iteration-between-walls.md), [shapes in the plane](../methods/spectral-elements.md). - [Recipes](../howto/index.md) for single tasks.