# Boxes with walls A box is an interval, a rectangle, or its like in any number of dimensions, with a wall at each end. This page is about the box itself: its faces, its periodic directions and its grid. What you state on the faces is on the page about [boundary conditions](boundary-conditions.md). ```python import numpy as np import sympy as sp import pdemagic as pm x, y = sp.symbols("x y", real=True) u = sp.Function("u")(x, y) ``` ## A box and its faces A `Box` is given by its sides: each coordinate with the two ends of the box along it. ```python box = pm.Box({x: (0, 1), y: (0, 1)}) assert box.faces == ((x, 0.0), (x, 1.0), (y, 0.0), (y, 1.0)) ``` A face is named by the coordinate that is constant on it and the end it lies at: `(x, 0)` is the face where `x` is 0. The order of the coordinates is the order in which the arrays of a solution are indexed. Every face needs a condition. This plate is kept at zero on three faces and heated along the fourth: ```python conditions = dict.fromkeys(box.faces, sp.Eq(u, 0)) conditions[y, 1] = sp.Eq(u, sp.sin(sp.pi * x)) plate = pm.Problem(sp.Eq(pm.laplacian(u), 0), domain=box, boundary=conditions) heated = pm.solve(plate) heated.plot() X, Y = heated.mesh() exact = np.sin(np.pi * X) * np.sinh(np.pi * Y) / np.sinh(np.pi) assert abs(heated[u] - exact).max() < 1e-10 ``` ## Walls and periodic directions A box can be periodic along some of its coordinates. It has no faces there. This channel repeats along `x` and has a wall at `y = 0` and at `y = 1`: ```python channel = pm.Box({x: (0, 2), y: (0, 1)}, periodic=[x]) assert channel.faces == ((y, 0.0), (y, 1.0)) flow = pm.Problem( sp.Eq(pm.laplacian(u), 0), domain=channel, boundary={(y, 0): sp.Eq(u, sp.cos(sp.pi * x)), (y, 1): sp.Eq(u, 0)}, ) pm.solve(flow).plot() ``` A box that is periodic along every coordinate has no walls at all. It is solved as a [periodic domain](free-space.md), which `pm.Space` states as well. ## The grid Along a coordinate with faces the grid points are Chebyshev points. They include both ends and crowd towards them. `solution.axes` returns them: ```python xs, ys = heated.axes assert xs[0] == 0.0 and xs[-1] == 1.0 assert xs[1] - xs[0] < xs[len(xs) // 2] - xs[len(xs) // 2 - 1] ``` The planner solves the problem on one grid after the other and takes the coarsest one on which the solution is resolved. A smooth solution needs few points. One with a kink in a corner needs more, and the plan says how well it was resolved. ## Values between the points Call the solution with coordinates for its value anywhere in the box. The value comes from the polynomial through the points of the grid, which is what the method solved with, so it is as accurate as the values on the grid: ```python middle = heated(0.5, 0.5) along_a_line = heated(np.linspace(0, 1, 7), 0.25) assert along_a_line.shape == (7,) assert np.isnan(heated(1.5, 0.5)) # outside the box ``` `heated.integral()` is the integral of the solution over the box, by the weights of the same points. [Solutions](solutions.md) has more on both. ## Arrays on a box An array, in the source or on a face, is given at the points of the grid, which the box tells you for any shape: ```python grid_x, grid_y = box.axes((16, 16)) assert grid_x[0] == 0.0 and grid_x[-1] == 1.0 ``` An array for a face is given like any other: on the whole grid. Only its values on the face are read. [Data from arrays](data.md) shows how. ## What is solved on a box A box takes equations of second order, and the plate equation, which is of fourth: - [Stationary equations](stationary.md): Poisson's and Laplace's equation, screened ones, diffusion with drift. Also [waves of one frequency](helmholtz.md), [media and potentials](varying.md), [nonlinear equations](nonlinear.md) and [beams and plates](plates.md). - [Problems in time](time.md): heat, waves and the Schrödinger equation between walls. - [Modes](modes.md): the tones of a string, a plate and a room. The full list is in the reference: [what is solved on boxes with walls](../ref/equations-boxes.md). ## What does not work yet - A box that is bounded along some directions and free along others. - Stationary systems of equations on a box. In time a [system](systems.md#between-walls) is solved. - An array for one face alone: it is given on the whole grid. ## See also - [Boundary conditions](boundary-conditions.md): what may be stated on a face. - [The reference of `Box`](../ref/domains.md). - [A heated plate](../intro/tutorial-2-boundaries.md), the tutorial on boxes. - Gallery: [a heated plate](../gallery/heated-plate.md), [a drum](../gallery/drum.md), [the tones of a room](../gallery/tones-of-a-room.md). - Method: [boxes with boundary conditions](../methods/collocation.md). - Recipes: [make one direction periodic](../howto/periodic-direction.md), [choose the grid yourself](../howto/choose-the-grid-yourself.md), and [the others](../howto/index.md).