# Accuracy and diagnostics Every solution reports how far it can be trusted. The plan says what the planner aimed for and whether it got there, and the **diagnostics** say afterwards how the solve went. This page explains what to look at, what "resolved" means, and what to do when a number is not small. ```python 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) problem = pm.Problem( sp.Eq(pm.laplacian(u), -sp.exp(-(x**2 + y**2) / 2)), domain=pm.FreeSpace(x, y), ) ``` ## How accurate the methods are Most methods are spectral: the error falls faster than any power of the grid spacing, and ten digits are common on modest grids. Two are of second order, where halving the grid spacing divides the error by four: finite cylinders with vacuum outside, and [bodies with sharp surfaces](bodies.md). In time, linear equations are propagated exactly. Only nonlinear terms and terms with coefficients that vary are stepped, with an error that a tolerance bounds. The reference has [the numbers for each method](../ref/accuracy.md). ## What resolved means A spectral method writes a function as a sum of waves or polynomials. The grid resolves the function when the highest terms of that sum, the finest that the grid holds, are tiny beside the largest. The planner aims at $10^{-10}$ for most problems, and takes the coarsest grid that reaches it. Two diagnostics report what was reached: `spectral_tail` for what is given, and `solution_tail` for the solution. ```python solution = pm.solve(problem) diagnostics = solution.diagnostics assert diagnostics["spectral_tail"] < 1e-10 assert diagnostics["source_boundary_ratio"] < 1e-11 ``` A grid that is too coarse shows there, and so does a box that is too narrow. In free space the method assumes that the source vanishes outside the box, and `source_boundary_ratio` is the source on the faces of the box, relative to its maximum: ```python coarse = pm.plan(problem) coarse.grid.shape = (12, 12) assert pm.solve(problem, coarse).diagnostics["spectral_tail"] > 0.1 narrow = pm.plan(problem) narrow.grid.lower = [-3.0, -3.0] narrow.grid.upper = [3.0, 3.0] assert pm.solve(problem, narrow).diagnostics["source_boundary_ratio"] > 1e-3 ``` When the planner cannot resolve a problem within its means, it does not fail. It solves on the best grid it has, and a note of the plan says `NOT resolved`, with what was reached and the entry to set. ## The diagnostics that matter most `solution.diagnostics` is a dictionary. Entries that do not apply to the method that was used are `None`. | Entry | Meaning | Good values | |---|---|---| | `spectral_tail` | How far the spectrum of the source has decayed at high frequencies. On a box with boundary conditions it covers the values given on the faces too. | tiny, or the grid is too coarse | | `solution_tail` | The same for the solution: between walls, on round domains, on a shape, for coefficients that vary and after time stepping. | tiny, or the solution has outgrown the grid, or has a singularity at a corner | | `source_boundary_ratio` | The source on the faces of the box, relative to its maximum. | tiny | | `iterations`, `residual` | For an iteration: the steps it took and the residual it reached. | residual at or below the tolerance | | `steps`, `rejected_steps` | For time stepping: the steps that were taken, and those that had to be repeated. | informational | | `removed_source_mean` | The mean that was removed from a source where the solution is fixed up to a constant only. | zero, unless you rely on it | The reference explains [every diagnostic](../ref/diagnostics.md). When a diagnostic is not small, the remedy is in the [plan](planner.md): a wider box for a source that reaches the faces, a finer grid for a spectrum that has not decayed. On a shape in the plane the grid plays no part in that: raise `method.degree` or lower `method.spacing`. ## Corners that point inwards A spectral method is as good as the solution is smooth. At a corner of the boundary that points into the domain, like the one of an L-shape, the solution of almost every problem has a singularity: its slope grows without bound towards the corner. On a [shape in the plane](regions.md) the planner sees such corners, and lets the elements shrink towards them, layer by layer, each half the size of the one before: ```python corners = [(0, 0), (1, 0), (1, 1), (-1, 1), (-1, -1), (0, -1)] membrane = pm.Problem( sp.Eq(-pm.laplacian(u), 1), domain=pm.Region(x, y, curves.Polygon(*corners)), boundary=sp.Eq(u, 0), ) layered = pm.plan(membrane) assert layered.method.layers == 18 assert "the solution has a singularity" in layered.notes[3] sagging = pm.solve(membrane, layered) assert abs(sagging(-0.5, 0.5) - 0.131052962031) < 1e-10 ``` With the layers the solution is right to ten digits and more. Without them, about four digits are left: ```python plain = layered.copy() plain.method.layers = 0 rough = pm.solve(membrane, plain) assert 1e-6 < abs(rough(-0.5, 0.5) - 0.131052962031) < 1e-4 ``` The diagnostic `solution_tail` stays above its aim here, at about $10^{-6}$, although the values are right: what is left is the singularity, inside the innermost layer. The plan says so in a note. The same happens where a condition changes from a value to a slope in the middle of a straight side, and it is treated in the same way. In a problem in time the mesh has no layers yet, and a corner that points inwards leaves three or four digits there. On a [box](boxes.md) the solution often has a kink in a corner, as a membrane under an even load has. The planner then stops at a grid beyond which finer ones gain little, and says how far the spectrum has fallen: ```python box = pm.Box({x: (0, 1), y: (0, 1)}) square = pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=box, boundary=sp.Eq(u, 0)) print(pm.plan(square).notes[2]) ``` ```text Grid: a candidate on which the spectrum of the solution has fallen to 4.2e-8 at high frequencies. Finer grids gain little: the solution has a kink or a singularity, as it often does at the corners and edges of a box, or where what is given on a face jumps. Set grid.shape to refine. ``` ## Waves on a shape Heat smooths whatever it starts from, and the solution is right to many digits. A wave keeps every detail that it starts with, and sends it across the region for good. On a shape in the plane a drum that is struck is right to three or four digits, and `solution_tail` tells how well the elements hold the solution at the end. For more, raise `plan.method.degree`. The cost grows fast: the modes of the shape come from one calculation that takes eight times as long for twice the points. The planner stays within a few seconds and says in the plan when that leaves something unresolved. A plan may ask for more, up to about 6 000 points, which take more than half a minute. A pulse that is narrower than about a tenth of the region is beyond that. ## Errors A problem that cannot be solved is not solved badly: pdemagic raises one of four errors, all of them subclasses of `pm.PdemagicError`, with a message that names what it found. | Error | Meaning | |---|---| | `InvalidProblemError` | The problem is inconsistent or incomplete, or has no solution. | | `UnsupportedProblemError` | The problem is fine, and pdemagic cannot solve it yet. | | `InvalidPlanError` | The plan does not fit the problem. | | `ConvergenceError` | An iteration or a time stepping did not reach its tolerance. No result is returned. | ```python try: pm.solve(pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=box)) except pm.InvalidProblemError as error: print(error) ``` ```text `u` has no boundary condition on the face `x` = 0 ``` The reference says [what each error means](../ref/errors.md) and what to do about it. ## Threads pdemagic uses all cores of the machine for its heavy loops. The results do not depend on how many there are. Small problems do not use threads: sharing their work would cost more than it saves. On a shape in the plane, the sparse factorization and the modes for problems in time use one core so far. To use fewer cores, set the environment variable `RAYON_NUM_THREADS` before Python starts: ```bash RAYON_NUM_THREADS=2 python my_script.py ``` ## See also - [The planner and its plan](planner.md), for how to change what a diagnostic complains about. - Tutorial: [What pdemagic decided](../intro/tutorial-4-plans.md). - Reference: [every diagnostic](../ref/diagnostics.md), [how accurate it is](../ref/accuracy.md), [the errors](../ref/errors.md) and the [known limits](../dev/limits.md). - Methods: [shapes with corners](../methods/element-corners.md), and each [method page](../methods/index.md) for measured errors.