# Iteration between walls A box or a round domain with boundary conditions is solved directly, in the modes of each axis, where the modes hold the equation: see [boxes with boundary conditions](collocation.md). This page explains how a stationary equation that they do not hold is solved by an iteration on that solver: one with coefficients that vary, with a mixed derivative, or with a nonlinear term. ## Coefficients that vary A stationary equation whose coefficients vary, $\sum_j c_j(\mathbf{x})\,D_j u = f$ with derivatives $D_j$ up to the second order, has no modes that are products. It is solved by an iteration on the [solver in the modes](collocation.md#modes), as an equation in free space is solved by an iteration on the solver in Fourier space ([coefficients that vary](varying-coefficients.md)). **The reference.** Every coefficient that varies has its mean over the domain stand for it. With the terms whose coefficients are constant, these means make an operator $L_0$ that the modes hold. The whole operator is $L u = L_0 u + R(u)$: the remainder $R$ holds every term that varies, less what stands for it in $L_0$. **The scaling.** The equation is divided by $w(\mathbf{x})$, the coefficient of the second derivatives relative to its mean. For $\nabla\cdot(\varepsilon\nabla u) = f$ that is $\varepsilon/\bar\varepsilon$, and what is left, $\bar\varepsilon\,\nabla^2 u + \bar\varepsilon\,\nabla(\ln\varepsilon) \cdot\nabla u = \bar\varepsilon f/\varepsilon$, has second derivatives with constant coefficients. What varies is of lower order, and that is what makes the iteration fast whatever the contrast of the medium. **The iteration.** With the effective source $s = L_0 u$, and $u = L_0^{-1}s$ the solution of the reference equation with what is given on the faces, the equation reads $$ \frac{s + R(L_0^{-1} s)}{w} = \frac{f}{w} . $$ It is linear in $s$ and is solved by GMRES. Each step solves once with $L_0$, directly, and evaluates the remainder at the points of the grid with the derivatives of collocation. A medium inside a divergence is taken as it is written: the flux $\varepsilon\,\partial_i u$ is formed at the points and differentiated again. On the unit square, with the value given on its sides, for a solution that is known: | Equation | Grid | Steps | Largest error | Time | |---|---|---|---|---| | $\nabla\cdot(\varepsilon\nabla u) = f$, $\varepsilon = 10^{xy}$ | $24^2$ | 10 | $10^{-12}$ | 0.01 s | | The same, $\varepsilon = 100^{xy}$ | $24^2$ | 17 | $7\cdot10^{-13}$ | 0.02 s | | The same, $\varepsilon = 1000^{xy}$ | $32^2$ | 27 | $2\cdot10^{-12}$ | 0.03 s | | $\varepsilon = 2 + \sin 6x\cos 5y$ | $32^2$ | 11 | $10^{-12}$ | 0.03 s | | $(1 + x^2)u_{xx} + (2 + \cos y)u_{yy} + x\,u_y - (1 + y)u = f$ | $24^2$ | 11 | $10^{-11}$ | 0.01 s | | $\nabla^2 u + 20(1 + \tfrac12\sin 3x)\,u = f$ | $24^2$ | 7 | $9\cdot10^{-11}$ | 0.01 s | | A medium in the unit cube | $24^3$ | 7 | $6\cdot10^{-14}$ | 0.2 s | | The unit disk, $\nabla^2 u - (1 + r^2\cos^2\varphi)\,u = f$ | $16\times32$ | 4 | $5\cdot10^{-12}$ | 0.01 s | The error falls with the number of points as for constant coefficients: the method stays spectral. The planner measures, beside the solution and what is given, the remainder of the solution, in which the coefficients show where they matter. On a round domain the derivatives of a field on the grid are not taken. There the term without derivatives may vary, and the second derivatives keep their constant. ### A mixed derivative A term $b\,\partial_i\partial_j u$ has nothing that the modes hold. No mean stands for it in $L_0$: all of it is in the remainder, whether $b$ varies or not, and the scaling leaves it alone. That changes what the iteration has to do. The remainder of a medium is of lower order than $L_0$ once the equation is scaled. A mixed derivative is of the same order, and $R\,L_0^{-1}$ does not become small for short waves. For $a\,u_{xx} + b\,u_{xy} + c\,u_{yy}$ a wave with the wave numbers $k_x$ and $k_y$ has $$ \left|\frac{b\,k_x k_y}{a\,k_x^2 + c\,k_y^2}\right| \le \frac{|b|}{2\sqrt{ac}} , $$ which is below one exactly when the equation is elliptic. So the iteration converges for every elliptic equation, in a number of steps that does not grow with the grid, and more slowly the closer the equation comes to the bound. On the unit square, with the value given on its sides, for $\nabla^2 u + b\,u_{xy} - u = f$ with a solution that is known: | $b$ | Grid | Steps | Largest error | Time | |---|---|---|---|---| | 0.5 | $24^2$ | 13 | $6\cdot10^{-13}$ | 0.01 s | | 1 | $24^2$ | 20 | $10^{-12}$ | 0.01 s | | 1.5 | $24^2$ | 30 | $4\cdot10^{-12}$ | 0.01 s | | 1.8 | $64^2$ | 53 | $4\cdot10^{-12}$ | 0.15 s | | 1.95 | $64^2$ | 98 | $10^{-11}$ | 0.24 s | | $\cos 3x$, in the medium $1 + x^2$ | $24^2$ | 17 | $2\cdot10^{-12}$ | 0.03 s | | The unit cube, with $\tfrac12 u_{xy} - 0.6\,u_{yz}$ | $16^3$ | 15 | $9\cdot10^{-14}$ | 0.09 s | **Ellipticity.** The coefficients of the second derivatives make a symmetric matrix at every point, with half of each mixed coefficient off its diagonal. Before the solve it is looked at on eight samples along each axis, by its real part: it must have one sign, which is whether it or its negative has a Cholesky factor. An equation that fails is refused. The iteration would not converge for it, and for a hyperbolic equation the conditions on the faces would not pose a problem that has one solution. **Corners.** A change of coordinates that removes the mixed derivative shears the box. For $\nabla^2 u + b\,u_{xy}$ the square becomes a parallelogram whose corners open to $\arccos(\mp b/2)$: 120° and 60° for $b = 1$. At a corner of the angle $\omega$ a solution that vanishes on both sides behaves like $r^{\pi/\omega}$, which is $r^{3/2}$ at 120°: stronger than the singularity of the square itself. For $\nabla^2 u + u_{xy} = -1$ with zero on the sides: | Points per axis | Value at the centre | Spectrum of the solution falls to | |---|---|---| | 17 | 0.07777064 | $4 \cdot 10^{-4}$ | | 33 | 0.07776972 | $10^{-5}$ | | 65 | 0.07776968 | $5 \cdot 10^{-7}$ | The planner judges such a grid by the solution, and stops where finer grids gain little: at 64 points, with a plan that says so. A [region of the plane](spectral-elements.md) takes the same equation with a mesh that [shrinks towards the corners](element-corners.md). ### Nonlinear equations A stationary equation with a term that is nonlinear in the unknown, $L u + N(u, \mathbf{x}) = f$, is solved by Newton's method. A step solves the linearized equation for the next iterate itself, $$ \big(L + N'(u_k)\big)\,u_{k+1} = f - N(u_k) + N'(u_k)\,u_k , $$ which is a linear equation with one more coefficient that varies, the slope $N'(u_k)$ at the points of the grid. It is solved by the iteration above, on the same reference operator in every step. That iteration returns the effective source of $u_{k+1}$ with it, which is what the operator of the modes makes of $u_{k+1}$. So the residual of the nonlinear equation at the new iterate, $L_0u + R(u) + N(u) - f$, is known without applying the operator, also along a step that is taken in part: from the second step on, a step that does not reduce the residual is halved until it does. The method stops when the residual has fallen to $10^{-10}$ of the larger of the source and the nonlinear term. For solutions that are known: | Equation | Grid | Steps of Newton's method | Steps inside them | Largest error | Time | |---|---|---|---|---|---| | $\nabla\cdot(\varepsilon\nabla u) - u^3 = f$ on the unit square, $\varepsilon = 1 + xy$ | $24^2$ | 4 | 35 | $3\cdot10^{-13}$ | 0.02 s | | $\nabla^2 u - u^3 = f$ in the unit cube | $24^3$ | 3 | 11 | $4\cdot10^{-14}$ | 0.09 s | | $\nabla^2 u - e^u = f$ in the unit ball | $24\times24\times48$ | 4 | 24 | $3\cdot10^{-13}$ | 0.3 s | | $u'' + e^u = 0$ on a string, from zero | 24 | 3 | 15 | $3\cdot10^{-15}$ | 0.004 s | The reference operator is the linear part of the equation. Where that part alone leaves the solution open up to a constant, with the slope given on every face and no linear term that holds the unknown, it has no inverse to iterate on, and the equation is refused. ## Limits - **In a stationary equation coefficients may vary**, by a formula and not by an array. On a round domain only the term without derivatives may. - **A coefficient of the second derivatives that vanishes on a face** leaves a solution with a singularity there. It is solved, and the plan says that it is not resolved. - **A solution that is fixed up to a constant** is not taken with coefficients that vary or with a mixed derivative. - **A mixed derivative** is taken in a stationary equation on a box. It is not taken in time, in an eigenproblem, or on a round domain. Close to where the equation stops being elliptic the iteration is slow: beyond $b^2 = 0.99\cdot4ac$ it needs more than the two hundred steps that it has by itself, and `method.max_iterations` raises them. - **A nonlinear term is a function of the unknown and of the position**, by a formula: no derivative of the unknown inside it, no array, and for a complex unknown no magnitude. The linear part of the equation must fix the solution by itself.