# Systems between walls This page explains how pdemagic solves a system in time on a box with walls or on a round domain: several unknowns, each with an equation in time, most of them of first order, $$ \frac{\partial u_j}{\partial t} = A_j u_j + N_j(u_1, \dots, u_n, \mathbf{x}, t), $$ and each with its own conditions on the faces. $A_j$ is a linear operator of second order with constant coefficients that acts on $u_j$ alone, such as $D_j \nabla^2 u_j$. $N_j$ is everything else: the terms that hold another unknown, terms that are nonlinear or have a coefficient that varies, and the source. The reaction and diffusion of several substances is of this kind, $$ a_t = D_a \nabla^2 a - a b^2 + F (1 - a), \qquad b_t = D_b \nabla^2 b + a b^2 - (F + k)\, b . $$ ## The method Each equation is sorted. The terms that hold its own unknown alone are read as a single equation on the domain is: its operator $A_j$ with the conditions of that unknown has modes, by [collocation](collocation.md) on a box and by [the modes of a round domain](round-domains.md) on a disk, a ring, a ball, a shell or a cylinder. So each unknown has its own modes. A substance that is held at a wall and one that the wall lets nothing of through are expanded in different functions on the same grid. In its modes, $A_j$ is a number for every coefficient, and the equations are stepped by the exponential scheme of fourth order that [one nonlinear equation](exponential.md#between-walls) is stepped by between walls: $A_j$ is exact, what the walls are given is integrated exactly, and only $N_j$ is approximated in time. The state of the scheme is the coefficients of all unknowns, one unknown after another. Each evaluation of the terms 1. turns the coefficients of every unknown into its values on the grid, with the values on the faces that its conditions give at that time, 2. evaluates all $N_j$ at the points, where one term can read several unknowns, and 3. turns each result back into the modes of its own unknown. The length of a step is chosen as for one equation: two schemes of different order are compared, and a step is repeated with half the length until they agree to the tolerance of the plan. So the stiffness of the diffusion costs nothing, and the steps follow the reaction. The grid is the coarsest on which the initial states, what is given, and all the terms $N_j$ at the initial state are resolved, with at least 32 points along the longest side. ### An equation of second order in time An unknown may obey an equation of second order in time, $\partial_t^2 u_j + \gamma_j\,\partial_t u_j = A_j u_j + N_j$: a wave beside a diffusion, or several waves. In the modes of such an unknown the equation couples each coefficient with its rate of change alone, by a matrix of two by two for the mode, as [one such equation](exponential.md#second-order-in-time-in-modes) is stepped. The state then holds a group of two for each mode of an unknown of second order, and one number for each mode of an unknown of first order, and the scheme steps all of them at once: the wave equation and the diffusion are exact, and the terms that couple them are stepped. For a string whose stiffness heat changes, and heat that the motion feeds, $u_{tt} = u_{xx} - u\,v + f$ and $v_t = \tfrac14 v_{xx} + u^2 + g$ on $[0, 1]$ with the solution $u = \cos 2t\,\sin\pi x$ and $v = e^{-t}\cos\pi x$, the planner takes 32 points and 40 steps, and both unknowns are right to $10^{-12}$. With steps of equal length, half the step leaves a 35th of the error of the string, or less. With walls that are shaken and that cool, $u = \cos 2t\,(1 + x)$ and $v = e^{-t}(2 - x)$ given on them, it takes 103 steps. ### A constraint An equation without a derivative in time, such as Poisson's equation $\nabla^2\varphi = -n$ beside an equation for $n$, is a constraint: it fixes one unknown, which has no equation in time, from the others at every time. That unknown is no part of the state. It is read as any unknown of the system is, with its own operator and its own conditions on the faces, and expanded in the modes of that operator, in which the constraint is solved mode by mode: the coefficient of a mode is the coefficient of the right-hand side, with what the faces are given, over the eigenvalue of the mode. At every evaluation of the terms that are stepped, the right-hand side of the constraint is evaluated at the points, turned into those modes, divided, and turned back, and the terms then read the unknown as they read the others. Its values on the faces may depend on time, and it has no initial value. The operator of the constraint must have no mode with the eigenvalue zero: a Laplacian with no flux through any wall has one, the constant, and is refused. A constraint without a derivative, $\varphi = R$, is a formula at every point, and is taken as well. For a substance that drifts in a potential which it makes itself, $u_t = \tfrac{1}{10}u_{xx} + u\,v - v_x + f$ and $v_{xx} = u$ on $[0, 1]$ with both held at zero and the solution $u = e^{-t}\sin\pi x$, the planner takes 20 steps, and $u$ is right to $10^{-12}$ and $v$ to $4\cdot10^{-15}$. ### An unknown that does not diffuse An unknown whose equation has no derivative in space, $w_t = c\,w + N$, has no operator with modes: nothing connects one point with the next, and no condition on a face could hold. For such an unknown the points of the grid are the modes. Its coefficients are its values, its linear part is the number $c$ at every point, and the steps above stay as they are, with nothing to transform and nothing given on a face. It is stepped in one state with the unknowns that diffuse, and may stand in their terms as they stand in its. For the model of FitzHugh and Nagumo with sources, $u_t = \tfrac1{20}u_{xx} + u - u^3 - w + f$ with no flux through the ends of $[0, 1]$ and $w_t = \tfrac12(u - 2w) + g$, whose solution is $u = e^{-t}\cos\pi x$ and $w = e^{-2t}(1 + x^2)$, up to the time 1: | Points | Error of $u$ | Error of $w$ | |---|---|---| | 6 | $6.9\cdot10^{-4}$ | $1.9\cdot10^{-4}$ | | 9 | $2.9\cdot10^{-6}$ | $7.9\cdot10^{-7}$ | | 12 | $2.3\cdot10^{-10}$ | $6.4\cdot10^{-11}$ | | 16 | $1.6\cdot10^{-11}$ | $7.6\cdot10^{-12}$ | The error of $w$ falls with that of $u$, which it follows, and ends at what the tolerance of the steps leaves. The planner takes 32 points and 40 steps. The order in time is kept whether or not what is stepped fits the walls. In the example above it does: its slope vanishes at walls that let nothing through, and half the step leaves a 33rd of the error of both unknowns. Where $u$ is held at zero and what is stepped in its equation is not zero at the walls, half the step leaves a 14th of the error of $u$ and a 20th of that of $w$. A scheme whose order rests on a term that obeys the conditions falls to the second order for $w$ there, which reads $u$ at every stage of a step: see [between walls](exponential.md#between-walls). In the same model on the unit disk, with $u$ held at zero on the rim, the planner's tolerance of $10^{-10}$ leaves $u$ right to $10^{-10}$ and $w$ to $5\cdot10^{-12}$, in 40 steps. ## How accurate it is Between two walls, $u = e^{-t}\sin\pi x$, which is held at the walls, and $v = e^{-2t}\cos\pi x$, which has no slope there, solve $u_t = \tfrac14 u_{xx} + u v + f$ and $v_t = \tfrac12 v_{xx} - u^2 + g$ for the right $f$ and $g$. To the time 1: | Points | Error | | Tolerance | Steps | Error | |---|---|---|---|---|---| | 6 | $1\cdot10^{-3}$ | | $10^{-6}$ | 20 | $7\cdot10^{-10}$ | | 8 | $1\cdot10^{-5}$ | | $10^{-8}$ | 33 | $7\cdot10^{-11}$ | | 10 | $9\cdot10^{-8}$ | | $10^{-10}$ | 80 | $7\cdot10^{-13}$ | | 12 | $4\cdot10^{-10}$ | | | | | | 16 | $7\cdot10^{-13}$ | | | | | The error falls faster than any power of the number of points. On the right the grid has 32 points, which is the planner's choice. A test that shares nothing with the method: between two walls that let nothing through, a start that is even about both walls evolves as on the periodic domain of twice the length, which is solved in [Fourier space](coupled.md). For the reaction above, with a seed that grows into a pulse, the two agree to $4\cdot10^{-9}$ at the time 50, after 265 steps. A reaction $a + b \to 2b$ changes neither the sum of the two substances nor lets a closed wall anything out. In a ball, a ring and a cylinder the integral of $a + b$ is kept to $10^{-13}$. ## What it costs Each evaluation transforms every unknown to the grid and back, so the cost of a step grows with the number of unknowns. A pair of substances between two walls takes 0.03 s to the time 1, in a ball on $32 \times 32 \times 64$ points 10 s, and the [reaction in a dish](../gallery/reaction-in-a-dish.md) of the gallery, on $64 \times 128$ points to the time 3000, 10 s with a tolerance of $10^{-4}$. ## Limits - **One equation in time for each unknown**, of first or second order. An unknown without a derivative in space has one of first order. An equation without a derivative in time is a constraint, and fixes one unknown. - **An unknown diffuses along every direction with faces, or along none.** One that diffuses along some of them alone is refused. And at least one unknown diffuses: a system of ordinary equations at every point is no problem between walls. - **The terms that couple are stepped.** A second derivative of another unknown in an equation, cross-diffusion, is taken on a box, but it is stiff, and the steps become short. On a round domain a term that is stepped holds the unknowns themselves, without derivatives. - **Each condition holds one unknown.** - **Values on the faces that depend on time** are taken, and are integrated exactly in every step: the example above with $u = e^{-t}(1 + x + \sin\pi x)$ given on the walls takes 80 steps and reaches $2\cdot10^{-11}$. - **Arrays** are taken for what is given, on one grid, which is then the grid of the solve: the planner cannot judge it without the values. - **Stationary systems and eigenproblems** for several unknowns are not solved between walls. On a [region of the plane](element-systems.md) stationary systems are.