# A heated plate: a box and its faces In [part 1](tutorial-1-first-problem.md) the charges had all of space around them. Most problems live between walls. This part puts a square plate between four edges and says on each of them what holds there: the temperature, or the heat that flows through it. ## Step 1: a box and its faces ```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` is given by its sides: each coordinate with the two ends of the box along it. In two dimensions it is a rectangle, and here the unit square: ```python box = pm.Box({x: (0, 1), y: (0, 1)}) print(box.faces) ``` ```text ((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 left edge of the plate. ## Step 2: values on the faces A boundary condition is an equation that holds on a face. The plain kind gives the value of the unknown there. Pass the conditions as a mapping from each face to its equation. The temperature `u` of a plate at rest obeys Laplace's equation, $\nabla^2 u = 0$. This plate is kept at zero on three edges and heated along the fourth, most in its middle: ```python conditions = { (x, 0): sp.Eq(u, 0), (x, 1): sp.Eq(u, 0), (y, 0): sp.Eq(u, 0), (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() ``` The heat enters at the top and fades towards the three cold edges. ## Step 3: check the result This problem has an exact solution, $\sin(\pi x)\,\sinh(\pi y)/\sinh(\pi)$, so you can see how good the result is: ```python X, Y = heated.mesh() exact = np.sin(np.pi * X) * np.sinh(np.pi * Y) / np.sinh(np.pi) print(heated) print(abs(heated[u] - exact).max() < 1e-10) ``` ```text Solution(unknowns=['u'], grid=24x24) True ``` Ten digits with 24 points along each side. The points are not equally spaced: they lie closer together towards the faces. On a box you are not bound to the grid. Call the solution with coordinates for its value at any point, here the centre of the plate: ```python print(round(float(heated(0.5, 0.5)), 6)) ``` ```text 0.199268 ``` ## Step 4: a slope in place of a value A condition can give the derivative of the unknown across the face in place of its value. Heat flows along the slope of the temperature, so a face that is insulated has the slope zero. Insulate the bottom edge and leave the rest as it was: ```python conditions[y, 0] = sp.Eq(u.diff(y), 0) insulated = pm.solve( pm.Problem(sp.Eq(pm.laplacian(u), 0), domain=box, boundary=conditions) ) insulated.plot() print(round(float(insulated(0.5, 0.0)), 6)) ``` ```text 0.086267 ``` The bottom edge is no longer at zero: it takes the temperature that the heat from above gives it, $1/\cosh(\pi)$ in the middle. A condition may also mix the value and the slope: step 6 has one. In books a value is called a Dirichlet condition, a slope a Neumann condition and a mix a Robin condition. ## Step 5: the slope along the normal `u.diff(y)` is the slope along the coordinate. At an upper face that is the slope out of the box. At a lower face it points into the box, and the sign turns around: heat that leaves through the face `y = 0` at the rate `q` is `sp.Eq(u.diff(y), q)`, and through the face `y = 1` it is `sp.Eq(u.diff(y), -q)`. `pm.normal_derivative(u)` spares you the signs. It is the slope along the normal that points out of the domain, on whatever face it is used. ## Step 6: one condition for all faces When the same equation holds on every face, pass it alone. This plate is heated evenly over its whole area, $-\nabla^2 u = 1$, and kept cold all around: ```python warmed = pm.Problem(sp.Eq(-pm.laplacian(u), 1), domain=box, boundary=sp.Eq(u, 0)) cold_edges = pm.solve(warmed) cold_edges.plot() ``` With `pm.normal_derivative` a condition on the slope can stand for all faces too. Now every edge loses heat in proportion to its temperature: ```python losing = pm.Problem( sp.Eq(-pm.laplacian(u), 1), domain=box, boundary=sp.Eq(pm.normal_derivative(u) + 2 * u, 0), ) warm_edges = pm.solve(losing) warm_edges.plot() print(round(float(cold_edges(0.5, 0.5)), 6), round(float(warm_edges(0.5, 0.5)), 6)) ``` ```text 0.073671 0.205421 ``` The plate that only loses some heat at its edges is almost three times as warm in the middle. ## Step 7: a face that was forgotten Every face needs its condition. If one is missing, pdemagic does not guess. It says which: ```python forgetful = pm.Problem( sp.Eq(pm.laplacian(u), 0), domain=box, boundary={(x, 0): sp.Eq(u, 0)} ) try: pm.solve(forgetful) except pm.InvalidProblemError as error: print(error) ``` ```text `u` has no boundary condition on the face `x` = 1 ``` ## What you have learned - A `pm.Box` has walls. Each face is named by a coordinate and an end, as in `(y, 1)`. - `boundary=` takes a mapping from each face to an equation, or one equation for all faces. - A condition gives the value, the slope, or a mix of both. `pm.normal_derivative(u)` is the slope out of the domain. - On a box, `solution(x, y)` gives the value at any point. [Boxes with walls](../topics/boxes.md) and [Boundary conditions](../topics/boundary-conditions.md) have the rest: periodic directions, more dimensions, and conditions from arrays. Next: [heat and waves](tutorial-3-time.md), where the plate changes in time.