# Nonlinear equations in time An equation in time may hold terms that are nonlinear in the unknown, or linear with coefficients that vary: a wave that steepens, a soliton, a population that grows. Such terms are stepped in time, with an error that a tolerance bounds, while the linear part of the equation stays exact. This page says what that means for the plan and for the diagnostics. ```python import numpy as np import sympy as sp import pdemagic as pm from pdemagic import curves x, y, t = sp.symbols("x y t", real=True) ``` ## A term that is stepped The problem is stated as any [problem in time](time.md). Burgers' equation on a periodic interval, $v_t + v\,v_x = \tfrac{1}{10} v_{xx}$: ```python ring = pm.Space(x, periodic={x: (0, 2 * sp.pi)}) v = sp.Function("v")(x, t) burgers = pm.Problem( sp.Eq(v.diff(t) + v * v.diff(x), v.diff(x, 2) / 10), domain=ring, time=(t, 0.0, 2.0), initial={v: sp.sin(x)}, ) steepened = pm.solve(burgers) assert steepened.plan.method.kind == "exponential_runge_kutta" ``` The planner splits the equation in two. The linear part with constant coefficients, here the diffusion, is propagated exactly, as in a linear equation. Everything else is stepped: the term $v\,v_x$ here. The length of a step is then limited by how fast that term changes, and not by the diffusion. ## Steps and their tolerance pdemagic chooses the steps during the solve, so that each errs by less than a tolerance, relative to the size of the solution. The tolerance is an entry of the plan, `method.tolerance`, and the planner sets it to $10^{-10}$. The diagnostics count the steps that were taken, and those that had to be repeated with a shorter step: ```python assert steepened.plan.method.tolerance == 1e-10 assert steepened.diagnostics["steps"] >= 20 assert steepened.diagnostics["rejected_steps"] < 10 ``` A looser tolerance takes fewer steps, which is plenty for a picture. The errors of the steps add up, so the error at the end may be larger than the tolerance. If the solution ceases to exist, as that of $u_t = u^2$ does, the steps shrink without end. pdemagic then stops with a `ConvergenceError`, when a step would need more than `method.max_steps` parts of the time between two recorded times. ## When the solution outgrows the grid A nonlinear equation creates finer structures than its initial state has, and the planner knows only the initial state. The diagnostic `solution_tail` says how much of the solution lies in the finest waves of the grid at the end: ```python # The sine steepens into a front, which the planner's 32 points do not hold. assert steepened.diagnostics["solution_tail"] > 1e-2 ``` Here that is 4 %, far too much, so the grid has to be finer: ```python finer = pm.plan(burgers) finer.grid.shape = (256,) resolved = pm.solve(burgers, finer) assert resolved.diagnostics["solution_tail"] < 1e-8 resolved.plot() ``` The sine leans forward into a front, which the viscosity keeps from breaking. Look at `solution_tail` after every nonlinear solve. ## Solitons The linear part may be of any order on a periodic domain. In the Korteweg–de Vries equation, $u_t + 6\,u\,u_x + u_{xxx} = 0$, it is a third derivative, which is exact like any other. A soliton moves with the speed 1 and keeps its shape: ```python line = pm.Space(x, periodic={x: (-30, 30)}) u = sp.Function("u")(x, t) kdv = pm.Problem( sp.Eq(u.diff(t) + 6 * u * u.diff(x) + u.diff(x, 3), 0), domain=line, time=(t, 0.0, 4.0), initial={u: 1 / (2 * sp.cosh(x / 2) ** 2)}, ) soliton = pm.solve(kdv) (points,) = soliton.axes moved = 1 / (2 * np.cosh((points - 4) / 2) ** 2) assert abs(soliton[u][-1] - moved).max() < 1e-9 ``` With a [complex unknown](complex.md) the same holds for the nonlinear Schrödinger equation, $i\,\psi_t = -\tfrac12\psi_{xx} - |\psi|^2\psi$, whose soliton stands still and turns its phase: ```python psi = sp.Function("psi")(x, t) light = pm.Problem( sp.Eq(sp.I * psi.diff(t), -psi.diff(x, 2) / 2 - sp.Abs(psi) ** 2 * psi), domain=line, time=(t, 0.0, 1.0), initial={psi: 1 / sp.cosh(x)}, ) pulse = pm.solve(light) (points,) = pulse.axes assert abs(pulse[psi][-1] - np.exp(0.5j) / np.cosh(points)).max() < 1e-9 ``` ## Between walls On a [box](boxes.md) with boundary conditions the terms are stepped in the same way. A population that grows and spreads between two walls it cannot cross, $p_t = \tfrac{1}{100} p_{xx} + p\,(1 - p)$: ```python p = sp.Function("p")(x, t) spreading = pm.Problem( sp.Eq(p.diff(t), p.diff(x, 2) / 100 + p * (1 - p)), domain=pm.Box({x: (0, 1)}), time=(t, 0, 6), initial={p: sp.exp(-100 * x**2)}, boundary=sp.Eq(p.diff(x), 0), ) front = pm.solve(spreading) assert front.plan.method.kind == "exponential_runge_kutta" assert front[p].min() > -1e-6 and front[p].max() < 1 + 1e-6 front.plot() ``` On a [round domain](round.md) the terms that are stepped may hold the unknown, but not its derivatives. ## Nonlinear waves between walls An equation of second order in time takes such terms too, on a box, a round domain and a shape. In the sine-Gordon equation, $u_{tt} = u_{xx} - \sin u$, a kink is a step of $2\pi$ that moves and keeps its shape. Here one runs against a free end: ```python u = sp.Function("u")(x, t) kink = 4 * sp.atan(sp.exp((x - 3 * t / 5) / sp.Rational(4, 5))) running = pm.Problem( sp.Eq(u.diff(t, 2), u.diff(x, 2) - sp.sin(u)), domain=pm.Box({x: (-12, 12)}), time=(t, 0, 40), initial={u: kink.subs(t, 0), u.diff(t): kink.diff(t).subs(t, 0)}, boundary=sp.Eq(u.diff(x), 0), ) turned = pm.solve(running) assert turned.plan.method.kind == "exponential_runge_kutta" assert abs(turned[u][0][-1] - 2 * np.pi) < 1e-5 assert abs(turned[u][-1][-1] + 2 * np.pi) < 1e-5 ``` The end was at $2\pi$ and is at $-2\pi$ once the kink has come back, as an antikink. The wave equation is exact, for the unknown and its rate of change together, and $\sin u$ is stepped. What a wall is given, also where it depends on time, is integrated exactly in every step. ## On a shape On a [shape in the plane](regions.md) the terms that are stepped may hold the unknown and its first derivatives: a reaction, a flow that turns, the term of Burgers' equation. The same population on a dish, with no flux through the rim: ```python q = sp.Function("q")(x, y, t) dish = pm.Problem( sp.Eq(q.diff(t), pm.laplacian(q) / 20 + q * (1 - q)), domain=pm.Region(x, y, curves.Circle((0, 0), 1)), time=(t, 0, 3), initial={q: sp.exp(-4 * ((x + 0.4) ** 2 + y**2)) / 2}, boundary=sp.Eq(pm.normal_derivative(q), 0), ) colony_plan = pm.plan(dish) assert colony_plan.method.kind == "element_stepping" colony_plan.method.degree = 6 colony = pm.solve(dish, colony_plan) assert colony.diagnostics["steps"] > 50 assert 0.0 < np.nanmin(colony[q][-1]) < np.nanmax(colony[q][-1]) < 1.0 colony.plot(time=1.5) ``` Stepping costs more per point than propagation: the planner stays below 1 700 points, and the degree 6 that this example sets is enough for a start as smooth as this one. A term with a derivative makes the steps smaller, the more so the stronger it is against the diffusion and the higher the degree. Derivatives above the first order are not stepped on a shape. ## Where it works Nonlinear terms need a domain that is periodic in every direction, or a box, a round domain or a shape with boundary conditions. In free space a term that is linear in the unknown is stepped if its coefficient settles to a constant away from where it acts: a source of heat in one place, `2 * sp.exp(-(x - 2)**2) * w`, or a barrier in the Schrödinger equation. [Complex unknowns](complex.md) has a particle that meets one. On a periodic domain a nonlinear equation of second order in time is solved as a [system](systems.md). With free directions it is not solved. ## See also - [Problems in time](time.md), for the linear part and the recorded times, and [Accuracy and diagnostics](accuracy.md). - [The planner and its plan](planner.md), for how to set a tolerance or a grid. - Gallery: [a wave that steepens](../gallery/wave-that-steepens.md), [solitons that pass](../gallery/solitons-that-pass.md), [a soliton of light](../gallery/soliton-of-light.md), [a kink and an antikink](../gallery/kink-and-antikink.md), [a kink at a free end](../gallery/kink-at-a-free-end.md), [patterns from a reaction](../gallery/patterns-from-a-reaction.md) and [a front that advances](../gallery/front-that-advances.md). - Methods: [nonlinear equations in time](../methods/exponential.md), and [shapes in the plane, in time](../methods/elements-in-time.md) for the terms that are stepped there.